This commit is contained in:
109
backend_v1/app/http/admin/orders.go
Normal file
109
backend_v1/app/http/admin/orders.go
Normal file
@@ -0,0 +1,109 @@
|
||||
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"
|
||||
)
|
||||
|
||||
type OrderListQuery struct {
|
||||
OrderNumber *string `query:"order_number"`
|
||||
UserID *int64 `query:"user_id"`
|
||||
}
|
||||
|
||||
// @provider
|
||||
type orders struct {
|
||||
wepay *wepay.Client
|
||||
}
|
||||
|
||||
// List users
|
||||
//
|
||||
// @Router /admin/orders [get]
|
||||
// @Bind pagination query
|
||||
// @Bind query query
|
||||
func (ctl *orders) List(
|
||||
ctx fiber.Ctx,
|
||||
pagination *requests.Pagination,
|
||||
query *OrderListQuery,
|
||||
) (*requests.Pager, error) {
|
||||
conds := []gen.Condition{}
|
||||
if query.OrderNumber != nil {
|
||||
conds = append(conds, models.OrderQuery.OrderNo.Eq(*query.OrderNumber))
|
||||
}
|
||||
|
||||
if query.UserID != nil {
|
||||
conds = append(conds, models.OrderQuery.UserID.Eq(*query.UserID))
|
||||
}
|
||||
|
||||
return services.Orders.List(ctx, pagination, conds...)
|
||||
}
|
||||
|
||||
// Refund
|
||||
// @Router /admin/orders/:id/refund [post]
|
||||
// @Bind id path
|
||||
func (ctl *orders) Refund(ctx fiber.Ctx, id int64) error {
|
||||
order, err := services.Orders.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user