import { Stripe , StripeConstructor } from '../types' ; export type LoadStripe = ( ... args : Parameters < StripeConstructor > ) => Promise < Stripe | null > ; export interface LoadParams { advancedFraudSignals : boolean ; } declare const _VERSION ; const V3_URL = 'https://js.stripe.com/v3' ; const V3_URL_REGEX = /^https:\/\/js\.stripe\.com\/v3\/?(\?.*)?$/ ; const EXISTING_SCRIPT_MESSAGE = 'loadStripe.setLoadParameters was called but an existing Stripe.js script already exists in the document; existing script parameters will be used' ; export const findScript = ( ) : HTMLScriptElement | null => { const scripts = document . querySelectorAll < HTMLScriptElement > ( `script[src^="${ V3_URL }"]` ) ; for ( let i = 0 ; i < scripts . length ; i ++ ) { const script = scripts [ i ] ; if ( ! V3_URL_REGEX . test ( script . src ) ) { continue ; } return script ; } return null ; } ; const injectScript = ( params ) : HTMLScriptElement => { const queryString = params && ! params . advancedFraudSignals ? '?advancedFraudSignals=false' : '' ; const script = document . createElement ( 'script' ) ; script . src = `${ V3_URL }${ queryString }` ; const headOrBody = document . head || document . body ; if ( ! headOrBody ) { throw new Error ( 'Expected document.body not to be null. Stripe.js requires a <body> element.' ) ; } headOrBody . appendChild ( script ) ; return script ; } ; const registerWrapper = ( stripe , startTime ) : void => { if ( ! stripe || ! stripe . _registerWrapper ) { return ; } stripe . _registerWrapper ( { name : 'stripe-js' , version : _VERSION , startTime } ) ; } ; let stripePromise = null ; export const loadScript = ( params ) : Promise < StripeConstructor | null > => { if ( stripePromise !== null ) { return stripePromise ; } stripePromise = new Promise ( ( resolve , reject ) => { if ( typeof window === 'undefined' ) { resolve ( null ) ; return ; } if ( window . Stripe && params ) { console . warn ( EXISTING_SCRIPT_MESSAGE ) ; } if ( window . Stripe ) { resolve ( window . Stripe ) ; return ; } try { let script = findScript ( ) ; if ( script && params ) { console . warn ( EXISTING_SCRIPT_MESSAGE ) ; } else if ( ! script ) { script = injectScript ( params ) ; } script . addEventListener ( 'load' , ( ) => { if ( window . Stripe ) { resolve ( window . Stripe ) ; } else { reject ( new Error ( 'Stripe.js not available' ) ) ; } } ) ; script . addEventListener ( 'error' , ( ) => { reject ( new Error ( 'Failed to load Stripe.js' ) ) ; } ) ; } catch ( error ) { reject ( error ) ; return ; } } ) ; return stripePromise ; } ; export const initStripe = ( maybeStripe , args , startTime ) : Stripe | null => { if ( maybeStripe === null ) { return null ; } const stripe = maybeStripe . apply ( undefined , args ) ; registerWrapper ( stripe , startTime ) ; return stripe ; } ; export const validateLoadParams = ( params ) : LoadParams => { const errorMessage = `invalid load parameters; expected object of shape

    {advancedFraudSignals: boolean}

but received

    ${ JSON . stringify ( params ) }
` ; if ( params === null || typeof params !== 'object' ) { throw new Error ( errorMessage ) ; } if ( Object . keys ( params ) . length === 1 && typeof params . advancedFraudSignals === 'boolean' ) { return params ; } throw new Error ( errorMessage ) ; } ;