feat: update metas
This commit is contained in:
@@ -64,8 +64,13 @@ func (w *WechatPayNotifyWorker) Work(ctx context.Context, job *Job[WechatPayNoti
|
||||
return JobCancel(fmt.Errorf("Order already paid, currently status: %d", order.Status))
|
||||
}
|
||||
|
||||
needToPay := order.Price * int64(order.Discount) / 100
|
||||
user, err := models.Users.GetByID(context.Background(), order.UserID)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "get user by id(%d) failed", order.UserID)
|
||||
}
|
||||
meta := order.Meta.Data
|
||||
|
||||
needToPay := order.Price*int64(order.Discount)/100 - meta.CostBalance
|
||||
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)
|
||||
@@ -75,9 +80,9 @@ func (w *WechatPayNotifyWorker) Work(ctx context.Context, job *Job[WechatPayNoti
|
||||
order.Currency = notify.Amount.Currency
|
||||
order.PaymentMethod = notify.TradeType
|
||||
order.Status = fields.OrderStatusCompleted
|
||||
order.Meta = fields.ToJson(fields.OrderMeta{
|
||||
PayNotify: notify,
|
||||
})
|
||||
|
||||
meta.PayNotify = notify
|
||||
order.Meta = fields.ToJson(meta)
|
||||
|
||||
log.Infof("Updated order details: %+v", order)
|
||||
tx, err := models.Transaction(ctx)
|
||||
@@ -86,6 +91,13 @@ func (w *WechatPayNotifyWorker) Work(ctx context.Context, job *Job[WechatPayNoti
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
// update user balance
|
||||
err = models.Users.SetBalance(ctx, user.ID, user.Balance-meta.CostBalance)
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user