Golang Micro-Credit payment service

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In general, the use of Golang is primarily written on the server side. So this article mainly talk about Golang in the processing of mobile payment service side of the unified next single interface and payment callback interface, as well as the query interface.

Payment process

is the official website of the payment process description:

The red part of the figure is payment, our system includes the app, the process that needs to participate in the backstage.
There are three processes that require the server to participate in the background:
1. Unified order and return to the client
2. Asynchronous notification result callback processing
3. Call the payment query interface

All interfaces are provided as HTTP restful APIs, so for server it is called these interfaces and handles the return values.

Golang's service-side implementation

1, call unified Next single interface
First you need to call: Https://api.mch.weixin.qq.com/pay/unifiedorder This is the API, the call will return us a prepay_id. The result of the call is returned with the correct return to our Prepay ID.

According to the documentation, the parameters of this interface are not, and the parameters we pass in need to be passed to the body portion of the HTTP request in XML form.

//First define a unifyorderreq to fill in the parameters we want to pass in. typeUnifyorderreqstruct{Appidstring ' xml: ' AppID 'Bodystring ' xml: ' Body 'mch_idstring ' xml: ' mch_id 'Nonce_strstring ' xml: ' Nonce_str 'Notify_urlstring ' xml: ' Notify_url 'Trade_typestring ' xml: ' Trade_type 'Spbill_create_ipstring ' xml: ' Spbill_create_ip 'Total_feeint    ' xml: ' Total_fee 'Out_trade_nostring ' xml: ' Out_trade_no 'Signstring ' xml: ' sign '}//wxpay function for calculating signaturesfuncWxpaycalcsign (mreqMap[string]Interface{}, keystring) (signstring) {FMT. Println ("Payment signature calculation, API KEY:", key)//step 1, the key is sorted in ascending order.Sorted_keys: = Make([]string,0) forK, _: =Rangemreq {Sorted_keys =Append(Sorted_keys, K)} Sort. Strings (Sorted_keys)//STEP2, the key value pair of Key=value is connected with &, slightly over null value .    varSignstringsstring     for_, K: =RangeSorted_keys {fmt. Printf ("K=%v, v=%v\n", K, mreq[k]) Value: = Fmt. Sprintf ("%v", Mreq[k])ifValue! =""{signstrings = signstrings + k +"="+ Value +"&"}    }//step3, at the end of the key-value pair, plus Key=api_key    ifKey! =""{signstrings = signstrings +"key="+ Key}//step4, make MD5 signature and capitalize all characters.MD5CTX: = MD5. New () Md5ctx.write ([]byte(signstrings)) CIPHERSTR: = Md5ctx.sum (Nil) Uppersign: = Strings. ToUpper (Hex. Encodetostring (CIPHERSTR))returnUppersign}
Request Unifiedorder Code var yourreq unifyorderreq yourreq. Appid="app_id"Open Platform App ID for the app we created Yourreq. Body="Trade name"Yourreq. Mch_id ="Merchant Number"Yourreq. Nonce_str ="Your nonce"Yourreq. Notify_url ="Www.yourserver.com/wxpayNotify"Yourreq. Trade_type ="APP"Yourreq. Spbill_CREATE_IP ="xxx.xxx.xxx.xxx"Yourreq. Total_fee =TenUnits are points, here is1Mao Qian Yourreq. out_trade_no ="Back-Office system tracking Number"var m map[string]interface{} m = Make (map[string]interface{},0) m["AppID"] = Yourreq. Appidm["Body"] = Yourreq. Bodym["mch_id"] = Yourreq. Mch_id m["Notify_url"] = Yourreq. Notify_url m["Trade_type"] = Yourreq. Trade_type m["Spbill_create_ip"] = Yourreq. Spbill_create_ip m["Total_fee"] = Yourreq. Total_fee m["Out_trade_no"] = Yourreq. out_trade_no m["Nonce_str"] = Yourreq. Nonce_str Yourreq. Sign= Wxpaycalcsign (M,"Wxpay_api_key")//This is the function that calculates the Wxpay signature above has been posted bytes_req, err: = XML. Marshal(yourreq) If err! = Nil {FMT. Println("Send error in XML format, Reason:", err) return} str_req: = String (bytes_req)//wxpay The Unifiedorder interface requires that the xmldoc root node in the HTTP body is <xml></ Xml> this, so we need to replace Str_req = strings. Replace(Str_req,"Xunifyorderreq","xml", -1) Bytes_req = []byte (str_req)//Send unified order request. Req, Err: = http. Newrequest("POST", Unify_order_req, bytes. Newreader(Bytes_req)) If err! = Nil {FMT. Println("The New Http request has an error, cause:", err) return} req. Header. Set("Accept","Application/xml")//The HTTP header settings here must be set. Req. Header. Set("Content-type","Application/xml;charset=utf-8") C: = http. Client{} resp, _err: = C. do(req) if _err! = Nil {FMT. Println("Request payment unified next single interface send error, cause:", _err) return}//To here the unified next single interface has been executed.

2, handling of callback functions
3, the client queries the order Request response

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.