From 4f1f22aa70723fb77d22cc1237a1b85b1d3ae1bd Mon Sep 17 00:00:00 2001
From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com>
Date: Wed, 16 Aug 2023 08:16:45 +0100
Subject: Parameterises PayPal product details
---
src/components/LallansIssue.astro | 3 +++
src/components/PayPalButtons.astro | 37 ++++++++++++++++++++++++++++---------
src/components/Scotsoun.astro | 3 +++
3 files changed, 34 insertions(+), 9 deletions(-)
(limited to 'src/components')
diff --git a/src/components/LallansIssue.astro b/src/components/LallansIssue.astro
index 690e96c..37875e3 100644
--- a/src/components/LallansIssue.astro
+++ b/src/components/LallansIssue.astro
@@ -28,6 +28,9 @@ const t = translate(locale);
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;
---
-
+
@@ -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['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);
diff --git a/src/components/Scotsoun.astro b/src/components/Scotsoun.astro
index a0600d1..c1d8fdf 100644
--- a/src/components/Scotsoun.astro
+++ b/src/components/Scotsoun.astro
@@ -33,6 +33,9 @@ const t = translate(locale);
--
cgit v1.2.3