Post quotes

A quote models prices and services for a customer. Default options for header, description, footer, and expires_at can be set in the dashboard via the quote template.

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 quotes
6
 * A quote models prices and services for a customer. Default options for header, description, footer, and expires_at can be set in the dashboard via the quote template.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    application_fee_amount?: number | "";
12
    application_fee_percent?: number | "";
13
    automatic_tax?: {
14
      enabled: boolean;
15
      liability?: {
16
        account?: string;
17
        type: "account" | "self";
18
        [k: string]: unknown;
19
      };
20
      [k: string]: unknown;
21
    };
22
    collection_method?: "charge_automatically" | "send_invoice";
23
    customer?: string;
24
    default_tax_rates?: string[] | "";
25
    description?: string | "";
26
    discounts?:
27
      | { coupon?: string; discount?: string; [k: string]: unknown }[]
28
      | "";
29
    expand?: string[];
30
    expires_at?: number;
31
    footer?: string | "";
32
    from_quote?: { is_revision?: boolean; quote: string; [k: string]: unknown };
33
    header?: string | "";
34
    invoice_settings?: {
35
      days_until_due?: number;
36
      issuer?: {
37
        account?: string;
38
        type: "account" | "self";
39
        [k: string]: unknown;
40
      };
41
      [k: string]: unknown;
42
    };
43
    line_items?: {
44
      price?: string;
45
      price_data?: {
46
        currency: string;
47
        product: string;
48
        recurring?: {
49
          interval: "day" | "month" | "week" | "year";
50
          interval_count?: number;
51
          [k: string]: unknown;
52
        };
53
        tax_behavior?: "exclusive" | "inclusive" | "unspecified";
54
        unit_amount?: number;
55
        unit_amount_decimal?: string;
56
        [k: string]: unknown;
57
      };
58
      quantity?: number;
59
      tax_rates?: string[] | "";
60
      [k: string]: unknown;
61
    }[];
62
    metadata?: { [k: string]: string };
63
    on_behalf_of?: string | "";
64
    subscription_data?: {
65
      description?: string;
66
      effective_date?: "current_period_end" | number | "";
67
      metadata?: { [k: string]: string };
68
      trial_period_days?: number | "";
69
      [k: string]: unknown;
70
    };
71
    test_clock?: string;
72
    transfer_data?:
73
      | {
74
          amount?: number;
75
          amount_percent?: number;
76
          destination: string;
77
          [k: string]: unknown;
78
        }
79
      | "";
80
  }
81
) {
82
  const url = new URL(`https://api.stripe.com/v1/quotes`);
83

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

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