refactor: 移除wepay依赖,简化订单退款逻辑
This commit is contained in:
@@ -1,16 +1,11 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/fields"
|
||||
"quyun/v2/providers/wepay"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/pkg/errors"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
@@ -20,9 +15,7 @@ type OrderListQuery struct {
|
||||
}
|
||||
|
||||
// @provider
|
||||
type orders struct {
|
||||
wepay *wepay.Client
|
||||
}
|
||||
type orders struct{}
|
||||
|
||||
// List
|
||||
//
|
||||
@@ -62,55 +55,5 @@ func (ctl *orders) List(
|
||||
// @Router /admin/orders/:id/refund [post]
|
||||
// @Bind order path key(id) model(id)
|
||||
func (ctl *orders) Refund(ctx fiber.Ctx, order *models.Order) error {
|
||||
user, err := services.Users.FindByID(ctx, order.UserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
post, err := services.Posts.FindByID(ctx, order.PostID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if order.PaymentMethod == "balance" {
|
||||
if err := services.Users.AddBalance(ctx, user.ID, order.Meta.Data().CostBalance); err != nil {
|
||||
return errors.Wrap(err, "add balance failed")
|
||||
}
|
||||
|
||||
if err := services.Users.RevokeUserPosts(ctx, user.ID, order.PostID); err != nil {
|
||||
return errors.Wrap(err, "revoke posts failed")
|
||||
}
|
||||
|
||||
order.Status = fields.OrderStatusRefundSuccess
|
||||
if _, err := order.Update(ctx); err != nil {
|
||||
return errors.Wrap(err, "update order failed")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
refundTotal := order.Price*int64(order.Discount)/100 - order.Meta.Data().CostBalance
|
||||
resp, err := ctl.wepay.Refund(ctx, func(bm *wepay.BodyMap) {
|
||||
bm.
|
||||
OutRefundNo(order.OrderNo).
|
||||
OutTradeNo(order.OrderNo).
|
||||
TransactionID(order.TransactionID).
|
||||
CNYRefundAmount(refundTotal, refundTotal).
|
||||
RefundReason(fmt.Sprintf("%s 退款", post.Title))
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
meta := order.Meta.Data()
|
||||
meta.RefundResp = resp
|
||||
order.Meta = meta.JsonType()
|
||||
order.RefundTransactionID = resp.RefundId
|
||||
order.Status = fields.OrderStatusRefundProcessing
|
||||
|
||||
if _, err := order.Update(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return services.Orders.Refund(ctx, order)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"quyun/v2/providers/app"
|
||||
"quyun/v2/providers/job"
|
||||
"quyun/v2/providers/jwt"
|
||||
"quyun/v2/providers/wepay"
|
||||
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
@@ -37,12 +36,8 @@ func Provide(opts ...opt.Option) error {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
wepay *wepay.Client,
|
||||
) (*orders, error) {
|
||||
obj := &orders{
|
||||
wepay: wepay,
|
||||
}
|
||||
if err := container.Container.Provide(func() (*orders, error) {
|
||||
obj := &orders{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"quyun/v2/providers/ali"
|
||||
"quyun/v2/providers/app"
|
||||
"quyun/v2/providers/job"
|
||||
"quyun/v2/providers/wepay"
|
||||
|
||||
"github.com/go-pay/gopay/wechat/v3"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
@@ -30,10 +29,9 @@ type ListQuery struct {
|
||||
|
||||
// @provider
|
||||
type posts struct {
|
||||
wepay *wepay.Client
|
||||
oss *ali.OSSClient
|
||||
job *job.Job
|
||||
app *app.Config
|
||||
oss *ali.OSSClient
|
||||
job *job.Job
|
||||
app *app.Config
|
||||
}
|
||||
|
||||
// List posts
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"quyun/v2/providers/ali"
|
||||
"quyun/v2/providers/app"
|
||||
"quyun/v2/providers/job"
|
||||
"quyun/v2/providers/wepay"
|
||||
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
@@ -18,13 +17,11 @@ func Provide(opts ...opt.Option) error {
|
||||
app *app.Config,
|
||||
job *job.Job,
|
||||
oss *ali.OSSClient,
|
||||
wepay *wepay.Client,
|
||||
) (*posts, error) {
|
||||
obj := &posts{
|
||||
app: app,
|
||||
job: job,
|
||||
oss: oss,
|
||||
wepay: wepay,
|
||||
app: app,
|
||||
job: job,
|
||||
oss: oss,
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
|
||||
@@ -77,12 +77,12 @@ func (m *orders) List(
|
||||
}
|
||||
|
||||
// Refund 订单退款(余额支付走本地退款;微信支付走微信退款并标记为退款处理中)。
|
||||
func (m *orders) Refund(ctx context.Context, id int64) error {
|
||||
func (m *orders) Refund(ctx context.Context, order *models.Order) error {
|
||||
// 余额支付:这里强调“状态一致性”,必须在一个事务中完成:余额退回 + 撤销购买权益 + 更新订单状态。
|
||||
return models.Q.Transaction(func(tx *models.Query) error {
|
||||
order, err := tx.Order.WithContext(ctx).GetByID(id)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get order in tx")
|
||||
// 已移除微信退款(wepay)能力:仅支持余额支付订单退款。
|
||||
if order.PaymentMethod != "balance" {
|
||||
return errors.New("暂不支持该支付方式退款")
|
||||
}
|
||||
|
||||
// 退回余额(使用原子自增,避免并发覆盖)。
|
||||
|
||||
Reference in New Issue
Block a user