/* global usScheduler, Stripe */
(function () {
'use strict';
const { restUrl, nonce, stripeKey } = usScheduler;
function apiFetch(path, options = {}) {
return fetch(restUrl + path, {
...options,
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': nonce,
...(options.headers || {}),
},
}).then(async (res) => {
const data = await res.json();
if (!res.ok) throw new Error(data.message || 'Request failed');
return data;
});
}
function escHtml(str) {
return String(str)
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"');
}
function money(amount, currency) {
return `${Number(amount).toFixed(2)} ${escHtml(currency || '')}`.trim();
}
// Render Stripe's Payment Element into mountEl and resolve once the card has
// been confirmed. The server is told the result out-of-band via webhook, so a
// successful confirm here just means "the charge is on its way".
function confirmCard(intent, mountEl) {
return new Promise((resolve, reject) => {
if (typeof Stripe === 'undefined') {
reject(new Error('Card payment is unavailable right now. Please try again later.'));
return;
}
const stripe = Stripe(intent.publishable_key || stripeKey);
const elements = stripe.elements({ clientSecret: intent.client_secret });
const paymentEl = elements.create('payment');
mountEl.innerHTML = `