Post webhook endpoints webhook endpoint

Updates the webhook endpoint. You may edit the url, the list of enabled_events, and the status of your endpoint.

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 webhook endpoints webhook endpoint
6
 * Updates the webhook endpoint. You may edit the url, the list of enabled_events, and the status of your endpoint.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  webhook_endpoint: string,
11
  body: {
12
    description?: string | "";
13
    disabled?: boolean;
14
    enabled_events?: (
15
      | "*"
16
      | "account.application.authorized"
17
      | "account.application.deauthorized"
18
      | "account.external_account.created"
19
      | "account.external_account.deleted"
20
      | "account.external_account.updated"
21
      | "account.updated"
22
      | "application_fee.created"
23
      | "application_fee.refund.updated"
24
      | "application_fee.refunded"
25
      | "balance.available"
26
      | "billing_portal.configuration.created"
27
      | "billing_portal.configuration.updated"
28
      | "billing_portal.session.created"
29
      | "capability.updated"
30
      | "cash_balance.funds_available"
31
      | "charge.captured"
32
      | "charge.dispute.closed"
33
      | "charge.dispute.created"
34
      | "charge.dispute.funds_reinstated"
35
      | "charge.dispute.funds_withdrawn"
36
      | "charge.dispute.updated"
37
      | "charge.expired"
38
      | "charge.failed"
39
      | "charge.pending"
40
      | "charge.refund.updated"
41
      | "charge.refunded"
42
      | "charge.succeeded"
43
      | "charge.updated"
44
      | "checkout.session.async_payment_failed"
45
      | "checkout.session.async_payment_succeeded"
46
      | "checkout.session.completed"
47
      | "checkout.session.expired"
48
      | "climate.order.canceled"
49
      | "climate.order.created"
50
      | "climate.order.delayed"
51
      | "climate.order.delivered"
52
      | "climate.order.product_substituted"
53
      | "climate.product.created"
54
      | "climate.product.pricing_updated"
55
      | "coupon.created"
56
      | "coupon.deleted"
57
      | "coupon.updated"
58
      | "credit_note.created"
59
      | "credit_note.updated"
60
      | "credit_note.voided"
61
      | "customer.created"
62
      | "customer.deleted"
63
      | "customer.discount.created"
64
      | "customer.discount.deleted"
65
      | "customer.discount.updated"
66
      | "customer.source.created"
67
      | "customer.source.deleted"
68
      | "customer.source.expiring"
69
      | "customer.source.updated"
70
      | "customer.subscription.created"
71
      | "customer.subscription.deleted"
72
      | "customer.subscription.paused"
73
      | "customer.subscription.pending_update_applied"
74
      | "customer.subscription.pending_update_expired"
75
      | "customer.subscription.resumed"
76
      | "customer.subscription.trial_will_end"
77
      | "customer.subscription.updated"
78
      | "customer.tax_id.created"
79
      | "customer.tax_id.deleted"
80
      | "customer.tax_id.updated"
81
      | "customer.updated"
82
      | "customer_cash_balance_transaction.created"
83
      | "file.created"
84
      | "financial_connections.account.created"
85
      | "financial_connections.account.deactivated"
86
      | "financial_connections.account.disconnected"
87
      | "financial_connections.account.reactivated"
88
      | "financial_connections.account.refreshed_balance"
89
      | "financial_connections.account.refreshed_ownership"
90
      | "financial_connections.account.refreshed_transactions"
91
      | "identity.verification_session.canceled"
92
      | "identity.verification_session.created"
93
      | "identity.verification_session.processing"
94
      | "identity.verification_session.redacted"
95
      | "identity.verification_session.requires_input"
96
      | "identity.verification_session.verified"
97
      | "invoice.created"
98
      | "invoice.deleted"
99
      | "invoice.finalization_failed"
100
      | "invoice.finalized"
101
      | "invoice.marked_uncollectible"
102
      | "invoice.paid"
103
      | "invoice.payment_action_required"
104
      | "invoice.payment_failed"
105
      | "invoice.payment_succeeded"
106
      | "invoice.sent"
107
      | "invoice.upcoming"
108
      | "invoice.updated"
109
      | "invoice.voided"
110
      | "invoiceitem.created"
111
      | "invoiceitem.deleted"
112
      | "issuing_authorization.created"
113
      | "issuing_authorization.request"
114
      | "issuing_authorization.updated"
115
      | "issuing_card.created"
116
      | "issuing_card.updated"
117
      | "issuing_cardholder.created"
118
      | "issuing_cardholder.updated"
119
      | "issuing_dispute.closed"
120
      | "issuing_dispute.created"
121
      | "issuing_dispute.funds_reinstated"
122
      | "issuing_dispute.submitted"
123
      | "issuing_dispute.updated"
124
      | "issuing_token.created"
125
      | "issuing_token.updated"
126
      | "issuing_transaction.created"
127
      | "issuing_transaction.updated"
128
      | "mandate.updated"
129
      | "payment_intent.amount_capturable_updated"
130
      | "payment_intent.canceled"
131
      | "payment_intent.created"
132
      | "payment_intent.partially_funded"
133
      | "payment_intent.payment_failed"
134
      | "payment_intent.processing"
135
      | "payment_intent.requires_action"
136
      | "payment_intent.succeeded"
137
      | "payment_link.created"
138
      | "payment_link.updated"
139
      | "payment_method.attached"
140
      | "payment_method.automatically_updated"
141
      | "payment_method.detached"
142
      | "payment_method.updated"
143
      | "payout.canceled"
144
      | "payout.created"
145
      | "payout.failed"
146
      | "payout.paid"
147
      | "payout.reconciliation_completed"
148
      | "payout.updated"
149
      | "person.created"
150
      | "person.deleted"
151
      | "person.updated"
152
      | "plan.created"
153
      | "plan.deleted"
154
      | "plan.updated"
155
      | "price.created"
156
      | "price.deleted"
157
      | "price.updated"
158
      | "product.created"
159
      | "product.deleted"
160
      | "product.updated"
161
      | "promotion_code.created"
162
      | "promotion_code.updated"
163
      | "quote.accepted"
164
      | "quote.canceled"
165
      | "quote.created"
166
      | "quote.finalized"
167
      | "radar.early_fraud_warning.created"
168
      | "radar.early_fraud_warning.updated"
169
      | "refund.created"
170
      | "refund.updated"
171
      | "reporting.report_run.failed"
172
      | "reporting.report_run.succeeded"
173
      | "reporting.report_type.updated"
174
      | "review.closed"
175
      | "review.opened"
176
      | "setup_intent.canceled"
177
      | "setup_intent.created"
178
      | "setup_intent.requires_action"
179
      | "setup_intent.setup_failed"
180
      | "setup_intent.succeeded"
181
      | "sigma.scheduled_query_run.created"
182
      | "source.canceled"
183
      | "source.chargeable"
184
      | "source.failed"
185
      | "source.mandate_notification"
186
      | "source.refund_attributes_required"
187
      | "source.transaction.created"
188
      | "source.transaction.updated"
189
      | "subscription_schedule.aborted"
190
      | "subscription_schedule.canceled"
191
      | "subscription_schedule.completed"
192
      | "subscription_schedule.created"
193
      | "subscription_schedule.expiring"
194
      | "subscription_schedule.released"
195
      | "subscription_schedule.updated"
196
      | "tax.settings.updated"
197
      | "tax_rate.created"
198
      | "tax_rate.updated"
199
      | "terminal.reader.action_failed"
200
      | "terminal.reader.action_succeeded"
201
      | "test_helpers.test_clock.advancing"
202
      | "test_helpers.test_clock.created"
203
      | "test_helpers.test_clock.deleted"
204
      | "test_helpers.test_clock.internal_failure"
205
      | "test_helpers.test_clock.ready"
206
      | "topup.canceled"
207
      | "topup.created"
208
      | "topup.failed"
209
      | "topup.reversed"
210
      | "topup.succeeded"
211
      | "transfer.created"
212
      | "transfer.reversed"
213
      | "transfer.updated"
214
      | "treasury.credit_reversal.created"
215
      | "treasury.credit_reversal.posted"
216
      | "treasury.debit_reversal.completed"
217
      | "treasury.debit_reversal.created"
218
      | "treasury.debit_reversal.initial_credit_granted"
219
      | "treasury.financial_account.closed"
220
      | "treasury.financial_account.created"
221
      | "treasury.financial_account.features_status_updated"
222
      | "treasury.inbound_transfer.canceled"
223
      | "treasury.inbound_transfer.created"
224
      | "treasury.inbound_transfer.failed"
225
      | "treasury.inbound_transfer.succeeded"
226
      | "treasury.outbound_payment.canceled"
227
      | "treasury.outbound_payment.created"
228
      | "treasury.outbound_payment.expected_arrival_date_updated"
229
      | "treasury.outbound_payment.failed"
230
      | "treasury.outbound_payment.posted"
231
      | "treasury.outbound_payment.returned"
232
      | "treasury.outbound_transfer.canceled"
233
      | "treasury.outbound_transfer.created"
234
      | "treasury.outbound_transfer.expected_arrival_date_updated"
235
      | "treasury.outbound_transfer.failed"
236
      | "treasury.outbound_transfer.posted"
237
      | "treasury.outbound_transfer.returned"
238
      | "treasury.received_credit.created"
239
      | "treasury.received_credit.failed"
240
      | "treasury.received_credit.succeeded"
241
      | "treasury.received_debit.created"
242
    )[];
243
    expand?: string[];
244
    metadata?: { [k: string]: string } | "";
245
    url?: string;
246
  }
247
) {
248
  const url = new URL(
249
    `https://api.stripe.com/v1/webhook_endpoints/${webhook_endpoint}`
250
  );
251

252
  const response = await fetch(url, {
253
    method: "POST",
254
    headers: {
255
      "Content-Type": "application/x-www-form-urlencoded",
256
      Authorization: "Bearer " + auth.token,
257
    },
258
    body: encodeParams(body),
259
  });
260
  if (!response.ok) {
261
    const text = await response.text();
262
    throw new Error(`${response.status} ${text}`);
263
  }
264
  return await response.json();
265
}
266

267
function encodeParams(o: any) {
268
  function iter(o: any, path: string) {
269
    if (Array.isArray(o)) {
270
      o.forEach(function (a) {
271
        iter(a, path + "[]");
272
      });
273
      return;
274
    }
275
    if (o !== null && typeof o === "object") {
276
      Object.keys(o).forEach(function (k) {
277
        iter(o[k], path + "[" + k + "]");
278
      });
279
      return;
280
    }
281
    data.push(path + "=" + o);
282
  }
283
  const data: string[] = [];
284
  Object.keys(o).forEach(function (k) {
285
    if (o[k] !== undefined) {
286
      iter(o[k], k);
287
    }
288
  });
289
  return new URLSearchParams(data.join("&"));
290
}
291