package http import ( "fmt" "net/http" "quyun/app/jobs" "quyun/providers/job" "quyun/providers/wepay" "github.com/go-pay/gopay" "github.com/go-pay/gopay/wechat/v3" "github.com/go-pay/util/js" "github.com/gofiber/fiber/v3" log "github.com/sirupsen/logrus" ) // @provider type pays struct { wepay *wepay.Client job *job.Job } // Callback // @Router /pay/callback/:channel [get] // @Bind channel path func (ctl *pays) Callback(ctx fiber.Ctx, channel string) error { log := log.WithField("method", "pays.Callback") body := ctx.Body() si := &wechat.SignInfo{ HeaderTimestamp: ctx.Get(wechat.HeaderTimestamp), HeaderNonce: ctx.Get(wechat.HeaderNonce), HeaderSignature: ctx.Get(wechat.HeaderSignature), HeaderSerial: ctx.Get(wechat.HeaderSerial), SignBody: string(body), } notifyReq := &wechat.V3NotifyReq{SignInfo: si} if err := js.UnmarshalBytes(body, notifyReq); err != nil { log.Errorf("json unmarshal error:%v", err) return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{"error": fmt.Sprintf("json unmarshal error:%v", err)}) } // 获取微信平台证书 certMap := ctl.wepay.WxPublicKeyMap() // 验证异步通知的签名 err := notifyReq.VerifySignByPKMap(certMap) if err != nil { log.Errorf("verify sign error:%v", err) return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{"error": "Invalid signature"}) } if err := ctl.job.Add(&jobs.WechatCallback{NotifyReq: notifyReq}); err != nil { log.Errorf("add job error:%v", err) return ctx.Status(http.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to add job"}) } return ctx.Status(http.StatusOK).JSON(&wechat.V3NotifyRsp{ Code: gopay.SUCCESS, Message: "成功", }) }