1 | |
2 | type Paypal = { |
3 | clientId: string; |
4 | clientSecret: string; |
5 | }; |
6 |
|
7 | async function getToken(auth: Paypal): Promise<string> { |
8 | const url = new URL(`https://api-m.paypal.com/v1/oauth2/token`); |
9 | const response = await fetch(url, { |
10 | method: "POST", |
11 | headers: { |
12 | Authorization: `Basic ${btoa(`${auth.clientId}:${auth.clientSecret}`)}`, |
13 | }, |
14 | body: new URLSearchParams({ |
15 | grant_type: "client_credentials", |
16 | }), |
17 | }); |
18 | if (!response.ok) { |
19 | const text = await response.text(); |
20 | throw new Error(`Could not get token: ${response.status} ${text}`); |
21 | } |
22 | const json = await response.json(); |
23 | return json.access_token; |
24 | } |
25 |
|
26 | |
27 | * Send invoice reminder |
28 | * Sends a reminder to the payer about an invoice, by ID. In the JSON request body, include a `notification` object that defines the subject of the reminder and other details. |
29 | */ |
30 | export async function main( |
31 | auth: Paypal, |
32 | invoice_id: string, |
33 | body: { |
34 | subject?: string; |
35 | note?: string; |
36 | send_to_invoicer?: false | true; |
37 | send_to_recipient?: false | true; |
38 | additional_recipients?: string[]; |
39 | } |
40 | ) { |
41 | const token = await getToken(auth); |
42 | const url = new URL( |
43 | `https://api-m.paypal.com/v2/invoicing/invoices/${invoice_id}/remind` |
44 | ); |
45 |
|
46 | const response = await fetch(url, { |
47 | method: "POST", |
48 | headers: { |
49 | "Content-Type": "application/json", |
50 | Authorization: "Bearer " + token, |
51 | }, |
52 | body: JSON.stringify(body), |
53 | }); |
54 | if (!response.ok) { |
55 | const text = await response.text(); |
56 | throw new Error(`${response.status} ${text}`); |
57 | } |
58 | return await response.json(); |
59 | } |
60 |
|