@paykit-sdk/medusajs 1.0.9 → 1.0.11

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/README.md CHANGED
@@ -20,16 +20,23 @@ Configure in `medusa-config.ts`:
20
20
 
21
21
  ```typescript
22
22
  import { defineConfig } from '@medusajs/framework/utils';
23
- import { createStripe } from '@paykit-sdk/stripe';
23
+ import { stripe } from '@paykit-sdk/stripe';
24
24
 
25
25
  export default defineConfig({
26
26
  modules: [
27
27
  {
28
- resolve: '@paykit-sdk/medusajs',
28
+ resolve: '@medusajs/payment',
29
29
  options: {
30
- provider: createStripe({ apiKey: process.env.STRIPE_API_KEY }),
31
- webhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
32
- debug: process.env.NODE_ENV === 'development',
30
+ providers: [
31
+ {
32
+ resolve: '@paykit-sdk/medusajs',
33
+ options: {
34
+ provider: stripe(),
35
+ webhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
36
+ debug: process.env.NODE_ENV === 'development',
37
+ },
38
+ },
39
+ ],
33
40
  },
34
41
  },
35
42
  ],
package/dist/index.js CHANGED
@@ -101,7 +101,8 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
101
101
  let customer;
102
102
  if (context?.account_holder?.data?.id) {
103
103
  customer = context.account_holder.data.id;
104
- } else if (data?.email) {
104
+ }
105
+ if (data?.email) {
105
106
  customer = { email: data.email };
106
107
  }
107
108
  if (!customer) {
@@ -112,7 +113,7 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
112
113
  }
113
114
  if (typeof customer === "object" && "email" in customer) {
114
115
  const customerName = data?.name ? data.name : customer.email.split("@")[0];
115
- await core.tryCatchAsync(
116
+ const [createdCustomer, createError] = await core.tryCatchAsync(
116
117
  this.paykit.customers.create({
117
118
  email: customer.email,
118
119
  phone: data?.phone ?? "",
@@ -122,6 +123,22 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
122
123
  }
123
124
  })
124
125
  );
126
+ if (createError) {
127
+ if (createError.name === "ProviderNotSupportedError") {
128
+ if (this.options.debug) {
129
+ console.info(
130
+ `[PayKit] Provider ${this.provider.providerName} doesn't support customer creation, using email object`
131
+ );
132
+ }
133
+ } else {
134
+ throw new utils.MedusaError(
135
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
136
+ `Failed to create customer: ${createError.message}`
137
+ );
138
+ }
139
+ } else {
140
+ customer = createdCustomer.id;
141
+ }
125
142
  } else {
126
143
  customer = customer;
127
144
  }
@@ -306,24 +323,7 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
306
323
  console.info("[PayKit] Getting webhook action and data", payload);
307
324
  }
308
325
  const { rawData, headers } = payload;
309
- const fullUrl = (() => {
310
- if (headers["origin"]) {
311
- return headers["origin"];
312
- }
313
- if (headers["x-forwarded-host"]) {
314
- const protocol = headers["x-forwarded-proto"] || "https";
315
- const host = headers["x-forwarded-host"];
316
- const path = headers["x-forwarded-path"] || "";
317
- return `${protocol}://${host}${path}`;
318
- }
319
- if (headers["host"]) {
320
- const protocol = headers["x-forwarded-proto"] || (String(headers["host"]).includes("localhost") ? "http" : "https");
321
- const host = headers["host"];
322
- const path = headers["x-original-url"] || headers["x-forwarded-path"] || "";
323
- return `${protocol}://${host}${path}`;
324
- }
325
- return "";
326
- })();
326
+ const bodyString = Buffer.isBuffer(rawData) ? rawData.toString("utf8") : rawData;
327
327
  const webhook = this.paykit.webhooks.setup({ webhookSecret: this.options.webhookSecret }).on("payment.created", async (event) => {
328
328
  return {
329
329
  action: utils.PaymentActions.PENDING,
@@ -362,12 +362,114 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
362
362
  Object.entries(headers).map(([key, value]) => [key, String(value)])
363
363
  );
364
364
  const webhookEvents = await webhook.handle({
365
- body: rawData,
365
+ body: bodyString,
366
366
  headers: new Headers(stringifiedHeaders),
367
- fullUrl
367
+ fullUrl: core.getURLFromHeaders(stringifiedHeaders)
368
368
  });
369
369
  return webhookEvents;
370
370
  };
371
+ createAccountHolder = async ({
372
+ context,
373
+ data
374
+ }) => {
375
+ if (this.options.debug) {
376
+ console.info("[PayKit] Creating account holder", context, data);
377
+ }
378
+ const { customer, account_holder } = context;
379
+ if (account_holder?.data?.id) {
380
+ return { id: account_holder.data.id };
381
+ }
382
+ if (!customer) {
383
+ throw new utils.MedusaError(
384
+ utils.MedusaError.Types.INVALID_DATA,
385
+ "Customer not found in context"
386
+ );
387
+ }
388
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
389
+ this.paykit.customers.create({
390
+ email: customer.email,
391
+ name: customer.email.split("@")[0],
392
+ phone: customer.phone,
393
+ metadata: {
394
+ PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
395
+ }
396
+ })
397
+ );
398
+ if (accountHolderError) {
399
+ throw new utils.MedusaError(
400
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
401
+ accountHolderError.message
402
+ );
403
+ }
404
+ return {
405
+ id: accountHolderResult.id,
406
+ data: accountHolderResult
407
+ };
408
+ };
409
+ updateAccountHolder = async ({
410
+ context,
411
+ data
412
+ }) => {
413
+ if (this.options.debug) {
414
+ console.info("[PayKit] Updating account holder", context, data);
415
+ }
416
+ const { account_holder, customer, idempotency_key } = context;
417
+ if (!account_holder.data?.id) {
418
+ throw new utils.MedusaError(
419
+ utils.MedusaError.Types.INVALID_DATA,
420
+ "Account holder not found in context"
421
+ );
422
+ }
423
+ if (!customer) {
424
+ return {};
425
+ }
426
+ const accountHolderId = account_holder.data.id;
427
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
428
+ this.paykit.customers.update(accountHolderId, {
429
+ email: customer.email,
430
+ name: customer.email.split("@")[0],
431
+ phone: customer.phone,
432
+ ...data?.metadata && {
433
+ metadata: core.stringifyMetadataValues(
434
+ data?.metadata ?? {}
435
+ )
436
+ }
437
+ })
438
+ );
439
+ if (accountHolderError) {
440
+ throw new utils.MedusaError(
441
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
442
+ accountHolderError.message
443
+ );
444
+ }
445
+ return { data: accountHolderResult };
446
+ };
447
+ deleteAccountHolder = async ({
448
+ context,
449
+ data
450
+ }) => {
451
+ if (this.options.debug) {
452
+ console.info("[PayKit] Deleting account holder", context, data);
453
+ }
454
+ const { account_holder } = context;
455
+ if (!account_holder.data?.id) {
456
+ throw new utils.MedusaError(
457
+ utils.MedusaError.Types.INVALID_DATA,
458
+ "Account holder not found in context"
459
+ );
460
+ }
461
+ const accountHolderId = account_holder.data.id;
462
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
463
+ this.paykit.customers.delete(accountHolderId)
464
+ );
465
+ if (accountHolderError) {
466
+ throw new utils.MedusaError(
467
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
468
+ accountHolderError.message
469
+ );
470
+ }
471
+ return { data: accountHolderResult };
472
+ };
371
473
  };
372
474
 
373
475
  // 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 } from '@paykit-sdk/core';
2
+ import { providerSchema, PayKit, tryCatchAsync, validateRequiredKeys, stringifyMetadataValues, getURLFromHeaders } from '@paykit-sdk/core';
3
3
  import { z } from 'zod';
4
4
 
5
5
  // src/index.ts
@@ -97,7 +97,8 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
97
97
  let customer;
98
98
  if (context?.account_holder?.data?.id) {
99
99
  customer = context.account_holder.data.id;
100
- } else if (data?.email) {
100
+ }
101
+ if (data?.email) {
101
102
  customer = { email: data.email };
102
103
  }
103
104
  if (!customer) {
@@ -108,7 +109,7 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
108
109
  }
109
110
  if (typeof customer === "object" && "email" in customer) {
110
111
  const customerName = data?.name ? data.name : customer.email.split("@")[0];
111
- await tryCatchAsync(
112
+ const [createdCustomer, createError] = await tryCatchAsync(
112
113
  this.paykit.customers.create({
113
114
  email: customer.email,
114
115
  phone: data?.phone ?? "",
@@ -118,6 +119,22 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
118
119
  }
119
120
  })
120
121
  );
122
+ if (createError) {
123
+ if (createError.name === "ProviderNotSupportedError") {
124
+ if (this.options.debug) {
125
+ console.info(
126
+ `[PayKit] Provider ${this.provider.providerName} doesn't support customer creation, using email object`
127
+ );
128
+ }
129
+ } else {
130
+ throw new MedusaError(
131
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
132
+ `Failed to create customer: ${createError.message}`
133
+ );
134
+ }
135
+ } else {
136
+ customer = createdCustomer.id;
137
+ }
121
138
  } else {
122
139
  customer = customer;
123
140
  }
@@ -302,24 +319,7 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
302
319
  console.info("[PayKit] Getting webhook action and data", payload);
303
320
  }
304
321
  const { rawData, headers } = payload;
305
- const fullUrl = (() => {
306
- if (headers["origin"]) {
307
- return headers["origin"];
308
- }
309
- if (headers["x-forwarded-host"]) {
310
- const protocol = headers["x-forwarded-proto"] || "https";
311
- const host = headers["x-forwarded-host"];
312
- const path = headers["x-forwarded-path"] || "";
313
- return `${protocol}://${host}${path}`;
314
- }
315
- if (headers["host"]) {
316
- const protocol = headers["x-forwarded-proto"] || (String(headers["host"]).includes("localhost") ? "http" : "https");
317
- const host = headers["host"];
318
- const path = headers["x-original-url"] || headers["x-forwarded-path"] || "";
319
- return `${protocol}://${host}${path}`;
320
- }
321
- return "";
322
- })();
322
+ const bodyString = Buffer.isBuffer(rawData) ? rawData.toString("utf8") : rawData;
323
323
  const webhook = this.paykit.webhooks.setup({ webhookSecret: this.options.webhookSecret }).on("payment.created", async (event) => {
324
324
  return {
325
325
  action: PaymentActions.PENDING,
@@ -358,12 +358,114 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
358
358
  Object.entries(headers).map(([key, value]) => [key, String(value)])
359
359
  );
360
360
  const webhookEvents = await webhook.handle({
361
- body: rawData,
361
+ body: bodyString,
362
362
  headers: new Headers(stringifiedHeaders),
363
- fullUrl
363
+ fullUrl: getURLFromHeaders(stringifiedHeaders)
364
364
  });
365
365
  return webhookEvents;
366
366
  };
367
+ createAccountHolder = async ({
368
+ context,
369
+ data
370
+ }) => {
371
+ if (this.options.debug) {
372
+ console.info("[PayKit] Creating account holder", context, data);
373
+ }
374
+ const { customer, account_holder } = context;
375
+ if (account_holder?.data?.id) {
376
+ return { id: account_holder.data.id };
377
+ }
378
+ if (!customer) {
379
+ throw new MedusaError(
380
+ MedusaError.Types.INVALID_DATA,
381
+ "Customer not found in context"
382
+ );
383
+ }
384
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
385
+ this.paykit.customers.create({
386
+ email: customer.email,
387
+ name: customer.email.split("@")[0],
388
+ phone: customer.phone,
389
+ metadata: {
390
+ PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
391
+ }
392
+ })
393
+ );
394
+ if (accountHolderError) {
395
+ throw new MedusaError(
396
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
397
+ accountHolderError.message
398
+ );
399
+ }
400
+ return {
401
+ id: accountHolderResult.id,
402
+ data: accountHolderResult
403
+ };
404
+ };
405
+ updateAccountHolder = async ({
406
+ context,
407
+ data
408
+ }) => {
409
+ if (this.options.debug) {
410
+ console.info("[PayKit] Updating account holder", context, data);
411
+ }
412
+ const { account_holder, customer, idempotency_key } = context;
413
+ if (!account_holder.data?.id) {
414
+ throw new MedusaError(
415
+ MedusaError.Types.INVALID_DATA,
416
+ "Account holder not found in context"
417
+ );
418
+ }
419
+ if (!customer) {
420
+ return {};
421
+ }
422
+ const accountHolderId = account_holder.data.id;
423
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
424
+ this.paykit.customers.update(accountHolderId, {
425
+ email: customer.email,
426
+ name: customer.email.split("@")[0],
427
+ phone: customer.phone,
428
+ ...data?.metadata && {
429
+ metadata: stringifyMetadataValues(
430
+ data?.metadata ?? {}
431
+ )
432
+ }
433
+ })
434
+ );
435
+ if (accountHolderError) {
436
+ throw new MedusaError(
437
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
438
+ accountHolderError.message
439
+ );
440
+ }
441
+ return { data: accountHolderResult };
442
+ };
443
+ deleteAccountHolder = async ({
444
+ context,
445
+ data
446
+ }) => {
447
+ if (this.options.debug) {
448
+ console.info("[PayKit] Deleting account holder", context, data);
449
+ }
450
+ const { account_holder } = context;
451
+ if (!account_holder.data?.id) {
452
+ throw new MedusaError(
453
+ MedusaError.Types.INVALID_DATA,
454
+ "Account holder not found in context"
455
+ );
456
+ }
457
+ const accountHolderId = account_holder.data.id;
458
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
459
+ this.paykit.customers.delete(accountHolderId)
460
+ );
461
+ if (accountHolderError) {
462
+ throw new MedusaError(
463
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
464
+ accountHolderError.message
465
+ );
466
+ }
467
+ return { data: accountHolderResult };
468
+ };
367
469
  };
368
470
 
369
471
  // 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 };
@@ -99,7 +99,8 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
99
99
  let customer;
100
100
  if (context?.account_holder?.data?.id) {
101
101
  customer = context.account_holder.data.id;
102
- } else if (data?.email) {
102
+ }
103
+ if (data?.email) {
103
104
  customer = { email: data.email };
104
105
  }
105
106
  if (!customer) {
@@ -110,7 +111,7 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
110
111
  }
111
112
  if (typeof customer === "object" && "email" in customer) {
112
113
  const customerName = data?.name ? data.name : customer.email.split("@")[0];
113
- await core.tryCatchAsync(
114
+ const [createdCustomer, createError] = await core.tryCatchAsync(
114
115
  this.paykit.customers.create({
115
116
  email: customer.email,
116
117
  phone: data?.phone ?? "",
@@ -120,6 +121,22 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
120
121
  }
121
122
  })
122
123
  );
124
+ if (createError) {
125
+ if (createError.name === "ProviderNotSupportedError") {
126
+ if (this.options.debug) {
127
+ console.info(
128
+ `[PayKit] Provider ${this.provider.providerName} doesn't support customer creation, using email object`
129
+ );
130
+ }
131
+ } else {
132
+ throw new utils.MedusaError(
133
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
134
+ `Failed to create customer: ${createError.message}`
135
+ );
136
+ }
137
+ } else {
138
+ customer = createdCustomer.id;
139
+ }
123
140
  } else {
124
141
  customer = customer;
125
142
  }
@@ -304,24 +321,7 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
304
321
  console.info("[PayKit] Getting webhook action and data", payload);
305
322
  }
306
323
  const { rawData, headers } = payload;
307
- const fullUrl = (() => {
308
- if (headers["origin"]) {
309
- return headers["origin"];
310
- }
311
- if (headers["x-forwarded-host"]) {
312
- const protocol = headers["x-forwarded-proto"] || "https";
313
- const host = headers["x-forwarded-host"];
314
- const path = headers["x-forwarded-path"] || "";
315
- return `${protocol}://${host}${path}`;
316
- }
317
- if (headers["host"]) {
318
- const protocol = headers["x-forwarded-proto"] || (String(headers["host"]).includes("localhost") ? "http" : "https");
319
- const host = headers["host"];
320
- const path = headers["x-original-url"] || headers["x-forwarded-path"] || "";
321
- return `${protocol}://${host}${path}`;
322
- }
323
- return "";
324
- })();
324
+ const bodyString = Buffer.isBuffer(rawData) ? rawData.toString("utf8") : rawData;
325
325
  const webhook = this.paykit.webhooks.setup({ webhookSecret: this.options.webhookSecret }).on("payment.created", async (event) => {
326
326
  return {
327
327
  action: utils.PaymentActions.PENDING,
@@ -360,12 +360,114 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
360
360
  Object.entries(headers).map(([key, value]) => [key, String(value)])
361
361
  );
362
362
  const webhookEvents = await webhook.handle({
363
- body: rawData,
363
+ body: bodyString,
364
364
  headers: new Headers(stringifiedHeaders),
365
- fullUrl
365
+ fullUrl: core.getURLFromHeaders(stringifiedHeaders)
366
366
  });
367
367
  return webhookEvents;
368
368
  };
369
+ createAccountHolder = async ({
370
+ context,
371
+ data
372
+ }) => {
373
+ if (this.options.debug) {
374
+ console.info("[PayKit] Creating account holder", context, data);
375
+ }
376
+ const { customer, account_holder } = context;
377
+ if (account_holder?.data?.id) {
378
+ return { id: account_holder.data.id };
379
+ }
380
+ if (!customer) {
381
+ throw new utils.MedusaError(
382
+ utils.MedusaError.Types.INVALID_DATA,
383
+ "Customer not found in context"
384
+ );
385
+ }
386
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
387
+ this.paykit.customers.create({
388
+ email: customer.email,
389
+ name: customer.email.split("@")[0],
390
+ phone: customer.phone,
391
+ metadata: {
392
+ PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
393
+ }
394
+ })
395
+ );
396
+ if (accountHolderError) {
397
+ throw new utils.MedusaError(
398
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
399
+ accountHolderError.message
400
+ );
401
+ }
402
+ return {
403
+ id: accountHolderResult.id,
404
+ data: accountHolderResult
405
+ };
406
+ };
407
+ updateAccountHolder = async ({
408
+ context,
409
+ data
410
+ }) => {
411
+ if (this.options.debug) {
412
+ console.info("[PayKit] Updating account holder", context, data);
413
+ }
414
+ const { account_holder, customer, idempotency_key } = context;
415
+ if (!account_holder.data?.id) {
416
+ throw new utils.MedusaError(
417
+ utils.MedusaError.Types.INVALID_DATA,
418
+ "Account holder not found in context"
419
+ );
420
+ }
421
+ if (!customer) {
422
+ return {};
423
+ }
424
+ const accountHolderId = account_holder.data.id;
425
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
426
+ this.paykit.customers.update(accountHolderId, {
427
+ email: customer.email,
428
+ name: customer.email.split("@")[0],
429
+ phone: customer.phone,
430
+ ...data?.metadata && {
431
+ metadata: core.stringifyMetadataValues(
432
+ data?.metadata ?? {}
433
+ )
434
+ }
435
+ })
436
+ );
437
+ if (accountHolderError) {
438
+ throw new utils.MedusaError(
439
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
440
+ accountHolderError.message
441
+ );
442
+ }
443
+ return { data: accountHolderResult };
444
+ };
445
+ deleteAccountHolder = async ({
446
+ context,
447
+ data
448
+ }) => {
449
+ if (this.options.debug) {
450
+ console.info("[PayKit] Deleting account holder", context, data);
451
+ }
452
+ const { account_holder } = context;
453
+ if (!account_holder.data?.id) {
454
+ throw new utils.MedusaError(
455
+ utils.MedusaError.Types.INVALID_DATA,
456
+ "Account holder not found in context"
457
+ );
458
+ }
459
+ const accountHolderId = account_holder.data.id;
460
+ const [accountHolderResult, accountHolderError] = await core.tryCatchAsync(
461
+ this.paykit.customers.delete(accountHolderId)
462
+ );
463
+ if (accountHolderError) {
464
+ throw new utils.MedusaError(
465
+ utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
466
+ accountHolderError.message
467
+ );
468
+ }
469
+ return { data: accountHolderResult };
470
+ };
369
471
  };
370
472
 
371
473
  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 } from '@paykit-sdk/core';
2
+ import { providerSchema, PayKit, tryCatchAsync, validateRequiredKeys, stringifyMetadataValues, getURLFromHeaders } from '@paykit-sdk/core';
3
3
  import { z } from 'zod';
4
4
 
5
5
  // src/providers/paykit-provider.ts
@@ -97,7 +97,8 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
97
97
  let customer;
98
98
  if (context?.account_holder?.data?.id) {
99
99
  customer = context.account_holder.data.id;
100
- } else if (data?.email) {
100
+ }
101
+ if (data?.email) {
101
102
  customer = { email: data.email };
102
103
  }
103
104
  if (!customer) {
@@ -108,7 +109,7 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
108
109
  }
109
110
  if (typeof customer === "object" && "email" in customer) {
110
111
  const customerName = data?.name ? data.name : customer.email.split("@")[0];
111
- await tryCatchAsync(
112
+ const [createdCustomer, createError] = await tryCatchAsync(
112
113
  this.paykit.customers.create({
113
114
  email: customer.email,
114
115
  phone: data?.phone ?? "",
@@ -118,6 +119,22 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
118
119
  }
119
120
  })
120
121
  );
122
+ if (createError) {
123
+ if (createError.name === "ProviderNotSupportedError") {
124
+ if (this.options.debug) {
125
+ console.info(
126
+ `[PayKit] Provider ${this.provider.providerName} doesn't support customer creation, using email object`
127
+ );
128
+ }
129
+ } else {
130
+ throw new MedusaError(
131
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
132
+ `Failed to create customer: ${createError.message}`
133
+ );
134
+ }
135
+ } else {
136
+ customer = createdCustomer.id;
137
+ }
121
138
  } else {
122
139
  customer = customer;
123
140
  }
@@ -302,24 +319,7 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
302
319
  console.info("[PayKit] Getting webhook action and data", payload);
303
320
  }
304
321
  const { rawData, headers } = payload;
305
- const fullUrl = (() => {
306
- if (headers["origin"]) {
307
- return headers["origin"];
308
- }
309
- if (headers["x-forwarded-host"]) {
310
- const protocol = headers["x-forwarded-proto"] || "https";
311
- const host = headers["x-forwarded-host"];
312
- const path = headers["x-forwarded-path"] || "";
313
- return `${protocol}://${host}${path}`;
314
- }
315
- if (headers["host"]) {
316
- const protocol = headers["x-forwarded-proto"] || (String(headers["host"]).includes("localhost") ? "http" : "https");
317
- const host = headers["host"];
318
- const path = headers["x-original-url"] || headers["x-forwarded-path"] || "";
319
- return `${protocol}://${host}${path}`;
320
- }
321
- return "";
322
- })();
322
+ const bodyString = Buffer.isBuffer(rawData) ? rawData.toString("utf8") : rawData;
323
323
  const webhook = this.paykit.webhooks.setup({ webhookSecret: this.options.webhookSecret }).on("payment.created", async (event) => {
324
324
  return {
325
325
  action: PaymentActions.PENDING,
@@ -358,12 +358,114 @@ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
358
358
  Object.entries(headers).map(([key, value]) => [key, String(value)])
359
359
  );
360
360
  const webhookEvents = await webhook.handle({
361
- body: rawData,
361
+ body: bodyString,
362
362
  headers: new Headers(stringifiedHeaders),
363
- fullUrl
363
+ fullUrl: getURLFromHeaders(stringifiedHeaders)
364
364
  });
365
365
  return webhookEvents;
366
366
  };
367
+ createAccountHolder = async ({
368
+ context,
369
+ data
370
+ }) => {
371
+ if (this.options.debug) {
372
+ console.info("[PayKit] Creating account holder", context, data);
373
+ }
374
+ const { customer, account_holder } = context;
375
+ if (account_holder?.data?.id) {
376
+ return { id: account_holder.data.id };
377
+ }
378
+ if (!customer) {
379
+ throw new MedusaError(
380
+ MedusaError.Types.INVALID_DATA,
381
+ "Customer not found in context"
382
+ );
383
+ }
384
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
385
+ this.paykit.customers.create({
386
+ email: customer.email,
387
+ name: customer.email.split("@")[0],
388
+ phone: customer.phone,
389
+ metadata: {
390
+ PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa-paykit-adapter" })
391
+ }
392
+ })
393
+ );
394
+ if (accountHolderError) {
395
+ throw new MedusaError(
396
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
397
+ accountHolderError.message
398
+ );
399
+ }
400
+ return {
401
+ id: accountHolderResult.id,
402
+ data: accountHolderResult
403
+ };
404
+ };
405
+ updateAccountHolder = async ({
406
+ context,
407
+ data
408
+ }) => {
409
+ if (this.options.debug) {
410
+ console.info("[PayKit] Updating account holder", context, data);
411
+ }
412
+ const { account_holder, customer, idempotency_key } = context;
413
+ if (!account_holder.data?.id) {
414
+ throw new MedusaError(
415
+ MedusaError.Types.INVALID_DATA,
416
+ "Account holder not found in context"
417
+ );
418
+ }
419
+ if (!customer) {
420
+ return {};
421
+ }
422
+ const accountHolderId = account_holder.data.id;
423
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
424
+ this.paykit.customers.update(accountHolderId, {
425
+ email: customer.email,
426
+ name: customer.email.split("@")[0],
427
+ phone: customer.phone,
428
+ ...data?.metadata && {
429
+ metadata: stringifyMetadataValues(
430
+ data?.metadata ?? {}
431
+ )
432
+ }
433
+ })
434
+ );
435
+ if (accountHolderError) {
436
+ throw new MedusaError(
437
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
438
+ accountHolderError.message
439
+ );
440
+ }
441
+ return { data: accountHolderResult };
442
+ };
443
+ deleteAccountHolder = async ({
444
+ context,
445
+ data
446
+ }) => {
447
+ if (this.options.debug) {
448
+ console.info("[PayKit] Deleting account holder", context, data);
449
+ }
450
+ const { account_holder } = context;
451
+ if (!account_holder.data?.id) {
452
+ throw new MedusaError(
453
+ MedusaError.Types.INVALID_DATA,
454
+ "Account holder not found in context"
455
+ );
456
+ }
457
+ const accountHolderId = account_holder.data.id;
458
+ const [accountHolderResult, accountHolderError] = await tryCatchAsync(
459
+ this.paykit.customers.delete(accountHolderId)
460
+ );
461
+ if (accountHolderError) {
462
+ throw new MedusaError(
463
+ MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
464
+ accountHolderError.message
465
+ );
466
+ }
467
+ return { data: accountHolderResult };
468
+ };
367
469
  };
368
470
 
369
471
  export { PaykitMedusaJSAdapter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paykit-sdk/medusajs",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "MedusaJS Integrations for Paykit",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",