WeChat payment v 3.3.6, payment 3.3.6

Source: Internet
Author: User

Payment v 3.3.6, payment 3.3.6

Text description;

Prerequisites: Register and apply for a service number, and activate payment.

Parameters involved: AppId, AppSecret, original ID (automatic reply), mch_id (merchant ID), and Key (merchant Key: set by yourself .)

Unified Specifications:

  • Requirements
  • Unified encryption mode: MD5.
  • Unified Signature generation method:
    • Input parameter ascii code by field nameSort from small to large, UseFormat of url key-value pairs(Key1 = value1 & key2 = value2...) concatenate the string(Note: parameters with null values do not participate in the signature). In the stringFinally, connect them.& Key = Key (merchant key)And then proceedMD5 Encryption.Convert string to uppercase.

Payment:

  • Jsapi payment and native payment both need to call the unified payment interface (URL address: https://api.mch.weixin.qq.com/pay/unifiedorder)
  • Jsapi payment
    • The openid parameter is required to call the unified payment interface of jsapi (obtained through Oath2.0 authorization ).
    • The return parameters of the unified payment interface must first verify the signature and then obtain the prepay_id.
    • Document. addEventListener ('weixinjsbridgeready', function onBridgeReady () {// jQuery ('# wxpay') paid by the public account '). click (function (e) {WeixinJSBridge. invoke ('getbrandwcpayrequest', {"appId": appId, // public account name, passed by the merchant "timeStamp": timeStamp, // timeStamp "nonceStr": nonceStr, // random string "package": package, // format: prepay_id = xxxx "signType": signType, // signature method: MD5 "paySign": paySign // signature: generate signature parameters appId, timeStamp, nonceStr, package, and sign Type and key are case sensitive. }, Function (res) {if (res. err_msg = "get_brand_wcpay_request: OK") {window. location. href = url; // The page displayed after successful payment} // else {// alert (res. err_code + res. err_desc + res. err_msg); //});}, false)
    • Note: The jsapi page clicks "pay" twice to generate two orders. You need to add a secondary click protection mechanism on the page's PAYMENT button.
  • Native payment
    • REQUIRED for the product_id Parameter
    • Call the unified payment interface to obtain the code_url and generate a QR code image.

Payment callback y_url:

Obtain xml format parameters, verify the signature, and process the logic (The notification background calls back multiple times. First, check whether the callback has been processed.).

 

Code area:

  • Generate the Sign public method:
  • Public string CreateSign (Dictionary <string, string> collection) {// sort List <KeyValuePair <string, string> list = new List <KeyValuePair <string, string >>( collection); list. sort (delegate (KeyValuePair <string, string> pair1, KeyValuePair <string, string> pair2) {return pair1.Key. compareTo (pair2.Key) ;}); StringBuilder sb = new StringBuilder (); foreach (KeyValuePair <string, string> pair in list) {sb. append (pair. key); sb. append ("="); sb. append (pair. value); sb. append ("&");} string str = sb. append ("key = value "). toString (); string signValue = MD5 (str ). toUpper (); // md5 encryption and conversion to big write return signValue ;}
  • Xml and Dictionary conversion methods:
  • public string DictionaryToXml(Dictionary<string, string> collection){  StringBuilder sb = new StringBuilder();  sb.Append("<xml>");  foreach (KeyValuePair<string, string> pair in collection)  {    sb.Append("<" + pair.Key + ">");    sb.Append("<![CDATA[" + pair.Value + "]]>");    sb.Append("</" + pair.Key + ">");  }  sb.Append("</xml>");  return sb.ToString();}public Dictionary<string, string> XmlToDictionary(XmlDocument doc){  Dictionary<string, string> collection = new Dictionary<string, string>();  foreach (XmlElement element in doc.DocumentElement.ChildNodes)  {    string key = element.Name;    string value = element.InnerText;    if (value != "")    {      collection.Add(key, value);    }  }  return collection;}
  • Obtain a Json key value:
  • Public string GetJosnValue (string jsonStr, string key) {string result = string. Empty; if (! String. isNullOrEmpty (jsonStr) {key = "\" "+ key. trim ('"') +" \ ""; int index = jsonStr. indexOf (key) + key. length + 1; if (index> key. length + 1) {// cut the comma first. If it is the last one, cut the "}" and take the minimum value int end = jsonStr. indexOf (',', index); if (end =-1) {end = jsonStr. indexOf ('}', index);} // index = json. indexOf ('"', index + key. length + 1) + 1; result = jsonStr. substring (index, end-index); // filter quotation marks or spaces. result = result. trim (new char [] {'"','', '\ ''}) ;}} return result ;}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.