summaryrefslogtreecommitdiff
path: root/src/components/SubscriptionPayPalButtons.astro
blob: bc77580c3f31c87759d4a5f3346835e1d6324693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---
import env from '$lib/env';
import type Price from '$types/Price';

interface Props {
  successPageUrl: string;
  productDescription: string;
  shortDescription: string;
  totalPrice: Price;
}

const { successPageUrl, productDescription, shortDescription, totalPrice } = Astro.props;

const PAYPAL_CLIENT_ID =
  env.ENVIRONMENT === 'prod' ? env.PAYPAL_LIVE_CLIENT_ID : env.PAYPAL_SANDBOX_CLIENT_ID;
---

<subscription-paypal-buttons
  data-paypal-client-id={PAYPAL_CLIENT_ID}
  data-success-page-url={successPageUrl}
  data-failure-page-url={failurePageUrl}
>
  <div id="paypal-button-container"></div>
</subscription-paypal-buttons>

<script>
  import { PayPalNamespace, loadScript } from '@paypal/paypal-js';

  const backendUrl = '/api';

  class ProductPayPalButtons extends HTMLElement {
    constructor() {
      super();

      const paypalClientId = this.getStringDataAttribute('paypalClientId');

      const paypalOptions = {
        clientId: paypalClientId,
        locale: 'en_GB',
        commit: false,
        currency: 'GBP',
        intent: 'capture',
      };

      loadScript(paypalOptions)
        .catch((err) => {
          throw new Error('Failed to load the PayPal JS SDK script. ', err);
        })
        .then((paypal) => {
          const constructButtons = paypal?.Buttons;
          if (paypal && constructButtons) {
            const successPageUrl = this.dataset['successPageUrl'] ?? '.';
            this.makePaypalButtons(constructButtons, successPageUrl).render(
              '#paypal-button-container'
            );
          }
        });
    }

    makePaypalButtons(
      constructButtons: Required<PayPalNamespace>['Buttons'],
      successPageUrl: string
    ) {
      // TODO: figure out where subscriptionId is coming from! Probably it’s going to be
      // generated by PayPal on the backend, and you don’t want it here at all.

      // TODO: get emailAddress from user session.

      return constructButtons({
        async createSubscription() {
          try {
            const response = await fetch("/api/create-subscription", {
              method: "POST",
              headers: {
                "Content-Type": "application/json",
              },
              body: JSON.stringify({ subscriptionId, emailAddress }),
            });

            const data = await response.json();

            if (data?.id) {
              console.info('Subscription successful. Redirecting to confirmation page...');
              window.location.href = successPageUrl;
              return data.id;
            } else {
              console.error(
                { callback: "createSubscription", serverResponse: data },
                JSON.stringify(data, null, 2),
              );
              console.info('Redirecting to failure page...');
              window.location.href = failurePageUrl;
            }
          } catch (error) {
            console.error('Redirecting to failure page, because of an error creating a subscription.' error);
            window.location.href = failurePageUrl;
          }
        },
      }).render("#paypal-button-container"); // Renders the PayPal button
    }

    getStringDataAttribute(key: string): string {
      const value = this.dataset[key];
      if (!value || typeof value !== 'string') {
        throw new Error(`data-${key} attribute not provided to ProductPayPalButtons component.`);
      }
      return value;
    }
  }

  customElements.define('product-paypal-buttons', ProductPayPalButtons);
</script>