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.04 kB
import { responseHandler } from "../utils/handlers.js"; import { stripeApiUrl, stripeApiVersion } from "../utils/constants.js"; /** * Add card to customer (from source or token). * * @param token - source or token string (see: https://stripe.com/docs/api/sources/object) * @param customerId - customer id (see: https://stripe.com/docs/api/customers/object#customer_object-id) * @param ephemeralKey - customer ephemeral key * @returns */ export const addSourceToCustomer = async function (token, customerId, ephemeralKey) { const stripeApiKey = this._apiKey; if (typeof stripeApiKey !== "string") throw new Error("Initialization failed."); // make request return fetch(`${stripeApiUrl}/customers/${customerId}/sources`, { body: `source=${token}`, headers: { Authorization: `Bearer ${ephemeralKey}`, "Content-Type": "application/x-www-form-urlencoded", "Stripe-Version": `${stripeApiVersion}`, }, method: "POST", }).then(responseHandler); };