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 | * List disputes |
28 | * Lists disputes with a summary set of details, which shows the dispute_id, reason, status, dispute_state, dispute_life_cycle_stage, dispute_channel, dispute_amount, create_time and update_time fields. |
29 | */ |
30 | export async function main( |
31 | auth: Paypal, |
32 | start_time: string | undefined, |
33 | disputed_transaction_id: string | undefined, |
34 | page_size: string | undefined, |
35 | next_page_token: string | undefined, |
36 | dispute_state: string | undefined, |
37 | update_time_before: string | undefined, |
38 | update_time_after: string | undefined |
39 | ) { |
40 | const token = await getToken(auth); |
41 | const url = new URL(`https://api-m.paypal.com/v1/customer/disputes`); |
42 | for (const [k, v] of [ |
43 | ["start_time", start_time], |
44 | ["disputed_transaction_id", disputed_transaction_id], |
45 | ["page_size", page_size], |
46 | ["next_page_token", next_page_token], |
47 | ["dispute_state", dispute_state], |
48 | ["update_time_before", update_time_before], |
49 | ["update_time_after", update_time_after], |
50 | ]) { |
51 | if (v !== undefined && v !== "" && k !== undefined) { |
52 | url.searchParams.append(k, v); |
53 | } |
54 | } |
55 | const response = await fetch(url, { |
56 | method: "GET", |
57 | headers: { |
58 | Authorization: "Bearer " + token, |
59 | }, |
60 | body: undefined, |
61 | }); |
62 | if (!response.ok) { |
63 | const text = await response.text(); |
64 | throw new Error(`${response.status} ${text}`); |
65 | } |
66 | return await response.json(); |
67 | } |
68 |
|