Update Subscription

Updates an account subscription.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Update Subscription
8
 * Updates an account subscription.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  subscription_identifier: string,
13
  account_identifier: string,
14
  body: {
15
    app?: { install_id?: string; [k: string]: unknown };
16
    component_values?: {
17
      default?: number;
18
      name?: string;
19
      price?: number;
20
      value?: number;
21
      [k: string]: unknown;
22
    }[];
23
    currency?: string;
24
    current_period_end?: string;
25
    current_period_start?: string;
26
    frequency?: "weekly" | "monthly" | "quarterly" | "yearly";
27
    id?: string;
28
    price?: number;
29
    rate_plan?: {
30
      currency?: string;
31
      externally_managed?: boolean;
32
      id?: { [k: string]: unknown };
33
      is_contract?: boolean;
34
      public_name?: string;
35
      scope?: string;
36
      sets?: string[];
37
      [k: string]: unknown;
38
    };
39
    state?:
40
      | "Trial"
41
      | "Provisioned"
42
      | "Paid"
43
      | "AwaitingPayment"
44
      | "Cancelled"
45
      | "Failed"
46
      | "Expired";
47
    zone?: { id?: string; name?: string; [k: string]: unknown };
48
    [k: string]: unknown;
49
  }
50
) {
51
  const url = new URL(
52
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/subscriptions/${subscription_identifier}`
53
  );
54

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