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 | * Make offer to resolve dispute |
27 | * Makes an offer to the other party to resolve a dispute, by ID. To make this call, the stage in the dispute lifecycle must be `INQUIRY`. If the customer accepts the offer, PayPal automatically makes a refund. Allowed offer_type values for the request is available in dispute details allowed response options object. |
28 | */ |
29 | export async function main( |
30 | auth: Paypal, |
31 | id: string, |
32 | body: { |
33 | note: string; |
34 | offer_amount?: { currency_code: string; value: string }; |
35 | return_shipping_address?: { |
36 | address_line_1?: string; |
37 | address_line_2?: string; |
38 | address_line_3?: string; |
39 | admin_area_4?: string; |
40 | admin_area_3?: string; |
41 | admin_area_2?: string; |
42 | admin_area_1?: string; |
43 | postal_code?: string; |
44 | country_code: string; |
45 | address_details?: { |
46 | street_number?: string; |
47 | street_name?: string; |
48 | street_type?: string; |
49 | delivery_service?: string; |
50 | building_name?: string; |
51 | sub_building?: string; |
52 | }; |
53 | }; |
54 | invoice_id?: string; |
55 | offer_type: |
56 | | "REFUND" |
57 | | "REFUND_WITH_RETURN" |
58 | | "REFUND_WITH_REPLACEMENT" |
59 | | "REPLACEMENT_WITHOUT_REFUND"; |
60 | }, |
61 | ) { |
62 | const token = await getToken(auth); |
63 | const url = new URL( |
64 | `https://api-m.paypal.com/v1/customer/disputes/${id}/make-offer`, |
65 | ); |
66 |
|
67 | const formData = new FormData(); |
68 | for (const [k, v] of Object.entries(body)) { |
69 | if (v !== undefined && v !== "") { |
70 | formData.append(k, String(v)); |
71 | } |
72 | } |
73 | const response = await fetch(url, { |
74 | method: "POST", |
75 | headers: { |
76 | "Content-Type": "application/json", |
77 | Authorization: "Bearer " + token, |
78 | }, |
79 | body: JSON.stringify(body), |
80 | }); |
81 | if (!response.ok) { |
82 | const text = await response.text(); |
83 | throw new Error(`${response.status} ${text}`); |
84 | } |
85 | return await response.json(); |
86 | } |
87 |
|