0

Revise plan or quantity of subscription

by
Published Apr 8, 2025

Updates the quantity of the product or service in a subscription. You can also use this method to switch the plan and update the `shipping_amount`, `shipping_address` values for the subscription. This type of update requires the buyer's consent.

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
 * Revise plan or quantity of subscription
27
 * Updates the quantity of the product or service in a subscription. You can also use this method to switch the plan and update the `shipping_amount`, `shipping_address` values for the subscription. This type of update requires the buyer's consent.
28
 */
29
export async function main(
30
  auth: Paypal,
31
  id: string,
32
  body: {
33
    plan_id?: string;
34
    quantity?: string;
35
    shipping_amount?: { currency_code: string; value: string };
36
    shipping_address?: {
37
      name?: {
38
        prefix?: string;
39
        given_name?: string;
40
        surname?: string;
41
        middle_name?: string;
42
        suffix?: string;
43
        alternate_full_name?: string;
44
        full_name?: string;
45
      };
46
      type?: "SHIPPING" | "PICKUP_IN_PERSON";
47
      address?: {
48
        address_line_1?: string;
49
        address_line_2?: string;
50
        address_line_3?: string;
51
        admin_area_4?: string;
52
        admin_area_3?: string;
53
        admin_area_2?: string;
54
        admin_area_1?: string;
55
        postal_code?: string;
56
        country_code: string;
57
        address_details?: {
58
          street_number?: string;
59
          street_name?: string;
60
          street_type?: string;
61
          delivery_service?: string;
62
          building_name?: string;
63
          sub_building?: string;
64
        };
65
      };
66
    };
67
    application_context?: {
68
      brand_name?: string;
69
      locale?: string;
70
      shipping_preference?:
71
        | "GET_FROM_FILE"
72
        | "NO_SHIPPING"
73
        | "SET_PROVIDED_ADDRESS";
74
      user_action?: "CONTINUE" | "SUBSCRIBE_NOW";
75
      payment_method?: {
76
        payer_selected?: string;
77
        payee_preferred?: "UNRESTRICTED" | "IMMEDIATE_PAYMENT_REQUIRED";
78
        standard_entry_class_code?: "TEL" | "WEB" | "CCD" | "PPD";
79
      };
80
      return_url: string;
81
      cancel_url: string;
82
    };
83
    plan?: {
84
      billing_cycles?: {
85
        pricing_scheme?: {
86
          version?: number;
87
          fixed_price?: { currency_code: string; value: string };
88
          pricing_model?: "VOLUME" | "TIERED";
89
          tiers?: {
90
            starting_quantity: string;
91
            ending_quantity?: string;
92
            amount: { currency_code: string; value: string };
93
          }[];
94
          create_time?: string;
95
          update_time?: string;
96
        };
97
        sequence: number;
98
        total_cycles?: number;
99
      }[];
100
      payment_preferences?: {
101
        auto_bill_outstanding?: false | true;
102
        setup_fee?: { currency_code: string; value: string };
103
        setup_fee_failure_action?: "CONTINUE" | "CANCEL";
104
        payment_failure_threshold?: number;
105
      };
106
      taxes?: { percentage?: string; inclusive?: false | true };
107
    };
108
  },
109
) {
110
  const token = await getToken(auth);
111
  const url = new URL(
112
    `https://api-m.paypal.com/v1/billing/subscriptions/${id}/revise`,
113
  );
114

115
  const response = await fetch(url, {
116
    method: "POST",
117
    headers: {
118
      "Content-Type": "application/json",
119
      Authorization: "Bearer " + token,
120
    },
121
    body: JSON.stringify(body),
122
  });
123
  if (!response.ok) {
124
    const text = await response.text();
125
    throw new Error(`${response.status} ${text}`);
126
  }
127
  return await response.json();
128
}
129