diff options
| author | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-08-16 08:16:45 +0100 |
|---|---|---|
| committer | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-08-16 08:16:45 +0100 |
| commit | 4f1f22aa70723fb77d22cc1237a1b85b1d3ae1bd (patch) | |
| tree | 59af5bdca7abdefa0b913490f4734c00f9d24253 /src/components/PayPalButtons.astro | |
| parent | 5f76bfe714c74b38019b0cdc316d003729905208 (diff) | |
Parameterises PayPal product details
Diffstat (limited to 'src/components/PayPalButtons.astro')
| -rw-r--r-- | src/components/PayPalButtons.astro | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/src/components/PayPalButtons.astro b/src/components/PayPalButtons.astro index 80d52ff..cdea5cb 100644 --- a/src/components/PayPalButtons.astro +++ b/src/components/PayPalButtons.astro @@ -1,17 +1,27 @@ --- import env from '$lib/env'; +import type Price from '$types/Price'; interface Props { successPageUrl: string; + productDescription: string; + shortDescription: string; + totalPrice: Price; } -const { successPageUrl } = Astro.props; +const { successPageUrl, productDescription, shortDescription, totalPrice } = Astro.props; const PAYPAL_CLIENT_ID = env.ENVIRONMENT === 'prod' ? env.PAYPAL_LIVE_CLIENT_ID : env.PAYPAL_SANDBOX_CLIENT_ID; --- -<paypal-buttons data-paypal-client-id={PAYPAL_CLIENT_ID} data-success-page-url={successPageUrl}> +<paypal-buttons + data-paypal-client-id={PAYPAL_CLIENT_ID} + data-success-page-url={successPageUrl} + data-product-description={productDescription} + data-short-description={shortDescription} + data-total-price={totalPrice} +> <div id="paypal-button-container"></div> </paypal-buttons> @@ -24,10 +34,7 @@ const PAYPAL_CLIENT_ID = constructor() { super(); - const paypalClientId = this.dataset['paypalClientId']; - if (!paypalClientId || typeof paypalClientId !== 'string') { - throw new Error('PayPal Client ID not provided to PayPalButtons component.'); - } + const paypalClientId = this.getStringDataAttribute('paypalClientId'); const paypalOptions = { clientId: paypalClientId, @@ -56,13 +63,17 @@ const PAYPAL_CLIENT_ID = constructButtons: Required<PayPalNamespace>['Buttons'], successPageUrl: string ) { + const productDescription = this.getStringDataAttribute('productDescription'); + const shortDescription = this.getStringDataAttribute('shortDescription'); + const totalPrice = this.getStringDataAttribute('totalPrice'); + return constructButtons({ // This function is called to set up the transaction details without actually carrying it out async createOrder() { const body = JSON.stringify({ - productDescription: 'my product description', - shortDescription: 'my product', - totalPrice: 15.5, + productDescription, + shortDescription, + totalPrice, }); const headers = { 'Content-Type': 'application/json', @@ -125,6 +136,14 @@ const PAYPAL_CLIENT_ID = }, }); } + + getStringDataAttribute(key: string): string { + const value = this.dataset[key]; + if (!value || typeof value !== 'string') { + throw new Error(`data-${key} attribute not provided to PayPalButtons component.`); + } + return value; + } } customElements.define('paypal-buttons', PayPalButtons); |
