乐趣区

关于后端:简单易用的APIv3版微信支付SDK

介绍

微信领取 SDK,基于全新的微信领取 APIv3 来实现。反对小程序领取、JSAPI 领取、Native 领取、APP 领取、H5 领取,反对直连商户模式和服务商模式。

装置阐明

go get gitee.com/haming123/wxpay4go

疾速上手

  1. 商户对象初始化

    package main
    
    import (
     "log"
     "wxpay"
    )
    
    var MchCtx *wxpay.MchWxApp = nil
    func MchCtxInit() error {pem_cert, err := wxpay.LoadCertificateWithPath("/path/to/merchant/apiclient_cert.pem")
     if err != nil {return err}
     pem_key, err := wxpay.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
     if err != nil {return err}
     MchCtx = wxpay.NewMchWxApp("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
     return nil
    }
    
    func main() {
     //......
     err := MchCtxInit()
     if err != nil {log.Print(err)
         return
     }
     //......
    }
  2. 领取下单

    func HandlerPayCreateDemo(w http.ResponseWriter, r *http.Request) {//................( 其余业务逻辑)
     var data_req wxpay.WxAppOrderData
     data_req.Description = "业务形容"
     data_req.Out_trade_no = "业务订单号"
     data_req.Notify_url = "接管领取告诉的 URL 地址"
     data_req.Sub_mchid = "子商户号"
     data_req.Sub_appid = "小程序的 APPID"
     data_req.Payer.Sub_openid = "用户在小程序中的 OPENID"
     data_req.Amount.Total = 20
     param, err := MchCtx.CreateOrder(data_req)
     if err!= nil {log.Print(err)
         return
     }
     //................(其余业务逻辑)
    }
  3. 领取订单查问

    func HandlerPayQueryDemo(w http.ResponseWriter, r *http.Request) {//................( 其余业务逻辑)
     out_trade_no := "业务订单号"
     sub_mchid := "子商户号"
     result, err := MchCtx.QueryByOutTradeNo(out_trade_no, sub_mchid)
     if err!= nil {log.Print(err)
         return
     }
     //................(其余业务逻辑)
    }
  4. 领取回调解决

    func HandlerPayCallBack(w http.ResponseWriter, r *http.Request) {
     // 解析回调数据
     req_info, err := MchCtx.ParsePayCallBack(r)
     if err != nil {wxpay.HttpCallBackReturn(w, 500,"FAIL", "FAIL")
         return
     }
    
     //................(其余业务逻辑)
    
     // 回调返回
     wxpay.HttpCallBackReturn(w, 200, "SUCCESS", "SUCCESS")
    }
  5. 退款申请

    func HandlerRefundDemo(w http.ResponseWriter, r *http.Request) {//................( 其余业务逻辑)
     var data wxpay.RefundCreateReq
     data.Sub_mchid = "子商户号"
     data.Out_trade_no = "业务订单号"
     data.Out_refund_no = "商户的退款单号"
     data.Amount.Total = 20
     data.Amount.Refund = 20
     data.Notify_url = "退款后果回调的 URL"
     result, err := MchCtx.RefundOrder(data)
     if err!= nil {log.Print(err)
     }
     //................(其余业务逻辑)
    }
  6. 退款回调解决

    func HandlerRefundCallBack(w http.ResponseWriter, r *http.Request) {
     // 解析回调数据
     req_info, err := MchCtx.ParseRefundCallBack(r)
     if err != nil {wxpay.HttpCallBackReturn(w, 500,"FAIL", "FAIL")
         return
     }
    
     //................(其余业务逻辑)
    
     // 回调返回
     wxpay.HttpCallBackReturn(w, 200, "SUCCESS", "SUCCESS")
    }

其余领取产品的应用

  1. 直连商户商户对象初始化

    // 小程序
    MchCtx = wxpay.NewMchWxApp("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
    //JsAPi
    MchCtx = wxpay.NewMchJsApi("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
    //H5
    MchCtx = wxpay.NewMchH5("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
    //Native
    MchCtx = wxpay.NewMchNative("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
    //App
    MchCtx = wxpay.NewMchApp("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
  2. 服务商商户对象初始化

    // 小程序
    MchCtx = wxpay.NewMidWxApp("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
    //JsAPi
    MchCtx = wxpay.NewMidJsApi("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
    //H5
    MchCtx = wxpay.NewMidH5("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
    //Native
    MchCtx = wxpay.NewMidNative("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
    //App
    MchCtx = wxpay.NewMidApp("mchID_string", "appID_string", "mchAPIv3Key_string", pem_cert, pem_key)
退出移动版