继续补齐核心模块 Context 超时控制
This commit is contained in:
@@ -69,22 +69,22 @@ func (c runtimePaymentConfig) isMockMode() bool {
|
||||
return c.Provider == "mock"
|
||||
}
|
||||
|
||||
func (r *Repository) defaultRuntimeConfig() (*runtimePaymentConfig, error) {
|
||||
func (r *Repository) defaultRuntimeConfig(ctx context.Context) (*runtimePaymentConfig, error) {
|
||||
if r.configRepo == nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
dto, err := r.configRepo.FindDefaultAny(true)
|
||||
dto, err := r.configRepo.FindDefaultAny(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return runtimeConfigFromDTO(dto), nil
|
||||
}
|
||||
|
||||
func (r *Repository) runtimeConfigForPayment(payment *model.PaymentOrder) (*runtimePaymentConfig, error) {
|
||||
func (r *Repository) runtimeConfigForPayment(ctx context.Context, payment *model.PaymentOrder) (*runtimePaymentConfig, error) {
|
||||
provider := firstNonEmpty(payment.Provider, "mock")
|
||||
merchantID := payment.MerchantID
|
||||
if r.configRepo != nil && merchantID != "" {
|
||||
dto, err := r.configRepo.FindByProviderMerchant(provider, merchantID, true)
|
||||
dto, err := r.configRepo.FindByProviderMerchant(ctx, provider, merchantID, true)
|
||||
if err == nil {
|
||||
return runtimeConfigFromDTO(dto), nil
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func (r *Repository) runtimeConfigForPayment(payment *model.PaymentOrder) (*runt
|
||||
}
|
||||
if provider != "leshua" {
|
||||
if provider == "lakala" && r.configRepo != nil {
|
||||
dto, err := r.configRepo.FindDefaultByProvider(provider, true)
|
||||
dto, err := r.configRepo.FindDefaultByProvider(ctx, provider, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -108,7 +108,7 @@ func (r *Repository) runtimeConfigForPayment(payment *model.PaymentOrder) (*runt
|
||||
if r.configRepo == nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
dto, err := r.configRepo.FindDefaultByProvider(provider, true)
|
||||
dto, err := r.configRepo.FindDefaultByProvider(ctx, provider, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -136,7 +136,7 @@ func runtimeConfigFromDTO(dto *paymentconfig.ConfigDTO) *runtimePaymentConfig {
|
||||
}
|
||||
|
||||
func (r *Repository) Start(ctx context.Context, userID uint64, orderID uint64, req StartPaymentRequest, clientIP string) (*PaymentDTO, error) {
|
||||
defaultConfig, err := r.defaultRuntimeConfig()
|
||||
defaultConfig, err := r.defaultRuntimeConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
@@ -144,12 +144,12 @@ func (r *Repository) Start(ctx context.Context, userID uint64, orderID uint64, r
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(payment)
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, payment)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
if payment.Status == "paid" {
|
||||
r.recordConfigUsage(runtimeConfig, payment)
|
||||
r.recordConfigUsage(ctx, runtimeConfig, payment)
|
||||
dto := toDTO(*payment)
|
||||
return &dto, nil
|
||||
}
|
||||
@@ -166,12 +166,12 @@ func (r *Repository) Start(ctx context.Context, userID uint64, orderID uint64, r
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.recordConfigUsage(runtimeConfig, latest)
|
||||
r.recordConfigUsage(ctx, runtimeConfig, latest)
|
||||
dto := toDTO(*latest)
|
||||
return &dto, nil
|
||||
}
|
||||
if payment.Status == "paying" && (payment.TDCode != "" || payment.JSPayURL != "" || payment.JSPayInfo != "") {
|
||||
r.recordConfigUsage(runtimeConfig, payment)
|
||||
r.recordConfigUsage(ctx, runtimeConfig, payment)
|
||||
dto := toDTO(*payment)
|
||||
return &dto, nil
|
||||
}
|
||||
@@ -221,7 +221,7 @@ func (r *Repository) Start(ctx context.Context, userID uint64, orderID uint64, r
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.recordConfigUsage(runtimeConfig, latest)
|
||||
r.recordConfigUsage(ctx, runtimeConfig, latest)
|
||||
log.Printf("[payment] payment result order_id=%d order_no=%s payment_id=%d provider=%s amount_cent=%d status=%s provider_order_id=%s",
|
||||
orderID, orderRow.OrderNo, latest.ID, runtimeConfig.Provider, latest.AmountCent, latest.Status, latest.ProviderOrderID)
|
||||
dto := toDTO(*latest)
|
||||
@@ -233,7 +233,7 @@ func (r *Repository) StartWalletRecharge(ctx context.Context, userID uint64, req
|
||||
if userID == 0 || amountCent < moneyCent(MinWalletRechargeAmount) {
|
||||
return nil, ErrPaymentCannotStart
|
||||
}
|
||||
runtimeConfig, err := r.defaultRuntimeConfig()
|
||||
runtimeConfig, err := r.defaultRuntimeConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
@@ -254,7 +254,7 @@ func (r *Repository) StartWalletRecharge(ctx context.Context, userID uint64, req
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.recordConfigUsage(runtimeConfig, latest)
|
||||
r.recordConfigUsage(ctx, runtimeConfig, latest)
|
||||
dto := toDTO(*latest)
|
||||
return &dto, nil
|
||||
}
|
||||
@@ -303,7 +303,7 @@ func (r *Repository) StartWalletRecharge(ctx context.Context, userID uint64, req
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.recordConfigUsage(runtimeConfig, latest)
|
||||
r.recordConfigUsage(ctx, runtimeConfig, latest)
|
||||
log.Printf("[payment] wallet recharge result user_id=%d payment_id=%d provider=%s amount_cent=%d status=%s provider_order_id=%s",
|
||||
userID, latest.ID, runtimeConfig.Provider, latest.AmountCent, latest.Status, latest.ProviderOrderID)
|
||||
dto := toDTO(*latest)
|
||||
@@ -318,7 +318,7 @@ func (r *Repository) QueryWalletRecharge(ctx context.Context, userID uint64, pay
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(&payment)
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, &payment)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
@@ -352,7 +352,7 @@ func (r *Repository) Query(ctx context.Context, userID uint64, orderID uint64) (
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(&payment)
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, &payment)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
@@ -392,7 +392,7 @@ func (r *Repository) HandleNotify(ctx context.Context, provider string, params m
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(payment)
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, payment)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
@@ -423,7 +423,7 @@ func (r *Repository) StartRefund(ctx context.Context, orderID uint64, refundAmou
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(&originalPayment)
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, &originalPayment)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
@@ -471,7 +471,7 @@ func (r *Repository) StartRefund(ctx context.Context, orderID uint64, refundAmou
|
||||
if err := r.db.WithContext(ctx).Create(&refundOrder).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.recordConfigUsage(runtimeConfig, &refundOrder)
|
||||
r.recordConfigUsage(ctx, runtimeConfig, &refundOrder)
|
||||
if err := r.updateOrderRefundStatus(ctx, orderID, refundAmountCent); err != nil {
|
||||
log.Printf("[payment] mock update order refund status failed order_id=%d err=%v", orderID, err)
|
||||
}
|
||||
@@ -484,7 +484,7 @@ func (r *Repository) StartRefund(ctx context.Context, orderID uint64, refundAmou
|
||||
}
|
||||
log.Printf("[payment] refund start order_id=%d order_no=%s payment_id=%d biz_type=%s provider=%s amount_cent=%d merchant_refund_id=%s origin_third_order_id=%s origin_provider_order_id=%s",
|
||||
orderID, originalPayment.OrderNo, refundOrder.ID, bizType, runtimeConfig.Provider, refundAmountCent, merchantRefundID, originalPayment.ThirdOrderID, refundOriginProviderOrderID(originalPayment))
|
||||
r.recordConfigUsage(runtimeConfig, &refundOrder)
|
||||
r.recordConfigUsage(ctx, runtimeConfig, &refundOrder)
|
||||
if err := r.markOrderRefunding(ctx, orderID, refundAmountCent); err != nil {
|
||||
log.Printf("[payment] mark order refunding failed order_id=%d err=%v", orderID, err)
|
||||
}
|
||||
@@ -560,7 +560,7 @@ func (r *Repository) QueryRefundStatus(ctx context.Context, orderID uint64) (*Re
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(&payment)
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, &payment)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
@@ -656,7 +656,7 @@ func (r *Repository) HandleRefundNotify(ctx context.Context, provider string, pa
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(payment)
|
||||
runtimeConfig, err := r.runtimeConfigForPayment(ctx, payment)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
@@ -947,7 +947,7 @@ func (r *Repository) confirmPaid(ctx context.Context, payment *model.PaymentOrde
|
||||
if r.orderRepo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
if err := r.orderRepo.ConfirmPaidFromChannel(payment.OrderID, firstNonEmpty(payment.ProviderOrderID, payment.PaymentNo)); err != nil {
|
||||
if err := r.orderRepo.ConfirmPaidFromChannel(ctx, payment.OrderID, firstNonEmpty(payment.ProviderOrderID, payment.PaymentNo)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -1041,11 +1041,11 @@ func (r *Repository) verifyNotify(ctx context.Context, payment *model.PaymentOrd
|
||||
return verify, nil
|
||||
}
|
||||
|
||||
func (r *Repository) recordConfigUsage(runtimeConfig *runtimePaymentConfig, payment *model.PaymentOrder) {
|
||||
func (r *Repository) recordConfigUsage(ctx context.Context, runtimeConfig *runtimePaymentConfig, payment *model.PaymentOrder) {
|
||||
if r.configRepo == nil || runtimeConfig == nil || payment == nil || runtimeConfig.ID == 0 {
|
||||
return
|
||||
}
|
||||
if err := r.configRepo.RecordUsage(runtimeConfig.ID, payment.ID, runtimeConfig.Provider, runtimeConfig.MerchantID, payment.AmountCent, payment.BizType); err != nil {
|
||||
if err := r.configRepo.RecordUsage(ctx, runtimeConfig.ID, payment.ID, runtimeConfig.Provider, runtimeConfig.MerchantID, payment.AmountCent, payment.BizType); err != nil {
|
||||
log.Printf("[payment] record config usage failed config_id=%d payment_id=%d err=%v", runtimeConfig.ID, payment.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user