This is a creation in Article, where the information may have evolved or changed.
First, the payment process
Open platform H5 payment access RELATED links: Https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4
The corresponding payment process:
Second, the relevant code
1, the Unified order
First define a unifyorderreq to fill in the parameters we want to pass in.
Type unifyorderreq struct {
Appid string ' xml: ' Appid '
Body string ' xml: ' body '
mch_id string ' xml: ' mch_id '
Nonce_str string ' xml: ' Nonce_str '
Notify_url string ' xml: ' Notify_url '
Trade_type string ' xml: ' Trade_type '
SPBILL_CREATE_IP string ' xml: ' Spbill_create_ip '
Total_fee int ' xml: "Total_fee" '
Out_trade_no string ' xml: ' Out_trade_no '
Sign string ' xml: ' sign '
}
Wxpay function for calculating signatures
Func (o *apihandler) wxpaycalcsign (Mreq map[string]interface{}, key String) (sign string) {
Log4go. Debug ("Wxpaycalcsign () ... API key:%s ", KEY)
STEP 1, sort the key in ascending order.
Sorted_keys: = Make ([]string, 0)
For k, _: = Range Mreq {
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.
var signstrings string
For _, K: = Range Sorted_keys {
Fmt. Printf ("K=%v, v=%v\n", K, Mreq[k])
Value: = FMT. Sprintf ("%v", Mreq[k])
if value! = "" {
Signstrings = signstrings + k + "=" + Value + "&"
}
}
STEP3, at the end of the key-value pair, plus Key=api_key
If key! = "" {
Signstrings = signstrings + "key=" + key
}
Log4go. Debug ("Wxpaycalcsign () signstrings:%s", signstrings)
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))
Return uppersign
}
Func (o *apihandler) wxunifychargereq (userId int64, OrderNo string) (string, error) {
Log4go. Debug ("Wxunifychargereq () userid:%d, orderno:%s", UserId, OrderNo)
Bookorder, err: = O.getorderbyordernodb (OrderNo)
If err! = Nil {
Log4go. Error ("Wxunifychargereq (): Getorderbyordernodb error.")
Return "", err
}
NONCESTR: = getrandomstring (32)
Code to request Unifiedorder
var yourreq unifyorderreq
Yourreq.appid = wx_app_id//Open Platform app ID of the app we created
Yourreq.body = Bookorder.linenum
yourreq.mch_id = wx_mch_id
Yourreq.nonce_str = Noncestr
Yourreq.notify_url = Wx_notify_url
Yourreq.trade_type = "APP"
YOURREQ.SPBILL_CREATE_IP = Wx_invoke_api_ip
Yourreq.total_fee = Int (bookorder.totalprice * 100)//Unit is divided
Yourreq.out_trade_no = OrderNo
var m map[string]interface{}
m = Make (map[string]interface{}, 0)
m["AppID"] = Yourreq.appid
m["Body"] = Yourreq.body
m["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 = O.wxpaycalcsign (M, Wx_pay_key)//This is the function that calculates the wxpay signature that has been posted
Bytes_req, err: = XML. Marshal (Yourreq)
If err! = Nil {
Log4go. Error ("Wxunifychargereq (): XML. Marshal error:%s ", err)
Return "", err
}
Str_req: = String (bytes_req)
Wxpay Unifiedorder interface requires HTTP body xmldoc root node is <xml></xml> this, so here need to replace a bit
Str_req = strings. Replace (Str_req, "Xunifyorderreq", "xml",-1)
Bytes_req = []byte (str_req)
Log4go. Debug ("Wxunifychargereq () str_req:%s", Str_req)
Send unified order Request.
Req, Err: = http. Newrequest ("POST", Wx_unifiedorder_api, Bytes. Newreader (Bytes_req))
If err! = Nil {
Log4go. Error ("wxunifychargereq (): http. Newrequest error:%s ", err)
Return "", err
}
Req. Header.set ("Accept", "Application/xml")
The settings for the HTTP header 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 {
Log4go. Error ("wxunifychargereq (): http. Do error:%s ", _err)
Return "", err
}
Defer resp. Body.close ()
Body, err: = Ioutil. ReadAll (resp. Body)
Retxml: = string (body)
Log4go. Debug ("Wxunifychargereq () retxml:%s", Retxml)
If err! = Nil {
Return "", err
}
Return Retxml, Nil
}
2. Verification (to be continued)