feat: update pay notify
This commit is contained in:
@@ -4,12 +4,11 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"quyun/app/models"
|
||||
"quyun/providers/ali"
|
||||
"quyun/providers/app"
|
||||
"quyun/providers/job"
|
||||
"time"
|
||||
|
||||
. "github.com/riverqueue/river"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -18,7 +17,7 @@ import (
|
||||
_ "go.ipao.vip/atom/contracts"
|
||||
)
|
||||
|
||||
var _ contracts.JobArgs = (*WechatCallback)(nil)
|
||||
var _ contracts.JobArgs = (*DownloadFromAliOSS)(nil)
|
||||
|
||||
type DownloadFromAliOSS struct {
|
||||
MediaHash string `json:"media_hash"`
|
||||
|
||||
@@ -2,13 +2,12 @@ package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"quyun/app/models"
|
||||
"quyun/app/service/testx"
|
||||
"quyun/providers/ali"
|
||||
"quyun/providers/app"
|
||||
"quyun/providers/job"
|
||||
"testing"
|
||||
|
||||
. "github.com/riverqueue/river"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
@@ -147,7 +147,7 @@ func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
__job *job.Job,
|
||||
) (contracts.Initial, error) {
|
||||
obj := &WechatCallbackWorker{}
|
||||
obj := &WechatPayNotifyWorker{}
|
||||
if err := river.AddWorkerSafely(__job.Workers, obj); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"quyun/app/models"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
@@ -11,6 +9,7 @@ import (
|
||||
"quyun/providers/ali"
|
||||
"quyun/providers/app"
|
||||
"quyun/providers/job"
|
||||
"time"
|
||||
|
||||
. "github.com/riverqueue/river"
|
||||
"github.com/samber/lo"
|
||||
|
||||
@@ -3,13 +3,12 @@ package jobs
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"quyun/app/models"
|
||||
"quyun/database/fields"
|
||||
"quyun/pkg/utils"
|
||||
"quyun/providers/app"
|
||||
"quyun/providers/job"
|
||||
"time"
|
||||
|
||||
. "github.com/riverqueue/river"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"quyun/app/models"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
@@ -13,6 +11,7 @@ import (
|
||||
"quyun/providers/ali"
|
||||
"quyun/providers/app"
|
||||
"quyun/providers/job"
|
||||
"time"
|
||||
|
||||
. "github.com/riverqueue/river"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
@@ -3,8 +3,6 @@ package jobs
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"quyun/app/models"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
@@ -12,6 +10,7 @@ import (
|
||||
"quyun/providers/ali"
|
||||
"quyun/providers/app"
|
||||
"quyun/providers/job"
|
||||
"time"
|
||||
|
||||
. "github.com/riverqueue/river"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
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)
|
||||
}
|
||||
108
backend/app/jobs/wechat_pay_notify.go
Normal file
108
backend/app/jobs/wechat_pay_notify.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"quyun/app/models"
|
||||
"quyun/database/fields"
|
||||
"quyun/providers/wepay"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
. "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 = (*WechatPayNotify)(nil)
|
||||
|
||||
type WechatPayNotify struct {
|
||||
PayNotify *wepay.PayNotify `json:"notify_req"`
|
||||
}
|
||||
|
||||
func (s WechatPayNotify) InsertOpts() InsertOpts {
|
||||
return InsertOpts{
|
||||
Queue: QueueDefault,
|
||||
Priority: PriorityDefault,
|
||||
}
|
||||
}
|
||||
|
||||
func (WechatPayNotify) Kind() string { return "wechat_callback" }
|
||||
func (a WechatPayNotify) UniqueID() string { return a.Kind() }
|
||||
|
||||
var _ Worker[WechatPayNotify] = (*WechatPayNotifyWorker)(nil)
|
||||
|
||||
// @provider(job)
|
||||
type WechatPayNotifyWorker struct {
|
||||
WorkerDefaults[WechatPayNotify]
|
||||
}
|
||||
|
||||
func (w *WechatPayNotifyWorker) Work(ctx context.Context, job *Job[WechatPayNotify]) 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())
|
||||
|
||||
notify := job.Args.PayNotify
|
||||
|
||||
if notify.TradeState != "SUCCESS" {
|
||||
log.Warnf("TradeState is not SUCCESS for order %s", notify.OutTradeNo)
|
||||
return JobCancel(errors.New("TradeState is not SUCCESS"))
|
||||
}
|
||||
|
||||
order, err := models.Orders.GetByOrderNo(context.Background(), notify.OutTradeNo)
|
||||
if err != nil {
|
||||
log.Errorf("GetByOrderNo error:%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if order.Status != fields.OrderStatusPending {
|
||||
log.Infof("Order %s is paid, processing...", job.Args.PayNotify.OutTradeNo)
|
||||
return JobCancel(fmt.Errorf("Order already paid, currently status: %d", order.Status))
|
||||
}
|
||||
|
||||
needToPay := order.Price * int64(order.Discount) / 100
|
||||
|
||||
if notify.Amount.Total != needToPay {
|
||||
log.Errorf("Order %s amount mismatch: expected %d, got %d", job.Args.PayNotify.OutTradeNo, needToPay, notify.Amount.Total)
|
||||
return fmt.Errorf("amount mismatch for order %s", job.Args.PayNotify.OutTradeNo)
|
||||
}
|
||||
|
||||
order.TransactionID = notify.TransactionID
|
||||
order.Currency = notify.Amount.Currency
|
||||
order.PaymentMethod = notify.TradeType
|
||||
order.Status = fields.OrderStatusCompleted
|
||||
order.Meta = fields.ToJson(fields.OrderMeta{
|
||||
PayNotify: *notify,
|
||||
})
|
||||
|
||||
log.Infof("Updated order details: %+v", order)
|
||||
tx, err := models.Transaction(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Transaction error")
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
if err := models.Users.BuyPosts(context.Background(), order.UserID, order.PostID, order.Price); err != nil {
|
||||
log.Errorf("BuyPosts error:%v", err)
|
||||
return errors.Wrap(err, "BuyPosts error")
|
||||
}
|
||||
|
||||
if err := models.Orders.Update(context.Background(), order); err != nil {
|
||||
log.Errorf("Update order error:%v", err)
|
||||
return errors.Wrap(err, "Update order error")
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
log.Errorf("Commit error:%v", err)
|
||||
return errors.Wrap(err, "Commit error")
|
||||
}
|
||||
|
||||
log.Infof("Successfully processed order %s", notify.OutTradeNo)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *WechatPayNotifyWorker) NextRetry(job *Job[WechatPayNotify]) time.Time {
|
||||
return time.Now().Add(30 * time.Second)
|
||||
}
|
||||
86
backend/app/jobs/wechat_pay_notify_test.go
Normal file
86
backend/app/jobs/wechat_pay_notify_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"quyun/app/models"
|
||||
"quyun/app/service/testx"
|
||||
"quyun/providers/ali"
|
||||
"quyun/providers/app"
|
||||
"quyun/providers/job"
|
||||
"quyun/providers/wepay"
|
||||
"testing"
|
||||
|
||||
. "github.com/riverqueue/river"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type WechatPayNotifySuiteInjectParams struct {
|
||||
dig.In
|
||||
|
||||
Initials []contracts.Initial `group:"initials"` // nolint:structcheck
|
||||
Job *job.Job
|
||||
Oss *ali.OSSClient
|
||||
App *app.Config
|
||||
}
|
||||
|
||||
type WechatPayNotifySuite struct {
|
||||
suite.Suite
|
||||
|
||||
WechatPayNotifySuiteInjectParams
|
||||
}
|
||||
|
||||
func Test_WechatPayNotify(t *testing.T) {
|
||||
providers := testx.Default().With(Provide, models.Provide)
|
||||
|
||||
testx.Serve(providers, t, func(p WechatPayNotifySuiteInjectParams) {
|
||||
suite.Run(t, &WechatPayNotifySuite{WechatPayNotifySuiteInjectParams: p})
|
||||
})
|
||||
}
|
||||
|
||||
func (t *WechatPayNotifySuite) Test_Work() {
|
||||
Convey("test_work", t.T(), func() {
|
||||
Convey("step 1", func() {
|
||||
notify := `{
|
||||
"mchid": "1702644947",
|
||||
"appid": "wx47649361b6eba174",
|
||||
"out_trade_no": "20250430192543",
|
||||
"transaction_id": "4200002602202504300651871941",
|
||||
"trade_type": "JSAPI",
|
||||
"trade_state": "SUCCESS",
|
||||
"trade_state_desc": "支付成功",
|
||||
"bank_type": "OTHERS",
|
||||
"attach": "",
|
||||
"success_time": "2025-04-30T19:25:51+08:00",
|
||||
"payer": {
|
||||
"openid": "o5Bzk644x3LOMJsKSZRlqWin74IU"
|
||||
},
|
||||
"amount": {
|
||||
"total": 1,
|
||||
"payer_total": 1,
|
||||
"currency": "CNY",
|
||||
"payer_currency": "CNY"
|
||||
}
|
||||
}`
|
||||
|
||||
var payNotify wepay.PayNotify
|
||||
err := json.Unmarshal([]byte(notify), &payNotify)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
job := &Job[WechatPayNotify]{
|
||||
Args: WechatPayNotify{
|
||||
PayNotify: &payNotify,
|
||||
},
|
||||
}
|
||||
|
||||
worker := &WechatPayNotifyWorker{}
|
||||
|
||||
err = worker.Work(context.Background(), job)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user