0

Post subscription items item

by
Published Oct 30, 2023

Updates the plan or quantity of an item on a current subscription.

Script stripe
  • Submitted by hugo697 Typescript (fetch-only)
    Created 536 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Post subscription items item
    6
     * Updates the plan or quantity of an item on a current subscription.
    7
     */
    8
    export async function main(
    9
      auth: Stripe,
    10
      item: string,
    11
      body: {
    12
        billing_thresholds?: { usage_gte: number; [k: string]: unknown } | "";
    13
        expand?: string[];
    14
        metadata?: { [k: string]: string } | "";
    15
        off_session?: boolean;
    16
        payment_behavior?:
    17
          | "allow_incomplete"
    18
          | "default_incomplete"
    19
          | "error_if_incomplete"
    20
          | "pending_if_incomplete";
    21
        price?: string;
    22
        price_data?: {
    23
          currency: string;
    24
          product: string;
    25
          recurring: {
    26
            interval: "day" | "month" | "week" | "year";
    27
            interval_count?: number;
    28
            [k: string]: unknown;
    29
          };
    30
          tax_behavior?: "exclusive" | "inclusive" | "unspecified";
    31
          unit_amount?: number;
    32
          unit_amount_decimal?: string;
    33
          [k: string]: unknown;
    34
        };
    35
        proration_behavior?: "always_invoice" | "create_prorations" | "none";
    36
        proration_date?: number;
    37
        quantity?: number;
    38
        tax_rates?: string[] | "";
    39
      }
    40
    ) {
    41
      const url = new URL(`https://api.stripe.com/v1/subscription_items/${item}`);
    42
    
    
    43
      const response = await fetch(url, {
    44
        method: "POST",
    45
        headers: {
    46
          "Content-Type": "application/x-www-form-urlencoded",
    47
          Authorization: "Bearer " + auth.token,
    48
        },
    49
        body: encodeParams(body),
    50
      });
    51
      if (!response.ok) {
    52
        const text = await response.text();
    53
        throw new Error(`${response.status} ${text}`);
    54
      }
    55
      return await response.json();
    56
    }
    57
    
    
    58
    function encodeParams(o: any) {
    59
      function iter(o: any, path: string) {
    60
        if (Array.isArray(o)) {
    61
          o.forEach(function (a) {
    62
            iter(a, path + "[]");
    63
          });
    64
          return;
    65
        }
    66
        if (o !== null && typeof o === "object") {
    67
          Object.keys(o).forEach(function (k) {
    68
            iter(o[k], path + "[" + k + "]");
    69
          });
    70
          return;
    71
        }
    72
        data.push(path + "=" + o);
    73
      }
    74
      const data: string[] = [];
    75
      Object.keys(o).forEach(function (k) {
    76
        if (o[k] !== undefined) {
    77
          iter(o[k], k);
    78
        }
    79
      });
    80
      return new URLSearchParams(data.join("&"));
    81
    }
    82