UNPKG

@sergdudko/stripe-js

Version:

Supercharge Your Stripe Integration with Enhanced Methods for stripe-js: Take Full Control of Customer Card Management Right from Your Frontend! Elevate Your Payment Processing Capabilities with Ease and Efficiency.

26 lines (25 loc) 1.31 kB
import { responseHandler } from "../utils/handlers.js"; import { stripeApiUrl, stripeApiVersion } from "../utils/constants.js"; /** * Confirm payment intent by customer's card * * @param paymentIntentSecret - stripe payment intent secret (see: https://stripe.com/docs/api/payment_intents/object#payment_intent_object-client_secret) * @param paymentMethodId - stripe customer payment method id (see: https://stripe.com/docs/api/cards/object#card_object-id) * @returns */ export const confirmPaymentIntentByCard = async function (paymentIntentSecret, paymentMethodId, returnUrl) { const stripeApiKey = this._apiKey; if (typeof stripeApiKey !== "string") throw new Error("Initialization failed."); // make request const paymentIntentId = paymentIntentSecret.replace(/_secret_.+$/i, ""); return fetch(`${stripeApiUrl}/payment_intents/${paymentIntentId}/confirm?client_secret=${paymentIntentSecret}`, { body: `payment_method=${paymentMethodId}${typeof returnUrl === 'string' ? `&return_url=${returnUrl}` : ''}`, headers: { Authorization: `Bearer ${stripeApiKey}`, "Content-Type": `application/x-www-form-urlencoded`, "Stripe-Version": `${stripeApiVersion}`, }, method: `POST`, }).then(responseHandler); };