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 transactions |
28 | * Lists transactions. Specify one or more query parameters to filter the transaction that appear in the response.Notes: If you specify one or more optional query parameters, the ending_balance response field is empty.It takes a maximum of three hours for executed transactions to appear in the list transactions call.This call lists transaction for the previous three years. |
29 | */ |
30 | export async function main( |
31 | auth: Paypal, |
32 | transaction_id: string | undefined, |
33 | transaction_type: string | undefined, |
34 | transaction_status: string | undefined, |
35 | transaction_amount: string | undefined, |
36 | transaction_currency: string | undefined, |
37 | start_date: string | undefined, |
38 | end_date: string | undefined, |
39 | payment_instrument_type: string | undefined, |
40 | store_id: string | undefined, |
41 | terminal_id: string | undefined, |
42 | fields: string | undefined, |
43 | balance_affecting_records_only: string | undefined, |
44 | page_size: string | undefined, |
45 | page: string | undefined |
46 | ) { |
47 | const token = await getToken(auth); |
48 | const url = new URL( |
49 | `https://api-m.paypal.com/v1/reporting/v1/reporting/transactions` |
50 | ); |
51 | for (const [k, v] of [ |
52 | ["transaction_id", transaction_id], |
53 | ["transaction_type", transaction_type], |
54 | ["transaction_status", transaction_status], |
55 | ["transaction_amount", transaction_amount], |
56 | ["transaction_currency", transaction_currency], |
57 | ["start_date", start_date], |
58 | ["end_date", end_date], |
59 | ["payment_instrument_type", payment_instrument_type], |
60 | ["store_id", store_id], |
61 | ["terminal_id", terminal_id], |
62 | ["fields", fields], |
63 | ["balance_affecting_records_only", balance_affecting_records_only], |
64 | ["page_size", page_size], |
65 | ["page", page], |
66 | ]) { |
67 | if (v !== undefined && v !== "" && k !== undefined) { |
68 | url.searchParams.append(k, v); |
69 | } |
70 | } |
71 | const response = await fetch(url, { |
72 | method: "GET", |
73 | headers: { |
74 | Authorization: "Bearer " + token, |
75 | }, |
76 | body: undefined, |
77 | }); |
78 | if (!response.ok) { |
79 | const text = await response.text(); |
80 | throw new Error(`${response.status} ${text}`); |
81 | } |
82 | return await response.json(); |
83 | } |
84 |
|