JezK
Edit File: sc-paypal-add-method.entry.js.map
{"file":"sc-paypal-add-method.entry.js","mappings":";;;;;;;AAAA,MAAM,oBAAoB,GAAG,8OAA8O,CAAC;AAC5Q,gCAAe,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iDCqEA,UAAE;;;;;oGAIQ,UAAE;;;;;;yFAMX,UAAE;;;;;;;6BAMjB,UAAE;;;;;;;;;;;;;;;;;;;;;;;sPAsBgD,UAAE;;;;;;;iRAWzC,UAAE;;;;;;;;;;","names":[],"sources":["src/components/ui/sc-paypal-add-method/sc-paypal-add-method.scss?tag=sc-paypal-add-method","src/components/ui/sc-paypal-add-method/sc-paypal-add-method.tsx"],"sourcesContent":[":host {\n display: block;\n}\n\n.paypal-buttons {\n position: relative;\n line-height: 0;\n text-align: center;\n\n &:after {\n content: \" \";\n border-bottom: 1px solid var(--sc-input-border-color);\n width: 100%;\n height: 0;\n top: 50%;\n left: 0;\n right: 0;\n position: absolute;\n }\n}\n","import { Component, h, Host, Prop, State, Watch } from '@stencil/core';\nimport { loadScript, PayPalNamespace } from '@paypal/paypal-js';\nimport { __ } from '@wordpress/i18n';\nimport apiFetch from '../../../functions/fetch';\nimport { PaymentIntent } from '../../../types';\nimport { getScriptLoadParams } from '../paypal-buttons/functions';\n\n@Component({\n tag: 'sc-paypal-add-method',\n styleUrl: 'sc-paypal-add-method.scss',\n shadow: false,\n})\nexport class ScPaypalAddMethod {\n /** Holds the card button */\n private container: HTMLDivElement;\n // holds the stripe instance.\n private paypal: PayPalNamespace;\n\n @Prop() liveMode: boolean = true;\n @Prop() customerId: string;\n @Prop() successUrl: string;\n @Prop() currency: string;\n\n @State() loading: boolean;\n @State() loaded: boolean;\n @State() error: string;\n @State() paymentIntent: PaymentIntent;\n\n componentWillLoad() {\n this.createPaymentIntent();\n }\n\n @Watch('paymentIntent')\n async handlePaymentIntentCreate() {\n const { external_intent_id } = this.paymentIntent || {};\n const { client_id, account_id, merchant_initiated } = this.paymentIntent?.processor_data?.paypal || {};\n // we need this data.\n if (!client_id || !account_id || !external_intent_id) return;\n\n // check if stripe has been initialized\n if (!this.paypal) {\n try {\n this.paypal = await loadScript(\n getScriptLoadParams({\n clientId: client_id,\n merchantId: account_id,\n merchantInitiated: merchant_initiated,\n reusable: true,\n locale: (window as any).scData?.locale,\n }),\n );\n\n this.paypal\n .Buttons({\n onInit: () => {\n this.loaded = true;\n },\n createBillingAgreement: () => {\n return new Promise(resolve => resolve(external_intent_id));\n },\n onApprove: async () => {\n try {\n this.loading = true;\n const intent = (await apiFetch({\n method: 'PATCH',\n path: `surecart/v1/payment_intents/${this.paymentIntent?.id}/capture`,\n })) as PaymentIntent;\n if (['succeeded', 'pending', 'requires_approval'].includes(intent?.status)) {\n window.location.assign(this.successUrl);\n } else {\n throw { message: __('The payment did not process. Please try again.', 'surecart') };\n }\n } catch (err) {\n console.error(err);\n this.error = err?.message || __('The payment did not process. Please try again.', 'surecart');\n this.loading = false;\n }\n },\n onError: err => {\n console.error(err);\n alert(err?.message || __('The payment did not process. Please try again.', 'surecart'));\n },\n })\n .render(this.container);\n } catch (err) {\n console.error('Failed to load the PayPal JS SDK script', err);\n this.error = __('Failed to load the PayPal JS SDK script', 'surecart');\n }\n }\n }\n\n async createPaymentIntent() {\n try {\n this.loading = true;\n this.error = '';\n this.paymentIntent = await apiFetch({\n method: 'POST',\n path: 'surecart/v1/payment_intents',\n data: {\n processor_type: 'paypal',\n reusable: true,\n live_mode: this.liveMode,\n customer_id: this.customerId,\n currency: this.currency,\n refresh_status: true,\n },\n });\n } catch (e) {\n this.error = e?.additional_errors?.[0]?.message || e?.message || __('Something went wrong', 'surecart');\n } finally {\n this.loading = false;\n }\n }\n\n render() {\n return (\n <Host>\n {this.error && (\n <sc-alert open={!!this.error} type=\"danger\">\n <span slot=\"title\">{__('Error', 'surecart')}</span>\n {this.error}\n </sc-alert>\n )}\n <div class=\"sc-paypal-button-container\" hidden={!this.loaded} ref={el => (this.container = el as HTMLDivElement)}></div>\n </Host>\n );\n }\n}\n"],"version":3}