0

Update pricing

by
Published Apr 8, 2025

Updates pricing for a plan. For example, you can update a regular billing cycle from $5 per month to $7 per month.

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
 * Update pricing
27
 * Updates pricing for a plan. For example, you can update a regular billing cycle from $5 per month to $7 per month.
28
 */
29
export async function main(
30
  auth: Paypal,
31
  id: string,
32
  body: {
33
    pricing_schemes: {
34
      billing_cycle_sequence: number;
35
      pricing_scheme: {
36
        version?: number;
37
        fixed_price?: { currency_code: string; value: string };
38
        pricing_model?: "VOLUME" | "TIERED";
39
        tiers?: {
40
          starting_quantity: string;
41
          ending_quantity?: string;
42
          amount: { currency_code: string; value: string };
43
        }[];
44
        create_time?: string;
45
        update_time?: string;
46
      };
47
    }[];
48
  },
49
) {
50
  const token = await getToken(auth);
51
  const url = new URL(
52
    `https://api-m.paypal.com/v1/billing/plans/${id}/update-pricing-schemes`,
53
  );
54

55
  const response = await fetch(url, {
56
    method: "POST",
57
    headers: {
58
      "Content-Type": "application/json",
59
      Authorization: "Bearer " + token,
60
    },
61
    body: JSON.stringify(body),
62
  });
63
  if (!response.ok) {
64
    const text = await response.text();
65
    throw new Error(`${response.status} ${text}`);
66
  }
67
  return await response.json();
68
}
69