Post prices

Creates a new price for an existing product. The price can be recurring or one-time.

Script stripe Verified

by hugo697 ยท 10/30/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 368 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post prices
6
 * Creates a new price for an existing product. The price can be recurring or one-time.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    active?: boolean;
12
    billing_scheme?: "per_unit" | "tiered";
13
    currency: string;
14
    currency_options?: {
15
      [k: string]: {
16
        custom_unit_amount?: {
17
          enabled: boolean;
18
          maximum?: number;
19
          minimum?: number;
20
          preset?: number;
21
          [k: string]: unknown;
22
        };
23
        tax_behavior?: "exclusive" | "inclusive" | "unspecified";
24
        tiers?: {
25
          flat_amount?: number;
26
          flat_amount_decimal?: string;
27
          unit_amount?: number;
28
          unit_amount_decimal?: string;
29
          up_to: "inf" | number;
30
          [k: string]: unknown;
31
        }[];
32
        unit_amount?: number;
33
        unit_amount_decimal?: string;
34
        [k: string]: unknown;
35
      };
36
    };
37
    custom_unit_amount?: {
38
      enabled: boolean;
39
      maximum?: number;
40
      minimum?: number;
41
      preset?: number;
42
      [k: string]: unknown;
43
    };
44
    expand?: string[];
45
    lookup_key?: string;
46
    metadata?: { [k: string]: string };
47
    nickname?: string;
48
    product?: string;
49
    product_data?: {
50
      active?: boolean;
51
      id?: string;
52
      metadata?: { [k: string]: string };
53
      name: string;
54
      statement_descriptor?: string;
55
      tax_code?: string;
56
      unit_label?: string;
57
      [k: string]: unknown;
58
    };
59
    recurring?: {
60
      aggregate_usage?: "last_during_period" | "last_ever" | "max" | "sum";
61
      interval: "day" | "month" | "week" | "year";
62
      interval_count?: number;
63
      usage_type?: "licensed" | "metered";
64
      [k: string]: unknown;
65
    };
66
    tax_behavior?: "exclusive" | "inclusive" | "unspecified";
67
    tiers?: {
68
      flat_amount?: number;
69
      flat_amount_decimal?: string;
70
      unit_amount?: number;
71
      unit_amount_decimal?: string;
72
      up_to: "inf" | number;
73
      [k: string]: unknown;
74
    }[];
75
    tiers_mode?: "graduated" | "volume";
76
    transfer_lookup_key?: boolean;
77
    transform_quantity?: {
78
      divide_by: number;
79
      round: "down" | "up";
80
      [k: string]: unknown;
81
    };
82
    unit_amount?: number;
83
    unit_amount_decimal?: string;
84
  }
85
) {
86
  const url = new URL(`https://api.stripe.com/v1/prices`);
87

88
  const response = await fetch(url, {
89
    method: "POST",
90
    headers: {
91
      "Content-Type": "application/x-www-form-urlencoded",
92
      Authorization: "Bearer " + auth.token,
93
    },
94
    body: encodeParams(body),
95
  });
96
  if (!response.ok) {
97
    const text = await response.text();
98
    throw new Error(`${response.status} ${text}`);
99
  }
100
  return await response.json();
101
}
102

103
function encodeParams(o: any) {
104
  function iter(o: any, path: string) {
105
    if (Array.isArray(o)) {
106
      o.forEach(function (a) {
107
        iter(a, path + "[]");
108
      });
109
      return;
110
    }
111
    if (o !== null && typeof o === "object") {
112
      Object.keys(o).forEach(function (k) {
113
        iter(o[k], path + "[" + k + "]");
114
      });
115
      return;
116
    }
117
    data.push(path + "=" + o);
118
  }
119
  const data: string[] = [];
120
  Object.keys(o).forEach(function (k) {
121
    if (o[k] !== undefined) {
122
      iter(o[k], k);
123
    }
124
  });
125
  return new URLSearchParams(data.join("&"));
126
}
127