Get invoices upcoming lines

When retrieving an upcoming invoice, you’ll get a lines property containing the total count of line items and the first handful of those items. There is also a URL where you can retrieve the full (paginated) list of line items.

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 lines
6
 * When retrieving an upcoming invoice, you’ll get a lines property containing the total count of line items and the first handful of those items. There is also a URL where you can retrieve the full (paginated) list of line items.
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
  ending_before: string | undefined,
17
  expand: any,
18
  invoice_items: any,
19
  issuer: any,
20
  limit: string | undefined,
21
  on_behalf_of: any,
22
  schedule: string | undefined,
23
  starting_after: string | undefined,
24
  subscription: string | undefined,
25
  subscription_billing_cycle_anchor: any,
26
  subscription_cancel_at: any,
27
  subscription_cancel_at_period_end: string | undefined,
28
  subscription_cancel_now: string | undefined,
29
  subscription_default_tax_rates: any,
30
  subscription_items: any,
31
  subscription_proration_behavior:
32
    | "always_invoice"
33
    | "create_prorations"
34
    | "none"
35
    | undefined,
36
  subscription_proration_date: string | undefined,
37
  subscription_resume_at: "now" | undefined,
38
  subscription_start_date: string | undefined,
39
  subscription_trial_end: any,
40
  subscription_trial_from_plan: string | undefined
41
) {
42
  const url = new URL(`https://api.stripe.com/v1/invoices/upcoming/lines`);
43
  for (const [k, v] of [
44
    ["coupon", coupon],
45
    ["currency", currency],
46
    ["customer", customer],
47
    ["ending_before", ending_before],
48
    ["limit", limit],
49
    ["schedule", schedule],
50
    ["starting_after", starting_after],
51
    ["subscription", subscription],
52
    ["subscription_cancel_at_period_end", subscription_cancel_at_period_end],
53
    ["subscription_cancel_now", subscription_cancel_now],
54
    ["subscription_proration_behavior", subscription_proration_behavior],
55
    ["subscription_proration_date", subscription_proration_date],
56
    ["subscription_resume_at", subscription_resume_at],
57
    ["subscription_start_date", subscription_start_date],
58
    ["subscription_trial_from_plan", subscription_trial_from_plan],
59
  ]) {
60
    if (v !== undefined && v !== "") {
61
      url.searchParams.append(k, v);
62
    }
63
  }
64
  encodeParams({
65
    automatic_tax,
66
    customer_details,
67
    discounts,
68
    expand,
69
    invoice_items,
70
    issuer,
71
    on_behalf_of,
72
    subscription_billing_cycle_anchor,
73
    subscription_cancel_at,
74
    subscription_default_tax_rates,
75
    subscription_items,
76
    subscription_trial_end,
77
  }).forEach((v, k) => {
78
    if (v !== undefined && v !== "") {
79
      url.searchParams.append(k, v);
80
    }
81
  });
82
  const response = await fetch(url, {
83
    method: "GET",
84
    headers: {
85
      "Content-Type": "application/x-www-form-urlencoded",
86
      Authorization: "Bearer " + auth.token,
87
    },
88
    body: undefined,
89
  });
90
  if (!response.ok) {
91
    const text = await response.text();
92
    throw new Error(`${response.status} ${text}`);
93
  }
94
  return await response.json();
95
}
96

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