@paykit-sdk/stripe 1.1.101 → 1.1.103

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,19 +4417,73 @@ 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") {
4419
- throw new core.InvalidTypeError("customer", "string (customer ID)", "object", {
4420
- provider: this.providerName,
4421
- method: "createPayment"
4420
+ const createCheckoutSession = async (customerEmail, customerId2) => {
4421
+ const checkoutMetadata = core.stringifyMetadataValues({
4422
+ ...rest.metadata,
4423
+ ...provider_metadata?.metadata ?? {},
4424
+ [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4422
4425
  });
4423
- }
4424
- const paymentMetadata = core.stringifyMetadataValues({
4425
- ...rest.metadata,
4426
- ...provider_metadata?.metadata ?? {},
4427
- [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4428
- });
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 = {
4432
+ mode: "payment",
4433
+ line_items: [
4434
+ {
4435
+ price_data: {
4436
+ currency: rest.currency,
4437
+ product_data: { name: `Payment for ${data.item_id || "item"}` },
4438
+ unit_amount: rest.amount
4439
+ },
4440
+ quantity: 1
4441
+ }
4442
+ ],
4443
+ metadata: checkoutMetadata,
4444
+ payment_intent_data: {
4445
+ capture_method,
4446
+ setup_future_usage: "off_session"
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);
4453
+ return {
4454
+ id: checkoutSession.id,
4455
+ amount: rest.amount,
4456
+ currency: rest.currency,
4457
+ customer: customerId2 ? customerId2 : customerEmail ? { email: customerEmail } : "",
4458
+ status: "pending",
4459
+ metadata: core.omitInternalMetadata(checkoutMetadata),
4460
+ item_id: data.item_id ?? null,
4461
+ requires_action: true,
4462
+ payment_url: checkoutSession.url
4463
+ };
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
+ );
4429
4485
  const customerWithDefaultPaymentMethod = await this.stripe.customers.retrieve(
4430
- customer,
4486
+ customerId,
4431
4487
  { expand: ["invoice_settings.default_payment_method"] }
4432
4488
  );
4433
4489
  if ("deleted" in customerWithDefaultPaymentMethod) {
@@ -4438,26 +4494,28 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4438
4494
  }
4439
4495
  let defaultPaymentMethod = customerWithDefaultPaymentMethod.invoice_settings?.default_payment_method;
4440
4496
  if (!defaultPaymentMethod) {
4441
- const paymentMethods = await this.stripe.paymentMethods.list({ customer });
4497
+ const paymentMethods = await this.stripe.paymentMethods.list({
4498
+ customer: customerId
4499
+ });
4442
4500
  if (paymentMethods.data.length === 0) {
4443
- throw new core.ValidationError(
4444
- `Customer ${customer} has no payment methods. Add a payment method for the customer before creating a payment intent`,
4445
- { provider: this.providerName, method: "createPayment" }
4446
- );
4501
+ return createCheckoutSession(void 0, customerId);
4447
4502
  }
4448
4503
  defaultPaymentMethod = paymentMethods.data[0].id;
4449
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
+ });
4450
4510
  const paymentIntentOptions = {
4451
4511
  currency: rest.currency,
4452
4512
  amount: rest.amount,
4453
4513
  metadata: paymentMetadata,
4454
- customer,
4514
+ customer: customerId,
4455
4515
  capture_method,
4456
4516
  confirm: true,
4457
- // automatically confirms the payment
4458
4517
  payment_method: defaultPaymentMethod,
4459
4518
  off_session: true
4460
- // uses customer's default payment method, avoids 3Ds/authentication
4461
4519
  };
4462
4520
  if (data.billing) {
4463
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, updatePaymentSchema, retrievePaymentSchema, tryCatchAsync, deletePaymentSchema, capturePaymentSchema, createRefundSchema, WebhookError, paykitEvent$InboundSchema, billingModeSchema, omitInternalMetadata, 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,19 +4411,73 @@ 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") {
4413
- throw new InvalidTypeError("customer", "string (customer ID)", "object", {
4414
- provider: this.providerName,
4415
- method: "createPayment"
4414
+ const createCheckoutSession = async (customerEmail, customerId2) => {
4415
+ const checkoutMetadata = stringifyMetadataValues({
4416
+ ...rest.metadata,
4417
+ ...provider_metadata?.metadata ?? {},
4418
+ [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4416
4419
  });
4417
- }
4418
- const paymentMetadata = stringifyMetadataValues({
4419
- ...rest.metadata,
4420
- ...provider_metadata?.metadata ?? {},
4421
- [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4422
- });
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 = {
4426
+ mode: "payment",
4427
+ line_items: [
4428
+ {
4429
+ price_data: {
4430
+ currency: rest.currency,
4431
+ product_data: { name: `Payment for ${data.item_id || "item"}` },
4432
+ unit_amount: rest.amount
4433
+ },
4434
+ quantity: 1
4435
+ }
4436
+ ],
4437
+ metadata: checkoutMetadata,
4438
+ payment_intent_data: {
4439
+ capture_method,
4440
+ setup_future_usage: "off_session"
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);
4447
+ return {
4448
+ id: checkoutSession.id,
4449
+ amount: rest.amount,
4450
+ currency: rest.currency,
4451
+ customer: customerId2 ? customerId2 : customerEmail ? { email: customerEmail } : "",
4452
+ status: "pending",
4453
+ metadata: omitInternalMetadata(checkoutMetadata),
4454
+ item_id: data.item_id ?? null,
4455
+ requires_action: true,
4456
+ payment_url: checkoutSession.url
4457
+ };
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
+ );
4423
4479
  const customerWithDefaultPaymentMethod = await this.stripe.customers.retrieve(
4424
- customer,
4480
+ customerId,
4425
4481
  { expand: ["invoice_settings.default_payment_method"] }
4426
4482
  );
4427
4483
  if ("deleted" in customerWithDefaultPaymentMethod) {
@@ -4432,26 +4488,28 @@ var StripeProvider = class extends AbstractPayKitProvider {
4432
4488
  }
4433
4489
  let defaultPaymentMethod = customerWithDefaultPaymentMethod.invoice_settings?.default_payment_method;
4434
4490
  if (!defaultPaymentMethod) {
4435
- const paymentMethods = await this.stripe.paymentMethods.list({ customer });
4491
+ const paymentMethods = await this.stripe.paymentMethods.list({
4492
+ customer: customerId
4493
+ });
4436
4494
  if (paymentMethods.data.length === 0) {
4437
- throw new ValidationError(
4438
- `Customer ${customer} has no payment methods. Add a payment method for the customer before creating a payment intent`,
4439
- { provider: this.providerName, method: "createPayment" }
4440
- );
4495
+ return createCheckoutSession(void 0, customerId);
4441
4496
  }
4442
4497
  defaultPaymentMethod = paymentMethods.data[0].id;
4443
4498
  }
4499
+ const paymentMetadata = stringifyMetadataValues({
4500
+ ...rest.metadata,
4501
+ ...provider_metadata?.metadata ?? {},
4502
+ [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4503
+ });
4444
4504
  const paymentIntentOptions = {
4445
4505
  currency: rest.currency,
4446
4506
  amount: rest.amount,
4447
4507
  metadata: paymentMetadata,
4448
- customer,
4508
+ customer: customerId,
4449
4509
  capture_method,
4450
4510
  confirm: true,
4451
- // automatically confirms the payment
4452
4511
  payment_method: defaultPaymentMethod,
4453
4512
  off_session: true
4454
- // uses customer's default payment method, avoids 3Ds/authentication
4455
4513
  };
4456
4514
  if (data.billing) {
4457
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,19 +4417,73 @@ 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") {
4419
- throw new core.InvalidTypeError("customer", "string (customer ID)", "object", {
4420
- provider: this.providerName,
4421
- method: "createPayment"
4420
+ const createCheckoutSession = async (customerEmail, customerId2) => {
4421
+ const checkoutMetadata = core.stringifyMetadataValues({
4422
+ ...rest.metadata,
4423
+ ...provider_metadata?.metadata ?? {},
4424
+ [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4422
4425
  });
4423
- }
4424
- const paymentMetadata = core.stringifyMetadataValues({
4425
- ...rest.metadata,
4426
- ...provider_metadata?.metadata ?? {},
4427
- [core.PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4428
- });
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 = {
4432
+ mode: "payment",
4433
+ line_items: [
4434
+ {
4435
+ price_data: {
4436
+ currency: rest.currency,
4437
+ product_data: { name: `Payment for ${data.item_id || "item"}` },
4438
+ unit_amount: rest.amount
4439
+ },
4440
+ quantity: 1
4441
+ }
4442
+ ],
4443
+ metadata: checkoutMetadata,
4444
+ payment_intent_data: {
4445
+ capture_method,
4446
+ setup_future_usage: "off_session"
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);
4453
+ return {
4454
+ id: checkoutSession.id,
4455
+ amount: rest.amount,
4456
+ currency: rest.currency,
4457
+ customer: customerId2 ? customerId2 : customerEmail ? { email: customerEmail } : "",
4458
+ status: "pending",
4459
+ metadata: core.omitInternalMetadata(checkoutMetadata),
4460
+ item_id: data.item_id ?? null,
4461
+ requires_action: true,
4462
+ payment_url: checkoutSession.url
4463
+ };
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
+ );
4429
4485
  const customerWithDefaultPaymentMethod = await this.stripe.customers.retrieve(
4430
- customer,
4486
+ customerId,
4431
4487
  { expand: ["invoice_settings.default_payment_method"] }
4432
4488
  );
4433
4489
  if ("deleted" in customerWithDefaultPaymentMethod) {
@@ -4438,26 +4494,28 @@ var StripeProvider = class extends core.AbstractPayKitProvider {
4438
4494
  }
4439
4495
  let defaultPaymentMethod = customerWithDefaultPaymentMethod.invoice_settings?.default_payment_method;
4440
4496
  if (!defaultPaymentMethod) {
4441
- const paymentMethods = await this.stripe.paymentMethods.list({ customer });
4497
+ const paymentMethods = await this.stripe.paymentMethods.list({
4498
+ customer: customerId
4499
+ });
4442
4500
  if (paymentMethods.data.length === 0) {
4443
- throw new core.ValidationError(
4444
- `Customer ${customer} has no payment methods. Add a payment method for the customer before creating a payment intent`,
4445
- { provider: this.providerName, method: "createPayment" }
4446
- );
4501
+ return createCheckoutSession(void 0, customerId);
4447
4502
  }
4448
4503
  defaultPaymentMethod = paymentMethods.data[0].id;
4449
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
+ });
4450
4510
  const paymentIntentOptions = {
4451
4511
  currency: rest.currency,
4452
4512
  amount: rest.amount,
4453
4513
  metadata: paymentMetadata,
4454
- customer,
4514
+ customer: customerId,
4455
4515
  capture_method,
4456
4516
  confirm: true,
4457
- // automatically confirms the payment
4458
4517
  payment_method: defaultPaymentMethod,
4459
4518
  off_session: true
4460
- // uses customer's default payment method, avoids 3Ds/authentication
4461
4519
  };
4462
4520
  if (data.billing) {
4463
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, updatePaymentSchema, retrievePaymentSchema, tryCatchAsync, deletePaymentSchema, capturePaymentSchema, createRefundSchema, WebhookError, paykitEvent$InboundSchema, billingModeSchema, omitInternalMetadata, 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,19 +4411,73 @@ 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") {
4413
- throw new InvalidTypeError("customer", "string (customer ID)", "object", {
4414
- provider: this.providerName,
4415
- method: "createPayment"
4414
+ const createCheckoutSession = async (customerEmail, customerId2) => {
4415
+ const checkoutMetadata = stringifyMetadataValues({
4416
+ ...rest.metadata,
4417
+ ...provider_metadata?.metadata ?? {},
4418
+ [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4416
4419
  });
4417
- }
4418
- const paymentMetadata = stringifyMetadataValues({
4419
- ...rest.metadata,
4420
- ...provider_metadata?.metadata ?? {},
4421
- [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4422
- });
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 = {
4426
+ mode: "payment",
4427
+ line_items: [
4428
+ {
4429
+ price_data: {
4430
+ currency: rest.currency,
4431
+ product_data: { name: `Payment for ${data.item_id || "item"}` },
4432
+ unit_amount: rest.amount
4433
+ },
4434
+ quantity: 1
4435
+ }
4436
+ ],
4437
+ metadata: checkoutMetadata,
4438
+ payment_intent_data: {
4439
+ capture_method,
4440
+ setup_future_usage: "off_session"
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);
4447
+ return {
4448
+ id: checkoutSession.id,
4449
+ amount: rest.amount,
4450
+ currency: rest.currency,
4451
+ customer: customerId2 ? customerId2 : customerEmail ? { email: customerEmail } : "",
4452
+ status: "pending",
4453
+ metadata: omitInternalMetadata(checkoutMetadata),
4454
+ item_id: data.item_id ?? null,
4455
+ requires_action: true,
4456
+ payment_url: checkoutSession.url
4457
+ };
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
+ );
4423
4479
  const customerWithDefaultPaymentMethod = await this.stripe.customers.retrieve(
4424
- customer,
4480
+ customerId,
4425
4481
  { expand: ["invoice_settings.default_payment_method"] }
4426
4482
  );
4427
4483
  if ("deleted" in customerWithDefaultPaymentMethod) {
@@ -4432,26 +4488,28 @@ var StripeProvider = class extends AbstractPayKitProvider {
4432
4488
  }
4433
4489
  let defaultPaymentMethod = customerWithDefaultPaymentMethod.invoice_settings?.default_payment_method;
4434
4490
  if (!defaultPaymentMethod) {
4435
- const paymentMethods = await this.stripe.paymentMethods.list({ customer });
4491
+ const paymentMethods = await this.stripe.paymentMethods.list({
4492
+ customer: customerId
4493
+ });
4436
4494
  if (paymentMethods.data.length === 0) {
4437
- throw new ValidationError(
4438
- `Customer ${customer} has no payment methods. Add a payment method for the customer before creating a payment intent`,
4439
- { provider: this.providerName, method: "createPayment" }
4440
- );
4495
+ return createCheckoutSession(void 0, customerId);
4441
4496
  }
4442
4497
  defaultPaymentMethod = paymentMethods.data[0].id;
4443
4498
  }
4499
+ const paymentMetadata = stringifyMetadataValues({
4500
+ ...rest.metadata,
4501
+ ...provider_metadata?.metadata ?? {},
4502
+ [PAYKIT_METADATA_KEY]: JSON.stringify({ itemId: data.item_id ?? null })
4503
+ });
4444
4504
  const paymentIntentOptions = {
4445
4505
  currency: rest.currency,
4446
4506
  amount: rest.amount,
4447
4507
  metadata: paymentMetadata,
4448
- customer,
4508
+ customer: customerId,
4449
4509
  capture_method,
4450
4510
  confirm: true,
4451
- // automatically confirms the payment
4452
4511
  payment_method: defaultPaymentMethod,
4453
4512
  off_session: true
4454
- // uses customer's default payment method, avoids 3Ds/authentication
4455
4513
  };
4456
4514
  if (data.billing) {
4457
4515
  paymentIntentOptions.shipping = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paykit-sdk/stripe",
3
- "version": "1.1.101",
3
+ "version": "1.1.103",
4
4
  "description": "Stripe provider for PayKit",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",