fix(review): harden payment, oauth, and migration paths
This commit is contained in:
@@ -139,6 +139,10 @@ func (s *PaymentService) createOrderInTx(ctx context.Context, req CreateOrderReq
|
||||
tm = defaultOrderTimeoutMin
|
||||
}
|
||||
exp := time.Now().Add(time.Duration(tm) * time.Minute)
|
||||
outTradeNo, err := s.allocateOutTradeNo(ctx, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
providerSnapshot := buildPaymentOrderProviderSnapshot(sel, req)
|
||||
selectedInstanceID := ""
|
||||
selectedProviderKey := ""
|
||||
@@ -155,7 +159,7 @@ func (s *PaymentService) createOrderInTx(ctx context.Context, req CreateOrderReq
|
||||
SetPayAmount(payAmount).
|
||||
SetFeeRate(feeRate).
|
||||
SetRechargeCode("").
|
||||
SetOutTradeNo(generateOutTradeNo()).
|
||||
SetOutTradeNo(outTradeNo).
|
||||
SetPaymentType(req.PaymentType).
|
||||
SetPaymentTradeNo("").
|
||||
SetOrderType(req.OrderType).
|
||||
@@ -193,6 +197,21 @@ func (s *PaymentService) createOrderInTx(ctx context.Context, req CreateOrderReq
|
||||
return order, nil
|
||||
}
|
||||
|
||||
func (s *PaymentService) allocateOutTradeNo(ctx context.Context, tx *dbent.Tx) (string, error) {
|
||||
const maxAttempts = 5
|
||||
for attempt := 0; attempt < maxAttempts; attempt++ {
|
||||
candidate := generateOutTradeNo()
|
||||
exists, err := tx.PaymentOrder.Query().Where(paymentorder.OutTradeNo(candidate)).Exist(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("check out_trade_no uniqueness: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return candidate, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("generate unique out_trade_no: exhausted %d attempts", maxAttempts)
|
||||
}
|
||||
|
||||
func (s *PaymentService) checkPendingLimit(ctx context.Context, tx *dbent.Tx, userID int64, max int) error {
|
||||
if max <= 0 {
|
||||
max = defaultMaxPendingOrders
|
||||
@@ -366,7 +385,10 @@ func (s *PaymentService) invokeProvider(ctx context.Context, order *dbent.Paymen
|
||||
}
|
||||
resumeToken := ""
|
||||
if resume := s.paymentResume(); resume != nil {
|
||||
if resume.isSigningConfigured() {
|
||||
if canonicalReturnURL != "" {
|
||||
if err := resume.ensureSigningKey(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resumeToken, err = resume.CreateToken(ResumeTokenClaims{
|
||||
OrderID: order.ID,
|
||||
UserID: order.UserID,
|
||||
@@ -482,6 +504,9 @@ func (s *PaymentService) buildWeChatOAuthRequiredResponse(ctx context.Context, r
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.paymentResume().ensureSigningKey(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
authorizeURL, err := buildWeChatPaymentOAuthStartURL(req, "snsapi_base")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user