0

List templates

by
Published Apr 8, 2025

Lists merchant-created templates with associated details. The associated details include the emails, addresses, and phone numbers from the user's PayPal profile.The user can select which values to show in the business information section of their template.

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
 * List templates
27
 * Lists merchant-created templates with associated details. The associated details include the emails, addresses, and phone numbers from the user's PayPal profile.The user can select which values to show in the business information section of their template.
28
 */
29
export async function main(
30
  auth: Paypal,
31
  fields: string | undefined,
32
  page: string | undefined,
33
  page_size: string | undefined,
34
) {
35
  const token = await getToken(auth);
36
  const url = new URL(`https://api-m.paypal.com/v2/invoicing/templates`);
37
  for (const [k, v] of [
38
    ["fields", fields],
39
    ["page", page],
40
    ["page_size", page_size],
41
  ]) {
42
    if (v !== undefined && v !== "" && k !== undefined) {
43
      url.searchParams.append(k, v);
44
    }
45
  }
46
  const response = await fetch(url, {
47
    method: "GET",
48
    headers: {
49
      Authorization: "Bearer " + token,
50
    },
51
    body: undefined,
52
  });
53
  if (!response.ok) {
54
    const text = await response.text();
55
    throw new Error(`${response.status} ${text}`);
56
  }
57
  return await response.json();
58
}
59