feat: add callback
This commit is contained in:
@@ -4,21 +4,27 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"quyun/app/jobs"
|
||||||
|
"quyun/providers/job"
|
||||||
"quyun/providers/wepay"
|
"quyun/providers/wepay"
|
||||||
|
|
||||||
"github.com/go-pay/gopay"
|
"github.com/go-pay/gopay"
|
||||||
"github.com/go-pay/gopay/wechat/v3"
|
"github.com/go-pay/gopay/wechat/v3"
|
||||||
"github.com/go-pay/util/js"
|
"github.com/go-pay/util/js"
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pays struct {
|
type pays struct {
|
||||||
wepay *wepay.Client
|
wepay *wepay.Client
|
||||||
|
job *job.Job
|
||||||
}
|
}
|
||||||
|
|
||||||
// Callback
|
// Callback
|
||||||
// @Router /pay/callback [get]
|
// @Router /pay/callback [get]
|
||||||
func (ctl *pays) Callback(ctx fiber.Ctx) error {
|
func (ctl *pays) Callback(ctx fiber.Ctx) error {
|
||||||
|
log := log.WithField("method", "pays.Callback")
|
||||||
|
|
||||||
body := ctx.Body()
|
body := ctx.Body()
|
||||||
si := &wechat.SignInfo{
|
si := &wechat.SignInfo{
|
||||||
HeaderTimestamp: ctx.Get(wechat.HeaderTimestamp),
|
HeaderTimestamp: ctx.Get(wechat.HeaderTimestamp),
|
||||||
@@ -29,6 +35,7 @@ func (ctl *pays) Callback(ctx fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
notifyReq := &wechat.V3NotifyReq{SignInfo: si}
|
notifyReq := &wechat.V3NotifyReq{SignInfo: si}
|
||||||
if err := js.UnmarshalBytes(body, notifyReq); err != nil {
|
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)})
|
return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{"error": fmt.Sprintf("json unmarshal error:%v", err)})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,10 +45,14 @@ func (ctl *pays) Callback(ctx fiber.Ctx) error {
|
|||||||
// 验证异步通知的签名
|
// 验证异步通知的签名
|
||||||
err := notifyReq.VerifySignByPKMap(certMap)
|
err := notifyReq.VerifySignByPKMap(certMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Errorf("verify sign error:%v", err)
|
||||||
return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{"error": "Invalid signature"})
|
return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{"error": "Invalid signature"})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add process order job
|
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{
|
return ctx.Status(http.StatusOK).JSON(&wechat.V3NotifyRsp{
|
||||||
Code: gopay.SUCCESS,
|
Code: gopay.SUCCESS,
|
||||||
|
|||||||
49
backend/app/jobs/wechat_callback.go
Normal file
49
backend/app/jobs/wechat_callback.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package jobs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-pay/gopay/wechat/v3"
|
||||||
|
. "github.com/riverqueue/river"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
_ "go.ipao.vip/atom"
|
||||||
|
"go.ipao.vip/atom/contracts"
|
||||||
|
_ "go.ipao.vip/atom/contracts"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ contracts.JobArgs = (*WechatCallback)(nil)
|
||||||
|
|
||||||
|
type WechatCallback struct {
|
||||||
|
NotifyReq *wechat.V3NotifyReq `json:"notify_req"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s WechatCallback) InsertOpts() InsertOpts {
|
||||||
|
return InsertOpts{
|
||||||
|
Queue: QueueDefault,
|
||||||
|
Priority: PriorityDefault,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (WechatCallback) Kind() string { return "wechat_callback" }
|
||||||
|
func (a WechatCallback) UniqueID() string { return a.Kind() }
|
||||||
|
|
||||||
|
var _ Worker[WechatCallback] = (*WechatCallbackWorker)(nil)
|
||||||
|
|
||||||
|
// @provider(job)
|
||||||
|
type WechatCallbackWorker struct {
|
||||||
|
WorkerDefaults[WechatCallback]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WechatCallbackWorker) Work(ctx context.Context, job *Job[WechatCallback]) error {
|
||||||
|
log := log.WithField("job", job.Args.Kind())
|
||||||
|
|
||||||
|
log.Infof("[Start] Working on job with strings: %+v", job.Args)
|
||||||
|
defer log.Infof("[End] Finished %s", job.Args.Kind())
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WechatCallbackWorker) NextRetry(job *Job[WechatCallback]) time.Time {
|
||||||
|
return time.Now().Add(30 * time.Second)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user