0

Get credit notes preview lines

by
Published Oct 30, 2023

When retrieving a credit note preview, you’ll get a lines property containing the first handful of those items. This URL you can retrieve the full (paginated) list of line items.

Script stripe Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Get credit notes preview lines
6
 * When retrieving a credit note preview, you’ll get a lines property containing the first handful of those items. This URL you can retrieve the full (paginated) list of line items.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  amount: string | undefined,
11
  credit_amount: string | undefined,
12
  effective_at: string | undefined,
13
  ending_before: string | undefined,
14
  expand: any,
15
  invoice: string | undefined,
16
  limit: string | undefined,
17
  lines: any,
18
  memo: string | undefined,
19
  metadata: any,
20
  out_of_band_amount: string | undefined,
21
  reason:
22
    | "duplicate"
23
    | "fraudulent"
24
    | "order_change"
25
    | "product_unsatisfactory"
26
    | undefined,
27
  refund: string | undefined,
28
  refund_amount: string | undefined,
29
  shipping_cost: any,
30
  starting_after: string | undefined
31
) {
32
  const url = new URL(`https://api.stripe.com/v1/credit_notes/preview/lines`);
33
  for (const [k, v] of [
34
    ["amount", amount],
35
    ["credit_amount", credit_amount],
36
    ["effective_at", effective_at],
37
    ["ending_before", ending_before],
38
    ["invoice", invoice],
39
    ["limit", limit],
40
    ["memo", memo],
41
    ["out_of_band_amount", out_of_band_amount],
42
    ["reason", reason],
43
    ["refund", refund],
44
    ["refund_amount", refund_amount],
45
    ["starting_after", starting_after],
46
  ]) {
47
    if (v !== undefined && v !== "") {
48
      url.searchParams.append(k, v);
49
    }
50
  }
51
  encodeParams({ expand, lines, metadata, shipping_cost }).forEach((v, k) => {
52
    if (v !== undefined && v !== "") {
53
      url.searchParams.append(k, v);
54
    }
55
  });
56
  const response = await fetch(url, {
57
    method: "GET",
58
    headers: {
59
      "Content-Type": "application/x-www-form-urlencoded",
60
      Authorization: "Bearer " + auth.token,
61
    },
62
    body: undefined,
63
  });
64
  if (!response.ok) {
65
    const text = await response.text();
66
    throw new Error(`${response.status} ${text}`);
67
  }
68
  return await response.json();
69
}
70

71
function encodeParams(o: any) {
72
  function iter(o: any, path: string) {
73
    if (Array.isArray(o)) {
74
      o.forEach(function (a) {
75
        iter(a, path + "[]");
76
      });
77
      return;
78
    }
79
    if (o !== null && typeof o === "object") {
80
      Object.keys(o).forEach(function (k) {
81
        iter(o[k], path + "[" + k + "]");
82
      });
83
      return;
84
    }
85
    data.push(path + "=" + o);
86
  }
87
  const data: string[] = [];
88
  Object.keys(o).forEach(function (k) {
89
    if (o[k] !== undefined) {
90
      iter(o[k], k);
91
    }
92
  });
93
  return new URLSearchParams(data.join("&"));
94
}
95