UNPKG

@blocklet/payment-react

Version:

Reusable react components for payment kit v2

22 lines (21 loc) 880 B
const NETWORK_ERROR_PATTERN = /timeout|timed out|network|failed to fetch|connection/i; function getErrorMessage(error) { return error instanceof Error ? error.message : String(error); } export async function recoverStripeConfirmResult(stripe, intentType, clientSecret, confirmError) { const message = getErrorMessage(confirmError); if (!NETWORK_ERROR_PATTERN.test(message)) { return { action: "failed", message }; } const retrieveMethod = intentType === "payment_intent" ? "retrievePaymentIntent" : "retrieveSetupIntent"; try { const result = await stripe[retrieveMethod](clientSecret); const intent = result.paymentIntent || result.setupIntent; if (intent && ["succeeded", "processing"].includes(intent.status)) { return { action: "complete" }; } return { action: "failed", message }; } catch { return { action: "pending" }; } }