feat: add refund statuses
This commit is contained in:
@@ -61,9 +61,11 @@ func (ctl *orders) Refund(ctx fiber.Ctx, id int64) error {
|
||||
meta.RefundResp = resp
|
||||
order.Meta = fields.ToJson(meta)
|
||||
order.RefundTransactionID = resp.RefundId
|
||||
order.Status = fields.OrderStatusRefundProcessing
|
||||
|
||||
if err := models.Orders.Update(ctx.Context(), order); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"quyun/providers/app"
|
||||
"quyun/providers/job"
|
||||
"quyun/providers/jwt"
|
||||
"quyun/providers/wepay"
|
||||
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
@@ -35,8 +36,12 @@ func Provide(opts ...opt.Option) error {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*orders, error) {
|
||||
obj := &orders{}
|
||||
if err := container.Container.Provide(func(
|
||||
wepay *wepay.Client,
|
||||
) (*orders, error) {
|
||||
obj := &orders{
|
||||
wepay: wepay,
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"quyun/providers/job"
|
||||
"quyun/providers/wepay"
|
||||
|
||||
"github.com/go-pay/gopay"
|
||||
"github.com/go-pay/gopay/wechat/v3"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -25,20 +24,21 @@ type pays struct {
|
||||
// @Bind channel path
|
||||
func (ctl *pays) Callback(ctx fiber.Ctx, channel string) error {
|
||||
log := log.WithField("method", "pays.Callback")
|
||||
|
||||
notify, err := ctl.wepay.ParseNotify(ctx)
|
||||
if err != nil {
|
||||
log.Errorf("ParseNotify error:%v", err)
|
||||
return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{"error": "Failed to parse notify"})
|
||||
}
|
||||
|
||||
if err := ctl.job.Add(&jobs.WechatPayNotify{PayNotify: notify}); 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: "成功",
|
||||
})
|
||||
return ctl.wepay.ParseNotify(
|
||||
ctx,
|
||||
func(ctx fiber.Ctx, notify *wechat.V3DecryptPayResult) error {
|
||||
if err := ctl.job.Add(&jobs.WechatPayNotify{Notify: notify}); err != nil {
|
||||
log.Errorf("add job error:%v", err)
|
||||
return ctx.Status(http.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to add job"})
|
||||
}
|
||||
return nil
|
||||
},
|
||||
func(c fiber.Ctx, notify *wechat.V3DecryptRefundResult) error {
|
||||
if err := ctl.job.Add(&jobs.WechatRefundNotify{Notify: notify}); err != nil {
|
||||
log.Errorf("add job error:%v", err)
|
||||
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to add job"})
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
100
backend/app/jobs/wechat_refund_notify.go
Normal file
100
backend/app/jobs/wechat_refund_notify.go
Normal 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)
|
||||
}
|
||||
@@ -448,3 +448,21 @@ func (m *usersModel) BuyPosts(ctx context.Context, userID, postID, price int64)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *usersModel) RevokePosts(ctx context.Context, userID, postID int64) error {
|
||||
tbl := table.UserPosts
|
||||
stmt := tbl.
|
||||
DELETE().
|
||||
WHERE(
|
||||
tbl.UserID.EQ(Int64(userID)).AND(
|
||||
tbl.PostID.EQ(Int64(postID)),
|
||||
),
|
||||
)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log.Errorf("error revoking user post: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user