feat: fix pay by balance

This commit is contained in:
Rogee
2025-05-13 09:03:30 +08:00
parent 10277a6db8
commit ec8a1558b8
3 changed files with 21 additions and 4 deletions

View File

@@ -280,10 +280,6 @@ func (ctl *posts) Buy(ctx fiber.Ctx, id int64, user *model.Users) (*wechat.JSAPI
payPrice := post.Price * int64(post.Discount) / 100
if user.Balance >= payPrice {
err = models.Users.SetBalance(ctx.Context(), user.ID, user.Balance-payPrice)
if err != nil {
return nil, errors.Wrap(err, "余额支付失败")
}
if err := ctl.job.Add(&jobs.BalancePayNotify{OrderNo: order.OrderNo}); err != nil {
log.Errorf("add job error:%v", err)

View File

@@ -56,8 +56,21 @@ func (w *BalancePayNotifyWorker) Work(ctx context.Context, job *Job[BalancePayNo
return JobCancel(fmt.Errorf("Order already paid, currently status: %d", order.Status))
}
user, err := models.Users.GetByID(context.Background(), order.UserID)
if err != nil {
log.Errorf("GetByID error:%v", err)
return errors.Wrap(err, "get user error")
}
payPrice := order.Price * int64(order.Discount) / 100
order.PaymentMethod = "balance"
order.Status = fields.OrderStatusCompleted
meta := order.Meta.Data
meta.CostBalance = payPrice
order.Meta = fields.ToJson(meta)
log.Infof("Updated order details: %+v", order)
tx, err := models.Transaction(ctx)
if err != nil {
@@ -65,6 +78,13 @@ func (w *BalancePayNotifyWorker) Work(ctx context.Context, job *Job[BalancePayNo
}
defer tx.Rollback()
// update user balance
err = models.Users.SetBalance(ctx, user.ID, user.Balance-payPrice)
if err != nil {
log.WithError(err).Error("SetBalance error")
return JobCancel(errors.Wrap(err, "set user balance failed"))
}
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")

View File

@@ -12,4 +12,5 @@ type OrderMeta struct {
PayNotify *wechat.V3DecryptPayResult `json:"pay_notify"`
RefundResp *wechat.RefundOrderResponse `json:"refund_resp"`
RefundNotify *wechat.V3DecryptRefundResult `json:"refund_notify"`
CostBalance int64 `json:"cost_balance"` // 余额支付的金额
}