0

Delete payment token

by
Published Apr 8, 2025

Delete the payment token associated with the payment token id.

Script paypal Verified

The script

Submitted by hugo697 Bun
Verified 427 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
 * Delete payment token
27
 * Delete the payment token associated with the payment token id.
28
 */
29
export async function main(auth: Paypal, id: string) {
30
  const token = await getToken(auth);
31
  const url = new URL(
32
    `https://api-m.paypal.com/v3/vault/payment-tokens/${id}`,
33
  );
34

35
  const response = await fetch(url, {
36
    method: "DELETE",
37
    headers: {
38
      Authorization: "Bearer " + token,
39
    },
40
    body: undefined,
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.text();
47
}
48