feat: update transaction handling and order service logic
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -12,10 +12,56 @@ type Transaction struct{}
|
|||||||
|
|
||||||
// Create Order
|
// Create Order
|
||||||
//
|
//
|
||||||
|
// @Summary Create Order
|
||||||
|
// @Description 创建订单
|
||||||
|
// @Tags Transaction
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Param form body dto.OrderCreateForm true "订单创建参数"
|
||||||
|
// @Success 200 {object} dto.OrderCreateResponse
|
||||||
// @Router /v1/t/:tenantCode/orders [post]
|
// @Router /v1/t/:tenantCode/orders [post]
|
||||||
|
// @Bind form body
|
||||||
|
func (t *Transaction) Create(ctx fiber.Ctx, form *dto.OrderCreateForm) (*dto.OrderCreateResponse, error) {
|
||||||
|
tenantID := getTenantID(ctx)
|
||||||
|
uid := getUserID(ctx)
|
||||||
|
return services.Order.Create(ctx, tenantID, uid, form)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pay Order
|
||||||
|
//
|
||||||
|
// @Summary Pay Order
|
||||||
|
// @Description 支付订单
|
||||||
|
// @Tags Transaction
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Param id path int64 true "订单ID"
|
||||||
|
// @Param form body dto.OrderPayForm true "支付参数"
|
||||||
|
// @Success 200 {object} dto.OrderPayResponse
|
||||||
// @Router /v1/t/:tenantCode/orders/:id<int>/pay [post]
|
// @Router /v1/t/:tenantCode/orders/:id<int>/pay [post]
|
||||||
|
// @Bind id path
|
||||||
|
// @Bind form body
|
||||||
|
func (t *Transaction) Pay(ctx fiber.Ctx, id int64, form *dto.OrderPayForm) (*dto.OrderPayResponse, error) {
|
||||||
|
tenantID := getTenantID(ctx)
|
||||||
|
uid := getUserID(ctx)
|
||||||
|
return services.Order.Pay(ctx, tenantID, uid, id, form)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Order Status
|
||||||
|
//
|
||||||
|
// @Summary Order Status
|
||||||
|
// @Description 查询订单状态
|
||||||
|
// @Tags Transaction
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Param id path int64 true "订单ID"
|
||||||
|
// @Success 200 {object} dto.OrderStatusResponse
|
||||||
// @Router /v1/t/:tenantCode/orders/:id<int>/status [get]
|
// @Router /v1/t/:tenantCode/orders/:id<int>/status [get]
|
||||||
// @Router /v1/t/:tenantCode/webhook/payment/notify [post]
|
// @Bind id path
|
||||||
|
func (t *Transaction) Status(ctx fiber.Ctx, id int64) (*dto.OrderStatusResponse, error) {
|
||||||
|
tenantID := getTenantID(ctx)
|
||||||
|
uid := getUserID(ctx)
|
||||||
|
return services.Order.Status(ctx, tenantID, uid, id)
|
||||||
|
}
|
||||||
|
|
||||||
// @Summary Payment Webhook
|
// @Summary Payment Webhook
|
||||||
// @Description Payment Webhook
|
// @Description Payment Webhook
|
||||||
@@ -24,6 +70,7 @@ type Transaction struct{}
|
|||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param form body dto.PaymentWebhookForm true "Webhook Data"
|
// @Param form body dto.PaymentWebhookForm true "Webhook Data"
|
||||||
// @Success 200 {string} string "success"
|
// @Success 200 {string} string "success"
|
||||||
|
// @Router /v1/t/:tenantCode/webhook/payment/notify [post]
|
||||||
// @Bind form body
|
// @Bind form body
|
||||||
func (t *Transaction) Webhook(ctx fiber.Ctx, form *dto.PaymentWebhookForm) (string, error) {
|
func (t *Transaction) Webhook(ctx fiber.Ctx, form *dto.PaymentWebhookForm) (string, error) {
|
||||||
tenantID := getTenantID(ctx)
|
tenantID := getTenantID(ctx)
|
||||||
|
|||||||
@@ -238,14 +238,21 @@ func (s *order) Pay(
|
|||||||
return nil, errorx.ErrStatusConflict.WithMsg("订单状态不可支付")
|
return nil, errorx.ErrStatusConflict.WithMsg("订单状态不可支付")
|
||||||
}
|
}
|
||||||
|
|
||||||
if form.Method == "balance" {
|
switch form.Method {
|
||||||
|
case "balance":
|
||||||
return s.payWithBalance(ctx, o)
|
return s.payWithBalance(ctx, o)
|
||||||
|
case "alipay", "external":
|
||||||
|
// mock external: 标记已支付,避免前端卡住
|
||||||
|
if err := s.settleOrder(ctx, o, "external", ""); err != nil {
|
||||||
|
if _, ok := err.(*errorx.AppError); ok {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||||
|
}
|
||||||
|
return &transaction_dto.OrderPayResponse{PayParams: "mock_pay_params"}, nil
|
||||||
|
default:
|
||||||
|
return nil, errorx.ErrBadRequest.WithMsg("unsupported payment method")
|
||||||
}
|
}
|
||||||
|
|
||||||
// External payment (mock) - normally returns URL/params
|
|
||||||
return &transaction_dto.OrderPayResponse{
|
|
||||||
PayParams: "mock_pay_params",
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessExternalPayment handles callback from payment gateway
|
// ProcessExternalPayment handles callback from payment gateway
|
||||||
@@ -308,13 +315,12 @@ func (s *order) settleOrder(ctx context.Context, o *models.Order, method, extern
|
|||||||
|
|
||||||
// 2. Update Order Status
|
// 2. Update Order Status
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
// snapshot := o.Snapshot // Preserve existing snapshot or update it with external ID
|
|
||||||
// TODO: Update snapshot with payment info
|
|
||||||
_, err := tx.Order.WithContext(ctx).Where(tx.Order.ID.Eq(o.ID)).Updates(&models.Order{
|
_, err := tx.Order.WithContext(ctx).Where(tx.Order.ID.Eq(o.ID)).Updates(&models.Order{
|
||||||
Status: consts.OrderStatusPaid,
|
Status: consts.OrderStatusPaid,
|
||||||
PaidAt: now,
|
PaidAt: now,
|
||||||
UpdatedAt: now,
|
UpdatedAt: now,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -355,7 +361,15 @@ func (s *order) settleOrder(ctx context.Context, o *models.Order, method, extern
|
|||||||
fee := int64(float64(amount) * 0.10)
|
fee := int64(float64(amount) * 0.10)
|
||||||
creatorIncome := amount - fee
|
creatorIncome := amount - fee
|
||||||
|
|
||||||
// Credit Tenant Owner Balance (Net Income)
|
// Credit Tenant Owner Balance (Net Income) 并记录余额快照
|
||||||
|
owner, err := tx.User.WithContext(ctx).
|
||||||
|
Where(tx.User.ID.Eq(tenantOwnerID)).
|
||||||
|
First()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
balanceBefore := owner.Balance
|
||||||
|
balanceAfter := balanceBefore + creatorIncome
|
||||||
_, err = tx.User.WithContext(ctx).
|
_, err = tx.User.WithContext(ctx).
|
||||||
Where(tx.User.ID.Eq(tenantOwnerID)).
|
Where(tx.User.ID.Eq(tenantOwnerID)).
|
||||||
Update(tx.User.Balance, gorm.Expr("balance + ?", creatorIncome))
|
Update(tx.User.Balance, gorm.Expr("balance + ?", creatorIncome))
|
||||||
@@ -369,8 +383,8 @@ func (s *order) settleOrder(ctx context.Context, o *models.Order, method, extern
|
|||||||
OrderID: o.ID,
|
OrderID: o.ID,
|
||||||
Type: consts.TenantLedgerTypeDebitPurchase, // Income from purchase
|
Type: consts.TenantLedgerTypeDebitPurchase, // Income from purchase
|
||||||
Amount: creatorIncome,
|
Amount: creatorIncome,
|
||||||
BalanceBefore: 0, // TODO
|
BalanceBefore: balanceBefore,
|
||||||
BalanceAfter: 0, // TODO
|
BalanceAfter: balanceAfter,
|
||||||
FrozenBefore: 0,
|
FrozenBefore: 0,
|
||||||
FrozenAfter: 0,
|
FrozenAfter: 0,
|
||||||
IdempotencyKey: uuid.NewString(),
|
IdempotencyKey: uuid.NewString(),
|
||||||
|
|||||||
Reference in New Issue
Block a user