SoFunction
Updated on 2025-03-07

Alipay payment implemented by c#

The specific code of the C# Alipay scan code payment display is for your reference. The specific content is as follows

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
 
namespace ZK_ECommerce. 
{ 
 public class AliPay 
 { 
 
  public static string GetMD5(string s, string _input_charset) 
  { 
 
   /// <summary> 
   /// ASP-compatible MD5 encryption algorithm   /// </summary> 
 
   MD5 md5 = new MD5CryptoServiceProvider(); 
   byte[] t = ((_input_charset).GetBytes(s)); 
   StringBuilder sb = new StringBuilder(32); 
   for (int i = 0; i < ; i++) 
   { 
    (t[i].ToString("x").PadLeft(2, '0')); 
   } 
   return (); 
  } 
 
  public static string[] BubbleSort(string[] r) 
  { 
   /// <summary> 
   /// Bubble sorting method   /// </summary> 
 
   int i, j; //Swap flags   string temp; 
 
   bool exchange; 
 
   for (i = 0; i < ; i++) //Make up to -1 order   { 
    exchange = false; //Before the order of this trip, the exchange flag should be false 
    for (j =  - 2; j >= i; j--) 
    { 
     if ((r[j + 1], r[j]) < 0) //Swap conditions     { 
      temp = r[j + 1]; 
      r[j + 1] = r[j]; 
      r[j] = temp; 
 
      exchange = true; //The exchange has occurred, so the exchange flag is set to true     } 
    } 
 
    if (!exchange) //There is no exchange in this order, and the algorithm is terminated early    { 
     break; 
    } 
 
   } 
   return r; 
  } 
 
  public string CreatUrl( 
   string gateway, 
   string service, 
   string partner, 
   string sign_type, 
   string out_trade_no, 
   string subject, 
   string body, 
   string payment_type, 
   string total_fee, 
   string show_url, 
   string seller_email, 
   string key, 
   string return_url, 
   string _input_charset, 
   string notify_url, 
   string extra_common_param = "" 
   ) 
  { 
   return CreatUrl( 
   gateway, 
   service, 
   partner, 
   sign_type, 
   out_trade_no, 
   subject, 
   body, 
   payment_type, 
   total_fee, 
   show_url, 
   seller_email, 
   key, 
   return_url, 
   _input_charset, 
   notify_url, 
   "", 
   extra_common_param); 
  } 
 
 
 
  public string CreatUrl( 
   string gateway, 
   string service, 
   string partner, 
   string sign_type, 
   string out_trade_no, 
   string subject, 
   string body, 
   string payment_type, 
   string total_fee, 
   string show_url, 
   string seller_email, 
   string key, 
   string return_url, 
   string _input_charset, 
   string notify_url, 
   string token, 
   string extra_common_param = "" 
   ) 
  { 
   /// <summary> 
   /// November 17, 2015 14:08:00   /// </summary> 
   int i; 
   string[] Oristr; 
   //Construct an array;   if (!(token)) 
   { 
    if (extra_common_param == "COD2MOTOPAY") 
    { 
     Oristr = new[]{ 
     "service="+service, 
     "partner=" + partner, 
     "subject=" + subject, 
     "body=" + body, 
     "out_trade_no=" + out_trade_no, 
     "total_fee=" + total_fee, 
     "show_url=" + show_url, 
     "payment_type=" + payment_type, 
     "seller_email=" + seller_email, 
     "notify_url=" + notify_url, 
     "_input_charset="+_input_charset,   
     "return_url=" + return_url, 
     "token="+token, 
     "extra_common_param="+extra_common_param 
     }; 
    } 
    else 
    { 
     Oristr = new[]{ 
     "service="+service, 
     "partner=" + partner, 
     "subject=" + subject, 
     "body=" + body, 
     "out_trade_no=" + out_trade_no, 
     "total_fee=" +total_fee, 
     "show_url=" + show_url, 
     "payment_type=" + payment_type, 
     "seller_email=" + seller_email, 
     "notify_url=" + notify_url, 
     "_input_charset="+_input_charset,   
     "return_url=" + return_url, 
     "token="+token 
     }; 
    } 
 
   } 
   else 
   { 
    if (extra_common_param == "COD2MOTOPAY") 
    { 
     Oristr = new[]{ 
     "service="+service, 
     "partner=" + partner, 
     "subject=" + subject, 
     "body=" + body, 
     "out_trade_no=" + out_trade_no, 
     "total_fee="+total_fee, 
     "show_url=" + show_url, 
     "payment_type=" + payment_type, 
     "seller_email=" + seller_email, 
     "notify_url=" + notify_url, 
     "_input_charset="+_input_charset,   
     "return_url=" + return_url, 
     "extra_common_param="+extra_common_param 
     }; 
    } 
    else 
    { 
     Oristr = new[]{ 
     "service="+service, 
     "partner=" + partner, 
     "subject=" + subject, 
     "body=" + body, 
     "out_trade_no=" + out_trade_no, 
     "total_fee=" + total_fee, 
     "show_url=" + show_url, 
     "payment_type=" + payment_type, 
     "seller_email=" + seller_email, 
     "notify_url=" + notify_url, 
     "_input_charset="+_input_charset,   
     "return_url=" + return_url 
     }; 
    } 
 
 
   } 
   //Sort;   string[] Sortedstr = BubbleSort(Oristr); 
 
 
   //Construct the md5 digest string; 
   StringBuilder prestr = new StringBuilder(); 
 
   for (i = 0; i < ; i++) 
   { 
    if (i ==  - 1) 
    { 
     (Sortedstr[i]); 
 
    } 
    else 
    { 
 
     (Sortedstr[i] + "&"); 
    } 
 
   } 
 
   (key); 
 
   //Generate Md5 digest;   string sign = GetMD5((), _input_charset); 
 
   //Construct payment Url;   char[] delimiterChars = { '=' }; 
   StringBuilder parameter = new StringBuilder(); 
   (gateway); 
   for (i = 0; i < ; i++) 
   { 
 
    (Sortedstr[i].Split(delimiterChars)[0] + "=" + (Sortedstr[i].Split(delimiterChars)[1]) + "&"); 
   } 
 
   ("sign=" + sign + "&sign_type=" + sign_type); 
 
 
   //Return to pay Url;   return (); 
 
  } 
 
  public string CreatUrl_MissPayments( 
        string gateway, 
        string service, 
        string partner, 
        string sign_type, 
        string out_trade_no, 
        string key, 
        string _input_charset 
 
   ) 
  { 
   /// <summary> 
   /// created by sunzhizhi 2006.5.21,sunzhizhi@。 
   /// </summary> 
   int i; 
 
   //Construct an array;   string[] Oristr ={ 
    "service="+service, 
    "partner=" + partner, 
    "out_trade_no=" + out_trade_no,  
    "_input_charset="+_input_charset   
 
    }; 
 
   //Sort;   string[] Sortedstr = BubbleSort(Oristr); 
 
 
   //Construct the md5 digest string; 
   StringBuilder prestr = new StringBuilder(); 
 
   for (i = 0; i < ; i++) 
   { 
    if (i ==  - 1) 
    { 
     (Sortedstr[i]); 
 
    } 
    else 
    { 
 
     (Sortedstr[i] + "&"); 
    } 
 
   } 
 
   (key); 
 
   //Generate Md5 digest;   string sign = GetMD5((), _input_charset); 
 
   //Construct payment Url;   char[] delimiterChars = { '=' }; 
   StringBuilder parameter = new StringBuilder(); 
   (gateway); 
   for (i = 0; i < ; i++) 
   { 
 
    (Sortedstr[i].Split(delimiterChars)[0] + "=" + (Sortedstr[i].Split(delimiterChars)[1]) + "&"); 
   } 
 
   ("sign=" + sign + "&sign_type=" + sign_type); 
 
 
   //Return to pay Url;   return (); 
 
  } 
 
 
  public static string GetTranSign(string TranData) 
  { 
   if ((TranData)) 
    return null; 
   string sign = GetMD5(TranData + "alskdjfaow;fjel;asdjf", "utf-8"); 
   return sign; 
  } 
 
 } 
}

Page processing:

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ZK_ECommerce.; 
 
namespace  
{ 
 public class ApliayDemoController : Controller 
 { 
  /// <summary> 
  /// Payment page  /// </summary> 
  /// <param name="ordercode"></param> 
  /// <returns></returns> 
  public ActionResult Index(string ordercode) 
  { 
 
 
   string out_trade_no = ordercode; 
   string gateway = "/?"; //Payment interface   string service = "create_direct_pay_by_user"; 
   string partner = "";  //Partner Partner ID reserved field   string sign_type = "MD5"; 
   string payment_type = "1";     //Payment type   string show_url = ""; 
   string seller_email = "";    //Seller's account   string key = "";    //Partner account Alipay security verification code   string return_url = "http://localhost:1396/ApliyDemo/ApliyReturn"; // Server notification return interface   string notify_url = "http://localhost:1396/ApiayDemo/AlipayNotify"; // Server notification returns interface   string _input_charset = "utf-8"; 
   string extra_common_param = "COD2MOTOPAY";//Indicates quick payment   string token = ; 
   if ((token)) 
    token = ""; 
   string subject = "Kangkang Medical Examination Package"; // subject product name   string body = "Test Products"; //Product Description   string total_fee = "0.01"; 
   AliPay ap = new AliPay(); 
   string aliay_url = (gateway, service, partner, sign_type, out_trade_no, subject, body, payment_type, 
           total_fee, show_url, seller_email, key, return_url, _input_charset, notify_url, token, extra_common_param); 
 
   return Redirect(aliay_url); 
 
 
  } 
 
 
  /// <summary> 
  /// Return to the notification page  /// </summary> 
  /// <returns></returns> 
  public ActionResult ApliyReturn() 
  { 
 
   string alipayNotifyURL = "/?"; 
   string key = ""; //Partner's corresponding transaction security verification code (must be filled in)   string _input_charset = "utf-8"; 
   string partner = "";  //partner partner id (must be filled in) 
   alipayNotifyURL = alipayNotifyURL + "service=notify_verify" + "&partner=" + partner + "¬ify_notify_id"]; 
 
   //Get the result of Alipay ATN, true is the correct order information, false is invalid   string responseTxt = Get_Http(alipayNotifyURL, 120000); 
 
   int i; 
   NameValueCollection coll; 
   coll = ; 
   String[] requestarr = ; 
   //Sort;   string[] Sortedstr = BubbleSort(requestarr); 
 
   //Construct the md5 digest string; 
   StringBuilder prestr = new StringBuilder(); 
 
   for (i = 0; i < ; i++) 
   { 
    if ([Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type") 
    { 
     if (i ==  - 1) 
     { 
      (Sortedstr[i] + "=" + [Sortedstr[i]]); 
     } 
     else 
     { 
      (Sortedstr[i] + "=" + [Sortedstr[i]] + "&"); 
 
     } 
    } 
 
 
   } 
 
   (key); 
   string mysign = GetMD5((), _input_charset); 
   string sign = ["sign"]; 
   string trade_status = ["trade_status"]; 
 
   if (mysign == sign && responseTxt == "true" && trade_status == "TRADE_SUCCESS") //Verify the message sent by the payment and whether the signature is correct   { 
    string order_amount = ["total_fee"].ToString().Trim(); 
    string order_code = ["out_trade_no"].ToString().Trim(); 
 
    
    //updateOrder(order_code);//Update order status    (""); //Skip to the payment success page   } 
   else 
   { 
 
    ("Pay failed."); 
    ("<br>Result:responseTxt=" + responseTxt); 
    ("<br>Result:mysign=" + mysign); 
    ("<br>Result:sign=" + sign); 
   } 
   return View(); 
  } 
 
 
  /// <summary> 
  /// Payment callback page  /// </summary> 
  /// <returns></returns> 
  public ActionResult AlipayNotify() 
  { 
 
 
   string alipayNotifyURL = "/?"; 
   string partner = "";  //partner partner id (must be filled in)   string key = ""; //Partner's corresponding transaction security verification code (must be filled in) 
   alipayNotifyURL = alipayNotifyURL + "service=notify_verify" + "&partner=" + partner + "¬ify_notify_id"]; 
 
   //Get the result of Alipay ATN, true is the correct order information, false is invalid   string responseTxt = Get_Http(alipayNotifyURL, 120000); 
 
   int i; 
   NameValueCollection coll; 
   //Load Form variables into NameValueCollection variable. 
   coll = ; 
 
   // Get names of all forms into a string array. 
   String[] requestarr = ; 
   //Sort;   string[] Sortedstr = BubbleSort(requestarr); 
   //Construct the md5 digest string;   StringBuilder prestr = new StringBuilder(); 
 
   for (i = 0; i < ; i++) 
   { 
    if ([Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type") 
    { 
     if (i ==  - 1) 
     { 
      (Sortedstr[i] + "=" + [Sortedstr[i]]); 
     } 
     else 
     { 
      (Sortedstr[i] + "=" + [Sortedstr[i]] + "&"); 
 
     } 
    } 
 
 
   } 
 
   (key); 
   string mysign = GetMD5(()); 
   string sign = ["sign"]; 
   string trade_status = ["trade_status"]; 
 
   if (mysign == sign && responseTxt == "true" && trade_status == "TRADE_SUCCESS") //Verify the message sent by the payment and whether the signature is correct   { 
    string order_amount = ["total_fee"].ToString().Trim(); 
    string order_code = ["out_trade_no"].ToString().Trim(); 
 
 
    
    // updateOrder(order_code);//Update order status    (""); //Skip to the payment success page   } 
   else 
   { 
 
    ("Pay failed."); 
    ("<br>Result:responseTxt=" + responseTxt); 
    ("<br>Result:mysign=" + mysign); 
    ("<br>Result:sign=" + sign); 
   } 
   return View(); 
  } 
 
  public static string GetMD5(string s, string _input_charset) 
  { 
   /// <summary> 
   /// ASP-compatible MD5 encryption algorithm   /// </summary> 
 
   MD5 md5 = new MD5CryptoServiceProvider(); 
   byte[] t = ((_input_charset).GetBytes(s)); 
   StringBuilder sb = new StringBuilder(32); 
   for (int i = 0; i < ; i++) 
   { 
    (t[i].ToString("x").PadLeft(2, '0')); 
   } 
   return (); 
  } 
 
 
 
  public static string GetMD5(string s) 
  { 
   /// <summary> 
   /// ASP-compatible MD5 encryption algorithm   /// </summary> 
 
   MD5 md5 = new MD5CryptoServiceProvider(); 
   byte[] t = (("utf-8").GetBytes(s)); 
   StringBuilder sb = new StringBuilder(32); 
   for (int i = 0; i < ; i++) 
   { 
    (t[i].ToString("x").PadLeft(2, '0')); 
   } 
   return (); 
  } 
 
  public static string[] BubbleSort(string[] r) 
  { 
   /// <summary> 
   /// Bubble sorting method   /// </summary> 
 
   int i, j; //Swap flags   string temp; 
 
   bool exchange; 
 
   for (i = 0; i < ; i++) //Make up to -1 order   { 
    exchange = false; //Before the order of this trip, the exchange flag should be false 
    for (j =  - 2; j >= i; j--) 
    { 
     if ((r[j + 1], r[j]) < 0) //Swap conditions     { 
      temp = r[j + 1]; 
      r[j + 1] = r[j]; 
      r[j] = temp; 
 
      exchange = true; //The exchange has occurred, so the exchange flag is set to true     } 
    } 
 
    if (!exchange) //There is no exchange in this order, and the algorithm is terminated early    { 
     break; 
    } 
 
   } 
   return r; 
  } 
  //Get the ATN results of the remote server  public String Get_Http(String a_strUrl, int timeout) 
  { 
   string strResult; 
   try 
   { 
 
    HttpWebRequest myReq = (HttpWebRequest)(a_strUrl); 
     = timeout; 
    HttpWebResponse HttpWResp = (HttpWebResponse)(); 
    Stream myStream = (); 
    StreamReader sr = new StreamReader(myStream, ); 
    StringBuilder strBuilder = new StringBuilder(); 
    while (-1 != ()) 
    { 
     (()); 
    } 
 
    strResult = (); 
   } 
   catch (Exception exp) 
   { 
 
    strResult = "mistake:" + ; 
   } 
 
   return strResult; 
  } 
 
 } 
}

The above is the detailed content of Alipay payment implemented by C#. For more information about C# Alipay payment, please pay attention to my other related articles!