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 | * Update or cancel tracking information for a PayPal order |
27 | * Updates or cancels the tracking information for a PayPal order, by ID. Updatable attributes or objects:AttributeOpNotesitemsreplaceUsing replace op for items will replace the entire items object with the value sent in request.notify_payerreplace, addstatusreplaceOnly patching status to CANCELLED is currently supported. |
28 | */ |
29 | export async function main( |
30 | auth: Paypal, |
31 | id: string, |
32 | tracker_id: string, |
33 | body: { |
34 | op: "add" | "remove" | "replace" | "move" | "copy" | "test"; |
35 | path?: string; |
36 | value?: {}; |
37 | from?: string; |
38 | }[], |
39 | ) { |
40 | const token = await getToken(auth); |
41 | const url = new URL( |
42 | `https://api-m.paypal.com/v2/checkout/orders/${id}/trackers/${tracker_id}`, |
43 | ); |
44 |
|
45 | const response = await fetch(url, { |
46 | method: "PATCH", |
47 | headers: { |
48 | "Content-Type": "application/json", |
49 | Authorization: "Bearer " + token, |
50 | }, |
51 | body: JSON.stringify(body), |
52 | }); |
53 | if (!response.ok) { |
54 | const text = await response.text(); |
55 | throw new Error(`${response.status} ${text}`); |
56 | } |
57 | return await response.json(); |
58 | } |
59 |
|