0

Partially update dispute

by
Published Apr 8, 2025

Partially updates a dispute, by ID. Seller can update the `communication_detail` value or The partner can add the `partner action` information.

Script paypal Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
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
 * Partially update dispute
27
 * Partially updates a dispute, by ID. Seller can update the `communication_detail` value or The partner can add the `partner action` information.
28
 */
29
export async function main(
30
  auth: Paypal,
31
  id: string,
32
  body: {
33
    op: "add" | "remove" | "replace" | "move" | "copy" | "test";
34
    path?: string;
35
    value?: {};
36
    from?: string;
37
  }[],
38
) {
39
  const token = await getToken(auth);
40
  const url = new URL(`https://api-m.paypal.com/v1/customer/disputes/${id}`);
41

42
  const formData = new FormData();
43
  for (const [k, v] of Object.entries(body)) {
44
    if (v !== undefined) {
45
      formData.append(k, String(v));
46
    }
47
  }
48
  const response = await fetch(url, {
49
    method: "PATCH",
50
    headers: {
51
      "Content-Type": "application/json",
52
      Authorization: "Bearer " + token,
53
    },
54
    body: JSON.stringify(body),
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62