簡單好用的微信支付SDK for Go

來源:互聯網
上載者:User
# wechatpay支付SDK for Go!包括商戶支付的全部功能!整合簡單!直接上連結:[支付SDK for Go](https://github.com/liyoung1992/wechatpay)## 安裝`go get -u github.com/liyoung1992/wechatpay`## 協助如果在整合過程中遇到問題,請聯絡:liyoung_1992@163.com## 目前實現的介面- 掃碼支付(NATIVE )- H5支付 (MWEB)- 公眾號支付 (JSAPI )- APP支付 (APP)- 小程式支付 (JSAPI)- 退款- 退款查詢## 整合方式強烈建議開發前仔細閱讀[支付官方文檔](https://pay.weixin.qq.com/wiki/doc/api/index.html)### 建立支付```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)}```### 統一支付(其他支付改對應的tradetype即可)```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支付、公眾號支付、小程式支付都是先返回:預支付交易單,然後用預支付交易碼在進行支付操作;小程式和公眾號支付都需要擷取使用者的openid。具體的擷取方式:[擷取openid](https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_4)### 發起退款```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)```### 退款查詢```govar refund_status wechatpay.OrderRefundQueryrefund_status.OutTradeNo = order_idresult, err := wechat_client.RefundQuery(refund_status)```## 非同步通知支付結果的非同步通知,這裡使用gin架構的例子,其他架構原理一樣(也可以不用架構),接受到通知,返回給資料即可!```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//進行簽名校正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 加入你的代碼,處理傳回值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 次點擊  ∙  1 贊  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.