@paykit-sdk/medusajs 1.0.11 → 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 +265 -240
- package/dist/index.mjs +266 -241
- package/dist/providers/paykit-provider.d.mts +19 -15
- package/dist/providers/paykit-provider.d.ts +19 -15
- package/dist/providers/paykit-provider.js +265 -240
- package/dist/providers/paykit-provider.mjs +266 -241
- 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,30 +55,49 @@ 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
|
+
);
|
|
77
81
|
}
|
|
78
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
|
+
);
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
79
101
|
initiatePayment = async ({
|
|
80
102
|
context,
|
|
81
103
|
amount,
|
|
@@ -85,325 +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
|
-
if (
|
|
115
|
-
const
|
|
116
|
-
|
|
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(
|
|
117
143
|
this.paykit.customers.create({
|
|
118
144
|
email: customer.email,
|
|
119
|
-
|
|
120
|
-
|
|
145
|
+
name: fullName,
|
|
146
|
+
phone: data?.phone ?? null,
|
|
147
|
+
billing: billingInfo.data ?? null,
|
|
121
148
|
metadata: {
|
|
122
|
-
PAYKIT_METADATA_KEY: JSON.stringify({
|
|
149
|
+
[core.PAYKIT_METADATA_KEY]: JSON.stringify({
|
|
150
|
+
source: "medusa-paykit-adapter"
|
|
151
|
+
})
|
|
123
152
|
}
|
|
124
|
-
})
|
|
153
|
+
}),
|
|
154
|
+
"Customer Creation",
|
|
155
|
+
{ allowUnsupported: true }
|
|
125
156
|
);
|
|
126
|
-
if (
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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;
|
|
157
|
+
if (created) {
|
|
158
|
+
customer = {
|
|
159
|
+
..."id" in created ? { id: created.id } : {},
|
|
160
|
+
..."email" in created ? { email: created.email } : {}
|
|
161
|
+
};
|
|
141
162
|
}
|
|
142
|
-
} else {
|
|
143
|
-
customer = customer;
|
|
144
163
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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"
|
|
148
179
|
);
|
|
149
|
-
if (paymentIntentError) {
|
|
150
|
-
throw new utils.MedusaError(
|
|
151
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
152
|
-
paymentIntentError.message
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
if (paymentIntentResult.requires_action && paymentIntentResult.payment_url) {
|
|
156
|
-
return {
|
|
157
|
-
id: paymentIntentResult.id,
|
|
158
|
-
status: utils.PaymentSessionStatus.REQUIRES_MORE,
|
|
159
|
-
data: {
|
|
160
|
-
payment_url: paymentIntentResult.payment_url
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
180
|
return {
|
|
165
|
-
id:
|
|
166
|
-
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 }
|
|
167
184
|
};
|
|
168
185
|
};
|
|
169
186
|
capturePayment = async (input) => {
|
|
170
187
|
if (this.options.debug) {
|
|
171
188
|
console.info("[PayKit] Capturing payment", input);
|
|
172
189
|
}
|
|
173
|
-
const { id, amount } = core.validateRequiredKeys(
|
|
190
|
+
const { id: paymentId, amount } = core.validateRequiredKeys(
|
|
174
191
|
["id", "amount"],
|
|
175
|
-
input
|
|
192
|
+
input.data ?? {},
|
|
176
193
|
"Missing required fields: {keys}",
|
|
177
194
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
178
195
|
);
|
|
179
|
-
const
|
|
180
|
-
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"
|
|
181
201
|
);
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
paymentIntentError.message
|
|
186
|
-
);
|
|
187
|
-
return { data: paymentIntentResult };
|
|
202
|
+
return {
|
|
203
|
+
data
|
|
204
|
+
};
|
|
188
205
|
};
|
|
189
206
|
authorizePayment = async (input) => {
|
|
190
|
-
|
|
191
|
-
|
|
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
|
+
);
|
|
192
222
|
}
|
|
193
|
-
|
|
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
|
+
};
|
|
194
234
|
};
|
|
195
235
|
cancelPayment = async (input) => {
|
|
196
236
|
if (this.options.debug) {
|
|
197
237
|
console.info("[PayKit] Canceling payment", input);
|
|
198
238
|
}
|
|
199
|
-
const { id } = core.validateRequiredKeys(
|
|
239
|
+
const { id: paymentId } = core.validateRequiredKeys(
|
|
200
240
|
["id"],
|
|
201
|
-
input
|
|
241
|
+
input.data ?? {},
|
|
202
242
|
"Missing required fields: {keys}",
|
|
203
243
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
204
244
|
);
|
|
205
|
-
const
|
|
206
|
-
this.paykit.payments.cancel(
|
|
245
|
+
const data = await this.exec(
|
|
246
|
+
this.paykit.payments.cancel(paymentId),
|
|
247
|
+
"Cancel"
|
|
207
248
|
);
|
|
208
|
-
|
|
209
|
-
throw new utils.MedusaError(
|
|
210
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
211
|
-
paymentIntentError.message
|
|
212
|
-
);
|
|
213
|
-
return { data: paymentIntentResult };
|
|
249
|
+
return { data };
|
|
214
250
|
};
|
|
215
251
|
deletePayment = async (input) => {
|
|
216
|
-
|
|
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 };
|
|
217
268
|
};
|
|
218
269
|
getPaymentStatus = async (input) => {
|
|
219
|
-
const { id } = core.validateRequiredKeys(
|
|
270
|
+
const { id: paymentId } = core.validateRequiredKeys(
|
|
220
271
|
["id"],
|
|
221
|
-
input
|
|
272
|
+
input.data ?? {},
|
|
222
273
|
"Missing required fields: {keys}",
|
|
223
274
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
224
275
|
);
|
|
225
|
-
const
|
|
226
|
-
this.paykit.payments.retrieve(
|
|
276
|
+
const payment = await this.exec(
|
|
277
|
+
this.paykit.payments.retrieve(paymentId),
|
|
278
|
+
"Status"
|
|
227
279
|
);
|
|
228
|
-
if (
|
|
229
|
-
throw new utils.MedusaError(
|
|
230
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
231
|
-
paymentIntentError.message
|
|
232
|
-
);
|
|
233
|
-
if (!paymentIntentResult)
|
|
280
|
+
if (!payment)
|
|
234
281
|
throw new utils.MedusaError(
|
|
235
|
-
utils.MedusaError.Types.
|
|
282
|
+
utils.MedusaError.Types.NOT_FOUND,
|
|
236
283
|
"Payment not found"
|
|
237
284
|
);
|
|
238
285
|
return {
|
|
239
|
-
status:
|
|
240
|
-
data:
|
|
286
|
+
status: PaymentStatus$inboundSchema(payment.status),
|
|
287
|
+
data: payment
|
|
241
288
|
};
|
|
242
289
|
};
|
|
243
290
|
refundPayment = async (input) => {
|
|
244
291
|
if (this.options.debug) {
|
|
245
292
|
console.info("[PayKit] Refunding payment", input);
|
|
246
293
|
}
|
|
247
|
-
const { id: paymentId } = core.validateRequiredKeys(
|
|
248
|
-
["id"],
|
|
249
|
-
input
|
|
294
|
+
const { id: paymentId, reason = "customer_request" } = core.validateRequiredKeys(
|
|
295
|
+
["id", "reason"],
|
|
296
|
+
input.data ?? {},
|
|
250
297
|
"Missing required fields: {keys}",
|
|
251
298
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
252
299
|
);
|
|
253
|
-
const
|
|
300
|
+
const refund = await this.exec(
|
|
254
301
|
this.paykit.refunds.create({
|
|
255
302
|
payment_id: paymentId,
|
|
256
|
-
amount: Number(input.amount),
|
|
257
|
-
reason
|
|
258
|
-
metadata: input.data?.metadata
|
|
259
|
-
provider_metadata: input.data?.provider_metadata
|
|
260
|
-
})
|
|
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"
|
|
261
309
|
);
|
|
262
|
-
|
|
263
|
-
throw new utils.MedusaError(
|
|
264
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
265
|
-
refundError.message
|
|
266
|
-
);
|
|
267
|
-
return { data: refundResult };
|
|
310
|
+
return { data: refund };
|
|
268
311
|
};
|
|
269
312
|
retrievePayment = async (input) => {
|
|
270
313
|
if (this.options.debug) {
|
|
271
314
|
console.info("[PayKit] Retrieving payment", input);
|
|
272
315
|
}
|
|
273
|
-
const { id } = core.validateRequiredKeys(
|
|
316
|
+
const { id: paymentId } = core.validateRequiredKeys(
|
|
274
317
|
["id"],
|
|
275
|
-
input
|
|
318
|
+
input.data ?? {},
|
|
276
319
|
"Missing required fields: {keys}",
|
|
277
320
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
278
321
|
);
|
|
279
|
-
const
|
|
280
|
-
this.paykit.payments.retrieve(
|
|
322
|
+
const payment = await this.exec(
|
|
323
|
+
this.paykit.payments.retrieve(paymentId),
|
|
324
|
+
"Retrieve"
|
|
281
325
|
);
|
|
282
|
-
|
|
283
|
-
throw new utils.MedusaError(
|
|
284
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
285
|
-
paymentIntentError.message
|
|
286
|
-
);
|
|
287
|
-
return { data: paymentIntentResult };
|
|
326
|
+
return { data: payment };
|
|
288
327
|
};
|
|
289
328
|
updatePayment = async (input) => {
|
|
290
329
|
if (this.options.debug) {
|
|
291
330
|
console.info("[PayKit] Updating payment", input);
|
|
292
331
|
}
|
|
293
|
-
const {
|
|
294
|
-
|
|
295
|
-
|
|
332
|
+
const {
|
|
333
|
+
amount,
|
|
334
|
+
id: paymentId,
|
|
335
|
+
currency_code: currencyCode
|
|
336
|
+
} = core.validateRequiredKeys(
|
|
337
|
+
["amount", "currency_code", "id"],
|
|
338
|
+
input.data ?? {},
|
|
296
339
|
"Missing required fields: {keys}",
|
|
297
340
|
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
298
341
|
);
|
|
299
|
-
const
|
|
300
|
-
["id"],
|
|
301
|
-
input.data,
|
|
302
|
-
"Missing required fields: {keys}",
|
|
303
|
-
(message) => new utils.MedusaError(utils.MedusaError.Types.INVALID_DATA, message)
|
|
304
|
-
);
|
|
305
|
-
const metadata = input.data?.metadata ?? {};
|
|
306
|
-
const [paymentIntentResult, paymentIntentError] = await core.tryCatchAsync(
|
|
342
|
+
const data = await this.exec(
|
|
307
343
|
this.paykit.payments.update(paymentId, {
|
|
308
|
-
amount: Number(amount),
|
|
309
|
-
currency:
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
344
|
+
amount: Number(amount) * this.options.amountToCentsMultiplier,
|
|
345
|
+
currency: currencyCode.toUpperCase(),
|
|
346
|
+
provider_metadata: input.data?.provider_metadata
|
|
347
|
+
}),
|
|
348
|
+
"Update"
|
|
313
349
|
);
|
|
314
|
-
|
|
315
|
-
throw new utils.MedusaError(
|
|
316
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
317
|
-
paymentIntentError.message
|
|
318
|
-
);
|
|
319
|
-
return { data: paymentIntentResult };
|
|
350
|
+
return { data };
|
|
320
351
|
};
|
|
321
352
|
getWebhookActionAndData = async (payload) => {
|
|
322
353
|
if (this.options.debug) {
|
|
323
|
-
console.info(
|
|
354
|
+
console.info(
|
|
355
|
+
"[PayKit] Resolving webhook action and data",
|
|
356
|
+
payload
|
|
357
|
+
);
|
|
324
358
|
}
|
|
325
359
|
const { rawData, headers } = payload;
|
|
326
|
-
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
|
+
};
|
|
327
376
|
const webhook = this.paykit.webhooks.setup({ webhookSecret: this.options.webhookSecret }).on("payment.created", async (event) => {
|
|
328
|
-
|
|
329
|
-
action:
|
|
377
|
+
result = {
|
|
378
|
+
action: statusMap[event?.data?.status ?? "pending"],
|
|
330
379
|
data: {
|
|
331
380
|
session_id: event.data?.metadata?.session_id,
|
|
332
|
-
amount: event.data?.amount
|
|
381
|
+
amount: event.data?.amount ? event.data?.amount * this.options.amountToCentsMultiplier : 0
|
|
333
382
|
}
|
|
334
383
|
};
|
|
335
384
|
}).on("payment.updated", async (event) => {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
processing: utils.PaymentActions.PENDING,
|
|
339
|
-
requires_action: utils.PaymentActions.REQUIRES_MORE,
|
|
340
|
-
requires_capture: utils.PaymentActions.AUTHORIZED,
|
|
341
|
-
succeeded: utils.PaymentActions.SUCCESSFUL,
|
|
342
|
-
failed: utils.PaymentActions.FAILED,
|
|
343
|
-
canceled: utils.PaymentActions.CANCELED
|
|
344
|
-
};
|
|
345
|
-
return {
|
|
346
|
-
action: event.data?.status ? statusActionMap[event.data.status] : utils.PaymentActions.PENDING,
|
|
385
|
+
result = {
|
|
386
|
+
action: statusMap[event?.data?.status ?? "pending"],
|
|
347
387
|
data: {
|
|
348
388
|
session_id: event.data?.metadata?.session_id,
|
|
349
|
-
amount: event.data?.amount
|
|
389
|
+
amount: event.data?.amount ? event.data?.amount * this.options.amountToCentsMultiplier : 0
|
|
350
390
|
}
|
|
351
391
|
};
|
|
352
|
-
}).on("payment.
|
|
353
|
-
|
|
392
|
+
}).on("payment.failed", async (event) => {
|
|
393
|
+
result = {
|
|
354
394
|
action: utils.PaymentActions.CANCELED,
|
|
355
395
|
data: {
|
|
356
|
-
|
|
357
|
-
|
|
396
|
+
amount: event.data?.amount ? event.data?.amount * this.options.amountToCentsMultiplier : 0,
|
|
397
|
+
session_id: event.data?.metadata?.session_id
|
|
358
398
|
}
|
|
359
399
|
};
|
|
360
400
|
});
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
body: bodyString,
|
|
366
|
-
headers: new Headers(stringifiedHeaders),
|
|
367
|
-
fullUrl: core.getURLFromHeaders(stringifiedHeaders)
|
|
401
|
+
await webhook.handle({
|
|
402
|
+
body,
|
|
403
|
+
headersAsObject: headersMap,
|
|
404
|
+
fullUrl: core.getURLFromHeaders(headersMap)
|
|
368
405
|
});
|
|
369
|
-
return
|
|
406
|
+
return result;
|
|
370
407
|
};
|
|
371
408
|
createAccountHolder = async ({
|
|
372
409
|
context,
|
|
373
410
|
data
|
|
374
411
|
}) => {
|
|
412
|
+
if (context.account_holder?.data?.id) {
|
|
413
|
+
return { id: context.account_holder.data.id };
|
|
414
|
+
}
|
|
375
415
|
if (this.options.debug) {
|
|
376
416
|
console.info("[PayKit] Creating account holder", context, data);
|
|
377
417
|
}
|
|
378
|
-
const {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
if (!
|
|
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) {
|
|
383
423
|
throw new utils.MedusaError(
|
|
384
424
|
utils.MedusaError.Types.INVALID_DATA,
|
|
385
|
-
|
|
425
|
+
`Invalid billing information, ${billingInfo.error.message} `
|
|
386
426
|
);
|
|
387
427
|
}
|
|
388
|
-
const
|
|
428
|
+
const res = await this.exec(
|
|
389
429
|
this.paykit.customers.create({
|
|
390
|
-
email: customer
|
|
391
|
-
name:
|
|
392
|
-
phone: customer
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
}
|
|
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 }
|
|
397
437
|
);
|
|
398
|
-
if (
|
|
399
|
-
throw new utils.MedusaError(
|
|
400
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
401
|
-
accountHolderError.message
|
|
402
|
-
);
|
|
403
|
-
}
|
|
438
|
+
if (!res) return {};
|
|
404
439
|
return {
|
|
405
|
-
id:
|
|
406
|
-
data:
|
|
440
|
+
id: res.id,
|
|
441
|
+
data: res
|
|
407
442
|
};
|
|
408
443
|
};
|
|
409
444
|
updateAccountHolder = async ({
|
|
@@ -413,36 +448,32 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
|
|
|
413
448
|
if (this.options.debug) {
|
|
414
449
|
console.info("[PayKit] Updating account holder", context, data);
|
|
415
450
|
}
|
|
416
|
-
const { account_holder, customer
|
|
451
|
+
const { account_holder, customer } = context;
|
|
417
452
|
if (!account_holder.data?.id) {
|
|
418
453
|
throw new utils.MedusaError(
|
|
419
454
|
utils.MedusaError.Types.INVALID_DATA,
|
|
420
455
|
"Account holder not found in context"
|
|
421
456
|
);
|
|
422
457
|
}
|
|
423
|
-
if (!customer) {
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
email:
|
|
430
|
-
name:
|
|
431
|
-
phone:
|
|
432
|
-
...data?.metadata
|
|
433
|
-
metadata:
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
458
|
+
if (!customer) return {};
|
|
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"
|
|
438
475
|
);
|
|
439
|
-
|
|
440
|
-
throw new utils.MedusaError(
|
|
441
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
442
|
-
accountHolderError.message
|
|
443
|
-
);
|
|
444
|
-
}
|
|
445
|
-
return { data: accountHolderResult };
|
|
476
|
+
return { data: res };
|
|
446
477
|
};
|
|
447
478
|
deleteAccountHolder = async ({
|
|
448
479
|
context,
|
|
@@ -458,17 +489,11 @@ var PaykitMedusaJSAdapter = class extends utils.AbstractPaymentProvider {
|
|
|
458
489
|
"Account holder not found in context"
|
|
459
490
|
);
|
|
460
491
|
}
|
|
461
|
-
const
|
|
462
|
-
|
|
463
|
-
|
|
492
|
+
const res = await this.exec(
|
|
493
|
+
this.paykit.customers.delete(account_holder.data.id),
|
|
494
|
+
"Delete Account Holder"
|
|
464
495
|
);
|
|
465
|
-
|
|
466
|
-
throw new utils.MedusaError(
|
|
467
|
-
utils.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
|
468
|
-
accountHolderError.message
|
|
469
|
-
);
|
|
470
|
-
}
|
|
471
|
-
return { data: accountHolderResult };
|
|
496
|
+
return { data: res };
|
|
472
497
|
};
|
|
473
498
|
};
|
|
474
499
|
|