@paykit-sdk/paypal 1.0.5 → 1.0.6
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 +8 -3
- package/dist/controllers/subscription.js +2 -2
- package/dist/controllers/subscription.mjs +2 -2
- package/dist/controllers/webhook.js +2 -2
- package/dist/controllers/webhook.mjs +2 -2
- package/dist/index.js +377 -66
- package/dist/index.mjs +379 -68
- package/dist/paypal-provider.js +377 -66
- package/dist/paypal-provider.mjs +379 -68
- package/dist/schema.js +1 -1
- package/dist/schema.mjs +1 -1
- package/dist/utils/mapper.d.mts +9 -3
- package/dist/utils/mapper.d.ts +9 -3
- package/dist/utils/mapper.js +257 -12
- package/dist/utils/mapper.mjs +252 -13
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -90,10 +90,15 @@ export async function POST(request: NextRequest) {
|
|
|
90
90
|
const body = await request.text();
|
|
91
91
|
const headers = Object.fromEntries(request.headers.entries());
|
|
92
92
|
const url = request.url;
|
|
93
|
-
await webhook.handle({ body, headers, fullUrl: url });
|
|
94
93
|
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
try {
|
|
95
|
+
console.log('Webhook handled');
|
|
96
|
+
await webhook.handle({ body, headers, fullUrl: url });
|
|
97
|
+
return NextResponse.json({ success: true });
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.log('Webhook Error', error);
|
|
100
|
+
return NextResponse.json({ success: false });
|
|
101
|
+
}
|
|
97
102
|
}
|
|
98
103
|
```
|
|
99
104
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var paypalServerSdk = require('@paypal/paypal-server-sdk');
|
|
4
|
-
var baseController = require('@paypal/paypal-server-sdk/dist/
|
|
5
|
-
var schema = require('@paypal/paypal-server-sdk/dist/
|
|
4
|
+
var baseController = require('@paypal/paypal-server-sdk/dist/cjs/controllers/baseController');
|
|
5
|
+
var schema = require('@paypal/paypal-server-sdk/dist/cjs/schema');
|
|
6
6
|
|
|
7
7
|
// src/controllers/subscription.ts
|
|
8
8
|
schema.object({
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CustomError } from '@paypal/paypal-server-sdk';
|
|
2
|
-
import { BaseController } from '@paypal/paypal-server-sdk/dist/
|
|
3
|
-
import { object, string, lazy, number, stringEnum, boolean } from '@paypal/paypal-server-sdk/dist/
|
|
2
|
+
import { BaseController } from '@paypal/paypal-server-sdk/dist/cjs/controllers/baseController';
|
|
3
|
+
import { object, string, lazy, number, stringEnum, boolean } from '@paypal/paypal-server-sdk/dist/cjs/schema';
|
|
4
4
|
|
|
5
5
|
// src/controllers/subscription.ts
|
|
6
6
|
object({
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var paypalServerSdk = require('@paypal/paypal-server-sdk');
|
|
4
|
-
var baseController = require('@paypal/paypal-server-sdk/dist/
|
|
5
|
-
var schema = require('@paypal/paypal-server-sdk/dist/
|
|
4
|
+
var baseController = require('@paypal/paypal-server-sdk/dist/cjs/controllers/baseController');
|
|
5
|
+
var schema = require('@paypal/paypal-server-sdk/dist/cjs/schema');
|
|
6
6
|
|
|
7
7
|
// src/controllers/webhook.ts
|
|
8
8
|
schema.object({
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CustomError } from '@paypal/paypal-server-sdk';
|
|
2
|
-
import { BaseController } from '@paypal/paypal-server-sdk/dist/
|
|
3
|
-
import { object, string, lazy, number, stringEnum, boolean } from '@paypal/paypal-server-sdk/dist/
|
|
2
|
+
import { BaseController } from '@paypal/paypal-server-sdk/dist/cjs/controllers/baseController';
|
|
3
|
+
import { object, string, lazy, number, stringEnum, boolean } from '@paypal/paypal-server-sdk/dist/cjs/schema';
|
|
4
4
|
|
|
5
5
|
// src/controllers/webhook.ts
|
|
6
6
|
object({
|
package/dist/index.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
var core = require('@paykit-sdk/core');
|
|
4
4
|
var paypalServerSdk = require('@paypal/paypal-server-sdk');
|
|
5
|
-
var baseController = require('@paypal/paypal-server-sdk/dist/
|
|
6
|
-
var schema = require('@paypal/paypal-server-sdk/dist/
|
|
5
|
+
var baseController = require('@paypal/paypal-server-sdk/dist/cjs/controllers/baseController');
|
|
6
|
+
var schema = require('@paypal/paypal-server-sdk/dist/cjs/schema');
|
|
7
7
|
|
|
8
8
|
var __defProp = Object.defineProperty;
|
|
9
9
|
var __export = (target, all) => {
|
|
@@ -4040,14 +4040,14 @@ var ostring = () => stringType().optional();
|
|
|
4040
4040
|
var onumber = () => numberType().optional();
|
|
4041
4041
|
var oboolean = () => booleanType().optional();
|
|
4042
4042
|
var coerce = {
|
|
4043
|
-
string: (arg) => ZodString.create({ ...arg, coerce: true }),
|
|
4044
|
-
number: (arg) => ZodNumber.create({ ...arg, coerce: true }),
|
|
4045
|
-
boolean: (arg) => ZodBoolean.create({
|
|
4043
|
+
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
4044
|
+
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
4045
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
4046
4046
|
...arg,
|
|
4047
4047
|
coerce: true
|
|
4048
|
-
}),
|
|
4049
|
-
bigint: (arg) => ZodBigInt.create({ ...arg, coerce: true }),
|
|
4050
|
-
date: (arg) => ZodDate.create({ ...arg, coerce: true })
|
|
4048
|
+
})),
|
|
4049
|
+
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
4050
|
+
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4051
4051
|
};
|
|
4052
4052
|
var NEVER = INVALID;
|
|
4053
4053
|
schema.object({
|
|
@@ -4251,12 +4251,18 @@ var WebhookController = class extends baseController.BaseController {
|
|
|
4251
4251
|
return req.callAsJson(verifyWebhookSchema);
|
|
4252
4252
|
}
|
|
4253
4253
|
};
|
|
4254
|
+
var paykitPayee$InboundSchema = (payee) => {
|
|
4255
|
+
let customer = { email: "" };
|
|
4256
|
+
if (typeof payee === "string") customer = payee;
|
|
4257
|
+
else if (payee?.emailAddress) customer = { email: payee?.emailAddress };
|
|
4258
|
+
return customer;
|
|
4259
|
+
};
|
|
4254
4260
|
var paykitRefund$InboundSchema = (refund) => {
|
|
4255
4261
|
return {
|
|
4256
4262
|
id: refund.id,
|
|
4257
4263
|
amount: refund.amount?.value ? parseFloat(refund.amount.value) : 0,
|
|
4258
4264
|
currency: refund.amount?.currencyCode || "USD",
|
|
4259
|
-
metadata:
|
|
4265
|
+
metadata: core.omitInternalMetadata(JSON.parse(refund.customId ?? "{}")),
|
|
4260
4266
|
reason: refund.noteToPayer ?? null
|
|
4261
4267
|
};
|
|
4262
4268
|
};
|
|
@@ -4266,10 +4272,12 @@ var paykitCheckout$InboundSchema = (order) => {
|
|
|
4266
4272
|
payment_url: order.links?.find((l) => l.rel === "approve")?.href || "",
|
|
4267
4273
|
amount: parseFloat(order.purchaseUnits?.[0]?.amount?.value || "0"),
|
|
4268
4274
|
currency: order.purchaseUnits?.[0]?.amount?.currencyCode || "USD",
|
|
4269
|
-
customer: order.payer
|
|
4275
|
+
customer: paykitPayee$InboundSchema(order.payer ?? {}),
|
|
4270
4276
|
session_type: "one_time",
|
|
4271
4277
|
products: [{ id: order.purchaseUnits?.[0]?.items?.[0]?.sku || "", quantity: 1 }],
|
|
4272
|
-
metadata:
|
|
4278
|
+
metadata: core.omitInternalMetadata(
|
|
4279
|
+
JSON.parse(order.purchaseUnits?.[0]?.customId ?? "{}")
|
|
4280
|
+
),
|
|
4273
4281
|
subscription: null
|
|
4274
4282
|
};
|
|
4275
4283
|
};
|
|
@@ -4283,14 +4291,193 @@ var paykitPayment$InboundSchema = (order) => {
|
|
|
4283
4291
|
PAYER_ACTION_REQUIRED: "requires_action"
|
|
4284
4292
|
};
|
|
4285
4293
|
const status = statusMap[order.status ?? statusMap.CREATED];
|
|
4294
|
+
const approveLink = order.links?.find((l) => l.rel === "approve")?.href ?? "";
|
|
4295
|
+
const requiresAction = [
|
|
4296
|
+
paypalServerSdk.OrderStatus.PayerActionRequired,
|
|
4297
|
+
paypalServerSdk.OrderStatus.Created,
|
|
4298
|
+
paypalServerSdk.OrderStatus.Saved
|
|
4299
|
+
].includes(order.status);
|
|
4286
4300
|
return {
|
|
4287
4301
|
id: order.id,
|
|
4288
4302
|
status,
|
|
4303
|
+
customer: paykitPayee$InboundSchema(order.payer ?? {}),
|
|
4289
4304
|
amount: parseFloat(order.purchaseUnits?.[0]?.amount?.value || "0"),
|
|
4290
4305
|
currency: order.purchaseUnits?.[0]?.amount?.currencyCode || "USD",
|
|
4291
|
-
metadata:
|
|
4292
|
-
|
|
4293
|
-
|
|
4306
|
+
metadata: core.omitInternalMetadata(
|
|
4307
|
+
JSON.parse(order.purchaseUnits?.[0]?.customId ?? "{}")
|
|
4308
|
+
),
|
|
4309
|
+
item_id: order.purchaseUnits?.[0]?.items?.[0]?.sku || "",
|
|
4310
|
+
requires_action: requiresAction,
|
|
4311
|
+
payment_url: approveLink
|
|
4312
|
+
};
|
|
4313
|
+
};
|
|
4314
|
+
var paykitPaymentWebhook$InboundSchema = (resource) => {
|
|
4315
|
+
const statusMap = {
|
|
4316
|
+
CREATED: "pending",
|
|
4317
|
+
SAVED: "pending",
|
|
4318
|
+
APPROVED: "requires_capture",
|
|
4319
|
+
VOIDED: "canceled",
|
|
4320
|
+
COMPLETED: "succeeded",
|
|
4321
|
+
PAYER_ACTION_REQUIRED: "requires_action"
|
|
4322
|
+
};
|
|
4323
|
+
const status = statusMap[resource.status ?? "CREATED"] ?? "pending";
|
|
4324
|
+
const payer = resource.payer;
|
|
4325
|
+
let customer = { email: "" };
|
|
4326
|
+
if (payer?.payer_id) {
|
|
4327
|
+
customer = payer.payer_id;
|
|
4328
|
+
} else if (payer?.email_address) {
|
|
4329
|
+
customer = { email: payer.email_address };
|
|
4330
|
+
}
|
|
4331
|
+
const purchaseUnits = resource.purchase_units ?? [];
|
|
4332
|
+
const firstUnit = purchaseUnits[0] ?? {};
|
|
4333
|
+
const amount = firstUnit.amount;
|
|
4334
|
+
const items = firstUnit.items ?? [];
|
|
4335
|
+
const metadata = (() => {
|
|
4336
|
+
try {
|
|
4337
|
+
const customId = firstUnit?.custom_id;
|
|
4338
|
+
return customId ? JSON.parse(customId) : {};
|
|
4339
|
+
} catch {
|
|
4340
|
+
return {};
|
|
4341
|
+
}
|
|
4342
|
+
})();
|
|
4343
|
+
const itemId = items[0]?.sku ?? firstUnit.reference_id ?? "";
|
|
4344
|
+
return {
|
|
4345
|
+
id: resource.id,
|
|
4346
|
+
status,
|
|
4347
|
+
customer,
|
|
4348
|
+
amount: parseFloat(amount?.value ?? "0"),
|
|
4349
|
+
currency: amount?.currency_code ?? "USD",
|
|
4350
|
+
metadata: core.omitInternalMetadata(metadata),
|
|
4351
|
+
item_id: itemId,
|
|
4352
|
+
requires_action: false,
|
|
4353
|
+
payment_url: null
|
|
4354
|
+
};
|
|
4355
|
+
};
|
|
4356
|
+
var paykitPaymentCaptureWebhook$InboundSchema = (capture) => {
|
|
4357
|
+
const statusMap = {
|
|
4358
|
+
COMPLETED: "succeeded",
|
|
4359
|
+
PENDING: "pending",
|
|
4360
|
+
REFUNDED: "succeeded",
|
|
4361
|
+
// Refunded but was successful
|
|
4362
|
+
PARTIALLY_REFUNDED: "succeeded"
|
|
4363
|
+
};
|
|
4364
|
+
const status = statusMap[capture.status ?? "PENDING"] ?? "pending";
|
|
4365
|
+
const supplementaryData = capture.supplementary_data;
|
|
4366
|
+
const relatedIds = supplementaryData?.related_ids;
|
|
4367
|
+
const orderId = relatedIds?.order_id ?? capture.id;
|
|
4368
|
+
const metadata = (() => {
|
|
4369
|
+
try {
|
|
4370
|
+
const customId = capture.custom_id;
|
|
4371
|
+
return customId ? JSON.parse(customId) : {};
|
|
4372
|
+
} catch {
|
|
4373
|
+
return {};
|
|
4374
|
+
}
|
|
4375
|
+
})();
|
|
4376
|
+
const amount = capture.amount;
|
|
4377
|
+
return {
|
|
4378
|
+
id: orderId,
|
|
4379
|
+
status,
|
|
4380
|
+
customer: { email: "" },
|
|
4381
|
+
// Not available in capture event
|
|
4382
|
+
amount: parseFloat(amount?.value ?? "0"),
|
|
4383
|
+
currency: amount?.currency_code ?? "USD",
|
|
4384
|
+
metadata: core.omitInternalMetadata(metadata),
|
|
4385
|
+
item_id: "",
|
|
4386
|
+
// Not available in capture event
|
|
4387
|
+
requires_action: false,
|
|
4388
|
+
payment_url: null
|
|
4389
|
+
};
|
|
4390
|
+
};
|
|
4391
|
+
var paykitRefundWebhook$InboundSchema = (refund) => {
|
|
4392
|
+
const amount = refund.amount;
|
|
4393
|
+
const refundAmount = amount?.total ? Math.abs(parseFloat(amount.total)) : 0;
|
|
4394
|
+
return {
|
|
4395
|
+
id: refund.id,
|
|
4396
|
+
amount: refundAmount,
|
|
4397
|
+
currency: amount?.currency ?? "USD",
|
|
4398
|
+
metadata: core.omitInternalMetadata({}),
|
|
4399
|
+
// No metadata in webhook refund
|
|
4400
|
+
reason: null
|
|
4401
|
+
// No reason in webhook refund
|
|
4402
|
+
};
|
|
4403
|
+
};
|
|
4404
|
+
var paykitSubscriptionWebhook$InboundSchema = (agreement) => {
|
|
4405
|
+
const state = agreement.state ?? "Pending";
|
|
4406
|
+
const statusMap = {
|
|
4407
|
+
Pending: "pending",
|
|
4408
|
+
Active: "active",
|
|
4409
|
+
Suspended: "past_due",
|
|
4410
|
+
Cancelled: "canceled",
|
|
4411
|
+
Expired: "canceled",
|
|
4412
|
+
Completed: "active"
|
|
4413
|
+
};
|
|
4414
|
+
const status = statusMap[state] ?? "pending";
|
|
4415
|
+
const plan = agreement.plan;
|
|
4416
|
+
const paymentDefinitions = plan?.payment_definitions ?? [];
|
|
4417
|
+
const regularDefinition = paymentDefinitions.find((def) => def.type === "REGULAR") ?? paymentDefinitions[0];
|
|
4418
|
+
const frequency = regularDefinition?.frequency ?? "Month";
|
|
4419
|
+
const billingIntervalMap = {
|
|
4420
|
+
Day: "day",
|
|
4421
|
+
Days: "day",
|
|
4422
|
+
Week: "week",
|
|
4423
|
+
Weeks: "week",
|
|
4424
|
+
Month: "month",
|
|
4425
|
+
Months: "month",
|
|
4426
|
+
Year: "year",
|
|
4427
|
+
Years: "year"
|
|
4428
|
+
};
|
|
4429
|
+
const billingInterval = billingIntervalMap[frequency] ?? "month";
|
|
4430
|
+
const startDate = agreement.start_date ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
4431
|
+
const periodStart = new Date(startDate);
|
|
4432
|
+
const periodEnd = (() => {
|
|
4433
|
+
const end = new Date(periodStart);
|
|
4434
|
+
switch (billingInterval) {
|
|
4435
|
+
case "day":
|
|
4436
|
+
end.setDate(end.getDate() + 1);
|
|
4437
|
+
break;
|
|
4438
|
+
case "week":
|
|
4439
|
+
end.setDate(end.getDate() + 7);
|
|
4440
|
+
break;
|
|
4441
|
+
case "month":
|
|
4442
|
+
end.setMonth(end.getMonth() + 1);
|
|
4443
|
+
break;
|
|
4444
|
+
case "year":
|
|
4445
|
+
end.setFullYear(end.getFullYear() + 1);
|
|
4446
|
+
break;
|
|
4447
|
+
default:
|
|
4448
|
+
end.setMonth(end.getMonth() + 1);
|
|
4449
|
+
}
|
|
4450
|
+
return end;
|
|
4451
|
+
})();
|
|
4452
|
+
const payer = agreement.payer;
|
|
4453
|
+
const payerInfo = payer?.payer_info;
|
|
4454
|
+
const email = payerInfo?.email ?? "";
|
|
4455
|
+
const planId = plan?.id ?? agreement.id;
|
|
4456
|
+
const amount = regularDefinition?.amount;
|
|
4457
|
+
const subscriptionAmount = amount?.value ? parseFloat(amount.value) : 0;
|
|
4458
|
+
const currency = plan?.curr_code ?? "USD";
|
|
4459
|
+
const metadata = (() => {
|
|
4460
|
+
try {
|
|
4461
|
+
const description = agreement.description;
|
|
4462
|
+
return description ? JSON.parse(description) : {};
|
|
4463
|
+
} catch {
|
|
4464
|
+
return {};
|
|
4465
|
+
}
|
|
4466
|
+
})();
|
|
4467
|
+
return {
|
|
4468
|
+
id: agreement.id,
|
|
4469
|
+
customer: { email },
|
|
4470
|
+
status,
|
|
4471
|
+
item_id: planId,
|
|
4472
|
+
current_period_start: periodStart,
|
|
4473
|
+
current_period_end: periodEnd,
|
|
4474
|
+
metadata: core.omitInternalMetadata(metadata),
|
|
4475
|
+
billing_interval: billingInterval,
|
|
4476
|
+
amount: subscriptionAmount,
|
|
4477
|
+
currency,
|
|
4478
|
+
custom_fields: null,
|
|
4479
|
+
requires_action: false,
|
|
4480
|
+
payment_url: null
|
|
4294
4481
|
};
|
|
4295
4482
|
};
|
|
4296
4483
|
|
|
@@ -4339,7 +4526,10 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4339
4526
|
* In PayPal, Order IS the checkout
|
|
4340
4527
|
*/
|
|
4341
4528
|
createCheckout = async (params) => {
|
|
4342
|
-
const
|
|
4529
|
+
const { error, data } = core.createCheckoutSchema.safeParse(params);
|
|
4530
|
+
if (error)
|
|
4531
|
+
throw core.ValidationError.fromZodError(error, this.providerName, "createCheckout");
|
|
4532
|
+
const stringifiedMetadata = JSON.stringify(data.metadata);
|
|
4343
4533
|
if (stringifiedMetadata.length > PAYPAL_METADATA_MAX_LENGTH) {
|
|
4344
4534
|
throw new core.ConstraintViolationError("Metadata exceeds maximum length", {
|
|
4345
4535
|
value: stringifiedMetadata.length,
|
|
@@ -4353,57 +4543,81 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4353
4543
|
itemName = "Untitled Item"
|
|
4354
4544
|
} = core.validateRequiredKeys(
|
|
4355
4545
|
["currency", "amount", "itemName"],
|
|
4356
|
-
|
|
4357
|
-
"Missing required parameters: {keys}"
|
|
4546
|
+
data.provider_metadata,
|
|
4547
|
+
"Missing required parameters in provider_metadata: {keys}"
|
|
4358
4548
|
);
|
|
4549
|
+
const totalAmount = parseFloat(amount);
|
|
4550
|
+
const quantity = data.quantity || 1;
|
|
4551
|
+
const unitAmount = (totalAmount / quantity).toFixed(2);
|
|
4359
4552
|
const orderOptionsBody = {
|
|
4360
4553
|
intent: paypalServerSdk.CheckoutPaymentIntent.Capture,
|
|
4361
4554
|
payer: {
|
|
4362
|
-
...typeof
|
|
4363
|
-
...typeof
|
|
4555
|
+
...typeof data.customer === "string" && { payerId: data.customer },
|
|
4556
|
+
...typeof data.customer === "object" && "email" in data.customer && { emailAddress: data.customer.email }
|
|
4364
4557
|
},
|
|
4365
4558
|
purchaseUnits: [
|
|
4366
4559
|
{
|
|
4367
|
-
amount: {
|
|
4560
|
+
amount: {
|
|
4561
|
+
currencyCode: currency,
|
|
4562
|
+
value: amount,
|
|
4563
|
+
breakdown: { itemTotal: { currencyCode: currency, value: amount } }
|
|
4564
|
+
},
|
|
4368
4565
|
customId: stringifiedMetadata,
|
|
4369
4566
|
items: [
|
|
4370
4567
|
{
|
|
4371
|
-
sku:
|
|
4372
|
-
quantity:
|
|
4568
|
+
sku: data.item_id,
|
|
4569
|
+
quantity: quantity.toString(),
|
|
4373
4570
|
name: itemName,
|
|
4374
|
-
unitAmount: { currencyCode: currency, value:
|
|
4571
|
+
unitAmount: { currencyCode: currency, value: unitAmount }
|
|
4375
4572
|
}
|
|
4376
4573
|
]
|
|
4377
4574
|
}
|
|
4378
4575
|
],
|
|
4379
|
-
applicationContext: {
|
|
4380
|
-
|
|
4576
|
+
applicationContext: {
|
|
4577
|
+
userAction: paypalServerSdk.OrderApplicationContextUserAction.PayNow,
|
|
4578
|
+
returnUrl: data.success_url,
|
|
4579
|
+
cancelUrl: data.cancel_url
|
|
4580
|
+
},
|
|
4581
|
+
...data.provider_metadata && { ...data.provider_metadata }
|
|
4381
4582
|
};
|
|
4382
|
-
if (
|
|
4583
|
+
if (data.billing) {
|
|
4383
4584
|
orderOptionsBody.purchaseUnits[0].shipping = {
|
|
4384
|
-
name: { fullName:
|
|
4585
|
+
name: { fullName: data.billing.address.name },
|
|
4385
4586
|
address: {
|
|
4386
|
-
addressLine1:
|
|
4387
|
-
addressLine2:
|
|
4388
|
-
adminArea1:
|
|
4389
|
-
adminArea2:
|
|
4390
|
-
postalCode:
|
|
4391
|
-
countryCode:
|
|
4587
|
+
addressLine1: data.billing.address.line1,
|
|
4588
|
+
addressLine2: data.billing.address.line2,
|
|
4589
|
+
adminArea1: data.billing.address.city,
|
|
4590
|
+
adminArea2: data.billing.address.state,
|
|
4591
|
+
postalCode: data.billing.address.postal_code,
|
|
4592
|
+
countryCode: data.billing.address.country
|
|
4392
4593
|
},
|
|
4393
|
-
...
|
|
4594
|
+
...data.billing.address.phone && {
|
|
4394
4595
|
phoneNumber: {
|
|
4395
|
-
nationalNumber:
|
|
4396
|
-
countryCode:
|
|
4596
|
+
nationalNumber: data.billing.address.phone,
|
|
4597
|
+
countryCode: data.billing.address.country
|
|
4397
4598
|
}
|
|
4398
4599
|
}
|
|
4399
4600
|
};
|
|
4400
4601
|
}
|
|
4401
|
-
|
|
4402
|
-
|
|
4602
|
+
try {
|
|
4603
|
+
const order = await this.ordersController.createOrder({ body: orderOptionsBody });
|
|
4604
|
+
return paykitCheckout$InboundSchema(order.result);
|
|
4605
|
+
} catch (error2) {
|
|
4606
|
+
throw new core.OperationFailedError("createCheckout", this.providerName, {
|
|
4607
|
+
cause: error2 instanceof Error ? error2 : new Error("Unknown error")
|
|
4608
|
+
});
|
|
4609
|
+
}
|
|
4403
4610
|
};
|
|
4404
4611
|
retrieveCheckout = async (id) => {
|
|
4405
|
-
|
|
4406
|
-
|
|
4612
|
+
try {
|
|
4613
|
+
const order = await this.ordersController.getOrder({ id });
|
|
4614
|
+
if (!order.result) throw new core.ResourceNotFoundError("Order", id);
|
|
4615
|
+
return paykitCheckout$InboundSchema(order.result);
|
|
4616
|
+
} catch (error) {
|
|
4617
|
+
throw new core.OperationFailedError("retrieveCheckout", this.providerName, {
|
|
4618
|
+
cause: error instanceof Error ? error : new Error("Unknown error")
|
|
4619
|
+
});
|
|
4620
|
+
}
|
|
4407
4621
|
};
|
|
4408
4622
|
updateCheckout = async (id, params) => {
|
|
4409
4623
|
throw new core.ProviderNotSupportedError("updateCheckout", this.providerName, {
|
|
@@ -4524,8 +4738,14 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4524
4738
|
}
|
|
4525
4739
|
};
|
|
4526
4740
|
}
|
|
4527
|
-
|
|
4528
|
-
|
|
4741
|
+
try {
|
|
4742
|
+
const order = await this.ordersController.createOrder({ body: orderOptionsBody });
|
|
4743
|
+
return paykitPayment$InboundSchema(order.result);
|
|
4744
|
+
} catch (error) {
|
|
4745
|
+
throw new core.OperationFailedError("createPayment", this.providerName, {
|
|
4746
|
+
cause: error instanceof Error ? error : new Error("Unknown error")
|
|
4747
|
+
});
|
|
4748
|
+
}
|
|
4529
4749
|
};
|
|
4530
4750
|
updatePayment = async (id, params) => {
|
|
4531
4751
|
throw new core.ProviderNotSupportedError("updatePayment", this.providerName, {
|
|
@@ -4533,8 +4753,15 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4533
4753
|
});
|
|
4534
4754
|
};
|
|
4535
4755
|
retrievePayment = async (id) => {
|
|
4536
|
-
|
|
4537
|
-
|
|
4756
|
+
try {
|
|
4757
|
+
const order = await this.ordersController.getOrder({ id });
|
|
4758
|
+
if (!order.result) throw new core.ResourceNotFoundError("Order", id);
|
|
4759
|
+
return paykitPayment$InboundSchema(order.result);
|
|
4760
|
+
} catch (error) {
|
|
4761
|
+
throw new core.OperationFailedError("retrievePayment", this.providerName, {
|
|
4762
|
+
cause: error instanceof Error ? error : new Error("Unknown error")
|
|
4763
|
+
});
|
|
4764
|
+
}
|
|
4538
4765
|
};
|
|
4539
4766
|
deletePayment = async (id) => {
|
|
4540
4767
|
throw new core.ProviderNotSupportedError("deletePayment", this.providerName, {
|
|
@@ -4542,8 +4769,15 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4542
4769
|
});
|
|
4543
4770
|
};
|
|
4544
4771
|
capturePayment = async (id) => {
|
|
4545
|
-
|
|
4546
|
-
|
|
4772
|
+
try {
|
|
4773
|
+
const captured = await this.ordersController.captureOrder({ id });
|
|
4774
|
+
if (!captured.result) throw new core.ResourceNotFoundError("Order", id);
|
|
4775
|
+
return paykitPayment$InboundSchema(captured.result);
|
|
4776
|
+
} catch (error) {
|
|
4777
|
+
throw new core.OperationFailedError("capturePayment", this.providerName, {
|
|
4778
|
+
cause: error instanceof Error ? error : new Error("Unknown error")
|
|
4779
|
+
});
|
|
4780
|
+
}
|
|
4547
4781
|
};
|
|
4548
4782
|
cancelPayment = async (id) => {
|
|
4549
4783
|
throw new core.ProviderNotSupportedError("cancelPayment", this.providerName, {
|
|
@@ -4554,18 +4788,26 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4554
4788
|
* Refund management
|
|
4555
4789
|
*/
|
|
4556
4790
|
createRefund = async (params) => {
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4563
|
-
|
|
4564
|
-
|
|
4565
|
-
|
|
4566
|
-
|
|
4567
|
-
|
|
4568
|
-
|
|
4791
|
+
try {
|
|
4792
|
+
const order = await this.ordersController.getOrder({ id: params.payment_id });
|
|
4793
|
+
if (!order.result) throw new core.ResourceNotFoundError("Order", params.payment_id);
|
|
4794
|
+
const captureIds = order.result.purchaseUnits?.[0]?.payments?.captures?.map((c) => c.id) || [];
|
|
4795
|
+
if (captureIds.length === 0) {
|
|
4796
|
+
throw new core.ResourceNotFoundError("Capture", params.payment_id, this.providerName);
|
|
4797
|
+
}
|
|
4798
|
+
const currencyCode = order.result.purchaseUnits?.[0]?.amount?.currencyCode || "USD";
|
|
4799
|
+
const amount = params.amount ? params.amount.toString() : order.result.purchaseUnits?.[0]?.amount?.value || "0";
|
|
4800
|
+
const refund = await this.paymentsController.refundCapturedPayment({
|
|
4801
|
+
captureId: captureIds[0],
|
|
4802
|
+
body: { amount: { currencyCode, value: amount } }
|
|
4803
|
+
});
|
|
4804
|
+
if (!refund.result) throw new core.ResourceNotFoundError("Refund", params.payment_id);
|
|
4805
|
+
return paykitRefund$InboundSchema(refund.result);
|
|
4806
|
+
} catch (error) {
|
|
4807
|
+
throw new core.OperationFailedError("createRefund", this.providerName, {
|
|
4808
|
+
cause: error instanceof Error ? error : new Error("Unknown error")
|
|
4809
|
+
});
|
|
4810
|
+
}
|
|
4569
4811
|
};
|
|
4570
4812
|
/**
|
|
4571
4813
|
* Webhook management
|
|
@@ -4590,8 +4832,9 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4590
4832
|
const eventType = event.event_type;
|
|
4591
4833
|
const webhookHandlers = {
|
|
4592
4834
|
"CHECKOUT.ORDER.APPROVED": async () => {
|
|
4593
|
-
const
|
|
4594
|
-
|
|
4835
|
+
const payment = paykitPaymentWebhook$InboundSchema(
|
|
4836
|
+
event.resource
|
|
4837
|
+
);
|
|
4595
4838
|
return [
|
|
4596
4839
|
core.paykitEvent$InboundSchema({
|
|
4597
4840
|
type: "payment.created",
|
|
@@ -4602,8 +4845,9 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4602
4845
|
];
|
|
4603
4846
|
},
|
|
4604
4847
|
"CHECKOUT.ORDER.COMPLETED": async () => {
|
|
4605
|
-
const
|
|
4606
|
-
|
|
4848
|
+
const payment = paykitPaymentWebhook$InboundSchema(
|
|
4849
|
+
event.resource
|
|
4850
|
+
);
|
|
4607
4851
|
return [
|
|
4608
4852
|
core.paykitEvent$InboundSchema({
|
|
4609
4853
|
type: "payment.updated",
|
|
@@ -4614,8 +4858,9 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4614
4858
|
];
|
|
4615
4859
|
},
|
|
4616
4860
|
"PAYMENT.CAPTURE.COMPLETED": async () => {
|
|
4617
|
-
const
|
|
4618
|
-
|
|
4861
|
+
const payment = paykitPaymentCaptureWebhook$InboundSchema(
|
|
4862
|
+
event.resource
|
|
4863
|
+
);
|
|
4619
4864
|
return [
|
|
4620
4865
|
core.paykitEvent$InboundSchema({
|
|
4621
4866
|
type: "payment.updated",
|
|
@@ -4626,8 +4871,9 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4626
4871
|
];
|
|
4627
4872
|
},
|
|
4628
4873
|
"PAYMENT.CAPTURE.REFUNDED": async () => {
|
|
4629
|
-
const
|
|
4630
|
-
|
|
4874
|
+
const refund = paykitRefundWebhook$InboundSchema(
|
|
4875
|
+
event.resource
|
|
4876
|
+
);
|
|
4631
4877
|
return [
|
|
4632
4878
|
core.paykitEvent$InboundSchema({
|
|
4633
4879
|
type: "refund.created",
|
|
@@ -4636,6 +4882,71 @@ var PayPalProvider = class extends core.AbstractPayKitProvider {
|
|
|
4636
4882
|
data: refund
|
|
4637
4883
|
})
|
|
4638
4884
|
];
|
|
4885
|
+
},
|
|
4886
|
+
"BILLING.SUBSCRIPTION.CREATED": async () => {
|
|
4887
|
+
const subscription = paykitSubscriptionWebhook$InboundSchema(
|
|
4888
|
+
event.resource
|
|
4889
|
+
);
|
|
4890
|
+
return [
|
|
4891
|
+
core.paykitEvent$InboundSchema({
|
|
4892
|
+
type: "subscription.created",
|
|
4893
|
+
created: Date.now() / 1e3,
|
|
4894
|
+
id: event.id,
|
|
4895
|
+
data: subscription
|
|
4896
|
+
})
|
|
4897
|
+
];
|
|
4898
|
+
},
|
|
4899
|
+
"BILLING.SUBSCRIPTION.UPDATED": async () => {
|
|
4900
|
+
const subscription = paykitSubscriptionWebhook$InboundSchema(
|
|
4901
|
+
event.resource
|
|
4902
|
+
);
|
|
4903
|
+
return [
|
|
4904
|
+
core.paykitEvent$InboundSchema({
|
|
4905
|
+
type: "subscription.updated",
|
|
4906
|
+
created: Date.now() / 1e3,
|
|
4907
|
+
id: event.id,
|
|
4908
|
+
data: subscription
|
|
4909
|
+
})
|
|
4910
|
+
];
|
|
4911
|
+
},
|
|
4912
|
+
"BILLING.SUBSCRIPTION.SUSPENDED": async () => {
|
|
4913
|
+
const subscription = paykitSubscriptionWebhook$InboundSchema(
|
|
4914
|
+
event.resource
|
|
4915
|
+
);
|
|
4916
|
+
return [
|
|
4917
|
+
core.paykitEvent$InboundSchema({
|
|
4918
|
+
type: "subscription.updated",
|
|
4919
|
+
created: Date.now() / 1e3,
|
|
4920
|
+
id: event.id,
|
|
4921
|
+
data: subscription
|
|
4922
|
+
})
|
|
4923
|
+
];
|
|
4924
|
+
},
|
|
4925
|
+
"BILLING.SUBSCRIPTION.CANCELLED": async () => {
|
|
4926
|
+
const subscription = paykitSubscriptionWebhook$InboundSchema(
|
|
4927
|
+
event.resource
|
|
4928
|
+
);
|
|
4929
|
+
return [
|
|
4930
|
+
core.paykitEvent$InboundSchema({
|
|
4931
|
+
type: "subscription.canceled",
|
|
4932
|
+
created: Date.now() / 1e3,
|
|
4933
|
+
id: event.id,
|
|
4934
|
+
data: subscription
|
|
4935
|
+
})
|
|
4936
|
+
];
|
|
4937
|
+
},
|
|
4938
|
+
"BILLING.SUBSCRIPTION.EXPIRED": async () => {
|
|
4939
|
+
const subscription = paykitSubscriptionWebhook$InboundSchema(
|
|
4940
|
+
event.resource
|
|
4941
|
+
);
|
|
4942
|
+
return [
|
|
4943
|
+
core.paykitEvent$InboundSchema({
|
|
4944
|
+
type: "subscription.canceled",
|
|
4945
|
+
created: Date.now() / 1e3,
|
|
4946
|
+
id: event.id,
|
|
4947
|
+
data: subscription
|
|
4948
|
+
})
|
|
4949
|
+
];
|
|
4639
4950
|
}
|
|
4640
4951
|
};
|
|
4641
4952
|
const handler = webhookHandlers[eventType];
|