Simple and easy to use WeChat Payment SDK for Go

Source: Internet
Author: User
Tags openid readfile
# Wechatpay Payment SDK for go! Includes all the features paid by the merchant! Easy integration! Directly on the link: [Payment SDK for Go] (https://github.com/liyoung1992/wechatpay) # # install ' Go get-u github.com/liyoung1992/wechatpay ' # # Help if you encounter problems in the integration process, please contact: liyoung_1992@163.com## currently implemented interface-Scan code payment (NATIVE)-H5 Pay (mweb)-Public number payment (JSAPI)-app payment (APP)-Small program payment (JSAPI)- Refund-Refund Query # # Integration method strongly recommended before development carefully read [payment official documents] (https://pay.weixin.qq.com/wiki/doc/api/index.html) # # # Create payment ' ' Gowechat_cert, err : = Ioutil. ReadFile ("Config/wechat/apiclient_cert.pem") if err! = Nil {panic (err)}wechat_key, err: = Ioutil. ReadFile ("Config/wechat/apiclient_key.pem") Wechat_client = Wechatpay. New (OS. Getenv ("Wechat_appid"), OS. Getenv ("Wechat_mchid"), OS. Getenv ("Wechat_apikey"), Wechat_key,wechat_cert) if err! = Nil {panic (err)} ' # # Unified Payment (other payment changes corresponding to Tradetype can) ' Govar Pay_data Wechatpay. Unitorderpay_data. Notifyurl = OS. Getenv ("Wechat_notify_url") pay_data. Tradetype = "NATIVE" Pay_data. Body = Payweb. Subjectpay_data. Spbillcreateip = C.clientip () pay_data. Totalfee = 1pay_data. Outtradeno = Payweb. Orderidresult, err:= Wechat_clieNt. Pay (Pay_data) "app payment, public number payment, small program payment are first returned: pre-paid transactions, and then use the pre-paid transaction code in the payment operation; both applet and the public number are required to obtain the user's OpenID. Specific access: [Get OpenID] (https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_4) # # # initiated refund "' Govar refund_data Wechatpay. Orderrefundrefund_data. Totalfee = 1 Refund_data. Outtradeno = Order_idrefund_data. Outrefundno = "R" + Order_idrefund_data. Refundfee = 1 result, err: = Wechat_client. Refund (Refund_data) ' # # Refund query ' Govar refund_status wechatpay. Orderrefundqueryrefund_status. Outtradeno = Order_idresult, err: = Wechat_client. Refundquery (Refund_status) "# # Asynchronous notification of the payment results asynchronously, here using the example of the gin framework, the other framework principle (also can not frame), receive notification, return to the data! "' Gofunc (This *wechatpay) Paynotifyurl (c *gin. Context) {body, err: = Ioutil. ReadAll (c.request.body) if err! = Nil {log. Error (Err, "Read notify body Error")}var wx_notify_req paynotifyresulterr = XML. Unmarshal (body, &wx_notify_req) if err! = Nil {log. Error (Err, "read HTTP body XML failed! ERR: "+err. Error ())}var reqmap Map[string]interface{}reqmap = Make (map[string]interface{}, 0) reqmap["Return_code"] = wx_notify_req. returncodereqmap["return_msg"] = wx_notify_req. returnmsgreqmap["AppID"] = wx_notify_req. appidreqmap["mch_id"] = wx_notify_req. mchidreqmap["nonce_str"] = wx_notify_req. noncestrreqmap["Result_code"] = wx_notify_req. resultcodereqmap["OpenID"] = wx_notify_req. openidreqmap["Is_subscribe"] = wx_notify_req. issubscribereqmap["Trade_type"] = wx_notify_req. tradetypereqmap["Bank_type"] = wx_notify_req. banktypereqmap["Total_fee"] = wx_notify_req. totalfeereqmap["Fee_type"] = wx_notify_req. feetypereqmap["Cash_fee"] = wx_notify_req. cashfeereqmap["Cash_fee_type"] = wx_notify_req. cashfeetypereqmap["transaction_id"] = wx_notify_req. transactionidreqmap["out_trade_no"] = wx_notify_req. outtradenoreqmap["Attach"] = Wx_notify_req. attachreqmap["time_end"] = wx_notify_req. timeend//The signature check if this. Verifysign (Reqmap, Wx_notify_req. Sign) {record, err: = json. Marshal (wx_notify_req) if err! = Nil {log. Error (Err, "WeChat pay Marshal Err:" +err. Error ())}//todo add your code to process the return value FMT. Println (String (record))//err = Wechat_pay_recoed_producer. Publish ("Wechat_pay", record) if err! = Nil {log. Error (Err, "WeChat Publish record err:" +err. Error ())}c.xml (http. Statusok, Gin. h{"Return_code": "SUCCESS", "return_msg": "OK",})} else {C.xml (http. Statusok, Gin. h{"Return_code": "FAIL", "return_msg": "Failed to verify sign, please retry!",})}return} "355 reads  ∙  1 likes  
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.