import type { StripeExpressCheckoutElementOptions, StripePaymentElementOptions, } from '@stripe/stripe-js'; type PaywallCheckoutExpressOptionsInput = { amountCents: number; summaryLabel: string; }; type PaywallCheckoutPaymentElementOptionsInput = { email?: string | null; }; type PaywallCheckoutBillingDetailsInput = { email?: string | null; name?: string | null; }; const normalizePaywallCheckoutBillingName = ({ email, name, }: PaywallCheckoutBillingDetailsInput): string => { const normalizedName = name?.trim(); if (normalizedName) { return normalizedName; } const normalizedEmail = email?.trim(); if (normalizedEmail) { const localPart = normalizedEmail .split('@')[0] ?.replace(/[._-]+/g, ' ') .trim(); if (localPart) { return localPart; } } return 'Customer'; }; export function buildPaywallCheckoutExpressOptions({ amountCents, summaryLabel, }: PaywallCheckoutExpressOptionsInput): StripeExpressCheckoutElementOptions { return { buttonHeight: 55, buttonTheme: { applePay: 'black', googlePay: 'black', }, buttonType: { applePay: 'check-out', googlePay: 'checkout', }, emailRequired: true, layout: { maxColumns: 1, maxRows: 0, overflow: 'never', }, lineItems: [ { name: summaryLabel.trim() || 'Selected plan', amount: amountCents, }, ], paymentMethodOrder: ['link', 'apple_pay', 'google_pay'], paymentMethods: { amazonPay: 'never', applePay: 'always', googlePay: 'always', klarna: 'never', paypal: 'never', }, }; } export function buildPaywallCheckoutPaymentElementOptions({ email, }: PaywallCheckoutPaymentElementOptionsInput): StripePaymentElementOptions { const normalizedEmail = email?.trim() || ''; const emailFieldMode = normalizedEmail ? 'never' : 'auto'; return { defaultValues: normalizedEmail ? { billingDetails: { email: normalizedEmail, }, } : undefined, fields: { billingDetails: { address: 'if_required', email: emailFieldMode, name: 'never', phone: 'auto', }, }, layout: { type: 'tabs', }, paymentMethodOrder: ['card'], wallets: { applePay: 'never', googlePay: 'never', link: 'never', }, }; } export function buildPaywallCheckoutBillingDetails({ email, name, }: PaywallCheckoutBillingDetailsInput): { email?: string; name: string; } { const normalizedEmail = email?.trim() || undefined; return { email: normalizedEmail, name: normalizePaywallCheckoutBillingName({ email: normalizedEmail, name, }), }; }