feat: support balance pay
This commit is contained in:
@@ -5,12 +5,14 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"quyun/app/jobs"
|
||||
"quyun/app/models"
|
||||
"quyun/app/requests"
|
||||
"quyun/database/conds"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/providers/ali"
|
||||
"quyun/providers/job"
|
||||
"quyun/providers/wepay"
|
||||
|
||||
"github.com/go-pay/gopay/wechat/v3"
|
||||
@@ -28,6 +30,7 @@ type ListQuery struct {
|
||||
type posts struct {
|
||||
wepay *wepay.Client
|
||||
oss *ali.OSSClient
|
||||
job *job.Job
|
||||
}
|
||||
|
||||
// List posts
|
||||
@@ -276,6 +279,22 @@ 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)
|
||||
return nil, errors.Wrap(err, "Failed to add job")
|
||||
}
|
||||
|
||||
return &wechat.JSAPIPayParams{
|
||||
AppId: "balance",
|
||||
}, nil
|
||||
}
|
||||
|
||||
prePayResp, err := ctl.wepay.V3TransactionJsapi(ctx.Context(), func(bm *wepay.BodyMap) {
|
||||
bm.
|
||||
Expire(30 * time.Minute).
|
||||
|
||||
Reference in New Issue
Block a user