feat: fix pay by balance
This commit is contained in:
@@ -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
|
payPrice := post.Price * int64(post.Discount) / 100
|
||||||
if user.Balance >= payPrice {
|
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 {
|
if err := ctl.job.Add(&jobs.BalancePayNotify{OrderNo: order.OrderNo}); err != nil {
|
||||||
log.Errorf("add job error:%v", err)
|
log.Errorf("add job error:%v", err)
|
||||||
|
|||||||
@@ -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))
|
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.PaymentMethod = "balance"
|
||||||
order.Status = fields.OrderStatusCompleted
|
order.Status = fields.OrderStatusCompleted
|
||||||
|
|
||||||
|
meta := order.Meta.Data
|
||||||
|
meta.CostBalance = payPrice
|
||||||
|
order.Meta = fields.ToJson(meta)
|
||||||
|
|
||||||
log.Infof("Updated order details: %+v", order)
|
log.Infof("Updated order details: %+v", order)
|
||||||
tx, err := models.Transaction(ctx)
|
tx, err := models.Transaction(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -65,6 +78,13 @@ func (w *BalancePayNotifyWorker) Work(ctx context.Context, job *Job[BalancePayNo
|
|||||||
}
|
}
|
||||||
defer tx.Rollback()
|
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 {
|
if err := models.Users.BuyPosts(context.Background(), order.UserID, order.PostID, order.Price); err != nil {
|
||||||
log.Errorf("BuyPosts error:%v", err)
|
log.Errorf("BuyPosts error:%v", err)
|
||||||
return errors.Wrap(err, "BuyPosts error")
|
return errors.Wrap(err, "BuyPosts error")
|
||||||
|
|||||||
@@ -12,4 +12,5 @@ type OrderMeta struct {
|
|||||||
PayNotify *wechat.V3DecryptPayResult `json:"pay_notify"`
|
PayNotify *wechat.V3DecryptPayResult `json:"pay_notify"`
|
||||||
RefundResp *wechat.RefundOrderResponse `json:"refund_resp"`
|
RefundResp *wechat.RefundOrderResponse `json:"refund_resp"`
|
||||||
RefundNotify *wechat.V3DecryptRefundResult `json:"refund_notify"`
|
RefundNotify *wechat.V3DecryptRefundResult `json:"refund_notify"`
|
||||||
|
CostBalance int64 `json:"cost_balance"` // 余额支付的金额
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user