import * as JSX from './jsx'; import * as user from './user'; import * as api from './api'; import $ from 'jquery'; import { Page, GroupBox, Spinner } from './components'; let stripe = null; let card = null; let selected_product = 2; async function loadStripe() { return new Promise( function (resolve, reject ) { const s = document.createElement( 'script' ); s.src = 'https://js.stripe.com/v3/'; s.onload = resolve; s.onerror = reject; document.head.appendChild( s ); } ); } function showError( text: string ) { $( "#payment-error" ).show(); $( "#payment-error" ).text( text ); } async function onFormSubmit( e: Event ) { e.preventDefault(); const { paymentMethod, error } = await stripe.createPaymentMethod( { type: 'card', card } ); if( error ) return showError( error.message ); const res = await api.post( 'create-payment-intent', { token: localStorage.getItem( 'session' ), paymentMethodId: paymentMethod.id, subLength: selected_product } ); if( res.status == 'ok' ) { const intent = res.paymentIntent; if( intent.status == 'succeeded' ) return JSX.navigateParams( '/payment-success', {} ); else return showError( "Payment failed. Contact support if the issue persists." ); } else if( res.msg ) return showError( "Error: " + res.msg ); else return showError( "Payment failed. Contact support if the issue persists." ); } function waitForStripe() { const iframes = $( 'iframe' ); if( !iframes.length ) return setTimeout( waitForStripe, 50 ); for( let iframe of iframes ) { if( iframe.attributes.getNamedItem( 'name' ).value == 'hcaptcha-invisible' ) { $( '#upgrade-loading' ).remove(); $( '#upgrade-inner' ).show(); return; } } setTimeout( waitForStripe, 50 ); } async function initStripe() { stripe = window['Stripe']( 'pk_live_51Q9JG602rmv2yeiGYNNfwFkqp5ntpXIDIMIoiwDoEOrB5IsC75WDy3XOqekvuwY6PecviSSo5ERt0umrdBdUtqhd00FyYZe5p3' ); const font = user.settings.site_prefs.font; const params = { mode: 'billing', style: { base: { color: '#fff', fontFamily: font, fontSize: '15px', '::placeholder': { color: '#ccc', }, }, }, } const el = getComputedStyle( document.body ); const back = el.getPropertyValue( '--back' ); const front = el.getPropertyValue( '--front' ); const appearance = { theme: 'stripe', disableAnimations: true, variables: { colorPrimary: front, colorBackground: back, colorText: '#fff', fontFamily: font, fontSizeBase: '15px', fontSizeSm: '13px', spacingUnit: '2px', borderRadius: '0px', }, rules: { ".Input": { border: 'none' } } } const elements = stripe.elements( { appearance } ); const name = elements.create( 'address', { mode: 'billing', appearance } ); name.mount( '#name-element' ); card = elements.create( 'card', params ); card.mount( '#card-element' ); } function onProductChange( e: Event ) { selected_product = parseInt( (e.target as HTMLInputElement).value ); } function SelectionRadio( params: any ) { return
{ params.checked && } { !params.checked && }
} function UpgradeLoaded() { return } export default function Upgrade() { setTimeout( () => { loadStripe().then( () => { $( ).insertAfter( "#upgrade-loading" ); initStripe(); waitForStripe(); } ); } ); return Loading...   }