The callback receives the PaymentRequirements from the 402 response (price, currency, min top-up, Stripe publishable key) and should return:
Return Value
Behavior
{ paymentMethodId: 'pm_...', topUpAmount?: number }
Retry with this payment method. topUpAmount defaults to minTopUp.
null
User declined to pay. The original 402 error is re-thrown.
Returns
The same AxiosInstance with the interceptor installed. The instance is modified in-place and also returned for chaining.
How It Works
The interceptor installs two handlers:
Success Handler
On any successful response (non-402), checks for a payment-response header and extracts the clientId for future requests:
Error Handler (402 Flow)
When a 402 response is received:
Prevent infinite loops: Checks for a _stripe402Retry flag on the request config. If set, the error is re-thrown (prevents looping).
Decode 402 requirements: Reads the payment-required header and decodes the PaymentRequiredResponse.
Try existing credits: If storedClientId exists and the error is NOT insufficient_credits, retries with just the client ID:
Request payment: If no stored client ID or credits are insufficient, calls config.onPaymentRequired(requirements).
Retry with payment: If the callback returns payment info, retries with:
User declined: If the callback returns null, the original 402 error is re-thrown.
Client ID Caching
The client ID is stored in a closure variable (storedClientId). This means:
It persists across requests made with the same Axios instance
It is lost if the Axios instance is garbage collected
It is initialized from config.clientId if provided
It is updated whenever a payment-response header contains a clientId
Retry Prevention
A _stripe402Retry flag is added to the request config before retrying. This prevents infinite loops where:
Request returns 402
Retry with payment also returns 402
Would retry again indefinitely
The flag ensures the interceptor only attempts one retry per original request.
getStoredClientId(instance)
Currently returns null. The stored client ID is in the interceptor's closure and not directly accessible externally. To track client IDs across sessions, extract them from the payment-response header in the onPaymentRequired callback or from successful responses.