//native
type Paypal = {
clientId: string;
clientSecret: string;
};
async function getToken(auth: Paypal): Promise<string> {
const url = new URL(`https://api-m.paypal.com/v1/oauth2/token`);
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Basic ${btoa(`${auth.clientId}:${auth.clientSecret}`)}`,
},
body: new URLSearchParams({
grant_type: "client_credentials",
}),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Could not get token: ${response.status} ${text}`);
}
const json = await response.json();
return json.access_token;
}
/**
* List templates
* 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.
*/
export async function main(
auth: Paypal,
fields: string | undefined,
page: string | undefined,
page_size: string | undefined,
) {
const token = await getToken(auth);
const url = new URL(`https://api-m.paypal.com/v2/invoicing/templates`);
for (const [k, v] of [
["fields", fields],
["page", page],
["page_size", page_size],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: "Bearer " + token,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 428 days ago
//native
type Paypal = {
token: string;
};
/**
* List templates
* 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.
*/
export async function main(
auth: Paypal,
fields: string | undefined,
page: string | undefined,
page_size: string | undefined,
) {
const url = new URL(`https://api-m.paypal.com/v2/invoicing/templates`);
for (const [k, v] of [
["fields", fields],
["page", page],
["page_size", page_size],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: "Bearer " + auth.token,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 428 days ago