feat: add refund statuses

This commit is contained in:
Rogee
2025-05-06 20:23:06 +08:00
parent b7ebdf1ce6
commit 41cdc821da
13 changed files with 266 additions and 114 deletions

View File

@@ -156,5 +156,17 @@ func Provide(opts ...opt.Option) error {
}, atom.GroupInitial); err != nil {
return err
}
if err := container.Container.Provide(func(
__job *job.Job,
) (contracts.Initial, error) {
obj := &WechatRefundNotifyWorker{}
if err := river.AddWorkerSafely(__job.Workers, obj); err != nil {
return nil, err
}
return obj, nil
}, atom.GroupInitial); err != nil {
return err
}
return nil
}

View File

@@ -7,8 +7,8 @@ import (
"quyun/app/models"
"quyun/database/fields"
"quyun/providers/wepay"
"github.com/go-pay/gopay/wechat/v3"
"github.com/pkg/errors"
. "github.com/riverqueue/river"
log "github.com/sirupsen/logrus"
@@ -20,7 +20,7 @@ import (
var _ contracts.JobArgs = (*WechatPayNotify)(nil)
type WechatPayNotify struct {
PayNotify *wepay.PayNotify `json:"notify_req"`
Notify *wechat.V3DecryptPayResult `json:"notify"`
}
func (s WechatPayNotify) InsertOpts() InsertOpts {
@@ -46,7 +46,7 @@ func (w *WechatPayNotifyWorker) Work(ctx context.Context, job *Job[WechatPayNoti
log.Infof("[Start] Working on job with strings: %+v", job.Args)
defer log.Infof("[End] Finished %s", job.Args.Kind())
notify := job.Args.PayNotify
notify := job.Args.Notify
if notify.TradeState != "SUCCESS" {
log.Warnf("TradeState is not SUCCESS for order %s", notify.OutTradeNo)
@@ -60,23 +60,23 @@ func (w *WechatPayNotifyWorker) Work(ctx context.Context, job *Job[WechatPayNoti
}
if order.Status != fields.OrderStatusPending {
log.Infof("Order %s is paid, processing...", job.Args.PayNotify.OutTradeNo)
log.Infof("Order %s is paid, processing...", job.Args.Notify.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)
if int64(notify.Amount.Total) != needToPay {
log.Errorf("Order %s amount mismatch: expected %d, got %d", job.Args.Notify.OutTradeNo, needToPay, notify.Amount.Total)
return fmt.Errorf("amount mismatch for order %s", job.Args.Notify.OutTradeNo)
}
order.TransactionID = notify.TransactionID
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,
PayNotify: notify,
})
log.Infof("Updated order details: %+v", order)

View File

@@ -7,8 +7,8 @@ import (
"quyun/app/models"
"quyun/app/service/testx"
"quyun/providers/wepay"
"github.com/go-pay/gopay/wechat/v3"
. "github.com/riverqueue/river"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/suite"
@@ -42,13 +42,13 @@ func (t *WechatPayNotifySuite) Test_Work() {
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
var payNotify wechat.V3DecryptPayResult
err := json.Unmarshal([]byte(notify), &payNotify)
So(err, ShouldBeNil)
job := &Job[WechatPayNotify]{
Args: WechatPayNotify{
PayNotify: &payNotify,
Notify: &payNotify,
},
}

View File

@@ -0,0 +1,100 @@
package jobs
import (
"context"
"time"
"quyun/app/models"
"quyun/database/fields"
"github.com/go-pay/gopay/wechat/v3"
"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 = (*WechatRefundNotify)(nil)
type WechatRefundNotify struct {
Notify *wechat.V3DecryptRefundResult `json:"notify"`
}
func (s WechatRefundNotify) InsertOpts() InsertOpts {
return InsertOpts{
Queue: QueueDefault,
Priority: PriorityDefault,
}
}
func (WechatRefundNotify) Kind() string { return "wechat_refund_notify" }
func (a WechatRefundNotify) UniqueID() string { return a.Kind() }
var _ Worker[WechatRefundNotify] = (*WechatRefundNotifyWorker)(nil)
// @provider(job)
type WechatRefundNotifyWorker struct {
WorkerDefaults[WechatRefundNotify]
}
func (w *WechatRefundNotifyWorker) Work(ctx context.Context, job *Job[WechatRefundNotify]) 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.Notify
order, err := models.Orders.GetByOrderNo(context.Background(), notify.OutTradeNo)
if err != nil {
log.Errorf("GetByOrderNo error:%v", err)
return err
}
meta := order.Meta.Data
meta.RefundNotify = notify
order.Status = fields.OrderStatusRefundProcessing
switch notify.RefundStatus {
case "SUCCESS":
order.Status = fields.OrderStatusRefundSuccess
case "CLOSED":
order.Status = fields.OrderStatusRefundClosed
case "PROCESSING":
order.Status = fields.OrderStatusRefundProcessing
case "ABNORMAL":
order.Status = fields.OrderStatusRefundAbnormal
}
order.Meta = fields.ToJson(meta)
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 order.Status == fields.OrderStatusRefundSuccess {
if err := models.Users.RevokePosts(context.Background(), order.UserID, order.PostID); err != nil {
log.Errorf("RevokePosts error:%v", err)
return errors.Wrap(err, "RevokePosts 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 *WechatRefundNotifyWorker) NextRetry(job *Job[WechatRefundNotify]) time.Time {
return time.Now().Add(30 * time.Second)
}