diff --git a/gatsby-browser.js b/gatsby-browser.js index 3830f57..ec7544a 100644 --- a/gatsby-browser.js +++ b/gatsby-browser.js @@ -1,3 +1,4 @@ +import React from 'react' import { LocalizationProvider, ReactLocalization } from '@fluent/react' import { useObservable, useObservableState } from 'observable-hooks' import { from } from 'rxjs' diff --git a/src/pages/payment.tsx b/src/pages/payment.tsx new file mode 100644 index 0000000..25b3668 --- /dev/null +++ b/src/pages/payment.tsx @@ -0,0 +1,103 @@ +import { useEffect, useState } from 'react' +import Layout from '~/components/layout' +import SEO from '~/components/seo' +import type { ReadonlyRouteComponentProps } from '~/util/readonly-types' + +export const Head = () => + +// documentation for SumUpCard: https://developer.sumup.com/online-payments/checkouts/card-widget + +const PaymentPage = (_: ReadonlyRouteComponentProps) => { + const checkoutParam = new URLSearchParams(window.location.search).get( + 'checkout' + ) + // ensure safe + const sanityRegex = /[^a-z0-9-]/g + const checkoutParamSanitized = checkoutParam + ? checkoutParam.replaceAll(sanityRegex, '') + : '' + + const [isScriptLoaded, setIsScriptLoaded] = useState(false) + const [scriptError, setScriptError] = useState(null) + + // Effect 1: Load the SumUp SDK script + useEffect(() => { + const script = document.createElement('script') + script.src = 'https://gateway.sumup.com/gateway/ecom/card/v2/sdk.js' + script.type = 'text/javascript' + script.async = true + + // Handle script load success + const handleLoad = () => { + console.log('SumUp script loaded!') + setIsScriptLoaded(true) + } + + // Handle script load error + const handleError = () => { + setScriptError('Failed to load SumUp script.') + } + + // Attach event listeners + script.addEventListener('load', handleLoad) + script.addEventListener('error', handleError) + + // Inject script into the DOM + document.body.appendChild(script) + + // Cleanup: Remove script and event listeners on unmount + return () => { + script.removeEventListener('load', handleLoad) + script.removeEventListener('error', handleError) + document.body.removeChild(script) + } + }, []) + + // Effect 2: Mount the widget AFTER the div is rendered + useEffect(() => { + if (!isScriptLoaded || !checkoutParamSanitized) return + + // @ts-expect-error - SumUpCard is loaded via external script + SumUpCard.mount({ + id: 'sumup-card', + checkoutId: checkoutParamSanitized, + onResponse: (type: any, body: any) => { + console.log('Type', type) + console.log('Body', body) + }, + }) + }, [isScriptLoaded, checkoutParamSanitized]) + + if (scriptError) { + return ( + +

Error: {scriptError}

+
+ ) + } else if (!isScriptLoaded) { + return ( + +

loading...

+
+ ) + } else if (!checkoutParamSanitized) { + return ( + +

no checkout id provided

+
+ ) + } else { + return ( + +

Please make your payment here:

+
+

+ Note that processing payments may sometimes take a bit. Do not pay + multiple times. +

+
+ ) + } +} + +export default PaymentPage