Get invoices upcoming

At any time, you can preview the upcoming invoice for a customer.

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
 * Get invoices upcoming
6
 * At any time, you can preview the upcoming invoice for a customer.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  automatic_tax: any,
11
  coupon: string | undefined,
12
  currency: string | undefined,
13
  customer: string | undefined,
14
  customer_details: any,
15
  discounts: any,
16
  expand: any,
17
  invoice_items: any,
18
  issuer: any,
19
  on_behalf_of: any,
20
  schedule: string | undefined,
21
  subscription: string | undefined,
22
  subscription_billing_cycle_anchor: any,
23
  subscription_cancel_at: any,
24
  subscription_cancel_at_period_end: string | undefined,
25
  subscription_cancel_now: string | undefined,
26
  subscription_default_tax_rates: any,
27
  subscription_items: any,
28
  subscription_proration_behavior:
29
    | "always_invoice"
30
    | "create_prorations"
31
    | "none"
32
    | undefined,
33
  subscription_proration_date: string | undefined,
34
  subscription_resume_at: "now" | undefined,
35
  subscription_start_date: string | undefined,
36
  subscription_trial_end: any,
37
  subscription_trial_from_plan: string | undefined
38
) {
39
  const url = new URL(`https://api.stripe.com/v1/invoices/upcoming`);
40
  for (const [k, v] of [
41
    ["coupon", coupon],
42
    ["currency", currency],
43
    ["customer", customer],
44
    ["schedule", schedule],
45
    ["subscription", subscription],
46
    ["subscription_cancel_at_period_end", subscription_cancel_at_period_end],
47
    ["subscription_cancel_now", subscription_cancel_now],
48
    ["subscription_proration_behavior", subscription_proration_behavior],
49
    ["subscription_proration_date", subscription_proration_date],
50
    ["subscription_resume_at", subscription_resume_at],
51
    ["subscription_start_date", subscription_start_date],
52
    ["subscription_trial_from_plan", subscription_trial_from_plan],
53
  ]) {
54
    if (v !== undefined && v !== "") {
55
      url.searchParams.append(k, v);
56
    }
57
  }
58
  encodeParams({
59
    automatic_tax,
60
    customer_details,
61
    discounts,
62
    expand,
63
    invoice_items,
64
    issuer,
65
    on_behalf_of,
66
    subscription_billing_cycle_anchor,
67
    subscription_cancel_at,
68
    subscription_default_tax_rates,
69
    subscription_items,
70
    subscription_trial_end,
71
  }).forEach((v, k) => {
72
    if (v !== undefined && v !== "") {
73
      url.searchParams.append(k, v);
74
    }
75
  });
76
  const response = await fetch(url, {
77
    method: "GET",
78
    headers: {
79
      "Content-Type": "application/x-www-form-urlencoded",
80
      Authorization: "Bearer " + auth.token,
81
    },
82
    body: undefined,
83
  });
84
  if (!response.ok) {
85
    const text = await response.text();
86
    throw new Error(`${response.status} ${text}`);
87
  }
88
  return await response.json();
89
}
90

91
function encodeParams(o: any) {
92
  function iter(o: any, path: string) {
93
    if (Array.isArray(o)) {
94
      o.forEach(function (a) {
95
        iter(a, path + "[]");
96
      });
97
      return;
98
    }
99
    if (o !== null && typeof o === "object") {
100
      Object.keys(o).forEach(function (k) {
101
        iter(o[k], path + "[" + k + "]");
102
      });
103
      return;
104
    }
105
    data.push(path + "=" + o);
106
  }
107
  const data: string[] = [];
108
  Object.keys(o).forEach(function (k) {
109
    if (o[k] !== undefined) {
110
      iter(o[k], k);
111
    }
112
  });
113
  return new URLSearchParams(data.join("&"));
114
}
115