0
Delete subscription items item
One script reply has been approved by the moderators Verified

Deletes an item from the subscription. Removing a subscription item from a subscription will not cancel the subscription.

Created by hugo697 189 days ago Viewed 5614 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 189 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Delete subscription items item
6
 * Deletes an item from the subscription. Removing a subscription item from a subscription will not cancel the subscription.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  item: string,
11
  body: {
12
    clear_usage?: boolean;
13
    proration_behavior?: "always_invoice" | "create_prorations" | "none";
14
    proration_date?: number;
15
  }
16
) {
17
  const url = new URL(`https://api.stripe.com/v1/subscription_items/${item}`);
18

19
  const response = await fetch(url, {
20
    method: "DELETE",
21
    headers: {
22
      "Content-Type": "application/x-www-form-urlencoded",
23
      Authorization: "Bearer " + auth.token,
24
    },
25
    body: new URLSearchParams(body as Record<string, string>),
26
  });
27
  if (!response.ok) {
28
    const text = await response.text();
29
    throw new Error(`${response.status} ${text}`);
30
  }
31
  return await response.json();
32
}
33