@paykit-sdk/medusajs 1.0.12 → 1.0.13
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 +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +264 -254
- package/dist/index.mjs +265 -255
- package/dist/providers/paykit-provider.d.mts +19 -15
- package/dist/providers/paykit-provider.d.ts +19 -15
- package/dist/providers/paykit-provider.js +264 -254
- package/dist/providers/paykit-provider.mjs +265 -255
- package/dist/utils/mapper.d.mts +5 -2
- package/dist/utils/mapper.d.ts +5 -2
- package/dist/utils/mapper.js +2 -2
- package/dist/utils/mapper.mjs +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ var core = require('@paykit-sdk/core');
|
|
|
7
7
|
var zod = require('zod');
|
|
8
8
|
|
|
9
9
|
// src/index.ts
|
|
10
|
-
var
|
|
10
|
+
var PaymentStatus$inboundSchema = (status) => {
|
|
11
11
|
switch (status) {
|
|
12
12
|
case "pending":
|
|
13
13
|
case "processing":
|
|
@@ -31,17 +31,20 @@ var medusaStatus$InboundSchema = (status) => {
|
|
|
31
31
|
var optionsSchema = zod.z.object({
|
|
32
32
|
/**
|
|
33
33
|
* The underlying PayKit provider instance (Stripe, PayPal, etc.)
|
|
34
|
-
* This is required and must be a valid PayKit provider instance
|
|
35
34
|
*/
|
|
36
35
|
provider: core.providerSchema,
|
|
37
36
|
/**
|
|
38
|
-
* The webhook secret for the provider
|
|
39
|
-
* This is required and must be a valid webhook secret
|
|
37
|
+
* The webhook secret for the provider.
|
|
40
38
|
*/
|
|
41
|
-
webhookSecret: zod.z.string(),
|
|
39
|
+
webhookSecret: zod.z.string().nullable().default(null),
|
|
40
|
+
/**
|
|
41
|
+
* Multiplier applied to the amount before sending to the payment provider.
|
|
42
|
+
* Use this when your provider expects amounts in the smallest currency unit (e.g. cents)
|
|
43
|
+
* but Medusa is passing amounts in the major unit (e.g. euros).
|
|
44
|
+
*/
|
|
45
|
+
amountToCentsMultiplier: zod.z.number().min(1).default(1),
|
|
42
46
|
/**
|
|
43
47
|
* Whether to enable debug mode
|
|
44
|
-
* If enabled, the adapter will log debug information to the console
|
|
45
48
|
*/
|
|
46
49
|
debug: zod.z.boolean().optional()
|
|
47
50
|
});
|
|
@@ -52,29 +55,48 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
|
|
|
52
55
|
*/
|
|
53
56
|
static identifier = "paykit";
|
|
54
57
|
paykit;
|
|
55
|
-
provider;
|
|
56
58
|
options;
|
|
57
59
|
static validateOptions(options) {
|
|
58
60
|
const { error } = optionsSchema.safeParse(options);
|
|
59
61
|
if (error) {
|
|
60
|
-
throw new utils.MedusaError(
|
|
62
|
+
throw new utils.MedusaError(
|
|
63
|
+
utils.MedusaError.Types.INVALID_DATA,
|
|
64
|
+
error.message
|
|
65
|
+
);
|
|
61
66
|
}
|
|
62
67
|
return;
|
|
63
68
|
}
|
|
64
69
|
/**
|
|
65
|
-
* Constructor receives Medusa's container and provider options
|
|
66
|
-
*
|
|
67
70
|
* @param cradle - Medusa's dependency injection container
|
|
68
71
|
* @param options - PayKit provider configuration
|
|
69
72
|
*/
|
|
70
73
|
constructor(cradle, options) {
|
|
71
74
|
super(cradle, options);
|
|
72
75
|
this.options = options;
|
|
73
|
-
this.
|
|
74
|
-
this.paykit = new core.PayKit(this.provider);
|
|
76
|
+
this.paykit = new core.PayKit(options.provider);
|
|
75
77
|
if (this.options.debug) {
|
|
76
|
-
console.info(
|
|
78
|
+
console.info(
|
|
79
|
+
`[PayKit] Initialized with provider: ${this.paykit.providerName}`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async exec(promise, context, options) {
|
|
84
|
+
const [result, error] = await core.tryCatchAsync(promise);
|
|
85
|
+
if (error) {
|
|
86
|
+
if (options?.allowUnsupported && error.name === "ProviderNotSupportedError") {
|
|
87
|
+
if (this.options.debug) {
|
|
88
|
+
console.warn(
|
|
89
|
+
`[PayKit ${context}] Operation not supported by ${this.paykit.providerName}.`
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
throw new utils.MedusaError(
|
|
95
|
+
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
96
|
+
`[PayKit ${context}] ${error.message}`
|
|
97
|
+
);
|
|
77
98
|
}
|
|
99
|
+
return result;
|
|
78
100
|
}
|
|
79
101
|
initiatePayment = async ({
|
|
80
102
|
context,
|
|
@@ -85,341 +107,338 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
|
|
|
85
107
|
if (this.options.debug) {
|
|
86
108
|
console.info("[PayKit] Initiating payment", {
|
|
87
109
|
context,
|
|
88
|
-
amount,
|
|
110
|
+
amount: Number(amount) * this.options.amountToCentsMultiplier,
|
|
89
111
|
currency_code,
|
|
90
112
|
data
|
|
91
113
|
});
|
|
92
114
|
}
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
currency: currency_code,
|
|
96
|
-
metadata: { ...data?.metadata ?? {}, session_id: data?.session_id ?? null },
|
|
97
|
-
provider_metadata: data?.provider_metadata,
|
|
98
|
-
capture_method: "manual",
|
|
99
|
-
item_id: data?.item_id
|
|
100
|
-
};
|
|
101
|
-
let customer;
|
|
115
|
+
const hasProviderCustomer = !!context?.account_holder?.data?.id;
|
|
116
|
+
let customer = null;
|
|
102
117
|
if (context?.account_holder?.data?.id) {
|
|
103
|
-
customer = context.account_holder.data.id;
|
|
104
|
-
}
|
|
105
|
-
|
|
118
|
+
customer = { id: context.account_holder.data.id };
|
|
119
|
+
} else if (context?.customer?.email) {
|
|
120
|
+
customer = { email: context.customer.email };
|
|
121
|
+
} else if (data?.email) {
|
|
106
122
|
customer = { email: data.email };
|
|
107
123
|
}
|
|
108
|
-
if (!customer) {
|
|
124
|
+
if (!customer || !core.isIdCustomer(customer) && !core.isEmailCustomer(customer)) {
|
|
109
125
|
throw new utils.MedusaError(
|
|
110
126
|
utils.MedusaError.Types.INVALID_DATA,
|
|
111
|
-
"
|
|
127
|
+
"Customer ID (account_holder) or Email (context.customer or data.email) required"
|
|
112
128
|
);
|
|
113
129
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
130
|
+
if (!hasProviderCustomer && core.isEmailCustomer(customer)) {
|
|
131
|
+
const { fullName } = core.parseCustomerName({
|
|
132
|
+
name: `${context?.customer?.first_name ?? ""} ${context?.customer?.last_name ?? ""}`.trim(),
|
|
133
|
+
email: customer.email
|
|
134
|
+
});
|
|
135
|
+
const billingInfo = core.billingSchema.safeParse(data?.billing);
|
|
136
|
+
if (data?.billing && !billingInfo.success) {
|
|
137
|
+
throw new utils.MedusaError(
|
|
138
|
+
utils.MedusaError.Types.INVALID_DATA,
|
|
139
|
+
`Invalid billing information, ${billingInfo.error.message} `
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
const created = await this.exec(
|
|
124
143
|
this.paykit.customers.create({
|
|
125
144
|
email: customer.email,
|
|
126
|
-
|
|
127
|
-
|
|
145
|
+
name: fullName,
|
|
146
|
+
phone: data?.phone ?? null,
|
|
147
|
+
billing: billingInfo.data ?? null,
|
|
128
148
|
metadata: {
|
|
129
|
-
PAYKIT_METADATA_KEY: JSON.stringify({
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
})
|
|
133
|
-
);
|
|
134
|
-
if (createError) {
|
|
135
|
-
if (createError.name === "ProviderNotSupportedError") {
|
|
136
|
-
if (this.options.debug) {
|
|
137
|
-
console.info(
|
|
138
|
-
`[PayKit] Provider ${this.provider.providerName} doesn't support customer creation, using email object`
|
|
139
|
-
);
|
|
149
|
+
[core.PAYKIT_METADATA_KEY]: JSON.stringify({
|
|
150
|
+
source: "medusa-paykit-adapter"
|
|
151
|
+
})
|
|
140
152
|
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
153
|
+
}),
|
|
154
|
+
"Customer Creation",
|
|
155
|
+
{ allowUnsupported: true }
|
|
156
|
+
);
|
|
157
|
+
if (created) {
|
|
158
|
+
customer = {
|
|
159
|
+
..."id" in created ? { id: created.id } : {},
|
|
160
|
+
..."email" in created ? { email: created.email } : {}
|
|
161
|
+
};
|
|
149
162
|
}
|
|
150
|
-
} else {
|
|
151
|
-
customer = customer;
|
|
152
163
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
164
|
+
const payment = await this.exec(
|
|
165
|
+
this.paykit.payments.create({
|
|
166
|
+
amount: Number(amount) * this.options.amountToCentsMultiplier,
|
|
167
|
+
currency: currency_code.toUpperCase(),
|
|
168
|
+
customer,
|
|
169
|
+
item_id: data?.item_id ?? null,
|
|
170
|
+
capture_method: "manual",
|
|
171
|
+
metadata: {
|
|
172
|
+
...data?.metadata,
|
|
173
|
+
session_id: data?.session_id
|
|
174
|
+
},
|
|
175
|
+
...data?.billing ? { billing: data.billing } : {},
|
|
176
|
+
provider_metadata: data?.provider_metadata
|
|
177
|
+
}),
|
|
178
|
+
"Initiate"
|
|
156
179
|
);
|
|
157
|
-
if (paymentIntentError) {
|
|
158
|
-
throw new utils.MedusaError(
|
|
159
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
160
|
-
paymentIntentError.message
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
if (paymentIntentResult.requires_action && paymentIntentResult.payment_url) {
|
|
164
|
-
return {
|
|
165
|
-
id: paymentIntentResult.id,
|
|
166
|
-
status: utils.PaymentSessionStatus.REQUIRES_MORE,
|
|
167
|
-
data: {
|
|
168
|
-
payment_url: paymentIntentResult.payment_url
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
180
|
return {
|
|
173
|
-
id:
|
|
174
|
-
status:
|
|
181
|
+
id: payment.id,
|
|
182
|
+
status: payment.requires_action ? utils.PaymentSessionStatus.REQUIRES_MORE : PaymentStatus$inboundSchema(payment.status),
|
|
183
|
+
data: { ...payment, payment_url: payment.payment_url }
|
|
175
184
|
};
|
|
176
185
|
};
|
|
177
186
|
capturePayment = async (input) => {
|
|
178
187
|
if (this.options.debug) {
|
|
179
188
|
console.info("[PayKit] Capturing payment", input);
|
|
180
189
|
}
|
|
181
|
-
const { id, amount } = core.validateRequiredKeys(
|
|
190
|
+
const { id: paymentId, amount } = core.validateRequiredKeys(
|
|
182
191
|
["id", "amount"],
|
|
183
|
-
input
|
|
192
|
+
input.data ?? {},
|
|
184
193
|
"Missing required fields: {keys}",
|
|
185
194
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
186
195
|
);
|
|
187
|
-
const
|
|
188
|
-
this.paykit.payments.capture(
|
|
196
|
+
const data = await this.exec(
|
|
197
|
+
this.paykit.payments.capture(paymentId, {
|
|
198
|
+
amount: Number(amount) * this.options.amountToCentsMultiplier
|
|
199
|
+
}),
|
|
200
|
+
"Capture"
|
|
189
201
|
);
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
paymentIntentError.message
|
|
194
|
-
);
|
|
195
|
-
return { data: paymentIntentResult };
|
|
202
|
+
return {
|
|
203
|
+
data
|
|
204
|
+
};
|
|
196
205
|
};
|
|
197
206
|
authorizePayment = async (input) => {
|
|
198
|
-
|
|
199
|
-
|
|
207
|
+
const { id: paymentId } = core.validateRequiredKeys(
|
|
208
|
+
["id"],
|
|
209
|
+
input.data ?? {},
|
|
210
|
+
"Missing required fields: {keys}",
|
|
211
|
+
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
212
|
+
);
|
|
213
|
+
const payment = await this.exec(
|
|
214
|
+
this.paykit.payments.retrieve(paymentId),
|
|
215
|
+
"Authorize"
|
|
216
|
+
);
|
|
217
|
+
if (!payment) {
|
|
218
|
+
throw new utils.MedusaError(
|
|
219
|
+
utils.MedusaError.Types.NOT_FOUND,
|
|
220
|
+
"Payment not found"
|
|
221
|
+
);
|
|
200
222
|
}
|
|
201
|
-
|
|
223
|
+
const resolvedStatus = (() => {
|
|
224
|
+
if (payment.requires_action)
|
|
225
|
+
return utils.PaymentSessionStatus.REQUIRES_MORE;
|
|
226
|
+
if (payment.status === "succeeded")
|
|
227
|
+
return utils.PaymentSessionStatus.AUTHORIZED;
|
|
228
|
+
return PaymentStatus$inboundSchema(payment.status);
|
|
229
|
+
})();
|
|
230
|
+
return {
|
|
231
|
+
status: resolvedStatus,
|
|
232
|
+
data: payment
|
|
233
|
+
};
|
|
202
234
|
};
|
|
203
235
|
cancelPayment = async (input) => {
|
|
204
236
|
if (this.options.debug) {
|
|
205
237
|
console.info("[PayKit] Canceling payment", input);
|
|
206
238
|
}
|
|
207
|
-
const { id } = core.validateRequiredKeys(
|
|
239
|
+
const { id: paymentId } = core.validateRequiredKeys(
|
|
208
240
|
["id"],
|
|
209
|
-
input
|
|
241
|
+
input.data ?? {},
|
|
210
242
|
"Missing required fields: {keys}",
|
|
211
243
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
212
244
|
);
|
|
213
|
-
const
|
|
214
|
-
this.paykit.payments.cancel(
|
|
245
|
+
const data = await this.exec(
|
|
246
|
+
this.paykit.payments.cancel(paymentId),
|
|
247
|
+
"Cancel"
|
|
215
248
|
);
|
|
216
|
-
|
|
217
|
-
throw new utils.MedusaError(
|
|
218
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
219
|
-
paymentIntentError.message
|
|
220
|
-
);
|
|
221
|
-
return { data: paymentIntentResult };
|
|
249
|
+
return { data };
|
|
222
250
|
};
|
|
223
251
|
deletePayment = async (input) => {
|
|
224
|
-
|
|
252
|
+
if (this.options.debug) {
|
|
253
|
+
console.info(
|
|
254
|
+
"[PayKit] Deleting payment (attempting cancel)",
|
|
255
|
+
input
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
const paymentId = input.data?.id;
|
|
259
|
+
if (!paymentId) {
|
|
260
|
+
return {};
|
|
261
|
+
}
|
|
262
|
+
const data = await this.exec(
|
|
263
|
+
this.paykit.payments.cancel(paymentId),
|
|
264
|
+
"Cancel",
|
|
265
|
+
{ allowUnsupported: true }
|
|
266
|
+
);
|
|
267
|
+
return { data };
|
|
225
268
|
};
|
|
226
269
|
getPaymentStatus = async (input) => {
|
|
227
|
-
const { id } = core.validateRequiredKeys(
|
|
270
|
+
const { id: paymentId } = core.validateRequiredKeys(
|
|
228
271
|
["id"],
|
|
229
|
-
input
|
|
272
|
+
input.data ?? {},
|
|
230
273
|
"Missing required fields: {keys}",
|
|
231
274
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
232
275
|
);
|
|
233
|
-
const
|
|
234
|
-
this.paykit.payments.retrieve(
|
|
276
|
+
const payment = await this.exec(
|
|
277
|
+
this.paykit.payments.retrieve(paymentId),
|
|
278
|
+
"Status"
|
|
235
279
|
);
|
|
236
|
-
if (
|
|
280
|
+
if (!payment)
|
|
237
281
|
throw new utils.MedusaError(
|
|
238
|
-
utils.MedusaError.Types.
|
|
239
|
-
paymentIntentError.message
|
|
240
|
-
);
|
|
241
|
-
if (!paymentIntentResult)
|
|
242
|
-
throw new utils.MedusaError(
|
|
243
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
282
|
+
utils.MedusaError.Types.NOT_FOUND,
|
|
244
283
|
"Payment not found"
|
|
245
284
|
);
|
|
246
285
|
return {
|
|
247
|
-
status:
|
|
248
|
-
data:
|
|
286
|
+
status: PaymentStatus$inboundSchema(payment.status),
|
|
287
|
+
data: payment
|
|
249
288
|
};
|
|
250
289
|
};
|
|
251
290
|
refundPayment = async (input) => {
|
|
252
291
|
if (this.options.debug) {
|
|
253
292
|
console.info("[PayKit] Refunding payment", input);
|
|
254
293
|
}
|
|
255
|
-
const { id: paymentId } = core.validateRequiredKeys(
|
|
256
|
-
["id"],
|
|
257
|
-
input
|
|
294
|
+
const { id: paymentId, reason = "customer_request" } = core.validateRequiredKeys(
|
|
295
|
+
["id", "reason"],
|
|
296
|
+
input.data ?? {},
|
|
258
297
|
"Missing required fields: {keys}",
|
|
259
298
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
260
299
|
);
|
|
261
|
-
const
|
|
300
|
+
const refund = await this.exec(
|
|
262
301
|
this.paykit.refunds.create({
|
|
263
302
|
payment_id: paymentId,
|
|
264
|
-
amount: Number(input.amount),
|
|
265
|
-
reason
|
|
266
|
-
metadata: input.data?.metadata
|
|
267
|
-
provider_metadata: input.data?.provider_metadata
|
|
268
|
-
})
|
|
303
|
+
amount: Number(input.amount) * this.options.amountToCentsMultiplier,
|
|
304
|
+
reason,
|
|
305
|
+
metadata: input.data?.metadata ?? null,
|
|
306
|
+
provider_metadata: input.data?.provider_metadata ?? void 0
|
|
307
|
+
}),
|
|
308
|
+
"Refund"
|
|
269
309
|
);
|
|
270
|
-
|
|
271
|
-
throw new utils.MedusaError(
|
|
272
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
273
|
-
refundError.message
|
|
274
|
-
);
|
|
275
|
-
return { data: refundResult };
|
|
310
|
+
return { data: refund };
|
|
276
311
|
};
|
|
277
312
|
retrievePayment = async (input) => {
|
|
278
313
|
if (this.options.debug) {
|
|
279
314
|
console.info("[PayKit] Retrieving payment", input);
|
|
280
315
|
}
|
|
281
|
-
const { id } = core.validateRequiredKeys(
|
|
316
|
+
const { id: paymentId } = core.validateRequiredKeys(
|
|
282
317
|
["id"],
|
|
283
|
-
input
|
|
318
|
+
input.data ?? {},
|
|
284
319
|
"Missing required fields: {keys}",
|
|
285
320
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
286
321
|
);
|
|
287
|
-
const
|
|
288
|
-
this.paykit.payments.retrieve(
|
|
322
|
+
const payment = await this.exec(
|
|
323
|
+
this.paykit.payments.retrieve(paymentId),
|
|
324
|
+
"Retrieve"
|
|
289
325
|
);
|
|
290
|
-
|
|
291
|
-
throw new utils.MedusaError(
|
|
292
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
293
|
-
paymentIntentError.message
|
|
294
|
-
);
|
|
295
|
-
return { data: paymentIntentResult };
|
|
326
|
+
return { data: payment };
|
|
296
327
|
};
|
|
297
328
|
updatePayment = async (input) => {
|
|
298
329
|
if (this.options.debug) {
|
|
299
330
|
console.info("[PayKit] Updating payment", input);
|
|
300
331
|
}
|
|
301
|
-
const {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
["id"],
|
|
309
|
-
input.data,
|
|
332
|
+
const {
|
|
333
|
+
amount,
|
|
334
|
+
id: paymentId,
|
|
335
|
+
currency_code: currencyCode
|
|
336
|
+
} = core.validateRequiredKeys(
|
|
337
|
+
["amount", "currency_code", "id"],
|
|
338
|
+
input.data ?? {},
|
|
310
339
|
"Missing required fields: {keys}",
|
|
311
340
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
312
341
|
);
|
|
313
|
-
const
|
|
314
|
-
const [paymentIntentResult, paymentIntentError] = await core.tryCatchAsync(
|
|
342
|
+
const data = await this.exec(
|
|
315
343
|
this.paykit.payments.update(paymentId, {
|
|
316
|
-
amount: Number(amount),
|
|
317
|
-
currency:
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
344
|
+
amount: Number(amount) * this.options.amountToCentsMultiplier,
|
|
345
|
+
currency: currencyCode.toUpperCase(),
|
|
346
|
+
provider_metadata: input.data?.provider_metadata
|
|
347
|
+
}),
|
|
348
|
+
"Update"
|
|
321
349
|
);
|
|
322
|
-
|
|
323
|
-
throw new utils.MedusaError(
|
|
324
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
325
|
-
paymentIntentError.message
|
|
326
|
-
);
|
|
327
|
-
return { data: paymentIntentResult };
|
|
350
|
+
return { data };
|
|
328
351
|
};
|
|
329
352
|
getWebhookActionAndData = async (payload) => {
|
|
330
353
|
if (this.options.debug) {
|
|
331
|
-
console.info(
|
|
354
|
+
console.info(
|
|
355
|
+
"[PayKit] Resolving webhook action and data",
|
|
356
|
+
payload
|
|
357
|
+
);
|
|
332
358
|
}
|
|
333
359
|
const { rawData, headers } = payload;
|
|
334
|
-
const
|
|
360
|
+
const body = Buffer.isBuffer(rawData) ? rawData.toString("utf8") : rawData;
|
|
361
|
+
const headersMap = Object.fromEntries(
|
|
362
|
+
Object.entries(headers).map(([k, v]) => [k, String(v)])
|
|
363
|
+
);
|
|
364
|
+
let result = {
|
|
365
|
+
action: utils.PaymentActions.NOT_SUPPORTED
|
|
366
|
+
};
|
|
367
|
+
const statusMap = {
|
|
368
|
+
pending: utils.PaymentActions.PENDING,
|
|
369
|
+
processing: utils.PaymentActions.PENDING,
|
|
370
|
+
requires_action: utils.PaymentActions.REQUIRES_MORE,
|
|
371
|
+
requires_capture: utils.PaymentActions.AUTHORIZED,
|
|
372
|
+
succeeded: utils.PaymentActions.SUCCESSFUL,
|
|
373
|
+
failed: utils.PaymentActions.FAILED,
|
|
374
|
+
canceled: utils.PaymentActions.CANCELED
|
|
375
|
+
};
|
|
335
376
|
const webhook = this.paykit.webhooks.setup({ webhookSecret: this.options.webhookSecret }).on("payment.created", async (event) => {
|
|
336
|
-
|
|
337
|
-
action:
|
|
377
|
+
result = {
|
|
378
|
+
action: statusMap[event?.data?.status ?? "pending"],
|
|
338
379
|
data: {
|
|
339
380
|
session_id: event.data?.metadata?.session_id,
|
|
340
|
-
amount: event.data?.amount
|
|
381
|
+
amount: event.data?.amount ? event.data?.amount * this.options.amountToCentsMultiplier : 0
|
|
341
382
|
}
|
|
342
383
|
};
|
|
343
384
|
}).on("payment.updated", async (event) => {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
processing: utils.PaymentActions.PENDING,
|
|
347
|
-
requires_action: utils.PaymentActions.REQUIRES_MORE,
|
|
348
|
-
requires_capture: utils.PaymentActions.AUTHORIZED,
|
|
349
|
-
succeeded: utils.PaymentActions.SUCCESSFUL,
|
|
350
|
-
failed: utils.PaymentActions.FAILED,
|
|
351
|
-
canceled: utils.PaymentActions.CANCELED
|
|
352
|
-
};
|
|
353
|
-
return {
|
|
354
|
-
action: event.data?.status ? statusActionMap[event.data.status] : utils.PaymentActions.PENDING,
|
|
385
|
+
result = {
|
|
386
|
+
action: statusMap[event?.data?.status ?? "pending"],
|
|
355
387
|
data: {
|
|
356
388
|
session_id: event.data?.metadata?.session_id,
|
|
357
|
-
amount: event.data?.amount
|
|
389
|
+
amount: event.data?.amount ? event.data?.amount * this.options.amountToCentsMultiplier : 0
|
|
358
390
|
}
|
|
359
391
|
};
|
|
360
|
-
}).on("payment.
|
|
361
|
-
|
|
392
|
+
}).on("payment.failed", async (event) => {
|
|
393
|
+
result = {
|
|
362
394
|
action: utils.PaymentActions.CANCELED,
|
|
363
395
|
data: {
|
|
364
|
-
|
|
365
|
-
|
|
396
|
+
amount: event.data?.amount ? event.data?.amount * this.options.amountToCentsMultiplier : 0,
|
|
397
|
+
session_id: event.data?.metadata?.session_id
|
|
366
398
|
}
|
|
367
399
|
};
|
|
368
400
|
});
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
body: bodyString,
|
|
374
|
-
headers: new Headers(stringifiedHeaders),
|
|
375
|
-
fullUrl: core.getURLFromHeaders(stringifiedHeaders)
|
|
401
|
+
await webhook.handle({
|
|
402
|
+
body,
|
|
403
|
+
headersAsObject: headersMap,
|
|
404
|
+
fullUrl: core.getURLFromHeaders(headersMap)
|
|
376
405
|
});
|
|
377
|
-
return
|
|
406
|
+
return result;
|
|
378
407
|
};
|
|
379
408
|
createAccountHolder = async ({
|
|
380
409
|
context,
|
|
381
410
|
data
|
|
382
411
|
}) => {
|
|
412
|
+
if (context.account_holder?.data?.id) {
|
|
413
|
+
return { id: context.account_holder.data.id };
|
|
414
|
+
}
|
|
383
415
|
if (this.options.debug) {
|
|
384
416
|
console.info("[PayKit] Creating account holder", context, data);
|
|
385
417
|
}
|
|
386
|
-
const {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
if (!
|
|
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) {
|
|
418
|
+
const { fullName } = core.parseCustomerName({
|
|
419
|
+
email: context.customer?.email
|
|
420
|
+
});
|
|
421
|
+
const billingInfo = core.billingSchema.safeParse(data?.billing);
|
|
422
|
+
if (data?.billing && !billingInfo.success) {
|
|
398
423
|
throw new utils.MedusaError(
|
|
399
424
|
utils.MedusaError.Types.INVALID_DATA,
|
|
400
|
-
`Invalid billing information, ${
|
|
425
|
+
`Invalid billing information, ${billingInfo.error.message} `
|
|
401
426
|
);
|
|
402
427
|
}
|
|
403
|
-
const
|
|
428
|
+
const res = await this.exec(
|
|
404
429
|
this.paykit.customers.create({
|
|
405
|
-
email: customer
|
|
406
|
-
name:
|
|
407
|
-
phone: customer
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
})
|
|
430
|
+
email: context.customer?.email,
|
|
431
|
+
name: fullName,
|
|
432
|
+
phone: context.customer?.phone ?? null,
|
|
433
|
+
billing: billingInfo.data ?? null
|
|
434
|
+
}),
|
|
435
|
+
"Create Account Holder",
|
|
436
|
+
{ allowUnsupported: true }
|
|
413
437
|
);
|
|
414
|
-
if (
|
|
415
|
-
throw new utils.MedusaError(
|
|
416
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
417
|
-
accountHolderError.message
|
|
418
|
-
);
|
|
419
|
-
}
|
|
438
|
+
if (!res) return {};
|
|
420
439
|
return {
|
|
421
|
-
id:
|
|
422
|
-
data:
|
|
440
|
+
id: res.id,
|
|
441
|
+
data: res
|
|
423
442
|
};
|
|
424
443
|
};
|
|
425
444
|
updateAccountHolder = async ({
|
|
@@ -437,27 +456,24 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
|
|
|
437
456
|
);
|
|
438
457
|
}
|
|
439
458
|
if (!customer) return {};
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
...data?.
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
},
|
|
451
|
-
|
|
452
|
-
|
|
459
|
+
const res = await this.exec(
|
|
460
|
+
this.paykit.customers.update(account_holder.data.id, {
|
|
461
|
+
...data?.billing != null ? {
|
|
462
|
+
billing: core.billingSchema.safeParse(data.billing).data ?? null
|
|
463
|
+
} : {},
|
|
464
|
+
...data?.email != null ? { email: data.email } : {},
|
|
465
|
+
...data?.name != null ? { name: data.name } : {},
|
|
466
|
+
...data?.phone != null ? { phone: data.phone } : {},
|
|
467
|
+
...data?.metadata != null ? {
|
|
468
|
+
metadata: data.metadata
|
|
469
|
+
} : {},
|
|
470
|
+
...data?.provider_metadata != null ? {
|
|
471
|
+
provider_metadata: data.provider_metadata
|
|
472
|
+
} : {}
|
|
473
|
+
}),
|
|
474
|
+
"Update Account Holder"
|
|
453
475
|
);
|
|
454
|
-
|
|
455
|
-
throw new utils.MedusaError(
|
|
456
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
457
|
-
accountHolderError.message
|
|
458
|
-
);
|
|
459
|
-
}
|
|
460
|
-
return { data: accountHolderResult };
|
|
476
|
+
return { data: res };
|
|
461
477
|
};
|
|
462
478
|
deleteAccountHolder = async ({
|
|
463
479
|
context,
|
|
@@ -473,17 +489,11 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
|
|
|
473
489
|
"Account holder not found in context"
|
|
474
490
|
);
|
|
475
491
|
}
|
|
476
|
-
const
|
|
477
|
-
|
|
478
|
-
|
|
492
|
+
const res = await this.exec(
|
|
493
|
+
this.paykit.customers.delete(account_holder.data.id),
|
|
494
|
+
"Delete Account Holder"
|
|
479
495
|
);
|
|
480
|
-
|
|
481
|
-
throw new utils.MedusaError(
|
|
482
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
483
|
-
accountHolderError.message
|
|
484
|
-
);
|
|
485
|
-
}
|
|
486
|
-
return { data: accountHolderResult };
|
|
496
|
+
return { data: res };
|
|
487
497
|
};
|
|
488
498
|
};
|
|
489
499
|
|