Payment callback of go zero

Time:2021-11-16

Payment callback of go zero

WeChat and Alipay pay

The previously written payment callback logic is processed at the gateway layer. The gateway generally receives parameters and responds to data. So it’s unreasonable. Some processing is done below

Wechat payment callback

It’s officialSDK, V3 version

Here is wechat paymentAPIInterface, need to be inhandlerafferent*http.Request

func WxNotifyHandleHandler(ctx *svc.ServiceContext) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        l := order.NewWxNotifyHandleLogic(r.Context(), ctx)
        Resp, err: = l.wxnotifyhandle (R) // http.request needs to be passed in here
        if err != nil {
            httpx.Error(w, err)
        } else {
            httpx.OkJson(w, resp)
        }
    }
}

apirequestrpcBefore, you need to encapsulate the request parameter typerpcAcceptable type

//Wechat payment callback
func (l *WxNotifyHandleLogic) WxNotifyHandle(req *http.Request) (*types.WxNotifyResp, error) {
    //The header is converted to byte type to facilitate the Pb syntax type defined by RPC
    header,_ := json.Marshal(req.Header)
    //So is the body
    body, err := getRequestBody(req)
    ....
}

func getRequestBody(request *http.Request) ([]byte, error) {
    body, err := ioutil.ReadAll(request.Body)
    if err != nil {
        Return nil, errors. New ("read body error:% s")
    }

    _ = request.Body.Close()
    request.Body = ioutil.NopCloser(bytes.NewBuffer(body))

    return body, nil
}

Here isrpcTo unseal parameters and convert to*http.RequestType to verify keys or certificates, etc

//RPC wechat callback
func (l *WxPayNotifyLogic) WxPayNotify(in *order.WxPayNotifyRequest) (*order.Response, error) {
    notifyUrl := l.svcCtx.Config.WxPay.NotifyUrl

    //Get the req body from the API, and then convert it to * http.request
    req, _ := http.NewRequest(http.MethodPost, notifyUrl, strings.NewReader(string(in.Body)))
    var mapHeader http.Header
    _ = json.Unmarshal(in.Header, &mapHeader)
    for k, v := range mapHeader {
        req.Header.Add(k, v[0])
    }

    ...
}

Alipay callback

sdk github

becausesdkProvides abodyturnjsonTherefore, it is simpler than wechat operation

// Alipay callback
func (l *AliPayNotifyHandleLogic) AliPayNotifyHandle(req *http.Request, resp http.ResponseWriter) {
    //Parse req into map
    notifyReq, err := alipay.ParseNotifyToBodyMap(req)
    if err != nil {
        Logx.Error ("Alipay callback parse req error:", ERR)
        return
    }
    //Body to JSON
    notifyBody := notifyReq.JsonBody()

    //Incoming RPC
    result, err := l.svcCtx.OrderClient.AliPayNotify(l.ctx, &orderclient.AliPayNotifyRequest{
        Body:   notifyBody,
    })

    ...
}

rpcHere putjsonTurn intomap

// Alipay callback
func (l *AliPayNotifyLogic) AliPayNotify(in *order.AliPayNotifyRequest) (*order.Response, error) {

    //JSON to map
    var bodyMap gopay.BodyMap
    _ = json.Unmarshal([]byte(in.Body), &bodyMap)

    //Pass in the map to the content of the public key certificate and verify the signature
    ok, err := alipay.VerifySignWithCert(aliPayCertPublicKeyContent, bodyMap)
    ...

}

So far, the gateway is only responsible for processing parameters, returning data, and RPC to process business logic. Realize the separation of gateway layer and business layer.

This work adoptsCC agreement, reprint must indicate the author and the link to this article