extend full-payload direct proxy flow to MPESA
Align MPESA direct payment with TELEBIRR_USSD by routing it through the provider's full checkout payload proxy endpoint for consistent gateway behavior. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
21ce61b910
commit
0983589e36
|
|
@ -604,9 +604,9 @@ func (s *ArifpayService) InitiateDirectPayment(ctx context.Context, userID int64
|
|||
directResp string
|
||||
)
|
||||
|
||||
if req.PaymentMethod == domain.DirectPaymentTelebirrUSSD {
|
||||
// TELEBIRR_USSD uses a direct proxy endpoint with full checkout payload.
|
||||
sessionID, directResp, err = s.initiateTelebirrUSSDDirect(ctx, checkoutReq)
|
||||
if req.PaymentMethod == domain.DirectPaymentTelebirrUSSD || req.PaymentMethod == domain.DirectPaymentMPesa {
|
||||
// TELEBIRR_USSD and MPESA use direct proxy endpoints with full checkout payload.
|
||||
sessionID, directResp, err = s.initiateFullPayloadDirectProxy(ctx, checkoutReq, req.PaymentMethod)
|
||||
} else {
|
||||
sessionID, err = s.createCheckoutSessionForDirect(ctx, checkoutReq)
|
||||
if err == nil {
|
||||
|
|
@ -728,13 +728,26 @@ func (s *ArifpayService) createCheckoutSessionForDirect(ctx context.Context, che
|
|||
return fmt.Sprintf("%v", data["sessionId"]), nil
|
||||
}
|
||||
|
||||
func (s *ArifpayService) initiateTelebirrUSSDDirect(ctx context.Context, checkoutReq domain.CheckoutSessionRequest) (string, string, error) {
|
||||
func (s *ArifpayService) initiateFullPayloadDirectProxy(
|
||||
ctx context.Context,
|
||||
checkoutReq domain.CheckoutSessionRequest,
|
||||
method domain.DirectPaymentMethod,
|
||||
) (string, string, error) {
|
||||
payload, err := json.Marshal(checkoutReq)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("failed to marshal telebirr ussd request: %w", err)
|
||||
return "", "", fmt.Errorf("failed to marshal direct proxy request: %w", err)
|
||||
}
|
||||
|
||||
var endpoint string
|
||||
switch method {
|
||||
case domain.DirectPaymentTelebirrUSSD:
|
||||
endpoint = fmt.Sprintf("%s/api/checkout/telebirr-ussd/transfer/direct", s.cfg.ARIFPAY.BaseURL)
|
||||
case domain.DirectPaymentMPesa:
|
||||
endpoint = fmt.Sprintf("%s/api/checkout/mpesa/transfer/direct", s.cfg.ARIFPAY.BaseURL)
|
||||
default:
|
||||
return "", "", fmt.Errorf("unsupported full-payload direct proxy method: %s", method)
|
||||
}
|
||||
|
||||
endpoint := fmt.Sprintf("%s/api/checkout/telebirr-ussd/transfer/direct", s.cfg.ARIFPAY.BaseURL)
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
|
|
@ -744,7 +757,7 @@ func (s *ArifpayService) initiateTelebirrUSSDDirect(ctx context.Context, checkou
|
|||
|
||||
resp, err := s.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("failed to call telebirr ussd direct API: %w", err)
|
||||
return "", "", fmt.Errorf("failed to call direct proxy API: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
|
@ -753,7 +766,7 @@ func (s *ArifpayService) initiateTelebirrUSSDDirect(ctx context.Context, checkou
|
|||
return "", "", err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", "", fmt.Errorf("telebirr ussd direct failed: %s", string(body))
|
||||
return "", "", fmt.Errorf("direct proxy request failed: %s", string(body))
|
||||
}
|
||||
|
||||
var result struct {
|
||||
|
|
@ -764,7 +777,7 @@ func (s *ArifpayService) initiateTelebirrUSSDDirect(ctx context.Context, checkou
|
|||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return "", "", fmt.Errorf("invalid telebirr ussd response: %w", err)
|
||||
return "", "", fmt.Errorf("invalid direct proxy response: %w", err)
|
||||
}
|
||||
|
||||
sessionID := fmt.Sprintf("%v", result.Data.SessionID)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user