@paykit-sdk/stripe 1.1.100 → 1.1.102

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.
package/dist/index.js CHANGED
@@ -4406,7 +4406,9 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4406
4406
  };
4407
4407
  /**
4408
4408
  * Payment management
4409
+ * Create a payment intent or checkout session for a payment
4409
4410
  */
4411
+ // In packages/stripe/src/stripe-provider.ts
4410
4412
  this.createPayment = async (params) => {
4411
4413
  const { error, data } = core.createPaymentSchema.safeParse(params);
4412
4414
  if (error)
@@ -4415,22 +4417,24 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4415
4417
  method: "createPayment"
4416
4418
  });
4417
4419
  const { provider_metadata, customer, capture_method, ...rest } = data;
4418
- if (typeof customer === "object" && "email" in customer) {
4420
+ const createCheckoutSession = async (customerEmail, customerId2) => {
4419
4421
  const checkoutMetadata = core.stringifyMetadataValues({
4420
4422
  ...rest.metadata,
4421
4423
  ...provider_metadata?.metadata ?? {},
4422
4424
  [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4423
4425
  });
4424
- const checkoutSession = await this.stripe.checkout.sessions.create({
4426
+ const { success_url } = core.validateRequiredKeys(
4427
+ ["success_url"],
4428
+ provider_metadata,
4429
+ "The following fields must be present in the provider_metadata of createPayment: {keys}"
4430
+ );
4431
+ const checkoutOptions = {
4425
4432
  mode: "payment",
4426
- customer_email: customer.email,
4427
4433
  line_items: [
4428
4434
  {
4429
4435
  price_data: {
4430
4436
  currency: rest.currency,
4431
- product_data: {
4432
- name: `Payment for ${data.item_id || "item"}`
4433
- },
4437
+ product_data: { name: `Payment for ${data.item_id || "item"}` },
4434
4438
  unit_amount: rest.amount
4435
4439
  },
4436
4440
  quantity: 1
@@ -4440,27 +4444,46 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4440
4444
  payment_intent_data: {
4441
4445
  capture_method,
4442
4446
  setup_future_usage: "off_session"
4443
- }
4444
- });
4447
+ },
4448
+ success_url
4449
+ };
4450
+ if (customerId2) checkoutOptions.customer = customerId2;
4451
+ else if (customerEmail) checkoutOptions.customer_email = customerEmail;
4452
+ const checkoutSession = await this.stripe.checkout.sessions.create(checkoutOptions);
4445
4453
  return {
4446
4454
  id: checkoutSession.id,
4447
4455
  amount: rest.amount,
4448
4456
  currency: rest.currency,
4449
- customer: customer.email,
4450
- status: "requires_action",
4457
+ customer: customerId2 ? customerId2 : customerEmail ? { email: customerEmail } : "",
4458
+ status: "pending",
4451
4459
  metadata: core.omitInternalMetadata(checkoutMetadata),
4452
4460
  item_id: data.item_id ?? null,
4453
4461
  requires_action: true,
4454
4462
  payment_url: checkoutSession.url
4455
4463
  };
4456
- }
4457
- const paymentMetadata = core.stringifyMetadataValues({
4458
- ...rest.metadata,
4459
- ...provider_metadata?.metadata ?? {},
4460
- [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4461
- });
4464
+ };
4465
+ let customerId;
4466
+ if (typeof customer === "object" && "email" in customer) {
4467
+ const existingCustomers = await this.stripe.customers.list({
4468
+ email: customer.email,
4469
+ limit: 1
4470
+ });
4471
+ if (existingCustomers.data.length > 0) customerId = existingCustomers.data[0].id;
4472
+ else return createCheckoutSession(customer.email);
4473
+ } else if (typeof customer === "string") {
4474
+ customerId = customer;
4475
+ } else
4476
+ throw new core.InvalidTypeError(
4477
+ "customer",
4478
+ "string (customer ID) or object with email",
4479
+ typeof customer,
4480
+ {
4481
+ provider: this.providerName,
4482
+ method: "createPayment"
4483
+ }
4484
+ );
4462
4485
  const customerWithDefaultPaymentMethod = await this.stripe.customers.retrieve(
4463
- customer,
4486
+ customerId,
4464
4487
  { expand: ["invoice_settings.default_payment_method"] }
4465
4488
  );
4466
4489
  if ("deleted" in customerWithDefaultPaymentMethod) {
@@ -4471,26 +4494,28 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4471
4494
  }
4472
4495
  let defaultPaymentMethod = customerWithDefaultPaymentMethod.invoice_settings?.default_payment_method;
4473
4496
  if (!defaultPaymentMethod) {
4474
- const paymentMethods = await this.stripe.paymentMethods.list({ customer });
4497
+ const paymentMethods = await this.stripe.paymentMethods.list({
4498
+ customer: customerId
4499
+ });
4475
4500
  if (paymentMethods.data.length === 0) {
4476
- throw new core.ValidationError(
4477
- `Customer ${customer} has no payment methods. Add a payment method for the customer before creating a payment intent`,
4478
- { provider: this.providerName, method: "createPayment" }
4479
- );
4501
+ return createCheckoutSession(void 0, customerId);
4480
4502
  }
4481
4503
  defaultPaymentMethod = paymentMethods.data[0].id;
4482
4504
  }
4505
+ const paymentMetadata = core.stringifyMetadataValues({
4506
+ ...rest.metadata,
4507
+ ...provider_metadata?.metadata ?? {},
4508
+ [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4509
+ });
4483
4510
  const paymentIntentOptions = {
4484
4511
  currency: rest.currency,
4485
4512
  amount: rest.amount,
4486
4513
  metadata: paymentMetadata,
4487
- customer,
4514
+ customer: customerId,
4488
4515
  capture_method,
4489
4516
  confirm: true,
4490
- // automatically confirms the payment
4491
4517
  payment_method: defaultPaymentMethod,
4492
4518
  off_session: true
4493
- // uses customer's default payment method, avoids 3Ds/authentication
4494
4519
  };
4495
4520
  if (data.billing) {
4496
4521
  paymentIntentOptions.shipping = {
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { schema, validateRequiredKeys, AbstractPayKitProvider, createCheckoutSchema, ValidationError, stringifyMetadataValues, InvalidTypeError, updateCheckoutSchema, ResourceNotFoundError, createCustomerSchema, createSubscriptionSchema, updateSubscriptionSchema, retrieveSubscriptionSchema, createPaymentSchema, PAYKIT_METADATA_KEY, omitInternalMetadata, updatePaymentSchema, retrievePaymentSchema, tryCatchAsync, deletePaymentSchema, capturePaymentSchema, createRefundSchema, WebhookError, paykitEvent$InboundSchema, billingModeSchema, invoiceStatusSchema } from '@paykit-sdk/core';
1
+ import { schema, validateRequiredKeys, AbstractPayKitProvider, createCheckoutSchema, ValidationError, stringifyMetadataValues, InvalidTypeError, updateCheckoutSchema, ResourceNotFoundError, createCustomerSchema, createSubscriptionSchema, updateSubscriptionSchema, retrieveSubscriptionSchema, createPaymentSchema, PAYKIT_METADATA_KEY, updatePaymentSchema, retrievePaymentSchema, tryCatchAsync, deletePaymentSchema, capturePaymentSchema, createRefundSchema, WebhookError, omitInternalMetadata, paykitEvent$InboundSchema, billingModeSchema, invoiceStatusSchema } from '@paykit-sdk/core';
2
2
  import Stripe from 'stripe';
3
3
 
4
4
  var __defProp = Object.defineProperty;
@@ -4400,7 +4400,9 @@ var StripeProvider = class extends AbstractPayKitProvider {
4400
4400
  };
4401
4401
  /**
4402
4402
  * Payment management
4403
+ * Create a payment intent or checkout session for a payment
4403
4404
  */
4405
+ // In packages/stripe/src/stripe-provider.ts
4404
4406
  this.createPayment = async (params) => {
4405
4407
  const { error, data } = createPaymentSchema.safeParse(params);
4406
4408
  if (error)
@@ -4409,22 +4411,24 @@ var StripeProvider = class extends AbstractPayKitProvider {
4409
4411
  method: "createPayment"
4410
4412
  });
4411
4413
  const { provider_metadata, customer, capture_method, ...rest } = data;
4412
- if (typeof customer === "object" && "email" in customer) {
4414
+ const createCheckoutSession = async (customerEmail, customerId2) => {
4413
4415
  const checkoutMetadata = stringifyMetadataValues({
4414
4416
  ...rest.metadata,
4415
4417
  ...provider_metadata?.metadata ?? {},
4416
4418
  [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4417
4419
  });
4418
- const checkoutSession = await this.stripe.checkout.sessions.create({
4420
+ const { success_url } = validateRequiredKeys(
4421
+ ["success_url"],
4422
+ provider_metadata,
4423
+ "The following fields must be present in the provider_metadata of createPayment: {keys}"
4424
+ );
4425
+ const checkoutOptions = {
4419
4426
  mode: "payment",
4420
- customer_email: customer.email,
4421
4427
  line_items: [
4422
4428
  {
4423
4429
  price_data: {
4424
4430
  currency: rest.currency,
4425
- product_data: {
4426
- name: `Payment for ${data.item_id || "item"}`
4427
- },
4431
+ product_data: { name: `Payment for ${data.item_id || "item"}` },
4428
4432
  unit_amount: rest.amount
4429
4433
  },
4430
4434
  quantity: 1
@@ -4434,27 +4438,46 @@ var StripeProvider = class extends AbstractPayKitProvider {
4434
4438
  payment_intent_data: {
4435
4439
  capture_method,
4436
4440
  setup_future_usage: "off_session"
4437
- }
4438
- });
4441
+ },
4442
+ success_url
4443
+ };
4444
+ if (customerId2) checkoutOptions.customer = customerId2;
4445
+ else if (customerEmail) checkoutOptions.customer_email = customerEmail;
4446
+ const checkoutSession = await this.stripe.checkout.sessions.create(checkoutOptions);
4439
4447
  return {
4440
4448
  id: checkoutSession.id,
4441
4449
  amount: rest.amount,
4442
4450
  currency: rest.currency,
4443
- customer: customer.email,
4444
- status: "requires_action",
4451
+ customer: customerId2 ? customerId2 : customerEmail ? { email: customerEmail } : "",
4452
+ status: "pending",
4445
4453
  metadata: omitInternalMetadata(checkoutMetadata),
4446
4454
  item_id: data.item_id ?? null,
4447
4455
  requires_action: true,
4448
4456
  payment_url: checkoutSession.url
4449
4457
  };
4450
- }
4451
- const paymentMetadata = stringifyMetadataValues({
4452
- ...rest.metadata,
4453
- ...provider_metadata?.metadata ?? {},
4454
- [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4455
- });
4458
+ };
4459
+ let customerId;
4460
+ if (typeof customer === "object" && "email" in customer) {
4461
+ const existingCustomers = await this.stripe.customers.list({
4462
+ email: customer.email,
4463
+ limit: 1
4464
+ });
4465
+ if (existingCustomers.data.length > 0) customerId = existingCustomers.data[0].id;
4466
+ else return createCheckoutSession(customer.email);
4467
+ } else if (typeof customer === "string") {
4468
+ customerId = customer;
4469
+ } else
4470
+ throw new InvalidTypeError(
4471
+ "customer",
4472
+ "string (customer ID) or object with email",
4473
+ typeof customer,
4474
+ {
4475
+ provider: this.providerName,
4476
+ method: "createPayment"
4477
+ }
4478
+ );
4456
4479
  const customerWithDefaultPaymentMethod = await this.stripe.customers.retrieve(
4457
- customer,
4480
+ customerId,
4458
4481
  { expand: ["invoice_settings.default_payment_method"] }
4459
4482
  );
4460
4483
  if ("deleted" in customerWithDefaultPaymentMethod) {
@@ -4465,26 +4488,28 @@ var StripeProvider = class extends AbstractPayKitProvider {
4465
4488
  }
4466
4489
  let defaultPaymentMethod = customerWithDefaultPaymentMethod.invoice_settings?.default_payment_method;
4467
4490
  if (!defaultPaymentMethod) {
4468
- const paymentMethods = await this.stripe.paymentMethods.list({ customer });
4491
+ const paymentMethods = await this.stripe.paymentMethods.list({
4492
+ customer: customerId
4493
+ });
4469
4494
  if (paymentMethods.data.length === 0) {
4470
- throw new ValidationError(
4471
- `Customer ${customer} has no payment methods. Add a payment method for the customer before creating a payment intent`,
4472
- { provider: this.providerName, method: "createPayment" }
4473
- );
4495
+ return createCheckoutSession(void 0, customerId);
4474
4496
  }
4475
4497
  defaultPaymentMethod = paymentMethods.data[0].id;
4476
4498
  }
4499
+ const paymentMetadata = stringifyMetadataValues({
4500
+ ...rest.metadata,
4501
+ ...provider_metadata?.metadata ?? {},
4502
+ [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4503
+ });
4477
4504
  const paymentIntentOptions = {
4478
4505
  currency: rest.currency,
4479
4506
  amount: rest.amount,
4480
4507
  metadata: paymentMetadata,
4481
- customer,
4508
+ customer: customerId,
4482
4509
  capture_method,
4483
4510
  confirm: true,
4484
- // automatically confirms the payment
4485
4511
  payment_method: defaultPaymentMethod,
4486
4512
  off_session: true
4487
- // uses customer's default payment method, avoids 3Ds/authentication
4488
4513
  };
4489
4514
  if (data.billing) {
4490
4515
  paymentIntentOptions.shipping = {
@@ -33,6 +33,7 @@ declare class StripeProvider extends AbstractPayKitProvider implements PayKitPro
33
33
  deleteSubscription: (id: string) => Promise<null>;
34
34
  /**
35
35
  * Payment management
36
+ * Create a payment intent or checkout session for a payment
36
37
  */
37
38
  createPayment: (params: CreatePaymentSchema) => Promise<Payment>;
38
39
  updatePayment: (id: string, params: UpdatePaymentSchema) => Promise<Payment>;
@@ -33,6 +33,7 @@ declare class StripeProvider extends AbstractPayKitProvider implements PayKitPro
33
33
  deleteSubscription: (id: string) => Promise<null>;
34
34
  /**
35
35
  * Payment management
36
+ * Create a payment intent or checkout session for a payment
36
37
  */
37
38
  createPayment: (params: CreatePaymentSchema) => Promise<Payment>;
38
39
  updatePayment: (id: string, params: UpdatePaymentSchema) => Promise<Payment>;
@@ -4406,7 +4406,9 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4406
4406
  };
4407
4407
  /**
4408
4408
  * Payment management
4409
+ * Create a payment intent or checkout session for a payment
4409
4410
  */
4411
+ // In packages/stripe/src/stripe-provider.ts
4410
4412
  this.createPayment = async (params) => {
4411
4413
  const { error, data } = core.createPaymentSchema.safeParse(params);
4412
4414
  if (error)
@@ -4415,22 +4417,24 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4415
4417
  method: "createPayment"
4416
4418
  });
4417
4419
  const { provider_metadata, customer, capture_method, ...rest } = data;
4418
- if (typeof customer === "object" && "email" in customer) {
4420
+ const createCheckoutSession = async (customerEmail, customerId2) => {
4419
4421
  const checkoutMetadata = core.stringifyMetadataValues({
4420
4422
  ...rest.metadata,
4421
4423
  ...provider_metadata?.metadata ?? {},
4422
4424
  [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4423
4425
  });
4424
- const checkoutSession = await this.stripe.checkout.sessions.create({
4426
+ const { success_url } = core.validateRequiredKeys(
4427
+ ["success_url"],
4428
+ provider_metadata,
4429
+ "The following fields must be present in the provider_metadata of createPayment: {keys}"
4430
+ );
4431
+ const checkoutOptions = {
4425
4432
  mode: "payment",
4426
- customer_email: customer.email,
4427
4433
  line_items: [
4428
4434
  {
4429
4435
  price_data: {
4430
4436
  currency: rest.currency,
4431
- product_data: {
4432
- name: `Payment for ${data.item_id || "item"}`
4433
- },
4437
+ product_data: { name: `Payment for ${data.item_id || "item"}` },
4434
4438
  unit_amount: rest.amount
4435
4439
  },
4436
4440
  quantity: 1
@@ -4440,27 +4444,46 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4440
4444
  payment_intent_data: {
4441
4445
  capture_method,
4442
4446
  setup_future_usage: "off_session"
4443
- }
4444
- });
4447
+ },
4448
+ success_url
4449
+ };
4450
+ if (customerId2) checkoutOptions.customer = customerId2;
4451
+ else if (customerEmail) checkoutOptions.customer_email = customerEmail;
4452
+ const checkoutSession = await this.stripe.checkout.sessions.create(checkoutOptions);
4445
4453
  return {
4446
4454
  id: checkoutSession.id,
4447
4455
  amount: rest.amount,
4448
4456
  currency: rest.currency,
4449
- customer: customer.email,
4450
- status: "requires_action",
4457
+ customer: customerId2 ? customerId2 : customerEmail ? { email: customerEmail } : "",
4458
+ status: "pending",
4451
4459
  metadata: core.omitInternalMetadata(checkoutMetadata),
4452
4460
  item_id: data.item_id ?? null,
4453
4461
  requires_action: true,
4454
4462
  payment_url: checkoutSession.url
4455
4463
  };
4456
- }
4457
- const paymentMetadata = core.stringifyMetadataValues({
4458
- ...rest.metadata,
4459
- ...provider_metadata?.metadata ?? {},
4460
- [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4461
- });
4464
+ };
4465
+ let customerId;
4466
+ if (typeof customer === "object" && "email" in customer) {
4467
+ const existingCustomers = await this.stripe.customers.list({
4468
+ email: customer.email,
4469
+ limit: 1
4470
+ });
4471
+ if (existingCustomers.data.length > 0) customerId = existingCustomers.data[0].id;
4472
+ else return createCheckoutSession(customer.email);
4473
+ } else if (typeof customer === "string") {
4474
+ customerId = customer;
4475
+ } else
4476
+ throw new core.InvalidTypeError(
4477
+ "customer",
4478
+ "string (customer ID) or object with email",
4479
+ typeof customer,
4480
+ {
4481
+ provider: this.providerName,
4482
+ method: "createPayment"
4483
+ }
4484
+ );
4462
4485
  const customerWithDefaultPaymentMethod = await this.stripe.customers.retrieve(
4463
- customer,
4486
+ customerId,
4464
4487
  { expand: ["invoice_settings.default_payment_method"] }
4465
4488
  );
4466
4489
  if ("deleted" in customerWithDefaultPaymentMethod) {
@@ -4471,26 +4494,28 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4471
4494
  }
4472
4495
  let defaultPaymentMethod = customerWithDefaultPaymentMethod.invoice_settings?.default_payment_method;
4473
4496
  if (!defaultPaymentMethod) {
4474
- const paymentMethods = await this.stripe.paymentMethods.list({ customer });
4497
+ const paymentMethods = await this.stripe.paymentMethods.list({
4498
+ customer: customerId
4499
+ });
4475
4500
  if (paymentMethods.data.length === 0) {
4476
- throw new core.ValidationError(
4477
- `Customer ${customer} has no payment methods. Add a payment method for the customer before creating a payment intent`,
4478
- { provider: this.providerName, method: "createPayment" }
4479
- );
4501
+ return createCheckoutSession(void 0, customerId);
4480
4502
  }
4481
4503
  defaultPaymentMethod = paymentMethods.data[0].id;
4482
4504
  }
4505
+ const paymentMetadata = core.stringifyMetadataValues({
4506
+ ...rest.metadata,
4507
+ ...provider_metadata?.metadata ?? {},
4508
+ [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4509
+ });
4483
4510
  const paymentIntentOptions = {
4484
4511
  currency: rest.currency,
4485
4512
  amount: rest.amount,
4486
4513
  metadata: paymentMetadata,
4487
- customer,
4514
+ customer: customerId,
4488
4515
  capture_method,
4489
4516
  confirm: true,
4490
- // automatically confirms the payment
4491
4517
  payment_method: defaultPaymentMethod,
4492
4518
  off_session: true
4493
- // uses customer's default payment method, avoids 3Ds/authentication
4494
4519
  };
4495
4520
  if (data.billing) {
4496
4521
  paymentIntentOptions.shipping = {
@@ -1,4 +1,4 @@
1
- import { schema, AbstractPayKitProvider, createCheckoutSchema, ValidationError, stringifyMetadataValues, InvalidTypeError, updateCheckoutSchema, ResourceNotFoundError, createCustomerSchema, createSubscriptionSchema, updateSubscriptionSchema, retrieveSubscriptionSchema, createPaymentSchema, PAYKIT_METADATA_KEY, omitInternalMetadata, updatePaymentSchema, retrievePaymentSchema, tryCatchAsync, deletePaymentSchema, capturePaymentSchema, createRefundSchema, WebhookError, paykitEvent$InboundSchema, billingModeSchema, invoiceStatusSchema } from '@paykit-sdk/core';
1
+ import { schema, AbstractPayKitProvider, createCheckoutSchema, ValidationError, stringifyMetadataValues, InvalidTypeError, updateCheckoutSchema, ResourceNotFoundError, createCustomerSchema, createSubscriptionSchema, updateSubscriptionSchema, retrieveSubscriptionSchema, createPaymentSchema, PAYKIT_METADATA_KEY, updatePaymentSchema, retrievePaymentSchema, tryCatchAsync, deletePaymentSchema, capturePaymentSchema, createRefundSchema, WebhookError, validateRequiredKeys, omitInternalMetadata, paykitEvent$InboundSchema, billingModeSchema, invoiceStatusSchema } from '@paykit-sdk/core';
2
2
  import Stripe from 'stripe';
3
3
 
4
4
  var __defProp = Object.defineProperty;
@@ -4400,7 +4400,9 @@ var StripeProvider = class extends AbstractPayKitProvider {
4400
4400
  };
4401
4401
  /**
4402
4402
  * Payment management
4403
+ * Create a payment intent or checkout session for a payment
4403
4404
  */
4405
+ // In packages/stripe/src/stripe-provider.ts
4404
4406
  this.createPayment = async (params) => {
4405
4407
  const { error, data } = createPaymentSchema.safeParse(params);
4406
4408
  if (error)
@@ -4409,22 +4411,24 @@ var StripeProvider = class extends AbstractPayKitProvider {
4409
4411
  method: "createPayment"
4410
4412
  });
4411
4413
  const { provider_metadata, customer, capture_method, ...rest } = data;
4412
- if (typeof customer === "object" && "email" in customer) {
4414
+ const createCheckoutSession = async (customerEmail, customerId2) => {
4413
4415
  const checkoutMetadata = stringifyMetadataValues({
4414
4416
  ...rest.metadata,
4415
4417
  ...provider_metadata?.metadata ?? {},
4416
4418
  [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4417
4419
  });
4418
- const checkoutSession = await this.stripe.checkout.sessions.create({
4420
+ const { success_url } = validateRequiredKeys(
4421
+ ["success_url"],
4422
+ provider_metadata,
4423
+ "The following fields must be present in the provider_metadata of createPayment: {keys}"
4424
+ );
4425
+ const checkoutOptions = {
4419
4426
  mode: "payment",
4420
- customer_email: customer.email,
4421
4427
  line_items: [
4422
4428
  {
4423
4429
  price_data: {
4424
4430
  currency: rest.currency,
4425
- product_data: {
4426
- name: `Payment for ${data.item_id || "item"}`
4427
- },
4431
+ product_data: { name: `Payment for ${data.item_id || "item"}` },
4428
4432
  unit_amount: rest.amount
4429
4433
  },
4430
4434
  quantity: 1
@@ -4434,27 +4438,46 @@ var StripeProvider = class extends AbstractPayKitProvider {
4434
4438
  payment_intent_data: {
4435
4439
  capture_method,
4436
4440
  setup_future_usage: "off_session"
4437
- }
4438
- });
4441
+ },
4442
+ success_url
4443
+ };
4444
+ if (customerId2) checkoutOptions.customer = customerId2;
4445
+ else if (customerEmail) checkoutOptions.customer_email = customerEmail;
4446
+ const checkoutSession = await this.stripe.checkout.sessions.create(checkoutOptions);
4439
4447
  return {
4440
4448
  id: checkoutSession.id,
4441
4449
  amount: rest.amount,
4442
4450
  currency: rest.currency,
4443
- customer: customer.email,
4444
- status: "requires_action",
4451
+ customer: customerId2 ? customerId2 : customerEmail ? { email: customerEmail } : "",
4452
+ status: "pending",
4445
4453
  metadata: omitInternalMetadata(checkoutMetadata),
4446
4454
  item_id: data.item_id ?? null,
4447
4455
  requires_action: true,
4448
4456
  payment_url: checkoutSession.url
4449
4457
  };
4450
- }
4451
- const paymentMetadata = stringifyMetadataValues({
4452
- ...rest.metadata,
4453
- ...provider_metadata?.metadata ?? {},
4454
- [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4455
- });
4458
+ };
4459
+ let customerId;
4460
+ if (typeof customer === "object" && "email" in customer) {
4461
+ const existingCustomers = await this.stripe.customers.list({
4462
+ email: customer.email,
4463
+ limit: 1
4464
+ });
4465
+ if (existingCustomers.data.length > 0) customerId = existingCustomers.data[0].id;
4466
+ else return createCheckoutSession(customer.email);
4467
+ } else if (typeof customer === "string") {
4468
+ customerId = customer;
4469
+ } else
4470
+ throw new InvalidTypeError(
4471
+ "customer",
4472
+ "string (customer ID) or object with email",
4473
+ typeof customer,
4474
+ {
4475
+ provider: this.providerName,
4476
+ method: "createPayment"
4477
+ }
4478
+ );
4456
4479
  const customerWithDefaultPaymentMethod = await this.stripe.customers.retrieve(
4457
- customer,
4480
+ customerId,
4458
4481
  { expand: ["invoice_settings.default_payment_method"] }
4459
4482
  );
4460
4483
  if ("deleted" in customerWithDefaultPaymentMethod) {
@@ -4465,26 +4488,28 @@ var StripeProvider = class extends AbstractPayKitProvider {
4465
4488
  }
4466
4489
  let defaultPaymentMethod = customerWithDefaultPaymentMethod.invoice_settings?.default_payment_method;
4467
4490
  if (!defaultPaymentMethod) {
4468
- const paymentMethods = await this.stripe.paymentMethods.list({ customer });
4491
+ const paymentMethods = await this.stripe.paymentMethods.list({
4492
+ customer: customerId
4493
+ });
4469
4494
  if (paymentMethods.data.length === 0) {
4470
- throw new ValidationError(
4471
- `Customer ${customer} has no payment methods. Add a payment method for the customer before creating a payment intent`,
4472
- { provider: this.providerName, method: "createPayment" }
4473
- );
4495
+ return createCheckoutSession(void 0, customerId);
4474
4496
  }
4475
4497
  defaultPaymentMethod = paymentMethods.data[0].id;
4476
4498
  }
4499
+ const paymentMetadata = stringifyMetadataValues({
4500
+ ...rest.metadata,
4501
+ ...provider_metadata?.metadata ?? {},
4502
+ [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4503
+ });
4477
4504
  const paymentIntentOptions = {
4478
4505
  currency: rest.currency,
4479
4506
  amount: rest.amount,
4480
4507
  metadata: paymentMetadata,
4481
- customer,
4508
+ customer: customerId,
4482
4509
  capture_method,
4483
4510
  confirm: true,
4484
- // automatically confirms the payment
4485
4511
  payment_method: defaultPaymentMethod,
4486
4512
  off_session: true
4487
- // uses customer's default payment method, avoids 3Ds/authentication
4488
4513
  };
4489
4514
  if (data.billing) {
4490
4515
  paymentIntentOptions.shipping = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paykit-sdk/stripe",
3
- "version": "1.1.100",
3
+ "version": "1.1.102",
4
4
  "description": "Stripe provider for PayKit",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",