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).
|
||||
|
||||
88
backend/app/jobs/balance_pay_notify.go
Normal file
88
backend/app/jobs/balance_pay_notify.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"quyun/app/models"
|
||||
"quyun/database/fields"
|
||||
|
||||
"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 = (*BalancePayNotify)(nil)
|
||||
|
||||
type BalancePayNotify struct {
|
||||
OrderNo string `json:"order_no"`
|
||||
}
|
||||
|
||||
func (s BalancePayNotify) InsertOpts() InsertOpts {
|
||||
return InsertOpts{
|
||||
Queue: QueueDefault,
|
||||
Priority: PriorityDefault,
|
||||
}
|
||||
}
|
||||
|
||||
func (BalancePayNotify) Kind() string { return "balance_pay_notify" }
|
||||
func (a BalancePayNotify) UniqueID() string { return a.Kind() }
|
||||
|
||||
var _ Worker[BalancePayNotify] = (*BalancePayNotifyWorker)(nil)
|
||||
|
||||
// @provider(job)
|
||||
type BalancePayNotifyWorker struct {
|
||||
WorkerDefaults[BalancePayNotify]
|
||||
}
|
||||
|
||||
func (w *BalancePayNotifyWorker) Work(ctx context.Context, job *Job[BalancePayNotify]) 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())
|
||||
|
||||
order, err := models.Orders.GetByOrderNo(context.Background(), job.Args.OrderNo)
|
||||
if err != nil {
|
||||
log.Errorf("GetByOrderNo error:%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if order.Status != fields.OrderStatusPending {
|
||||
log.Infof("Order %s is paid, processing...", job.Args.OrderNo)
|
||||
return JobCancel(fmt.Errorf("Order already paid, currently status: %d", order.Status))
|
||||
}
|
||||
|
||||
order.PaymentMethod = "balance"
|
||||
order.Status = fields.OrderStatusCompleted
|
||||
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 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")
|
||||
}
|
||||
|
||||
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", order.OrderNo)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *BalancePayNotifyWorker) NextRetry(job *Job[BalancePayNotify]) time.Time {
|
||||
return time.Now().Add(30 * time.Second)
|
||||
}
|
||||
@@ -474,3 +474,21 @@ func (m *usersModel) RevokePosts(ctx context.Context, userID, postID int64) erro
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetBalance
|
||||
func (m *usersModel) SetBalance(ctx context.Context, id int64, balance int64) error {
|
||||
tbl := table.Users
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.Balance).
|
||||
SET(Int64(balance)).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)),
|
||||
)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log.Errorf("error updating user balance: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user