@paykit-sdk/medusajs 1.0.0

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.
@@ -0,0 +1,303 @@
1
+ 'use strict';
2
+
3
+ var utils = require('@medusajs/framework/utils');
4
+ var core = require('@paykit-sdk/core');
5
+ var zod = require('zod');
6
+
7
+ // src/providers/paykit-provider.ts
8
+ var medusaStatus$InboundSchema = (status) => {
9
+ switch (status) {
10
+ case "pending":
11
+ case "processing":
12
+ return utils.PaymentSessionStatus.PENDING;
13
+ case "requires_action":
14
+ return utils.PaymentSessionStatus.REQUIRES_MORE;
15
+ case "requires_capture":
16
+ return utils.PaymentSessionStatus.AUTHORIZED;
17
+ case "succeeded":
18
+ return utils.PaymentSessionStatus.CAPTURED;
19
+ case "canceled":
20
+ return utils.PaymentSessionStatus.CANCELED;
21
+ case "failed":
22
+ return utils.PaymentSessionStatus.ERROR;
23
+ default:
24
+ return utils.PaymentSessionStatus.PENDING;
25
+ }
26
+ };
27
+
28
+ // src/providers/paykit-provider.ts
29
+ var optionsSchema = zod.z.object({
30
+ /**
31
+ * The underlying PayKit provider instance (Stripe, PayPal, etc.)
32
+ * This is required and must be a valid PayKit provider instance
33
+ */
34
+ provider: core.providerSchema,
35
+ /**
36
+ * The webhook secret for the provider
37
+ * This is required and must be a valid webhook secret
38
+ */
39
+ webhookSecret: zod.z.string(),
40
+ /**
41
+ * Whether to enable debug mode
42
+ * If enabled, the adapter will log debug information to the console
43
+ */
44
+ debug: zod.z.boolean().optional()
45
+ });
46
+ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
47
+ /**
48
+ * The unique identifier for this payment provider
49
+ * Will be stored as `pp_paykit_{id}` in Medusa
50
+ */
51
+ static identifier = "paykit";
52
+ paykit;
53
+ provider;
54
+ options;
55
+ static validateOptions(options) {
56
+ const { error } = optionsSchema.safeParse(options);
57
+ if (error) {
58
+ throw new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, error.message);
59
+ }
60
+ return;
61
+ }
62
+ /**
63
+ * Constructor receives Medusa's container and provider options
64
+ *
65
+ * @param cradle - Medusa's dependency injection container
66
+ * @param options - PayKit provider configuration
67
+ */
68
+ constructor(cradle, options) {
69
+ super(cradle, options);
70
+ this.options = options;
71
+ this.provider = options.provider;
72
+ this.paykit = new core.PayKit(this.provider);
73
+ if (this.options.debug) {
74
+ console.info(`[PayKit] Initialized with provider: ${this.provider.providerName}`);
75
+ }
76
+ }
77
+ initiatePayment = async ({
78
+ context,
79
+ amount,
80
+ currency_code,
81
+ data
82
+ }) => {
83
+ if (this.options.debug) {
84
+ console.info("[PayKit] Initiating payment", { context, amount, currency_code, data });
85
+ }
86
+ const intent = {
87
+ status: "pending",
88
+ amount: Number(amount),
89
+ currency: currency_code,
90
+ metadata: { ...data?.metadata ?? {}, session_id: data?.session_id ?? "" },
91
+ provider_metadata: data?.provider_metadata ?? void 0
92
+ };
93
+ let customer;
94
+ if (context?.account_holder?.data?.id) {
95
+ customer = context.account_holder.data.id;
96
+ } else if (data?.email) {
97
+ customer = { email: data.email };
98
+ }
99
+ if (!customer) {
100
+ throw new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, "Required: customer ID (account_holder) or email (data)");
101
+ }
102
+ if (typeof customer === "object" && "email" in customer) {
103
+ await core.tryCatchAsync(
104
+ this.paykit.customers.create({
105
+ email: customer.email,
106
+ phone: data?.phone ?? "",
107
+ name: data?.name ? data.name : customer.email.split("@")[0],
108
+ metadata: { PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa" }) }
109
+ })
110
+ );
111
+ } else {
112
+ customer = customer;
113
+ }
114
+ intent.customer = customer;
115
+ const [paymentIntentResult, paymentIntentError] = await core.tryCatchAsync(
116
+ this.paykit.payments.create(intent)
117
+ );
118
+ if (paymentIntentError)
119
+ throw new utils.MedusaError(utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
120
+ return { id: paymentIntentResult.id, status: medusaStatus$InboundSchema(paymentIntentResult.status) };
121
+ };
122
+ capturePayment = async (input) => {
123
+ if (this.options.debug) {
124
+ console.info("[PayKit] Capturing payment", input);
125
+ }
126
+ const { id, amount } = core.validateRequiredKeys(
127
+ ["id", "amount"],
128
+ input?.data ?? {},
129
+ "Missing required payment ID"
130
+ );
131
+ if (!id) throw new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, "Missing required payment ID");
132
+ if (!amount) throw new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, "Missing required payment amount");
133
+ const [paymentIntentResult, paymentIntentError] = await core.tryCatchAsync(
134
+ this.paykit.payments.capture(id, { amount: Number(amount) })
135
+ );
136
+ if (paymentIntentError)
137
+ throw new utils.MedusaError(utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
138
+ return { data: paymentIntentResult };
139
+ };
140
+ authorizePayment = async (input) => {
141
+ if (this.options.debug) {
142
+ console.info("[PayKit] Authorizing payment", input);
143
+ }
144
+ return this.getPaymentStatus(input);
145
+ };
146
+ cancelPayment = async (input) => {
147
+ if (this.options.debug) {
148
+ console.info("[PayKit] Canceling payment", input);
149
+ }
150
+ const { id } = core.validateRequiredKeys(
151
+ ["id"],
152
+ input?.data ?? {},
153
+ "Missing required payment ID"
154
+ );
155
+ if (!id) throw new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, "Missing required payment ID");
156
+ const [paymentIntentResult, paymentIntentError] = await core.tryCatchAsync(this.paykit.payments.cancel(id));
157
+ if (paymentIntentError)
158
+ throw new utils.MedusaError(utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
159
+ return { data: paymentIntentResult };
160
+ };
161
+ deletePayment = async (input) => {
162
+ return this.cancelPayment(input);
163
+ };
164
+ getPaymentStatus = async (input) => {
165
+ const { id } = core.validateRequiredKeys(
166
+ ["id"],
167
+ input?.data ?? {},
168
+ "Missing required payment ID"
169
+ );
170
+ if (!id) throw new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, "Missing required payment ID");
171
+ const [paymentIntentResult, paymentIntentError] = await core.tryCatchAsync(this.paykit.payments.retrieve(id));
172
+ if (paymentIntentError)
173
+ throw new utils.MedusaError(utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
174
+ if (!paymentIntentResult) throw new utils.MedusaError(utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, "Payment not found");
175
+ return {
176
+ status: medusaStatus$InboundSchema(paymentIntentResult.status),
177
+ data: paymentIntentResult
178
+ };
179
+ };
180
+ refundPayment = async (input) => {
181
+ if (this.options.debug) {
182
+ console.info("[PayKit] Refunding payment", input);
183
+ }
184
+ const { id: paymentId } = core.validateRequiredKeys(
185
+ ["id"],
186
+ input?.data ?? {},
187
+ "Missing required payment ID"
188
+ );
189
+ if (!paymentId) throw new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, "Missing required payment ID");
190
+ const [refundResult, refundError] = await core.tryCatchAsync(
191
+ this.paykit.refunds.create({
192
+ payment_id: paymentId,
193
+ amount: Number(input.amount),
194
+ reason: null,
195
+ metadata: input.data?.metadata ? input.data.metadata : null,
196
+ provider_metadata: input.data?.provider_metadata ? input.data.provider_metadata : void 0
197
+ })
198
+ );
199
+ if (refundError) throw new utils.MedusaError(utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, refundError.message);
200
+ return { data: refundResult };
201
+ };
202
+ retrievePayment = async (input) => {
203
+ if (this.options.debug) {
204
+ console.info("[PayKit] Retrieving payment", input);
205
+ }
206
+ const { id } = core.validateRequiredKeys(
207
+ ["id"],
208
+ input?.data ?? {},
209
+ "Missing required payment ID"
210
+ );
211
+ if (!id) throw new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, "Missing required payment ID");
212
+ const [paymentIntentResult, paymentIntentError] = await core.tryCatchAsync(this.paykit.payments.retrieve(id));
213
+ if (paymentIntentError)
214
+ throw new utils.MedusaError(utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
215
+ return { data: paymentIntentResult };
216
+ };
217
+ updatePayment = async (input) => {
218
+ if (this.options.debug) {
219
+ console.info("[PayKit] Updating payment", input);
220
+ }
221
+ const { amount, currency_code } = core.validateRequiredKeys(
222
+ ["amount", "currency_code"],
223
+ input,
224
+ "Missing required payment ID"
225
+ );
226
+ if (!amount || !currency_code)
227
+ throw new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, "Missing required amount or currency code");
228
+ const { id: paymentId } = core.validateRequiredKeys(
229
+ ["id"],
230
+ input.data,
231
+ "Missing required payment ID"
232
+ );
233
+ if (!paymentId) throw new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, "Missing required payment ID");
234
+ const [paymentIntentResult, paymentIntentError] = await core.tryCatchAsync(
235
+ this.paykit.payments.update(paymentId, {
236
+ amount: Number(amount),
237
+ currency: currency_code,
238
+ metadata: input.data?.metadata ? input.data.metadata : {},
239
+ provider_metadata: input.data?.provider_metadata ? input.data.provider_metadata : void 0
240
+ })
241
+ );
242
+ if (paymentIntentError)
243
+ throw new utils.MedusaError(utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
244
+ return { data: paymentIntentResult };
245
+ };
246
+ getWebhookActionAndData = async (payload) => {
247
+ if (this.options.debug) {
248
+ console.info("[PayKit] Getting webhook action and data", payload);
249
+ }
250
+ const { rawData, headers } = payload;
251
+ const fullUrl = (() => {
252
+ if (headers["origin"]) {
253
+ return headers["origin"];
254
+ }
255
+ if (headers["x-forwarded-host"]) {
256
+ const protocol = headers["x-forwarded-proto"] || "https";
257
+ const host = headers["x-forwarded-host"];
258
+ const path = headers["x-forwarded-path"] || "";
259
+ return `${protocol}://${host}${path}`;
260
+ }
261
+ if (headers["host"]) {
262
+ const protocol = headers["x-forwarded-proto"] || (String(headers["host"]).includes("localhost") ? "http" : "https");
263
+ const host = headers["host"];
264
+ const path = headers["x-original-url"] || headers["x-forwarded-path"] || "";
265
+ return `${protocol}://${host}${path}`;
266
+ }
267
+ return "";
268
+ })();
269
+ const webhook = this.paykit.webhooks.setup({ webhookSecret: this.options.webhookSecret }).on("payment.created", async (event) => {
270
+ return {
271
+ action: utils.PaymentActions.PENDING,
272
+ data: { session_id: event.data?.metadata?.session_id, amount: event.data?.amount }
273
+ };
274
+ }).on("payment.updated", async (event) => {
275
+ const statusActionMap = {
276
+ pending: utils.PaymentActions.PENDING,
277
+ processing: utils.PaymentActions.PENDING,
278
+ requires_action: utils.PaymentActions.REQUIRES_MORE,
279
+ requires_capture: utils.PaymentActions.AUTHORIZED,
280
+ succeeded: utils.PaymentActions.SUCCESSFUL,
281
+ failed: utils.PaymentActions.FAILED,
282
+ canceled: utils.PaymentActions.CANCELED
283
+ };
284
+ return {
285
+ action: event.data?.status ? statusActionMap[event.data.status] : utils.PaymentActions.PENDING,
286
+ data: { session_id: event.data?.metadata?.session_id, amount: event.data?.amount }
287
+ };
288
+ }).on("payment.canceled", async (event) => {
289
+ return {
290
+ action: utils.PaymentActions.CANCELED,
291
+ data: { session_id: event.data?.metadata?.session_id, amount: event.data?.amount }
292
+ };
293
+ });
294
+ const webhookEvents = await webhook.handle({
295
+ body: rawData,
296
+ headers,
297
+ fullUrl
298
+ });
299
+ return webhookEvents;
300
+ };
301
+ };
302
+
303
+ exports.PaykitMedusaJSAdapter = PaykitMedusaJSAdapter;
@@ -0,0 +1,301 @@
1
+ import { AbstractPaymentProvider, MedusaError, PaymentActions, PaymentSessionStatus } from '@medusajs/framework/utils';
2
+ import { providerSchema, PayKit, tryCatchAsync, validateRequiredKeys } from '@paykit-sdk/core';
3
+ import { z } from 'zod';
4
+
5
+ // src/providers/paykit-provider.ts
6
+ var medusaStatus$InboundSchema = (status) => {
7
+ switch (status) {
8
+ case "pending":
9
+ case "processing":
10
+ return PaymentSessionStatus.PENDING;
11
+ case "requires_action":
12
+ return PaymentSessionStatus.REQUIRES_MORE;
13
+ case "requires_capture":
14
+ return PaymentSessionStatus.AUTHORIZED;
15
+ case "succeeded":
16
+ return PaymentSessionStatus.CAPTURED;
17
+ case "canceled":
18
+ return PaymentSessionStatus.CANCELED;
19
+ case "failed":
20
+ return PaymentSessionStatus.ERROR;
21
+ default:
22
+ return PaymentSessionStatus.PENDING;
23
+ }
24
+ };
25
+
26
+ // src/providers/paykit-provider.ts
27
+ var optionsSchema = z.object({
28
+ /**
29
+ * The underlying PayKit provider instance (Stripe, PayPal, etc.)
30
+ * This is required and must be a valid PayKit provider instance
31
+ */
32
+ provider: providerSchema,
33
+ /**
34
+ * The webhook secret for the provider
35
+ * This is required and must be a valid webhook secret
36
+ */
37
+ webhookSecret: z.string(),
38
+ /**
39
+ * Whether to enable debug mode
40
+ * If enabled, the adapter will log debug information to the console
41
+ */
42
+ debug: z.boolean().optional()
43
+ });
44
+ var PaykitMedusaJSAdapter = class extends AbstractPaymentProvider {
45
+ /**
46
+ * The unique identifier for this payment provider
47
+ * Will be stored as `pp_paykit_{id}` in Medusa
48
+ */
49
+ static identifier = "paykit";
50
+ paykit;
51
+ provider;
52
+ options;
53
+ static validateOptions(options) {
54
+ const { error } = optionsSchema.safeParse(options);
55
+ if (error) {
56
+ throw new MedusaError(MedusaError.Types.INVALID_DATA, error.message);
57
+ }
58
+ return;
59
+ }
60
+ /**
61
+ * Constructor receives Medusa's container and provider options
62
+ *
63
+ * @param cradle - Medusa's dependency injection container
64
+ * @param options - PayKit provider configuration
65
+ */
66
+ constructor(cradle, options) {
67
+ super(cradle, options);
68
+ this.options = options;
69
+ this.provider = options.provider;
70
+ this.paykit = new PayKit(this.provider);
71
+ if (this.options.debug) {
72
+ console.info(`[PayKit] Initialized with provider: ${this.provider.providerName}`);
73
+ }
74
+ }
75
+ initiatePayment = async ({
76
+ context,
77
+ amount,
78
+ currency_code,
79
+ data
80
+ }) => {
81
+ if (this.options.debug) {
82
+ console.info("[PayKit] Initiating payment", { context, amount, currency_code, data });
83
+ }
84
+ const intent = {
85
+ status: "pending",
86
+ amount: Number(amount),
87
+ currency: currency_code,
88
+ metadata: { ...data?.metadata ?? {}, session_id: data?.session_id ?? "" },
89
+ provider_metadata: data?.provider_metadata ?? void 0
90
+ };
91
+ let customer;
92
+ if (context?.account_holder?.data?.id) {
93
+ customer = context.account_holder.data.id;
94
+ } else if (data?.email) {
95
+ customer = { email: data.email };
96
+ }
97
+ if (!customer) {
98
+ throw new MedusaError(MedusaError.Types.INVALID_DATA, "Required: customer ID (account_holder) or email (data)");
99
+ }
100
+ if (typeof customer === "object" && "email" in customer) {
101
+ await tryCatchAsync(
102
+ this.paykit.customers.create({
103
+ email: customer.email,
104
+ phone: data?.phone ?? "",
105
+ name: data?.name ? data.name : customer.email.split("@")[0],
106
+ metadata: { PAYKIT_METADATA_KEY: JSON.stringify({ source: "medusa" }) }
107
+ })
108
+ );
109
+ } else {
110
+ customer = customer;
111
+ }
112
+ intent.customer = customer;
113
+ const [paymentIntentResult, paymentIntentError] = await tryCatchAsync(
114
+ this.paykit.payments.create(intent)
115
+ );
116
+ if (paymentIntentError)
117
+ throw new MedusaError(MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
118
+ return { id: paymentIntentResult.id, status: medusaStatus$InboundSchema(paymentIntentResult.status) };
119
+ };
120
+ capturePayment = async (input) => {
121
+ if (this.options.debug) {
122
+ console.info("[PayKit] Capturing payment", input);
123
+ }
124
+ const { id, amount } = validateRequiredKeys(
125
+ ["id", "amount"],
126
+ input?.data ?? {},
127
+ "Missing required payment ID"
128
+ );
129
+ if (!id) throw new MedusaError(MedusaError.Types.INVALID_DATA, "Missing required payment ID");
130
+ if (!amount) throw new MedusaError(MedusaError.Types.INVALID_DATA, "Missing required payment amount");
131
+ const [paymentIntentResult, paymentIntentError] = await tryCatchAsync(
132
+ this.paykit.payments.capture(id, { amount: Number(amount) })
133
+ );
134
+ if (paymentIntentError)
135
+ throw new MedusaError(MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
136
+ return { data: paymentIntentResult };
137
+ };
138
+ authorizePayment = async (input) => {
139
+ if (this.options.debug) {
140
+ console.info("[PayKit] Authorizing payment", input);
141
+ }
142
+ return this.getPaymentStatus(input);
143
+ };
144
+ cancelPayment = async (input) => {
145
+ if (this.options.debug) {
146
+ console.info("[PayKit] Canceling payment", input);
147
+ }
148
+ const { id } = validateRequiredKeys(
149
+ ["id"],
150
+ input?.data ?? {},
151
+ "Missing required payment ID"
152
+ );
153
+ if (!id) throw new MedusaError(MedusaError.Types.INVALID_DATA, "Missing required payment ID");
154
+ const [paymentIntentResult, paymentIntentError] = await tryCatchAsync(this.paykit.payments.cancel(id));
155
+ if (paymentIntentError)
156
+ throw new MedusaError(MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
157
+ return { data: paymentIntentResult };
158
+ };
159
+ deletePayment = async (input) => {
160
+ return this.cancelPayment(input);
161
+ };
162
+ getPaymentStatus = async (input) => {
163
+ const { id } = validateRequiredKeys(
164
+ ["id"],
165
+ input?.data ?? {},
166
+ "Missing required payment ID"
167
+ );
168
+ if (!id) throw new MedusaError(MedusaError.Types.INVALID_DATA, "Missing required payment ID");
169
+ const [paymentIntentResult, paymentIntentError] = await tryCatchAsync(this.paykit.payments.retrieve(id));
170
+ if (paymentIntentError)
171
+ throw new MedusaError(MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
172
+ if (!paymentIntentResult) throw new MedusaError(MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, "Payment not found");
173
+ return {
174
+ status: medusaStatus$InboundSchema(paymentIntentResult.status),
175
+ data: paymentIntentResult
176
+ };
177
+ };
178
+ refundPayment = async (input) => {
179
+ if (this.options.debug) {
180
+ console.info("[PayKit] Refunding payment", input);
181
+ }
182
+ const { id: paymentId } = validateRequiredKeys(
183
+ ["id"],
184
+ input?.data ?? {},
185
+ "Missing required payment ID"
186
+ );
187
+ if (!paymentId) throw new MedusaError(MedusaError.Types.INVALID_DATA, "Missing required payment ID");
188
+ const [refundResult, refundError] = await tryCatchAsync(
189
+ this.paykit.refunds.create({
190
+ payment_id: paymentId,
191
+ amount: Number(input.amount),
192
+ reason: null,
193
+ metadata: input.data?.metadata ? input.data.metadata : null,
194
+ provider_metadata: input.data?.provider_metadata ? input.data.provider_metadata : void 0
195
+ })
196
+ );
197
+ if (refundError) throw new MedusaError(MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, refundError.message);
198
+ return { data: refundResult };
199
+ };
200
+ retrievePayment = async (input) => {
201
+ if (this.options.debug) {
202
+ console.info("[PayKit] Retrieving payment", input);
203
+ }
204
+ const { id } = validateRequiredKeys(
205
+ ["id"],
206
+ input?.data ?? {},
207
+ "Missing required payment ID"
208
+ );
209
+ if (!id) throw new MedusaError(MedusaError.Types.INVALID_DATA, "Missing required payment ID");
210
+ const [paymentIntentResult, paymentIntentError] = await tryCatchAsync(this.paykit.payments.retrieve(id));
211
+ if (paymentIntentError)
212
+ throw new MedusaError(MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
213
+ return { data: paymentIntentResult };
214
+ };
215
+ updatePayment = async (input) => {
216
+ if (this.options.debug) {
217
+ console.info("[PayKit] Updating payment", input);
218
+ }
219
+ const { amount, currency_code } = validateRequiredKeys(
220
+ ["amount", "currency_code"],
221
+ input,
222
+ "Missing required payment ID"
223
+ );
224
+ if (!amount || !currency_code)
225
+ throw new MedusaError(MedusaError.Types.INVALID_DATA, "Missing required amount or currency code");
226
+ const { id: paymentId } = validateRequiredKeys(
227
+ ["id"],
228
+ input.data,
229
+ "Missing required payment ID"
230
+ );
231
+ if (!paymentId) throw new MedusaError(MedusaError.Types.INVALID_DATA, "Missing required payment ID");
232
+ const [paymentIntentResult, paymentIntentError] = await tryCatchAsync(
233
+ this.paykit.payments.update(paymentId, {
234
+ amount: Number(amount),
235
+ currency: currency_code,
236
+ metadata: input.data?.metadata ? input.data.metadata : {},
237
+ provider_metadata: input.data?.provider_metadata ? input.data.provider_metadata : void 0
238
+ })
239
+ );
240
+ if (paymentIntentError)
241
+ throw new MedusaError(MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, paymentIntentError.message);
242
+ return { data: paymentIntentResult };
243
+ };
244
+ getWebhookActionAndData = async (payload) => {
245
+ if (this.options.debug) {
246
+ console.info("[PayKit] Getting webhook action and data", payload);
247
+ }
248
+ const { rawData, headers } = payload;
249
+ const fullUrl = (() => {
250
+ if (headers["origin"]) {
251
+ return headers["origin"];
252
+ }
253
+ if (headers["x-forwarded-host"]) {
254
+ const protocol = headers["x-forwarded-proto"] || "https";
255
+ const host = headers["x-forwarded-host"];
256
+ const path = headers["x-forwarded-path"] || "";
257
+ return `${protocol}://${host}${path}`;
258
+ }
259
+ if (headers["host"]) {
260
+ const protocol = headers["x-forwarded-proto"] || (String(headers["host"]).includes("localhost") ? "http" : "https");
261
+ const host = headers["host"];
262
+ const path = headers["x-original-url"] || headers["x-forwarded-path"] || "";
263
+ return `${protocol}://${host}${path}`;
264
+ }
265
+ return "";
266
+ })();
267
+ const webhook = this.paykit.webhooks.setup({ webhookSecret: this.options.webhookSecret }).on("payment.created", async (event) => {
268
+ return {
269
+ action: PaymentActions.PENDING,
270
+ data: { session_id: event.data?.metadata?.session_id, amount: event.data?.amount }
271
+ };
272
+ }).on("payment.updated", async (event) => {
273
+ const statusActionMap = {
274
+ pending: PaymentActions.PENDING,
275
+ processing: PaymentActions.PENDING,
276
+ requires_action: PaymentActions.REQUIRES_MORE,
277
+ requires_capture: PaymentActions.AUTHORIZED,
278
+ succeeded: PaymentActions.SUCCESSFUL,
279
+ failed: PaymentActions.FAILED,
280
+ canceled: PaymentActions.CANCELED
281
+ };
282
+ return {
283
+ action: event.data?.status ? statusActionMap[event.data.status] : PaymentActions.PENDING,
284
+ data: { session_id: event.data?.metadata?.session_id, amount: event.data?.amount }
285
+ };
286
+ }).on("payment.canceled", async (event) => {
287
+ return {
288
+ action: PaymentActions.CANCELED,
289
+ data: { session_id: event.data?.metadata?.session_id, amount: event.data?.amount }
290
+ };
291
+ });
292
+ const webhookEvents = await webhook.handle({
293
+ body: rawData,
294
+ headers,
295
+ fullUrl
296
+ });
297
+ return webhookEvents;
298
+ };
299
+ };
300
+
301
+ export { PaykitMedusaJSAdapter };
@@ -0,0 +1,6 @@
1
+ import { PaymentSessionStatus } from '@medusajs/framework/utils';
2
+ import { PaymentStatus } from '@paykit-sdk/core';
3
+
4
+ declare const medusaStatus$InboundSchema: (status: PaymentStatus) => PaymentSessionStatus;
5
+
6
+ export { medusaStatus$InboundSchema };
@@ -0,0 +1,6 @@
1
+ import { PaymentSessionStatus } from '@medusajs/framework/utils';
2
+ import { PaymentStatus } from '@paykit-sdk/core';
3
+
4
+ declare const medusaStatus$InboundSchema: (status: PaymentStatus) => PaymentSessionStatus;
5
+
6
+ export { medusaStatus$InboundSchema };
@@ -0,0 +1,26 @@
1
+ 'use strict';
2
+
3
+ var utils = require('@medusajs/framework/utils');
4
+
5
+ // src/utils/mapper.ts
6
+ var medusaStatus$InboundSchema = (status) => {
7
+ switch (status) {
8
+ case "pending":
9
+ case "processing":
10
+ return utils.PaymentSessionStatus.PENDING;
11
+ case "requires_action":
12
+ return utils.PaymentSessionStatus.REQUIRES_MORE;
13
+ case "requires_capture":
14
+ return utils.PaymentSessionStatus.AUTHORIZED;
15
+ case "succeeded":
16
+ return utils.PaymentSessionStatus.CAPTURED;
17
+ case "canceled":
18
+ return utils.PaymentSessionStatus.CANCELED;
19
+ case "failed":
20
+ return utils.PaymentSessionStatus.ERROR;
21
+ default:
22
+ return utils.PaymentSessionStatus.PENDING;
23
+ }
24
+ };
25
+
26
+ exports.medusaStatus$InboundSchema = medusaStatus$InboundSchema;
@@ -0,0 +1,24 @@
1
+ import { PaymentSessionStatus } from '@medusajs/framework/utils';
2
+
3
+ // src/utils/mapper.ts
4
+ var medusaStatus$InboundSchema = (status) => {
5
+ switch (status) {
6
+ case "pending":
7
+ case "processing":
8
+ return PaymentSessionStatus.PENDING;
9
+ case "requires_action":
10
+ return PaymentSessionStatus.REQUIRES_MORE;
11
+ case "requires_capture":
12
+ return PaymentSessionStatus.AUTHORIZED;
13
+ case "succeeded":
14
+ return PaymentSessionStatus.CAPTURED;
15
+ case "canceled":
16
+ return PaymentSessionStatus.CANCELED;
17
+ case "failed":
18
+ return PaymentSessionStatus.ERROR;
19
+ default:
20
+ return PaymentSessionStatus.PENDING;
21
+ }
22
+ };
23
+
24
+ export { medusaStatus$InboundSchema };