|
| 1 | +import { getJSON } from 'https://cdn.kernvalley.us/js/std-js/http.js'; |
| 2 | +import { loadScript } from 'https://cdn.kernvalley.us/js/std-js/loader.js'; |
| 3 | +import { map } from 'https://cdn.kernvalley.us/js/std-js/dom.js'; |
| 4 | + |
| 5 | +const STRIPE = 'https://js.stripe.com/v3/'; |
| 6 | +const ENDPOINT = '/api/stripe'; |
| 7 | + |
| 8 | +export const getStripeKey = (async ({ signal } = {}) => { |
| 9 | + const { key, error } = await getJSON(ENDPOINT, { signal }); |
| 10 | + if (typeof error !== 'undefined') { |
| 11 | + throw new Error(error.message); |
| 12 | + } else if (typeof key !== 'string' || key.length === 0) { |
| 13 | + throw new Error('Error loading stripe key'); |
| 14 | + } else { |
| 15 | + return key; |
| 16 | + } |
| 17 | +}).once(); |
| 18 | + |
| 19 | +export const getSecret = async (body = [{}], { signal } = {}) => { |
| 20 | + const resp = await fetch(ENDPOINT, { |
| 21 | + method: 'POST', |
| 22 | + headers: new Headers({ 'Content-Type': 'application/json' }), |
| 23 | + body: JSON.stringify(body), |
| 24 | + signal, |
| 25 | + }); |
| 26 | + |
| 27 | + if (resp.ok) { |
| 28 | + const { clientSecret } = await resp.json(); |
| 29 | + return clientSecret; |
| 30 | + } else { |
| 31 | + throw new Error('Error creating payment request'); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +export const loadStripe = (() => loadScript(STRIPE)).once(); |
| 36 | + |
| 37 | +export const getStripe = (async ({ signal } = {}) => { |
| 38 | + const [key] = await Promise.all([getStripeKey({ signal }), loadStripe()]); |
| 39 | + |
| 40 | + if (! ('Stripe' in globalThis)) { |
| 41 | + throw new Error('Stripe failed to load'); |
| 42 | + } else { |
| 43 | + return globalThis.Stripe(key); |
| 44 | + } |
| 45 | +}).once(); |
| 46 | + |
| 47 | +export const getElements = (async (items = [], { |
| 48 | + appearance = { theme: 'stripe' }, |
| 49 | + signal, |
| 50 | +} = {}) => { |
| 51 | + const [stripe, clientSecret] = await Promise.all([ |
| 52 | + getStripe({ signal }), |
| 53 | + getSecret(items, { signal }), |
| 54 | + ]); |
| 55 | + |
| 56 | + return stripe.elements({ appearance, clientSecret }); |
| 57 | +}).once(); |
| 58 | + |
| 59 | +export const createElement = async (elements, { type, selector, style, events }) => { |
| 60 | + const el = elements.create(type, { style }); |
| 61 | + |
| 62 | + if (typeof events === 'object' && ! Object.is(events, null)) { |
| 63 | + Object.entries(events).forEach(([event, callback]) => el.on(event, callback)); |
| 64 | + } |
| 65 | + |
| 66 | + el.mount(selector); |
| 67 | + return el; |
| 68 | +}; |
| 69 | + |
| 70 | +export const initElements = async ({ |
| 71 | + base = document.body, |
| 72 | + items, |
| 73 | + events, |
| 74 | + styles, |
| 75 | + theme, |
| 76 | + signal, |
| 77 | +} = {}) => { |
| 78 | + const elements = await getElements(items, { theme, signal }); |
| 79 | + |
| 80 | + return await Promise.all(map('[data-stripe-element][id]', el => { |
| 81 | + const type = el.dataset.stripeElement; |
| 82 | + |
| 83 | + return createElement(elements, { |
| 84 | + type: type, |
| 85 | + selector: `#${el.id}`, |
| 86 | + events: typeof events === 'undefined' ? undefined : events[type], |
| 87 | + style: typeof styles === 'undefined' ? undefined : styles[type], |
| 88 | + }); |
| 89 | + }, { base: typeof base === 'string' ? document.forms[base] : base })); |
| 90 | +}; |
0 commit comments