summaryrefslogtreecommitdiff
path: root/api/_paypal.ts
blob: 5abb198c6260e0e44e6993a21dd25fb388ea4d5d (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
113
114
115
import type Order from './_Order';
import env from './_env';

export async function paypalCreateOrder(order: Order) {
  const accessToken = await getLazyAccessToken();
  const url = `${PAYPAL_URL}/v2/checkout/orders`;
  const response = await fetch(url, {
    method: 'POST',
    headers: paypalCreateOrderRequestHeaders(accessToken),
    body: paypalCreateOrderRequestBody(order),
  });
  return await handle(response);
}

export async function paypalCaptureOrder(orderId: string) {
  const accessToken = await getLazyAccessToken();
  const url = `${PAYPAL_URL}/v2/checkout/orders/${orderId}/capture`;
  const response = await fetch(url, {
    method: 'POST',
    headers: paypalCaptureOrderRequestHeaders(accessToken),
  });
  return await handle(response);
}

async function handle(response: Response) {
  if (response.ok) {
    return await response.json();
  }
  throw new Error('Failed to communicate with PayPal. ' + await response.text());
}

function paypalCreateOrderRequestBody(order: Order) {
  const body = JSON.stringify({
    intent: 'CAPTURE',
    purchase_units: [
      {
        description: order.productDescription,
        soft_descriptor: order.shortDescription,
        amount: {
          currency_code: 'GBP',
          value: order.totalPrice,
        },
      },
    ],
  });
  return body;
}

function paypalCreateOrderRequestHeaders(accessToken: string) {
  return {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${accessToken}`,
  };
}

function paypalCaptureOrderRequestHeaders(accessToken: string) {
  return {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${accessToken}`,
  };
}

const getLazyAccessToken = (() => {
  let accessToken: string;
  let timeRequested: number;
  let timeExpires: number;
  return async function (): Promise<string> {
    const isNearlyExpired = timeExpires - Date.now() < 10_000;
    if (!accessToken || isNearlyExpired) {
      timeRequested = Date.now();
      let secondsUntilExpires: number;
      ({ accessToken, secondsUntilExpires } = await generateAccessToken());
      const msUntilExpires = secondsUntilExpires * 1000;
      timeExpires = timeRequested + msUntilExpires;
    }
    return accessToken;
  };
})();

async function generateAccessToken(): Promise<AccessTokenResponse> {
  const auth = Buffer.from(PAYPAL_CLIENT_ID + ':' + PAYPAL_SECRET).toString('base64');
  const response = await fetch(`${PAYPAL_URL}/v1/oauth2/token`, {
    method: 'POST',
    body: 'grant_type=client_credentials',
    headers: {
      Authorization: `Basic ${auth}`,
    },
  });
  const { access_token, expires_in } = await response.json();
  if (access_token && expires_in) {
    return {
      accessToken: access_token,
      secondsUntilExpires: expires_in,
    };
  }
  throw new Error('Could not fetch access token from PayPal.');
}

type AccessTokenResponse = {
  accessToken: string;
  secondsUntilExpires: number;
};

const PAYPAL_CLIENT_ID =
    env.ENVIRONMENT === 'prod'
    ? env.PAYPAL_LIVE_CLIENT_ID
    : env.PAYPAL_SANDBOX_CLIENT_ID;
const PAYPAL_SECRET =
    env.ENVIRONMENT === 'prod'
    ? env.PAYPAL_LIVE_SECRET
    : env.PAYPAL_SANDBOX_SECRET;
const PAYPAL_URL =
    env.ENVIRONMENT === 'prod'
    ? 'https://api-m.paypal.com'
    : 'https://api-m.sandbox.paypal.com'