//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;
}
/**
* Update pricing
* Updates pricing for a plan. For example, you can update a regular billing cycle from $5 per month to $7 per month.
*/
export async function main(
auth: Paypal,
id: string,
body: {
pricing_schemes: {
billing_cycle_sequence: number;
pricing_scheme: {
version?: number;
fixed_price?: { currency_code: string; value: string };
pricing_model?: "VOLUME" | "TIERED";
tiers?: {
starting_quantity: string;
ending_quantity?: string;
amount: { currency_code: string; value: string };
}[];
create_time?: string;
update_time?: string;
};
}[];
},
) {
const token = await getToken(auth);
const url = new URL(
`https://api-m.paypal.com/v1/billing/plans/${id}/update-pricing-schemes`,
);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
body: JSON.stringify(body),
});
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;
};
/**
* Update pricing
* Updates pricing for a plan. For example, you can update a regular billing cycle from $5 per month to $7 per month.
*/
export async function main(
auth: Paypal,
id: string,
body: {
pricing_schemes: {
billing_cycle_sequence: number;
pricing_scheme: {
version?: number;
fixed_price?: { currency_code: string; value: string };
pricing_model?: "VOLUME" | "TIERED";
tiers?: {
starting_quantity: string;
ending_quantity?: string;
amount: { currency_code: string; value: string };
}[];
create_time?: string;
update_time?: string;
};
}[];
},
) {
const url = new URL(
`https://api-m.paypal.com/v1/billing/plans/${id}/update-pricing-schemes`,
);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + auth.token,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 428 days ago