@paykit-sdk/medusajs 1.0.10 → 1.0.12

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
@@ -111,6 +111,13 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
111
111
  "Required: customer ID (account_holder) or email (data)"
112
112
  );
113
113
  }
114
+ const billingInfoParsed = core.billingSchema.safeParse(data?.billing);
115
+ if (data?.billing && !billingInfoParsed.success) {
116
+ throw new utils.MedusaError(
117
+ utils.MedusaError.Types.INVALID_DATA,
118
+ `Invalid billing information, ${billingInfoParsed.error.message} `
119
+ );
120
+ }
114
121
  if (typeof customer === "object" && "email" in customer) {
115
122
  const customerName = data?.name ? data.name : customer.email.split("@")[0];
116
123
  const [createdCustomer, createError] = await core.tryCatchAsync(
@@ -120,7 +127,8 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
120
127
  name: customerName,
121
128
  metadata: {
122
129
  PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
123
- }
130
+ },
131
+ billing: billingInfoParsed.data ?? null
124
132
  })
125
133
  );
126
134
  if (createError) {
@@ -368,6 +376,115 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
368
376
  });
369
377
  return webhookEvents;
370
378
  };
379
+ createAccountHolder = async ({
380
+ context,
381
+ data
382
+ }) => {
383
+ if (this.options.debug) {
384
+ console.info("[PayKit] Creating account holder", context, data);
385
+ }
386
+ const { customer, account_holder } = context;
387
+ if (account_holder?.data?.id) {
388
+ return { id: account_holder.data.id };
389
+ }
390
+ if (!customer) {
391
+ throw new utils.MedusaError(
392
+ utils.MedusaError.Types.INVALID_DATA,
393
+ "Customer not found in context"
394
+ );
395
+ }
396
+ const billingInfoParsed = core.billingSchema.safeParse(data?.billing);
397
+ if (data?.billing && !billingInfoParsed.success) {
398
+ throw new utils.MedusaError(
399
+ utils.MedusaError.Types.INVALID_DATA,
400
+ `Invalid billing information, ${billingInfoParsed.error.message} `
401
+ );
402
+ }
403
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
404
+ this.paykit.customers.create({
405
+ email: customer.email,
406
+ name: customer.email.split("@")[0],
407
+ phone: customer.phone,
408
+ metadata: {
409
+ PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
410
+ },
411
+ billing: billingInfoParsed.data ?? null
412
+ })
413
+ );
414
+ if (accountHolderError) {
415
+ throw new utils.MedusaError(
416
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
417
+ accountHolderError.message
418
+ );
419
+ }
420
+ return {
421
+ id: accountHolderResult.id,
422
+ data: accountHolderResult
423
+ };
424
+ };
425
+ updateAccountHolder = async ({
426
+ context,
427
+ data
428
+ }) => {
429
+ if (this.options.debug) {
430
+ console.info("[PayKit] Updating account holder", context, data);
431
+ }
432
+ const { account_holder, customer } = context;
433
+ if (!account_holder.data?.id) {
434
+ throw new utils.MedusaError(
435
+ utils.MedusaError.Types.INVALID_DATA,
436
+ "Account holder not found in context"
437
+ );
438
+ }
439
+ if (!customer) return {};
440
+ const accountHolderId = account_holder.data.id;
441
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
442
+ this.paykit.customers.update(accountHolderId, {
443
+ email: customer.email,
444
+ name: customer.email.split("@")[0],
445
+ phone: customer.phone,
446
+ ...data?.metadata && {
447
+ metadata: core.stringifyMetadataValues(
448
+ data?.metadata ?? {}
449
+ )
450
+ },
451
+ billing: data?.billing ? core.billingSchema.safeParse(data.billing).data : void 0
452
+ })
453
+ );
454
+ if (accountHolderError) {
455
+ throw new utils.MedusaError(
456
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
457
+ accountHolderError.message
458
+ );
459
+ }
460
+ return { data: accountHolderResult };
461
+ };
462
+ deleteAccountHolder = async ({
463
+ context,
464
+ data
465
+ }) => {
466
+ if (this.options.debug) {
467
+ console.info("[PayKit] Deleting account holder", context, data);
468
+ }
469
+ const { account_holder } = context;
470
+ if (!account_holder.data?.id) {
471
+ throw new utils.MedusaError(
472
+ utils.MedusaError.Types.INVALID_DATA,
473
+ "Account holder not found in context"
474
+ );
475
+ }
476
+ const accountHolderId = account_holder.data.id;
477
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
478
+ this.paykit.customers.delete(accountHolderId)
479
+ );
480
+ if (accountHolderError) {
481
+ throw new utils.MedusaError(
482
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
483
+ accountHolderError.message
484
+ );
485
+ }
486
+ return { data: accountHolderResult };
487
+ };
371
488
  };
372
489
 
373
490
  // src/index.ts
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ModuleProvider, Modules, AbstractPaymentProvider, MedusaError, PaymentSessionStatus, PaymentActions } from '@medusajs/framework/utils';
2
- import { providerSchema, PayKit, tryCatchAsync, validateRequiredKeys, stringifyMetadataValues, getURLFromHeaders } from '@paykit-sdk/core';
2
+ import { providerSchema, PayKit, billingSchema, tryCatchAsync, validateRequiredKeys, stringifyMetadataValues, getURLFromHeaders } from '@paykit-sdk/core';
3
3
  import { z } from 'zod';
4
4
 
5
5
  // src/index.ts
@@ -107,6 +107,13 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
107
107
  "Required: customer ID (account_holder) or email (data)"
108
108
  );
109
109
  }
110
+ const billingInfoParsed = billingSchema.safeParse(data?.billing);
111
+ if (data?.billing && !billingInfoParsed.success) {
112
+ throw new MedusaError(
113
+ MedusaError.Types.INVALID_DATA,
114
+ `Invalid billing information, ${billingInfoParsed.error.message} `
115
+ );
116
+ }
110
117
  if (typeof customer === "object" && "email" in customer) {
111
118
  const customerName = data?.name ? data.name : customer.email.split("@")[0];
112
119
  const [createdCustomer, createError] = await tryCatchAsync(
@@ -116,7 +123,8 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
116
123
  name: customerName,
117
124
  metadata: {
118
125
  PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
119
- }
126
+ },
127
+ billing: billingInfoParsed.data ?? null
120
128
  })
121
129
  );
122
130
  if (createError) {
@@ -364,6 +372,115 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
364
372
  });
365
373
  return webhookEvents;
366
374
  };
375
+ createAccountHolder = async ({
376
+ context,
377
+ data
378
+ }) => {
379
+ if (this.options.debug) {
380
+ console.info("[PayKit] Creating account holder", context, data);
381
+ }
382
+ const { customer, account_holder } = context;
383
+ if (account_holder?.data?.id) {
384
+ return { id: account_holder.data.id };
385
+ }
386
+ if (!customer) {
387
+ throw new MedusaError(
388
+ MedusaError.Types.INVALID_DATA,
389
+ "Customer not found in context"
390
+ );
391
+ }
392
+ const billingInfoParsed = billingSchema.safeParse(data?.billing);
393
+ if (data?.billing && !billingInfoParsed.success) {
394
+ throw new MedusaError(
395
+ MedusaError.Types.INVALID_DATA,
396
+ `Invalid billing information, ${billingInfoParsed.error.message} `
397
+ );
398
+ }
399
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
400
+ this.paykit.customers.create({
401
+ email: customer.email,
402
+ name: customer.email.split("@")[0],
403
+ phone: customer.phone,
404
+ metadata: {
405
+ PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
406
+ },
407
+ billing: billingInfoParsed.data ?? null
408
+ })
409
+ );
410
+ if (accountHolderError) {
411
+ throw new MedusaError(
412
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
413
+ accountHolderError.message
414
+ );
415
+ }
416
+ return {
417
+ id: accountHolderResult.id,
418
+ data: accountHolderResult
419
+ };
420
+ };
421
+ updateAccountHolder = async ({
422
+ context,
423
+ data
424
+ }) => {
425
+ if (this.options.debug) {
426
+ console.info("[PayKit] Updating account holder", context, data);
427
+ }
428
+ const { account_holder, customer } = context;
429
+ if (!account_holder.data?.id) {
430
+ throw new MedusaError(
431
+ MedusaError.Types.INVALID_DATA,
432
+ "Account holder not found in context"
433
+ );
434
+ }
435
+ if (!customer) return {};
436
+ const accountHolderId = account_holder.data.id;
437
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
438
+ this.paykit.customers.update(accountHolderId, {
439
+ email: customer.email,
440
+ name: customer.email.split("@")[0],
441
+ phone: customer.phone,
442
+ ...data?.metadata && {
443
+ metadata: stringifyMetadataValues(
444
+ data?.metadata ?? {}
445
+ )
446
+ },
447
+ billing: data?.billing ? billingSchema.safeParse(data.billing).data : void 0
448
+ })
449
+ );
450
+ if (accountHolderError) {
451
+ throw new MedusaError(
452
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
453
+ accountHolderError.message
454
+ );
455
+ }
456
+ return { data: accountHolderResult };
457
+ };
458
+ deleteAccountHolder = async ({
459
+ context,
460
+ data
461
+ }) => {
462
+ if (this.options.debug) {
463
+ console.info("[PayKit] Deleting account holder", context, data);
464
+ }
465
+ const { account_holder } = context;
466
+ if (!account_holder.data?.id) {
467
+ throw new MedusaError(
468
+ MedusaError.Types.INVALID_DATA,
469
+ "Account holder not found in context"
470
+ );
471
+ }
472
+ const accountHolderId = account_holder.data.id;
473
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
474
+ this.paykit.customers.delete(accountHolderId)
475
+ );
476
+ if (accountHolderError) {
477
+ throw new MedusaError(
478
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
479
+ accountHolderError.message
480
+ );
481
+ }
482
+ return { data: accountHolderResult };
483
+ };
367
484
  };
368
485
 
369
486
  // src/index.ts
@@ -1,4 +1,4 @@
1
- import { InitiatePaymentInput, InitiatePaymentOutput, CapturePaymentInput, CapturePaymentOutput, AuthorizePaymentInput, AuthorizePaymentOutput, CancelPaymentInput, CancelPaymentOutput, DeletePaymentInput, DeletePaymentOutput, GetPaymentStatusInput, GetPaymentStatusOutput, RefundPaymentInput, RefundPaymentOutput, RetrievePaymentInput, RetrievePaymentOutput, UpdatePaymentInput, UpdatePaymentOutput, ProviderWebhookPayload, WebhookActionResult } from '@medusajs/framework/types';
1
+ import { InitiatePaymentInput, InitiatePaymentOutput, CapturePaymentInput, CapturePaymentOutput, AuthorizePaymentInput, AuthorizePaymentOutput, CancelPaymentInput, CancelPaymentOutput, DeletePaymentInput, DeletePaymentOutput, GetPaymentStatusInput, GetPaymentStatusOutput, RefundPaymentInput, RefundPaymentOutput, RetrievePaymentInput, RetrievePaymentOutput, UpdatePaymentInput, UpdatePaymentOutput, ProviderWebhookPayload, WebhookActionResult, CreateAccountHolderInput, CreateAccountHolderOutput, UpdateAccountHolderInput, UpdateAccountHolderOutput, DeleteAccountHolderInput, DeleteAccountHolderOutput } from '@medusajs/framework/types';
2
2
  import { AbstractPaymentProvider } from '@medusajs/framework/utils';
3
3
  import { PayKitProvider, PayKit } from '@paykit-sdk/core';
4
4
  import { z } from 'zod';
@@ -56,6 +56,9 @@ declare class PaykitMedusaJSAdapter extends AbstractPaymentProvider<PaykitMedusa
56
56
  retrievePayment: (input: RetrievePaymentInput) => Promise<RetrievePaymentOutput>;
57
57
  updatePayment: (input: UpdatePaymentInput) => Promise<UpdatePaymentOutput>;
58
58
  getWebhookActionAndData: (payload: ProviderWebhookPayload["payload"]) => Promise<WebhookActionResult>;
59
+ createAccountHolder: ({ context, data, }: CreateAccountHolderInput) => Promise<CreateAccountHolderOutput>;
60
+ updateAccountHolder: ({ context, data, }: UpdateAccountHolderInput) => Promise<UpdateAccountHolderOutput>;
61
+ deleteAccountHolder: ({ context, data, }: DeleteAccountHolderInput) => Promise<DeleteAccountHolderOutput>;
59
62
  }
60
63
 
61
64
  export { PaykitMedusaJSAdapter, type PaykitMedusaJSAdapterOptions };
@@ -1,4 +1,4 @@
1
- import { InitiatePaymentInput, InitiatePaymentOutput, CapturePaymentInput, CapturePaymentOutput, AuthorizePaymentInput, AuthorizePaymentOutput, CancelPaymentInput, CancelPaymentOutput, DeletePaymentInput, DeletePaymentOutput, GetPaymentStatusInput, GetPaymentStatusOutput, RefundPaymentInput, RefundPaymentOutput, RetrievePaymentInput, RetrievePaymentOutput, UpdatePaymentInput, UpdatePaymentOutput, ProviderWebhookPayload, WebhookActionResult } from '@medusajs/framework/types';
1
+ import { InitiatePaymentInput, InitiatePaymentOutput, CapturePaymentInput, CapturePaymentOutput, AuthorizePaymentInput, AuthorizePaymentOutput, CancelPaymentInput, CancelPaymentOutput, DeletePaymentInput, DeletePaymentOutput, GetPaymentStatusInput, GetPaymentStatusOutput, RefundPaymentInput, RefundPaymentOutput, RetrievePaymentInput, RetrievePaymentOutput, UpdatePaymentInput, UpdatePaymentOutput, ProviderWebhookPayload, WebhookActionResult, CreateAccountHolderInput, CreateAccountHolderOutput, UpdateAccountHolderInput, UpdateAccountHolderOutput, DeleteAccountHolderInput, DeleteAccountHolderOutput } from '@medusajs/framework/types';
2
2
  import { AbstractPaymentProvider } from '@medusajs/framework/utils';
3
3
  import { PayKitProvider, PayKit } from '@paykit-sdk/core';
4
4
  import { z } from 'zod';
@@ -56,6 +56,9 @@ declare class PaykitMedusaJSAdapter extends AbstractPaymentProvider<PaykitMedusa
56
56
  retrievePayment: (input: RetrievePaymentInput) => Promise<RetrievePaymentOutput>;
57
57
  updatePayment: (input: UpdatePaymentInput) => Promise<UpdatePaymentOutput>;
58
58
  getWebhookActionAndData: (payload: ProviderWebhookPayload["payload"]) => Promise<WebhookActionResult>;
59
+ createAccountHolder: ({ context, data, }: CreateAccountHolderInput) => Promise<CreateAccountHolderOutput>;
60
+ updateAccountHolder: ({ context, data, }: UpdateAccountHolderInput) => Promise<UpdateAccountHolderOutput>;
61
+ deleteAccountHolder: ({ context, data, }: DeleteAccountHolderInput) => Promise<DeleteAccountHolderOutput>;
59
62
  }
60
63
 
61
64
  export { PaykitMedusaJSAdapter, type PaykitMedusaJSAdapterOptions };
@@ -109,6 +109,13 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
109
109
  "Required: customer ID (account_holder) or email (data)"
110
110
  );
111
111
  }
112
+ const billingInfoParsed = core.billingSchema.safeParse(data?.billing);
113
+ if (data?.billing && !billingInfoParsed.success) {
114
+ throw new utils.MedusaError(
115
+ utils.MedusaError.Types.INVALID_DATA,
116
+ `Invalid billing information, ${billingInfoParsed.error.message} `
117
+ );
118
+ }
112
119
  if (typeof customer === "object" && "email" in customer) {
113
120
  const customerName = data?.name ? data.name : customer.email.split("@")[0];
114
121
  const [createdCustomer, createError] = await core.tryCatchAsync(
@@ -118,7 +125,8 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
118
125
  name: customerName,
119
126
  metadata: {
120
127
  PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
121
- }
128
+ },
129
+ billing: billingInfoParsed.data ?? null
122
130
  })
123
131
  );
124
132
  if (createError) {
@@ -366,6 +374,115 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
366
374
  });
367
375
  return webhookEvents;
368
376
  };
377
+ createAccountHolder = async ({
378
+ context,
379
+ data
380
+ }) => {
381
+ if (this.options.debug) {
382
+ console.info("[PayKit] Creating account holder", context, data);
383
+ }
384
+ const { customer, account_holder } = context;
385
+ if (account_holder?.data?.id) {
386
+ return { id: account_holder.data.id };
387
+ }
388
+ if (!customer) {
389
+ throw new utils.MedusaError(
390
+ utils.MedusaError.Types.INVALID_DATA,
391
+ "Customer not found in context"
392
+ );
393
+ }
394
+ const billingInfoParsed = core.billingSchema.safeParse(data?.billing);
395
+ if (data?.billing && !billingInfoParsed.success) {
396
+ throw new utils.MedusaError(
397
+ utils.MedusaError.Types.INVALID_DATA,
398
+ `Invalid billing information, ${billingInfoParsed.error.message} `
399
+ );
400
+ }
401
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
402
+ this.paykit.customers.create({
403
+ email: customer.email,
404
+ name: customer.email.split("@")[0],
405
+ phone: customer.phone,
406
+ metadata: {
407
+ PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
408
+ },
409
+ billing: billingInfoParsed.data ?? null
410
+ })
411
+ );
412
+ if (accountHolderError) {
413
+ throw new utils.MedusaError(
414
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
415
+ accountHolderError.message
416
+ );
417
+ }
418
+ return {
419
+ id: accountHolderResult.id,
420
+ data: accountHolderResult
421
+ };
422
+ };
423
+ updateAccountHolder = async ({
424
+ context,
425
+ data
426
+ }) => {
427
+ if (this.options.debug) {
428
+ console.info("[PayKit] Updating account holder", context, data);
429
+ }
430
+ const { account_holder, customer } = context;
431
+ if (!account_holder.data?.id) {
432
+ throw new utils.MedusaError(
433
+ utils.MedusaError.Types.INVALID_DATA,
434
+ "Account holder not found in context"
435
+ );
436
+ }
437
+ if (!customer) return {};
438
+ const accountHolderId = account_holder.data.id;
439
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
440
+ this.paykit.customers.update(accountHolderId, {
441
+ email: customer.email,
442
+ name: customer.email.split("@")[0],
443
+ phone: customer.phone,
444
+ ...data?.metadata && {
445
+ metadata: core.stringifyMetadataValues(
446
+ data?.metadata ?? {}
447
+ )
448
+ },
449
+ billing: data?.billing ? core.billingSchema.safeParse(data.billing).data : void 0
450
+ })
451
+ );
452
+ if (accountHolderError) {
453
+ throw new utils.MedusaError(
454
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
455
+ accountHolderError.message
456
+ );
457
+ }
458
+ return { data: accountHolderResult };
459
+ };
460
+ deleteAccountHolder = async ({
461
+ context,
462
+ data
463
+ }) => {
464
+ if (this.options.debug) {
465
+ console.info("[PayKit] Deleting account holder", context, data);
466
+ }
467
+ const { account_holder } = context;
468
+ if (!account_holder.data?.id) {
469
+ throw new utils.MedusaError(
470
+ utils.MedusaError.Types.INVALID_DATA,
471
+ "Account holder not found in context"
472
+ );
473
+ }
474
+ const accountHolderId = account_holder.data.id;
475
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
476
+ this.paykit.customers.delete(accountHolderId)
477
+ );
478
+ if (accountHolderError) {
479
+ throw new utils.MedusaError(
480
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
481
+ accountHolderError.message
482
+ );
483
+ }
484
+ return { data: accountHolderResult };
485
+ };
369
486
  };
370
487
 
371
488
  exports.PaykitMedusaJSAdapter = PaykitMedusaJSAdapter;
@@ -1,5 +1,5 @@
1
1
  import { AbstractPaymentProvider, MedusaError, PaymentSessionStatus, PaymentActions } from '@medusajs/framework/utils';
2
- import { providerSchema, PayKit, tryCatchAsync, validateRequiredKeys, stringifyMetadataValues, getURLFromHeaders } from '@paykit-sdk/core';
2
+ import { providerSchema, PayKit, billingSchema, tryCatchAsync, validateRequiredKeys, stringifyMetadataValues, getURLFromHeaders } from '@paykit-sdk/core';
3
3
  import { z } from 'zod';
4
4
 
5
5
  // src/providers/paykit-provider.ts
@@ -107,6 +107,13 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
107
107
  "Required: customer ID (account_holder) or email (data)"
108
108
  );
109
109
  }
110
+ const billingInfoParsed = billingSchema.safeParse(data?.billing);
111
+ if (data?.billing && !billingInfoParsed.success) {
112
+ throw new MedusaError(
113
+ MedusaError.Types.INVALID_DATA,
114
+ `Invalid billing information, ${billingInfoParsed.error.message} `
115
+ );
116
+ }
110
117
  if (typeof customer === "object" && "email" in customer) {
111
118
  const customerName = data?.name ? data.name : customer.email.split("@")[0];
112
119
  const [createdCustomer, createError] = await tryCatchAsync(
@@ -116,7 +123,8 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
116
123
  name: customerName,
117
124
  metadata: {
118
125
  PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
119
- }
126
+ },
127
+ billing: billingInfoParsed.data ?? null
120
128
  })
121
129
  );
122
130
  if (createError) {
@@ -364,6 +372,115 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
364
372
  });
365
373
  return webhookEvents;
366
374
  };
375
+ createAccountHolder = async ({
376
+ context,
377
+ data
378
+ }) => {
379
+ if (this.options.debug) {
380
+ console.info("[PayKit] Creating account holder", context, data);
381
+ }
382
+ const { customer, account_holder } = context;
383
+ if (account_holder?.data?.id) {
384
+ return { id: account_holder.data.id };
385
+ }
386
+ if (!customer) {
387
+ throw new MedusaError(
388
+ MedusaError.Types.INVALID_DATA,
389
+ "Customer not found in context"
390
+ );
391
+ }
392
+ const billingInfoParsed = billingSchema.safeParse(data?.billing);
393
+ if (data?.billing && !billingInfoParsed.success) {
394
+ throw new MedusaError(
395
+ MedusaError.Types.INVALID_DATA,
396
+ `Invalid billing information, ${billingInfoParsed.error.message} `
397
+ );
398
+ }
399
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
400
+ this.paykit.customers.create({
401
+ email: customer.email,
402
+ name: customer.email.split("@")[0],
403
+ phone: customer.phone,
404
+ metadata: {
405
+ PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
406
+ },
407
+ billing: billingInfoParsed.data ?? null
408
+ })
409
+ );
410
+ if (accountHolderError) {
411
+ throw new MedusaError(
412
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
413
+ accountHolderError.message
414
+ );
415
+ }
416
+ return {
417
+ id: accountHolderResult.id,
418
+ data: accountHolderResult
419
+ };
420
+ };
421
+ updateAccountHolder = async ({
422
+ context,
423
+ data
424
+ }) => {
425
+ if (this.options.debug) {
426
+ console.info("[PayKit] Updating account holder", context, data);
427
+ }
428
+ const { account_holder, customer } = context;
429
+ if (!account_holder.data?.id) {
430
+ throw new MedusaError(
431
+ MedusaError.Types.INVALID_DATA,
432
+ "Account holder not found in context"
433
+ );
434
+ }
435
+ if (!customer) return {};
436
+ const accountHolderId = account_holder.data.id;
437
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
438
+ this.paykit.customers.update(accountHolderId, {
439
+ email: customer.email,
440
+ name: customer.email.split("@")[0],
441
+ phone: customer.phone,
442
+ ...data?.metadata && {
443
+ metadata: stringifyMetadataValues(
444
+ data?.metadata ?? {}
445
+ )
446
+ },
447
+ billing: data?.billing ? billingSchema.safeParse(data.billing).data : void 0
448
+ })
449
+ );
450
+ if (accountHolderError) {
451
+ throw new MedusaError(
452
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
453
+ accountHolderError.message
454
+ );
455
+ }
456
+ return { data: accountHolderResult };
457
+ };
458
+ deleteAccountHolder = async ({
459
+ context,
460
+ data
461
+ }) => {
462
+ if (this.options.debug) {
463
+ console.info("[PayKit] Deleting account holder", context, data);
464
+ }
465
+ const { account_holder } = context;
466
+ if (!account_holder.data?.id) {
467
+ throw new MedusaError(
468
+ MedusaError.Types.INVALID_DATA,
469
+ "Account holder not found in context"
470
+ );
471
+ }
472
+ const accountHolderId = account_holder.data.id;
473
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
474
+ this.paykit.customers.delete(accountHolderId)
475
+ );
476
+ if (accountHolderError) {
477
+ throw new MedusaError(
478
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
479
+ accountHolderError.message
480
+ );
481
+ }
482
+ return { data: accountHolderResult };
483
+ };
367
484
  };
368
485
 
369
486
  export { PaykitMedusaJSAdapter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paykit-sdk/medusajs",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "MedusaJS Integrations for Paykit",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",