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