@paykit-sdk/stripe 1.1.9 → 1.1.93

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,8 @@ interface StripeOptions extends PaykitProviderOptions<Stripe.StripeConfig> {
6
6
  }
7
7
  declare class StripeProvider extends AbstractPayKitProvider implements PayKitProvider {
8
8
  private stripe;
9
- constructor(config: StripeOptions);
9
+ private opts;
10
+ constructor(opts: StripeOptions);
10
11
  readonly providerName = "stripe";
11
12
  /**
12
13
  * Checkout management
@@ -4052,13 +4052,16 @@ var coerce = {
4052
4052
  date: (arg) => ZodDate.create({ ...arg, coerce: true })
4053
4053
  };
4054
4054
  var NEVER = INVALID;
4055
- var paykitCheckout$InboundSchema = (checkout) => {
4055
+ var paykitCheckout$InboundSchema = (checkout, lineItems) => {
4056
4056
  return {
4057
4057
  id: checkout.id,
4058
4058
  customer: typeof checkout.customer === "string" ? checkout.customer : checkout.customer?.id ?? "",
4059
4059
  session_type: checkout.mode === "subscription" ? "recurring" : "one_time",
4060
4060
  payment_url: checkout.url,
4061
- products: checkout.line_items.data.map((item) => ({ id: item.price.id, quantity: item.quantity })),
4061
+ products: lineItems.map((item) => ({
4062
+ id: item.id,
4063
+ quantity: item.quantity
4064
+ })),
4062
4065
  currency: checkout.currency,
4063
4066
  amount: checkout.amount_total,
4064
4067
  subscription: null,
@@ -4077,10 +4080,13 @@ var paykitCustomer$InboundSchema = (customer) => {
4077
4080
  var paykitSubscription$InboundSchema = (subscription) => {
4078
4081
  const status = (() => {
4079
4082
  if (["active", "trialing"].includes(subscription.status)) return "active";
4080
- if (["incomplete_expired", "incomplete", "past_due"].includes(subscription.status)) return "past_due";
4083
+ if (["incomplete_expired", "incomplete", "past_due"].includes(subscription.status)) {
4084
+ return "past_due";
4085
+ }
4081
4086
  if (["canceled"].includes(subscription.status)) return "canceled";
4082
4087
  if (["expired"].includes(subscription.status)) return "expired";
4083
- throw new Error(`Unknown status: ${subscription.status}`);
4088
+ console.log(`Unknown subscription status: ${subscription.status}`);
4089
+ return "pending";
4084
4090
  })();
4085
4091
  const metadata = core.omitInternalMetadata(subscription.metadata ?? {});
4086
4092
  return {
@@ -4100,7 +4106,8 @@ var paykitSubscription$InboundSchema = (subscription) => {
4100
4106
  var paykitInvoice$InboundSchema = (invoice) => {
4101
4107
  const status = (() => {
4102
4108
  if (invoice.status == "paid") return "paid";
4103
- if (["draft", "open", "uncollectible", "void"].includes(invoice.status)) return "open";
4109
+ if (["draft", "open", "uncollectible", "void"].includes(invoice.status))
4110
+ return "open";
4104
4111
  throw new Error(`Unknown status: ${invoice.status}`);
4105
4112
  })();
4106
4113
  const customerId = (() => {
@@ -4114,7 +4121,10 @@ var paykitInvoice$InboundSchema = (invoice) => {
4114
4121
  customer: customerId,
4115
4122
  billing_mode: invoice.billingMode,
4116
4123
  amount_paid: invoice.amount_paid,
4117
- line_items: invoice.lines.data.map((line) => ({ id: line.id, quantity: line.quantity })),
4124
+ line_items: invoice.lines.data.map((line) => ({
4125
+ id: line.id,
4126
+ quantity: line.quantity
4127
+ })),
4118
4128
  subscription_id: invoice.parent?.subscription_details?.subscription?.toString() ?? null,
4119
4129
  status,
4120
4130
  paid_at: new Date(invoice.created * 1e3).toISOString(),
@@ -4148,6 +4158,7 @@ var stripeToPaykitStatus = (status) => {
4148
4158
  }
4149
4159
  };
4150
4160
  var paykitPayment$InboundSchema = (intent) => {
4161
+ const itemId = JSON.parse(intent.metadata?.[core.PAYKIT_METADATA_KEY] ?? "{}").itemId;
4151
4162
  return {
4152
4163
  id: intent.id,
4153
4164
  amount: intent.amount,
@@ -4155,7 +4166,7 @@ var paykitPayment$InboundSchema = (intent) => {
4155
4166
  customer: intent.customer ?? "",
4156
4167
  status: stripeToPaykitStatus(intent.status),
4157
4168
  metadata: core.omitInternalMetadata(intent.metadata ?? {}),
4158
- product_id: intent.metadata?.product_id
4169
+ item_id: itemId
4159
4170
  };
4160
4171
  };
4161
4172
  var paykitRefund$InboundSchema = (refund) => {
@@ -4167,6 +4178,22 @@ var paykitRefund$InboundSchema = (refund) => {
4167
4178
  metadata: core.omitInternalMetadata(refund.metadata ?? {})
4168
4179
  };
4169
4180
  };
4181
+ var paykitRefundReason$OutboundSchema = (reason) => {
4182
+ if (!reason) return void 0;
4183
+ const normalized = reason.toLowerCase().trim();
4184
+ if (/duplicate|duplicated|double.*charge|charged.*twice/i.test(normalized)) {
4185
+ return "duplicate";
4186
+ }
4187
+ if (/fraud|fraudulent|unauthorized|scam|stolen.*card|not.*authorized/i.test(normalized)) {
4188
+ return "fraudulent";
4189
+ }
4190
+ if (/customer.*request|requested.*customer|customer.*want|cancel|refund.*request|changed.*mind/i.test(
4191
+ normalized
4192
+ )) {
4193
+ return "requested_by_customer";
4194
+ }
4195
+ return "requested_by_customer";
4196
+ };
4170
4197
 
4171
4198
  // src/stripe-provider.ts
4172
4199
  var stripeOptionsSchema = core.schema()(
@@ -4177,26 +4204,53 @@ var stripeOptionsSchema = core.schema()(
4177
4204
  );
4178
4205
  var providerName = "stripe";
4179
4206
  var StripeProvider = class extends core.AbstractPayKitProvider {
4180
- constructor(config) {
4181
- super(stripeOptionsSchema, config, providerName);
4207
+ constructor(opts) {
4208
+ super(stripeOptionsSchema, opts, providerName);
4182
4209
  this.providerName = providerName;
4183
4210
  /**
4184
4211
  * Checkout management
4185
4212
  */
4186
4213
  this.createCheckout = async (params) => {
4187
- const metadata = Object.fromEntries(Object.entries(params.metadata ?? {}).map(([key, value]) => [key, JSON.stringify(value)]));
4214
+ const { error, data } = core.createCheckoutSchema.safeParse(params);
4215
+ if (error) {
4216
+ throw core.ValidationError.fromZodError(error, this.providerName, "createCheckout");
4217
+ }
4218
+ const checkoutMetadata = core.stringifyMetadataValues(data.metadata ?? {});
4188
4219
  const checkoutOptions = {
4189
- ...typeof params.customer === "string" && { customer: params.customer },
4190
- ...typeof params.customer === "object" && { customer: params.customer.email },
4191
- mode: params.session_type === "one_time" ? "payment" : "subscription",
4192
- line_items: [{ price: params.item_id, quantity: params.quantity }],
4193
- ...params.session_type == "one_time" && { metadata },
4194
- ...params.session_type == "recurring" && { subscription_data: { metadata } },
4195
- ...params.provider_metadata
4220
+ ...data.provider_metadata,
4221
+ mode: data.session_type === "one_time" ? "payment" : "subscription",
4222
+ line_items: [{ price: data.item_id, quantity: data.quantity }],
4223
+ success_url: data.success_url,
4224
+ cancel_url: data.cancel_url
4196
4225
  };
4226
+ if (params.session_type == "recurring") {
4227
+ checkoutOptions.subscription_data = { metadata: checkoutMetadata };
4228
+ } else if (params.session_type == "one_time") {
4229
+ checkoutOptions.metadata = checkoutMetadata;
4230
+ checkoutOptions.payment_intent_data = {
4231
+ setup_future_usage: "off_session"
4232
+ };
4233
+ }
4234
+ if (typeof params.customer === "string") {
4235
+ checkoutOptions.customer = params.customer;
4236
+ } else if (typeof params.customer === "object" && "email" in params.customer) {
4237
+ checkoutOptions.customer_email = params.customer.email;
4238
+ } else {
4239
+ throw new core.InvalidTypeError(
4240
+ "customer",
4241
+ "string (customer ID) or object (customer) with email",
4242
+ "object",
4243
+ {
4244
+ provider: this.providerName,
4245
+ method: "createCheckout"
4246
+ }
4247
+ );
4248
+ }
4197
4249
  if (params.billing) {
4198
4250
  checkoutOptions.shipping_address_collection = {
4199
- allowed_countries: [params.billing.address.country]
4251
+ allowed_countries: [
4252
+ params.billing.address.country
4253
+ ]
4200
4254
  };
4201
4255
  checkoutOptions.shipping_options = [
4202
4256
  {
@@ -4209,18 +4263,48 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4209
4263
  ];
4210
4264
  }
4211
4265
  const checkout = await this.stripe.checkout.sessions.create(checkoutOptions);
4212
- return paykitCheckout$InboundSchema(checkout);
4266
+ return paykitCheckout$InboundSchema(checkout, [
4267
+ { id: params.item_id, quantity: params.quantity }
4268
+ ]);
4213
4269
  };
4214
4270
  this.retrieveCheckout = async (id) => {
4215
- const checkout = await this.stripe.checkout.sessions.retrieve(id);
4216
- return paykitCheckout$InboundSchema(checkout);
4271
+ const checkout = await this.stripe.checkout.sessions.retrieve(id, {
4272
+ expand: ["line_items"]
4273
+ });
4274
+ return paykitCheckout$InboundSchema(
4275
+ checkout,
4276
+ checkout.line_items?.data.map((item) => ({
4277
+ id: item.price?.id ?? "",
4278
+ quantity: item.quantity ?? 0
4279
+ })) ?? []
4280
+ );
4217
4281
  };
4218
4282
  this.updateCheckout = async (id, params) => {
4219
- const checkout = await this.stripe.checkout.sessions.update(id, params);
4220
- return paykitCheckout$InboundSchema(checkout);
4283
+ const { error, data } = core.updateCheckoutSchema.safeParse(params);
4284
+ if (error) {
4285
+ throw core.ValidationError.fromZodError(error, this.providerName, "updateCheckout");
4286
+ }
4287
+ const originalCheckout = await this.stripe.checkout.sessions.retrieve(id, {
4288
+ expand: ["line_items"]
4289
+ });
4290
+ if (!originalCheckout) {
4291
+ throw new core.ResourceNotFoundError("checkout", id, this.providerName);
4292
+ }
4293
+ const updatedCheckout = await this.stripe.checkout.sessions.update(id, {
4294
+ ...data,
4295
+ metadata: core.stringifyMetadataValues(data.metadata ?? {})
4296
+ });
4297
+ return paykitCheckout$InboundSchema(
4298
+ updatedCheckout,
4299
+ updatedCheckout.line_items?.data.map((item) => ({
4300
+ id: item.price?.id ?? "",
4301
+ quantity: item.quantity ?? 0
4302
+ })) ?? []
4303
+ );
4221
4304
  };
4222
4305
  this.deleteCheckout = async (id) => {
4223
- throw new core.NotImplementedError("deleteCheckout", "stripe", { futureSupport: false });
4306
+ await this.stripe.checkout.sessions.expire(id);
4307
+ return null;
4224
4308
  };
4225
4309
  /**
4226
4310
  * Customer management
@@ -4236,7 +4320,11 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4236
4320
  };
4237
4321
  this.updateCustomer = async (id, params) => {
4238
4322
  const { provider_metadata, ...rest } = params;
4239
- const customer = await this.stripe.customers.update(id, { ...rest, ...provider_metadata });
4323
+ const customer = await this.stripe.customers.update(id, {
4324
+ ...provider_metadata,
4325
+ ...rest,
4326
+ metadata: core.stringifyMetadataValues(rest.metadata ?? {})
4327
+ });
4240
4328
  return paykitCustomer$InboundSchema(customer);
4241
4329
  };
4242
4330
  this.deleteCustomer = async (id) => {
@@ -4262,11 +4350,20 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4262
4350
  method: "createSubscription"
4263
4351
  });
4264
4352
  }
4353
+ if (this.opts.debug) {
4354
+ console.info(
4355
+ "Creating subscription with default payment method, this can be overridden by passing `default_payment_method` in the provider_metadata e.g pm_xxx"
4356
+ );
4357
+ }
4358
+ const defaultPaymentMethod = data.provider_metadata?.default_payment_method;
4265
4359
  const subscription = await this.stripe.subscriptions.create({
4360
+ ...data.provider_metadata,
4361
+ ...defaultPaymentMethod && { default_payment_method: defaultPaymentMethod },
4266
4362
  customer: data.customer,
4267
4363
  items: [{ price: data.item_id }],
4268
- metadata: Object.fromEntries(Object.entries(data.metadata ?? {}).map(([key, value]) => [key, JSON.stringify(value)])),
4269
- ...data.provider_metadata
4364
+ metadata: core.stringifyMetadataValues(data.metadata ?? {}),
4365
+ payment_behavior: "default_incomplete"
4366
+ // customer's default payment method will be used if available
4270
4367
  });
4271
4368
  return paykitSubscription$InboundSchema(subscription);
4272
4369
  };
@@ -4279,19 +4376,19 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4279
4376
  if (error) {
4280
4377
  throw core.ValidationError.fromZodError(error, this.providerName, "updateSubscription");
4281
4378
  }
4282
- const subscription = await this.stripe.subscriptions.retrieve(id);
4283
- const metadata = Object.fromEntries(
4284
- Object.entries({ ...subscription.metadata ?? {}, ...data.metadata ?? {} }).map(([key, value]) => [key, JSON.stringify(value)])
4285
- );
4286
4379
  const updatedSubscription = await this.stripe.subscriptions.update(id, {
4287
- metadata
4380
+ metadata: core.stringifyMetadataValues(data.metadata ?? {})
4288
4381
  });
4289
4382
  return paykitSubscription$InboundSchema(updatedSubscription);
4290
4383
  };
4291
4384
  this.retrieveSubscription = async (id) => {
4292
4385
  const { error, data } = core.retrieveSubscriptionSchema.safeParse({ id });
4293
4386
  if (error) {
4294
- throw core.ValidationError.fromZodError(error, this.providerName, "retrieveSubscription");
4387
+ throw core.ValidationError.fromZodError(
4388
+ error,
4389
+ this.providerName,
4390
+ "retrieveSubscription"
4391
+ );
4295
4392
  }
4296
4393
  const subscription = await this.stripe.subscriptions.retrieve(data.id);
4297
4394
  return paykitSubscription$InboundSchema(subscription);
@@ -4309,20 +4406,56 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4309
4406
  */
4310
4407
  this.createPayment = async (params) => {
4311
4408
  const { error, data } = core.createPaymentSchema.safeParse(params);
4312
- if (error) throw new core.ValidationError(error.message.split("\n").join(" "), { provider: this.providerName, method: "createPayment" });
4313
- const { provider_metadata, customer, ...rest } = data;
4409
+ if (error)
4410
+ throw new core.ValidationError(error.message.split("\n").join(" "), {
4411
+ provider: this.providerName,
4412
+ method: "createPayment"
4413
+ });
4414
+ const { provider_metadata, customer, capture_method, ...rest } = data;
4314
4415
  if (typeof customer === "object") {
4315
4416
  throw new core.InvalidTypeError("customer", "string (customer ID)", "object", {
4316
4417
  provider: this.providerName,
4317
4418
  method: "createPayment"
4318
4419
  });
4319
4420
  }
4320
- const metadata = Object.fromEntries(
4321
- Object.entries({ ...rest.metadata ?? {}, ...provider_metadata?.metadata ?? {}, product_id: params.product_id ?? null }).map(
4322
- ([key, value]) => [key, JSON.stringify(value)]
4323
- )
4421
+ const paymentMetadata = core.stringifyMetadataValues({
4422
+ ...rest.metadata,
4423
+ ...provider_metadata?.metadata ?? {},
4424
+ [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4425
+ });
4426
+ const customerWithDefaultPaymentMethod = await this.stripe.customers.retrieve(
4427
+ customer,
4428
+ { expand: ["invoice_settings.default_payment_method"] }
4324
4429
  );
4325
- const paymentIntentOptions = { ...provider_metadata, ...rest, metadata, customer };
4430
+ if ("deleted" in customerWithDefaultPaymentMethod) {
4431
+ throw new core.ValidationError("Customer has been deleted", {
4432
+ provider: this.providerName,
4433
+ method: "createPayment"
4434
+ });
4435
+ }
4436
+ let defaultPaymentMethod = customerWithDefaultPaymentMethod.invoice_settings?.default_payment_method;
4437
+ if (!defaultPaymentMethod) {
4438
+ const paymentMethods = await this.stripe.paymentMethods.list({ customer });
4439
+ if (paymentMethods.data.length === 0) {
4440
+ throw new core.ValidationError(
4441
+ `Customer ${customer} has no payment methods. Add a payment method for the customer before creating a payment intent`,
4442
+ { provider: this.providerName, method: "createPayment" }
4443
+ );
4444
+ }
4445
+ defaultPaymentMethod = paymentMethods.data[0].id;
4446
+ }
4447
+ const paymentIntentOptions = {
4448
+ currency: rest.currency,
4449
+ amount: rest.amount,
4450
+ metadata: paymentMetadata,
4451
+ customer,
4452
+ capture_method,
4453
+ confirm: true,
4454
+ // automatically confirms the payment
4455
+ payment_method: defaultPaymentMethod,
4456
+ off_session: true
4457
+ // uses customer's default payment method, avoids 3Ds/authentication
4458
+ };
4326
4459
  if (data.billing) {
4327
4460
  paymentIntentOptions.shipping = {
4328
4461
  name: data.billing.address.name,
@@ -4346,21 +4479,38 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4346
4479
  throw core.ValidationError.fromZodError(error, this.providerName, "updatePayment");
4347
4480
  }
4348
4481
  const { provider_metadata, customer, ...rest } = data;
4349
- if (typeof customer === "object") {
4350
- throw new core.InvalidTypeError("customer", "string (customer ID)", "object", {
4351
- provider: this.providerName,
4352
- method: "updatePayment"
4353
- });
4482
+ const payment = await this.stripe.paymentIntents.retrieve(id);
4483
+ const paymentOptions = {
4484
+ ...provider_metadata,
4485
+ metadata: core.stringifyMetadataValues({
4486
+ ...rest.metadata,
4487
+ ...provider_metadata?.metadata ?? {}
4488
+ })
4489
+ };
4490
+ if (["requires_payment_method", "requires_confirmation", "requires_action"].includes(
4491
+ payment.status
4492
+ )) {
4493
+ paymentOptions.amount = rest.amount;
4494
+ paymentOptions.currency = rest.currency;
4354
4495
  }
4355
- const payment = await this.stripe.paymentIntents.update(id, { ...rest, ...provider_metadata, customer });
4356
- return paykitPayment$InboundSchema(payment);
4496
+ if (typeof customer === "string") {
4497
+ paymentOptions.customer = customer;
4498
+ } else if (typeof customer == "object" && "email" in customer) {
4499
+ paymentOptions.receipt_email = customer.email;
4500
+ }
4501
+ const updatedPayment = await this.stripe.paymentIntents.update(id, paymentOptions);
4502
+ return paykitPayment$InboundSchema(updatedPayment);
4357
4503
  };
4358
4504
  this.retrievePayment = async (id) => {
4359
4505
  const { error, data } = core.retrievePaymentSchema.safeParse({ id });
4360
4506
  if (error) {
4361
4507
  throw core.ValidationError.fromZodError(error, this.providerName, "retrievePayment");
4362
4508
  }
4363
- const payment = await this.stripe.paymentIntents.retrieve(data.id);
4509
+ const [payment, paymentError] = await core.tryCatchAsync(
4510
+ this.stripe.paymentIntents.retrieve(data.id)
4511
+ );
4512
+ if (!payment || paymentError?.code === "resource_missing")
4513
+ return null;
4364
4514
  return paykitPayment$InboundSchema(payment);
4365
4515
  };
4366
4516
  this.deletePayment = async (id) => {
@@ -4373,13 +4523,17 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4373
4523
  };
4374
4524
  this.capturePayment = async (id, params) => {
4375
4525
  const { error, data } = core.capturePaymentSchema.safeParse(params);
4376
- if (error) throw new core.ValidationError(error.message, { provider: this.providerName, method: "capturePayment" });
4377
- const payment = await this.stripe.paymentIntents.capture(id, { amount_to_capture: data.amount });
4526
+ if (error) {
4527
+ throw core.ValidationError.fromZodError(error, this.providerName, "capturePayment");
4528
+ }
4529
+ const payment = await this.stripe.paymentIntents.capture(id, {
4530
+ amount_to_capture: data.amount
4531
+ });
4378
4532
  return paykitPayment$InboundSchema(payment);
4379
4533
  };
4380
4534
  this.cancelPayment = async (id) => {
4381
- const payment = await this.stripe.paymentIntents.cancel(id);
4382
- return paykitPayment$InboundSchema(payment);
4535
+ const canceledPayment = await this.stripe.paymentIntents.cancel(id);
4536
+ return paykitPayment$InboundSchema(canceledPayment);
4383
4537
  };
4384
4538
  /**
4385
4539
  * Refund management
@@ -4389,8 +4543,19 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4389
4543
  if (error) {
4390
4544
  throw core.ValidationError.fromZodError(error, this.providerName, "createRefund");
4391
4545
  }
4392
- const { provider_metadata, ...rest } = params;
4393
- const refund = await this.stripe.refunds.create({ ...rest, reason: rest.reason, ...provider_metadata });
4546
+ const { provider_metadata, ...rest } = data;
4547
+ const stripeRefundOptions = {
4548
+ ...provider_metadata,
4549
+ reason: paykitRefundReason$OutboundSchema(rest.reason),
4550
+ amount: rest.amount,
4551
+ metadata: core.stringifyMetadataValues(rest.metadata ?? {})
4552
+ };
4553
+ if (data.payment_id.startsWith("pi_")) {
4554
+ stripeRefundOptions.payment_intent = data.payment_id;
4555
+ } else {
4556
+ stripeRefundOptions.charge = data.payment_id;
4557
+ }
4558
+ const refund = await this.stripe.refunds.create(stripeRefundOptions);
4394
4559
  return paykitRefund$InboundSchema(refund);
4395
4560
  };
4396
4561
  /**
@@ -4398,9 +4563,17 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4398
4563
  */
4399
4564
  this.handleWebhook = async (params) => {
4400
4565
  const { body, headers, webhookSecret } = params;
4401
- const stripeHeaders = core.headersExtractor(headers, ["x-stripe-signature"]);
4402
- const signature = stripeHeaders[0].value;
4403
- const event = this.stripe.webhooks.constructEvent(body, signature, webhookSecret);
4566
+ const stripeSignature = headers.get("stripe-signature");
4567
+ if (!stripeSignature) {
4568
+ throw new core.WebhookError("Missing Stripe signature", {
4569
+ provider: this.providerName
4570
+ });
4571
+ }
4572
+ const event = this.stripe.webhooks.constructEvent(
4573
+ body,
4574
+ stripeSignature,
4575
+ webhookSecret
4576
+ );
4404
4577
  const stripePaymentIntentUpdateEventsWithHandlers = [
4405
4578
  "payment_intent.processing",
4406
4579
  "payment_intent.requires_action",
@@ -4435,20 +4608,29 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4435
4608
  paid_at: new Date(event2.created * 1e3).toISOString(),
4436
4609
  amount_paid: data.amount_total ?? 0,
4437
4610
  currency: data.currency ?? "",
4438
- metadata: Object.fromEntries(Object.entries(data.metadata ?? {}).map(([key, value]) => [key, JSON.stringify(value)])),
4611
+ metadata: core.omitInternalMetadata(data.metadata ?? {}),
4439
4612
  customer: typeof data.customer === "string" ? data.customer : data.customer?.id ?? "",
4440
4613
  billing_mode: core.billingModeSchema.parse("one_time"),
4441
4614
  subscription_id: null,
4442
4615
  custom_fields: customFields ?? null,
4443
- line_items: data.line_items?.data.map((item) => ({ id: item.price.id, quantity: item.quantity })) ?? []
4616
+ line_items: data.line_items?.data.map((item) => ({
4617
+ id: item.price.id,
4618
+ quantity: item.quantity
4619
+ })) ?? []
4620
+ };
4621
+ const invoice = {
4622
+ type: "invoice.generated",
4623
+ created: event2.created,
4624
+ id: event2.id,
4625
+ data: invoiceData
4444
4626
  };
4445
- const invoice = { type: "invoice.generated", created: event2.created, id: event2.id, data: invoiceData };
4446
4627
  return [core.paykitEvent$InboundSchema(invoice)];
4447
4628
  },
4448
4629
  "invoice.paid": async (event2) => {
4449
4630
  const data = event2.data.object;
4450
4631
  const relevantBillingReasons = ["subscription_create", "subscription_cycle"];
4451
- if (data.status !== "paid" && !relevantBillingReasons.includes(data.billing_reason)) return null;
4632
+ if (data.status !== "paid" && !relevantBillingReasons.includes(data.billing_reason))
4633
+ return null;
4452
4634
  const invoice = {
4453
4635
  type: "invoice.generated",
4454
4636
  created: event2.created,
@@ -4462,16 +4644,31 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4462
4644
  */
4463
4645
  "customer.created": async (event2) => {
4464
4646
  const data = event2.data.object;
4465
- const customer = { type: "customer.created", created: event2.created, id: event2.id, data: paykitCustomer$InboundSchema(data) };
4647
+ const customer = {
4648
+ type: "customer.created",
4649
+ created: event2.created,
4650
+ id: event2.id,
4651
+ data: paykitCustomer$InboundSchema(data)
4652
+ };
4466
4653
  return [core.paykitEvent$InboundSchema(customer)];
4467
4654
  },
4468
4655
  "customer.updated": async (event2) => {
4469
4656
  const data = event2.data.object;
4470
- const customer = { type: "customer.updated", created: event2.created, id: event2.id, data: paykitCustomer$InboundSchema(data) };
4657
+ const customer = {
4658
+ type: "customer.updated",
4659
+ created: event2.created,
4660
+ id: event2.id,
4661
+ data: paykitCustomer$InboundSchema(data)
4662
+ };
4471
4663
  return [core.paykitEvent$InboundSchema(customer)];
4472
4664
  },
4473
4665
  "customer.deleted": async (event2) => {
4474
- const customer = { type: "customer.deleted", created: event2.created, id: event2.id, data: null };
4666
+ const customer = {
4667
+ type: "customer.deleted",
4668
+ created: event2.created,
4669
+ id: event2.id,
4670
+ data: null
4671
+ };
4475
4672
  return [core.paykitEvent$InboundSchema(customer)];
4476
4673
  },
4477
4674
  /**
@@ -4480,46 +4677,104 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4480
4677
  "customer.subscription.created": async (event2) => {
4481
4678
  const data = event2.data.object;
4482
4679
  const subscription = paykitSubscription$InboundSchema(data);
4483
- return [core.paykitEvent$InboundSchema({ type: "subscription.created", created: event2.created, id: event2.id, data: subscription })];
4680
+ return [
4681
+ core.paykitEvent$InboundSchema({
4682
+ type: "subscription.created",
4683
+ created: event2.created,
4684
+ id: event2.id,
4685
+ data: subscription
4686
+ })
4687
+ ];
4484
4688
  },
4485
4689
  "customer.subscription.updated": async (event2) => {
4486
4690
  const data = event2.data.object;
4487
4691
  const subscription = paykitSubscription$InboundSchema(data);
4488
- return [core.paykitEvent$InboundSchema({ type: "subscription.updated", created: event2.created, id: event2.id, data: subscription })];
4692
+ return [
4693
+ core.paykitEvent$InboundSchema({
4694
+ type: "subscription.updated",
4695
+ created: event2.created,
4696
+ id: event2.id,
4697
+ data: subscription
4698
+ })
4699
+ ];
4489
4700
  },
4490
4701
  "customer.subscription.deleted": async (event2) => {
4491
4702
  const subscription = null;
4492
- return [core.paykitEvent$InboundSchema({ type: "subscription.canceled", created: event2.created, id: event2.id, data: subscription })];
4703
+ return [
4704
+ core.paykitEvent$InboundSchema({
4705
+ type: "subscription.canceled",
4706
+ created: event2.created,
4707
+ id: event2.id,
4708
+ data: subscription
4709
+ })
4710
+ ];
4493
4711
  },
4494
4712
  "payment_intent.created": async (event2) => {
4495
4713
  const data = event2.data.object;
4496
4714
  const payment = paykitPayment$InboundSchema(data);
4497
- return [core.paykitEvent$InboundSchema({ type: "payment.created", created: event2.created, id: event2.id, data: payment })];
4715
+ return [
4716
+ core.paykitEvent$InboundSchema({
4717
+ type: "payment.created",
4718
+ created: event2.created,
4719
+ id: event2.id,
4720
+ data: payment
4721
+ })
4722
+ ];
4498
4723
  },
4499
- ...stripePaymentIntentUpdateEventsWithHandlers.map(() => async (event2) => {
4500
- const data = event2.data.object;
4501
- const payment = paykitPayment$InboundSchema(data);
4502
- return [core.paykitEvent$InboundSchema({ type: "payment.updated", created: event2.created, id: event2.id, data: payment })];
4503
- }),
4724
+ ...stripePaymentIntentUpdateEventsWithHandlers.map(
4725
+ () => async (event2) => {
4726
+ const data = event2.data.object;
4727
+ const payment = paykitPayment$InboundSchema(data);
4728
+ return [
4729
+ core.paykitEvent$InboundSchema({
4730
+ type: "payment.updated",
4731
+ created: event2.created,
4732
+ id: event2.id,
4733
+ data: payment
4734
+ })
4735
+ ];
4736
+ }
4737
+ ),
4504
4738
  "payment_intent.canceled": async (event2) => {
4505
4739
  const data = event2.data.object;
4506
4740
  const payment = paykitPayment$InboundSchema(data);
4507
- return [core.paykitEvent$InboundSchema({ type: "payment.canceled", created: event2.created, id: event2.id, data: payment })];
4741
+ return [
4742
+ core.paykitEvent$InboundSchema({
4743
+ type: "payment.canceled",
4744
+ created: event2.created,
4745
+ id: event2.id,
4746
+ data: payment
4747
+ })
4748
+ ];
4508
4749
  },
4509
4750
  "refund.created": async (event2) => {
4510
4751
  const data = event2.data.object;
4511
4752
  const refund = paykitRefund$InboundSchema(data);
4512
- return [core.paykitEvent$InboundSchema({ type: "refund.created", created: event2.created, id: event2.id, data: refund })];
4753
+ return [
4754
+ core.paykitEvent$InboundSchema({
4755
+ type: "refund.created",
4756
+ created: event2.created,
4757
+ id: event2.id,
4758
+ data: refund
4759
+ })
4760
+ ];
4513
4761
  }
4514
4762
  };
4515
4763
  const handler = webhookHandlers[event.type];
4516
- if (!handler) throw new core.WebhookError(`Unhandled event type: ${event.type}`, { provider: this.providerName });
4764
+ if (!handler)
4765
+ throw new core.WebhookError(`Unhandled event type: ${event.type}`, {
4766
+ provider: this.providerName
4767
+ });
4517
4768
  const result = await handler(event);
4518
- if (!result) throw new core.WebhookError(`Unhandled event type: ${event.type}`, { provider: this.providerName });
4769
+ if (!result)
4770
+ throw new core.WebhookError(`Unhandled event type: ${event.type}`, {
4771
+ provider: this.providerName
4772
+ });
4519
4773
  return result;
4520
4774
  };
4521
- const { debug, apiKey, ...rest } = config;
4775
+ const { debug = true, apiKey, ...rest } = opts;
4522
4776
  this.stripe = new Stripe__default.default(apiKey, rest);
4777
+ this.opts = opts;
4523
4778
  }
4524
4779
  };
4525
4780