fix(order): 支持客服跨站处理订单与退款
This commit is contained in:
@@ -21,6 +21,7 @@ func (r *Repository) StartRefund(ctx context.Context, orderID uint64, refundAmou
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx = scopedTenantContext(ctx, originalPayment.TenantID)
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, &originalPayment)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
@@ -165,6 +166,11 @@ func (r *Repository) StartRefund(ctx context.Context, orderID uint64, refundAmou
|
||||
}
|
||||
func (r *Repository) QueryRefundStatus(ctx context.Context, orderID uint64) (*RefundDTO, error) {
|
||||
var payment model.PaymentOrder
|
||||
tenantID, err := r.paymentTenantIDForOrder(ctx, orderID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx = scopedTenantContext(ctx, tenantID)
|
||||
query := r.db.WithContext(ctx).Where("order_id = ? AND biz_type IN ?", orderID, refundBizTypes)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
@@ -201,9 +207,39 @@ func (r *Repository) SyncRefundStatusByPaymentID(ctx context.Context, paymentID
|
||||
return &dto, nil
|
||||
}
|
||||
func (r *Repository) findOriginalPayment(ctx context.Context, orderID uint64) (model.PaymentOrder, error) {
|
||||
tenantID, err := r.paymentTenantIDForOrder(ctx, orderID)
|
||||
if err != nil {
|
||||
return model.PaymentOrder{}, err
|
||||
}
|
||||
return r.findOriginalPaymentInTenant(ctx, orderID, tenantID)
|
||||
}
|
||||
|
||||
func (r *Repository) paymentTenantIDForOrder(ctx context.Context, orderID uint64) (uint64, error) {
|
||||
var order struct {
|
||||
BuyerTenantID uint64
|
||||
PaymentTenantID uint64
|
||||
}
|
||||
if err := r.db.WithContext(ctx).
|
||||
Model(&model.RentalOrder{}).
|
||||
Select("buyer_tenant_id, payment_tenant_id").
|
||||
Where("id = ?", orderID).
|
||||
Take(&order).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, ErrPaymentNotFound
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if order.PaymentTenantID > 0 {
|
||||
return order.PaymentTenantID, nil
|
||||
}
|
||||
return order.BuyerTenantID, nil
|
||||
}
|
||||
|
||||
func (r *Repository) findOriginalPaymentInTenant(ctx context.Context, orderID uint64, tenantID uint64) (model.PaymentOrder, error) {
|
||||
var originalPayment model.PaymentOrder
|
||||
query := r.db.WithContext(ctx).Where("order_id = ? AND status = 'paid' AND biz_type = 'order_pay'", orderID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query := r.db.WithContext(scopedTenantContext(ctx, tenantID)).
|
||||
Where("order_id = ? AND status = 'paid' AND biz_type = 'order_pay'", orderID)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.Order("id DESC").First(&originalPayment).Error; err != nil {
|
||||
@@ -400,10 +436,16 @@ func refundQueryRequest(payment model.PaymentOrder, originalPayment model.Paymen
|
||||
}
|
||||
|
||||
func (r *Repository) findOriginalPaymentForRefund(ctx context.Context, payment *model.PaymentOrder) (model.PaymentOrder, error) {
|
||||
if payment != nil {
|
||||
ctx = scopedTenantContext(ctx, payment.TenantID)
|
||||
}
|
||||
if payment != nil && isMohongRefundBizType(payment.BizType) {
|
||||
return r.findOriginalMohongPayment(ctx, payment.OrderID)
|
||||
}
|
||||
return r.findOriginalPayment(ctx, payment.OrderID)
|
||||
if payment == nil {
|
||||
return model.PaymentOrder{}, ErrPaymentNotFound
|
||||
}
|
||||
return r.findOriginalPaymentInTenant(ctx, payment.OrderID, payment.TenantID)
|
||||
}
|
||||
|
||||
func (r *Repository) updateRefundedOrderStatus(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string) error {
|
||||
|
||||
Reference in New Issue
Block a user