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