@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/dist/utils/mapper.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var core = require('@paykit-sdk/core');
|
|
4
|
+
var paypalServerSdk = require('@paypal/paypal-server-sdk');
|
|
4
5
|
|
|
5
6
|
// src/utils/mapper.ts
|
|
7
|
+
var paykitPayee$InboundSchema = (payee) => {
|
|
8
|
+
let customer = { email: "" };
|
|
9
|
+
if (typeof payee === "string") customer = payee;
|
|
10
|
+
else if (payee?.emailAddress) customer = { email: payee?.emailAddress };
|
|
11
|
+
return customer;
|
|
12
|
+
};
|
|
6
13
|
var paykitRefund$InboundSchema = (refund) => {
|
|
7
14
|
return {
|
|
8
15
|
id: refund.id,
|
|
9
16
|
amount: refund.amount?.value ? parseFloat(refund.amount.value) : 0,
|
|
10
17
|
currency: refund.amount?.currencyCode || "USD",
|
|
11
|
-
metadata:
|
|
18
|
+
metadata: core.omitInternalMetadata(JSON.parse(refund.customId ?? "{}")),
|
|
12
19
|
reason: refund.noteToPayer ?? null
|
|
13
20
|
};
|
|
14
21
|
};
|
|
@@ -18,10 +25,12 @@ var paykitCheckout$InboundSchema = (order) => {
|
|
|
18
25
|
payment_url: order.links?.find((l) => l.rel === "approve")?.href || "",
|
|
19
26
|
amount: parseFloat(order.purchaseUnits?.[0]?.amount?.value || "0"),
|
|
20
27
|
currency: order.purchaseUnits?.[0]?.amount?.currencyCode || "USD",
|
|
21
|
-
customer: order.payer
|
|
28
|
+
customer: paykitPayee$InboundSchema(order.payer ?? {}),
|
|
22
29
|
session_type: "one_time",
|
|
23
30
|
products: [{ id: order.purchaseUnits?.[0]?.items?.[0]?.sku || "", quantity: 1 }],
|
|
24
|
-
metadata:
|
|
31
|
+
metadata: core.omitInternalMetadata(
|
|
32
|
+
JSON.parse(order.purchaseUnits?.[0]?.customId ?? "{}")
|
|
33
|
+
),
|
|
25
34
|
subscription: null
|
|
26
35
|
};
|
|
27
36
|
};
|
|
@@ -35,14 +44,24 @@ var paykitPayment$InboundSchema = (order) => {
|
|
|
35
44
|
PAYER_ACTION_REQUIRED: "requires_action"
|
|
36
45
|
};
|
|
37
46
|
const status = statusMap[order.status ?? statusMap.CREATED];
|
|
47
|
+
const approveLink = order.links?.find((l) => l.rel === "approve")?.href ?? "";
|
|
48
|
+
const requiresAction = [
|
|
49
|
+
paypalServerSdk.OrderStatus.PayerActionRequired,
|
|
50
|
+
paypalServerSdk.OrderStatus.Created,
|
|
51
|
+
paypalServerSdk.OrderStatus.Saved
|
|
52
|
+
].includes(order.status);
|
|
38
53
|
return {
|
|
39
54
|
id: order.id,
|
|
40
55
|
status,
|
|
56
|
+
customer: paykitPayee$InboundSchema(order.payer ?? {}),
|
|
41
57
|
amount: parseFloat(order.purchaseUnits?.[0]?.amount?.value || "0"),
|
|
42
58
|
currency: order.purchaseUnits?.[0]?.amount?.currencyCode || "USD",
|
|
43
|
-
metadata:
|
|
44
|
-
|
|
45
|
-
|
|
59
|
+
metadata: core.omitInternalMetadata(
|
|
60
|
+
JSON.parse(order.purchaseUnits?.[0]?.customId ?? "{}")
|
|
61
|
+
),
|
|
62
|
+
item_id: order.purchaseUnits?.[0]?.items?.[0]?.sku || "",
|
|
63
|
+
requires_action: requiresAction,
|
|
64
|
+
payment_url: approveLink
|
|
46
65
|
};
|
|
47
66
|
};
|
|
48
67
|
var paykitSubscription$InboundSchema = (subscription) => {
|
|
@@ -55,23 +74,249 @@ var paykitSubscription$InboundSchema = (subscription) => {
|
|
|
55
74
|
EXPIRED: "canceled"
|
|
56
75
|
};
|
|
57
76
|
const status = statusMap[subscription.status ?? statusMap.APPROVAL_PENDING];
|
|
77
|
+
const tenureType = subscription.billing_info?.cycle_executions?.[0]?.tenure_type?.toUpperCase();
|
|
78
|
+
const billingIntervalMap = {
|
|
79
|
+
DAY: "day",
|
|
80
|
+
WEEK: "week",
|
|
81
|
+
MONTH: "month",
|
|
82
|
+
YEAR: "year"
|
|
83
|
+
};
|
|
84
|
+
const billingInterval = tenureType && billingIntervalMap[tenureType] ? billingIntervalMap[tenureType] : "month";
|
|
85
|
+
const periodStart = subscription.start_time ? new Date(subscription.start_time) : /* @__PURE__ */ new Date();
|
|
86
|
+
const periodEnd = (() => {
|
|
87
|
+
if (!subscription.start_time) {
|
|
88
|
+
return /* @__PURE__ */ new Date();
|
|
89
|
+
}
|
|
90
|
+
const start = new Date(subscription.start_time);
|
|
91
|
+
const end = new Date(start);
|
|
92
|
+
switch (billingInterval) {
|
|
93
|
+
case "day":
|
|
94
|
+
end.setDate(end.getDate() + 1);
|
|
95
|
+
break;
|
|
96
|
+
case "week":
|
|
97
|
+
end.setDate(end.getDate() + 7);
|
|
98
|
+
break;
|
|
99
|
+
case "month":
|
|
100
|
+
end.setMonth(end.getMonth() + 1);
|
|
101
|
+
break;
|
|
102
|
+
case "year":
|
|
103
|
+
end.setFullYear(end.getFullYear() + 1);
|
|
104
|
+
break;
|
|
105
|
+
default:
|
|
106
|
+
end.setMonth(end.getMonth() + 1);
|
|
107
|
+
}
|
|
108
|
+
return end;
|
|
109
|
+
})();
|
|
58
110
|
return {
|
|
59
111
|
id: subscription.id,
|
|
60
112
|
customer: { email: subscription.subscriber?.email_address ?? "" },
|
|
61
113
|
status,
|
|
62
114
|
item_id: subscription.plan_id,
|
|
63
|
-
current_period_start:
|
|
64
|
-
current_period_end:
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
billing_interval: "month",
|
|
115
|
+
current_period_start: periodStart,
|
|
116
|
+
current_period_end: periodEnd,
|
|
117
|
+
metadata: core.omitInternalMetadata(JSON.parse(subscription?.customId ?? "{}")),
|
|
118
|
+
billing_interval: billingInterval,
|
|
68
119
|
amount: 0,
|
|
69
120
|
currency: "USD",
|
|
70
|
-
custom_fields: null
|
|
121
|
+
custom_fields: null,
|
|
122
|
+
requires_action: false,
|
|
123
|
+
payment_url: null
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
var paykitInvoice$InboundSchema = (order) => {
|
|
127
|
+
return {
|
|
128
|
+
id: order.id,
|
|
129
|
+
amount_paid: parseFloat(order.purchaseUnits?.[0]?.amount?.value || "0"),
|
|
130
|
+
currency: order.purchaseUnits?.[0]?.amount?.currencyCode || "USD",
|
|
131
|
+
customer: paykitPayee$InboundSchema(order.payer ?? {}),
|
|
132
|
+
status: "paid",
|
|
133
|
+
paid_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
134
|
+
metadata: core.omitInternalMetadata(
|
|
135
|
+
JSON.parse(order.purchaseUnits?.[0]?.customId ?? "{}")
|
|
136
|
+
),
|
|
137
|
+
custom_fields: null,
|
|
138
|
+
subscription_id: null,
|
|
139
|
+
billing_mode: "one_time",
|
|
140
|
+
line_items: [{ id: order.purchaseUnits?.[0]?.items?.[0]?.sku || "", quantity: 1 }]
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
var paykitPaymentWebhook$InboundSchema = (resource) => {
|
|
144
|
+
const statusMap = {
|
|
145
|
+
CREATED: "pending",
|
|
146
|
+
SAVED: "pending",
|
|
147
|
+
APPROVED: "requires_capture",
|
|
148
|
+
VOIDED: "canceled",
|
|
149
|
+
COMPLETED: "succeeded",
|
|
150
|
+
PAYER_ACTION_REQUIRED: "requires_action"
|
|
151
|
+
};
|
|
152
|
+
const status = statusMap[resource.status ?? "CREATED"] ?? "pending";
|
|
153
|
+
const payer = resource.payer;
|
|
154
|
+
let customer = { email: "" };
|
|
155
|
+
if (payer?.payer_id) {
|
|
156
|
+
customer = payer.payer_id;
|
|
157
|
+
} else if (payer?.email_address) {
|
|
158
|
+
customer = { email: payer.email_address };
|
|
159
|
+
}
|
|
160
|
+
const purchaseUnits = resource.purchase_units ?? [];
|
|
161
|
+
const firstUnit = purchaseUnits[0] ?? {};
|
|
162
|
+
const amount = firstUnit.amount;
|
|
163
|
+
const items = firstUnit.items ?? [];
|
|
164
|
+
const metadata = (() => {
|
|
165
|
+
try {
|
|
166
|
+
const customId = firstUnit?.custom_id;
|
|
167
|
+
return customId ? JSON.parse(customId) : {};
|
|
168
|
+
} catch {
|
|
169
|
+
return {};
|
|
170
|
+
}
|
|
171
|
+
})();
|
|
172
|
+
const itemId = items[0]?.sku ?? firstUnit.reference_id ?? "";
|
|
173
|
+
return {
|
|
174
|
+
id: resource.id,
|
|
175
|
+
status,
|
|
176
|
+
customer,
|
|
177
|
+
amount: parseFloat(amount?.value ?? "0"),
|
|
178
|
+
currency: amount?.currency_code ?? "USD",
|
|
179
|
+
metadata: core.omitInternalMetadata(metadata),
|
|
180
|
+
item_id: itemId,
|
|
181
|
+
requires_action: false,
|
|
182
|
+
payment_url: null
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
var paykitPaymentCaptureWebhook$InboundSchema = (capture) => {
|
|
186
|
+
const statusMap = {
|
|
187
|
+
COMPLETED: "succeeded",
|
|
188
|
+
PENDING: "pending",
|
|
189
|
+
REFUNDED: "succeeded",
|
|
190
|
+
// Refunded but was successful
|
|
191
|
+
PARTIALLY_REFUNDED: "succeeded"
|
|
192
|
+
};
|
|
193
|
+
const status = statusMap[capture.status ?? "PENDING"] ?? "pending";
|
|
194
|
+
const supplementaryData = capture.supplementary_data;
|
|
195
|
+
const relatedIds = supplementaryData?.related_ids;
|
|
196
|
+
const orderId = relatedIds?.order_id ?? capture.id;
|
|
197
|
+
const metadata = (() => {
|
|
198
|
+
try {
|
|
199
|
+
const customId = capture.custom_id;
|
|
200
|
+
return customId ? JSON.parse(customId) : {};
|
|
201
|
+
} catch {
|
|
202
|
+
return {};
|
|
203
|
+
}
|
|
204
|
+
})();
|
|
205
|
+
const amount = capture.amount;
|
|
206
|
+
return {
|
|
207
|
+
id: orderId,
|
|
208
|
+
status,
|
|
209
|
+
customer: { email: "" },
|
|
210
|
+
// Not available in capture event
|
|
211
|
+
amount: parseFloat(amount?.value ?? "0"),
|
|
212
|
+
currency: amount?.currency_code ?? "USD",
|
|
213
|
+
metadata: core.omitInternalMetadata(metadata),
|
|
214
|
+
item_id: "",
|
|
215
|
+
// Not available in capture event
|
|
216
|
+
requires_action: false,
|
|
217
|
+
payment_url: null
|
|
218
|
+
};
|
|
219
|
+
};
|
|
220
|
+
var paykitRefundWebhook$InboundSchema = (refund) => {
|
|
221
|
+
const amount = refund.amount;
|
|
222
|
+
const refundAmount = amount?.total ? Math.abs(parseFloat(amount.total)) : 0;
|
|
223
|
+
return {
|
|
224
|
+
id: refund.id,
|
|
225
|
+
amount: refundAmount,
|
|
226
|
+
currency: amount?.currency ?? "USD",
|
|
227
|
+
metadata: core.omitInternalMetadata({}),
|
|
228
|
+
// No metadata in webhook refund
|
|
229
|
+
reason: null
|
|
230
|
+
// No reason in webhook refund
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
var paykitSubscriptionWebhook$InboundSchema = (agreement) => {
|
|
234
|
+
const state = agreement.state ?? "Pending";
|
|
235
|
+
const statusMap = {
|
|
236
|
+
Pending: "pending",
|
|
237
|
+
Active: "active",
|
|
238
|
+
Suspended: "past_due",
|
|
239
|
+
Cancelled: "canceled",
|
|
240
|
+
Expired: "canceled",
|
|
241
|
+
Completed: "active"
|
|
242
|
+
};
|
|
243
|
+
const status = statusMap[state] ?? "pending";
|
|
244
|
+
const plan = agreement.plan;
|
|
245
|
+
const paymentDefinitions = plan?.payment_definitions ?? [];
|
|
246
|
+
const regularDefinition = paymentDefinitions.find((def) => def.type === "REGULAR") ?? paymentDefinitions[0];
|
|
247
|
+
const frequency = regularDefinition?.frequency ?? "Month";
|
|
248
|
+
const billingIntervalMap = {
|
|
249
|
+
Day: "day",
|
|
250
|
+
Days: "day",
|
|
251
|
+
Week: "week",
|
|
252
|
+
Weeks: "week",
|
|
253
|
+
Month: "month",
|
|
254
|
+
Months: "month",
|
|
255
|
+
Year: "year",
|
|
256
|
+
Years: "year"
|
|
257
|
+
};
|
|
258
|
+
const billingInterval = billingIntervalMap[frequency] ?? "month";
|
|
259
|
+
const startDate = agreement.start_date ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
260
|
+
const periodStart = new Date(startDate);
|
|
261
|
+
const periodEnd = (() => {
|
|
262
|
+
const end = new Date(periodStart);
|
|
263
|
+
switch (billingInterval) {
|
|
264
|
+
case "day":
|
|
265
|
+
end.setDate(end.getDate() + 1);
|
|
266
|
+
break;
|
|
267
|
+
case "week":
|
|
268
|
+
end.setDate(end.getDate() + 7);
|
|
269
|
+
break;
|
|
270
|
+
case "month":
|
|
271
|
+
end.setMonth(end.getMonth() + 1);
|
|
272
|
+
break;
|
|
273
|
+
case "year":
|
|
274
|
+
end.setFullYear(end.getFullYear() + 1);
|
|
275
|
+
break;
|
|
276
|
+
default:
|
|
277
|
+
end.setMonth(end.getMonth() + 1);
|
|
278
|
+
}
|
|
279
|
+
return end;
|
|
280
|
+
})();
|
|
281
|
+
const payer = agreement.payer;
|
|
282
|
+
const payerInfo = payer?.payer_info;
|
|
283
|
+
const email = payerInfo?.email ?? "";
|
|
284
|
+
const planId = plan?.id ?? agreement.id;
|
|
285
|
+
const amount = regularDefinition?.amount;
|
|
286
|
+
const subscriptionAmount = amount?.value ? parseFloat(amount.value) : 0;
|
|
287
|
+
const currency = plan?.curr_code ?? "USD";
|
|
288
|
+
const metadata = (() => {
|
|
289
|
+
try {
|
|
290
|
+
const description = agreement.description;
|
|
291
|
+
return description ? JSON.parse(description) : {};
|
|
292
|
+
} catch {
|
|
293
|
+
return {};
|
|
294
|
+
}
|
|
295
|
+
})();
|
|
296
|
+
return {
|
|
297
|
+
id: agreement.id,
|
|
298
|
+
customer: { email },
|
|
299
|
+
status,
|
|
300
|
+
item_id: planId,
|
|
301
|
+
current_period_start: periodStart,
|
|
302
|
+
current_period_end: periodEnd,
|
|
303
|
+
metadata: core.omitInternalMetadata(metadata),
|
|
304
|
+
billing_interval: billingInterval,
|
|
305
|
+
amount: subscriptionAmount,
|
|
306
|
+
currency,
|
|
307
|
+
custom_fields: null,
|
|
308
|
+
requires_action: false,
|
|
309
|
+
payment_url: null
|
|
71
310
|
};
|
|
72
311
|
};
|
|
73
312
|
|
|
74
313
|
exports.paykitCheckout$InboundSchema = paykitCheckout$InboundSchema;
|
|
314
|
+
exports.paykitInvoice$InboundSchema = paykitInvoice$InboundSchema;
|
|
315
|
+
exports.paykitPayee$InboundSchema = paykitPayee$InboundSchema;
|
|
75
316
|
exports.paykitPayment$InboundSchema = paykitPayment$InboundSchema;
|
|
317
|
+
exports.paykitPaymentCaptureWebhook$InboundSchema = paykitPaymentCaptureWebhook$InboundSchema;
|
|
318
|
+
exports.paykitPaymentWebhook$InboundSchema = paykitPaymentWebhook$InboundSchema;
|
|
76
319
|
exports.paykitRefund$InboundSchema = paykitRefund$InboundSchema;
|
|
320
|
+
exports.paykitRefundWebhook$InboundSchema = paykitRefundWebhook$InboundSchema;
|
|
77
321
|
exports.paykitSubscription$InboundSchema = paykitSubscription$InboundSchema;
|
|
322
|
+
exports.paykitSubscriptionWebhook$InboundSchema = paykitSubscriptionWebhook$InboundSchema;
|
package/dist/utils/mapper.mjs
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { omitInternalMetadata } from '@paykit-sdk/core';
|
|
2
|
+
import { OrderStatus } from '@paypal/paypal-server-sdk';
|
|
2
3
|
|
|
3
4
|
// src/utils/mapper.ts
|
|
5
|
+
var paykitPayee$InboundSchema = (payee) => {
|
|
6
|
+
let customer = { email: "" };
|
|
7
|
+
if (typeof payee === "string") customer = payee;
|
|
8
|
+
else if (payee?.emailAddress) customer = { email: payee?.emailAddress };
|
|
9
|
+
return customer;
|
|
10
|
+
};
|
|
4
11
|
var paykitRefund$InboundSchema = (refund) => {
|
|
5
12
|
return {
|
|
6
13
|
id: refund.id,
|
|
7
14
|
amount: refund.amount?.value ? parseFloat(refund.amount.value) : 0,
|
|
8
15
|
currency: refund.amount?.currencyCode || "USD",
|
|
9
|
-
metadata:
|
|
16
|
+
metadata: omitInternalMetadata(JSON.parse(refund.customId ?? "{}")),
|
|
10
17
|
reason: refund.noteToPayer ?? null
|
|
11
18
|
};
|
|
12
19
|
};
|
|
@@ -16,10 +23,12 @@ var paykitCheckout$InboundSchema = (order) => {
|
|
|
16
23
|
payment_url: order.links?.find((l) => l.rel === "approve")?.href || "",
|
|
17
24
|
amount: parseFloat(order.purchaseUnits?.[0]?.amount?.value || "0"),
|
|
18
25
|
currency: order.purchaseUnits?.[0]?.amount?.currencyCode || "USD",
|
|
19
|
-
customer: order.payer
|
|
26
|
+
customer: paykitPayee$InboundSchema(order.payer ?? {}),
|
|
20
27
|
session_type: "one_time",
|
|
21
28
|
products: [{ id: order.purchaseUnits?.[0]?.items?.[0]?.sku || "", quantity: 1 }],
|
|
22
|
-
metadata:
|
|
29
|
+
metadata: omitInternalMetadata(
|
|
30
|
+
JSON.parse(order.purchaseUnits?.[0]?.customId ?? "{}")
|
|
31
|
+
),
|
|
23
32
|
subscription: null
|
|
24
33
|
};
|
|
25
34
|
};
|
|
@@ -33,14 +42,24 @@ var paykitPayment$InboundSchema = (order) => {
|
|
|
33
42
|
PAYER_ACTION_REQUIRED: "requires_action"
|
|
34
43
|
};
|
|
35
44
|
const status = statusMap[order.status ?? statusMap.CREATED];
|
|
45
|
+
const approveLink = order.links?.find((l) => l.rel === "approve")?.href ?? "";
|
|
46
|
+
const requiresAction = [
|
|
47
|
+
OrderStatus.PayerActionRequired,
|
|
48
|
+
OrderStatus.Created,
|
|
49
|
+
OrderStatus.Saved
|
|
50
|
+
].includes(order.status);
|
|
36
51
|
return {
|
|
37
52
|
id: order.id,
|
|
38
53
|
status,
|
|
54
|
+
customer: paykitPayee$InboundSchema(order.payer ?? {}),
|
|
39
55
|
amount: parseFloat(order.purchaseUnits?.[0]?.amount?.value || "0"),
|
|
40
56
|
currency: order.purchaseUnits?.[0]?.amount?.currencyCode || "USD",
|
|
41
|
-
metadata:
|
|
42
|
-
|
|
43
|
-
|
|
57
|
+
metadata: omitInternalMetadata(
|
|
58
|
+
JSON.parse(order.purchaseUnits?.[0]?.customId ?? "{}")
|
|
59
|
+
),
|
|
60
|
+
item_id: order.purchaseUnits?.[0]?.items?.[0]?.sku || "",
|
|
61
|
+
requires_action: requiresAction,
|
|
62
|
+
payment_url: approveLink
|
|
44
63
|
};
|
|
45
64
|
};
|
|
46
65
|
var paykitSubscription$InboundSchema = (subscription) => {
|
|
@@ -53,20 +72,240 @@ var paykitSubscription$InboundSchema = (subscription) => {
|
|
|
53
72
|
EXPIRED: "canceled"
|
|
54
73
|
};
|
|
55
74
|
const status = statusMap[subscription.status ?? statusMap.APPROVAL_PENDING];
|
|
75
|
+
const tenureType = subscription.billing_info?.cycle_executions?.[0]?.tenure_type?.toUpperCase();
|
|
76
|
+
const billingIntervalMap = {
|
|
77
|
+
DAY: "day",
|
|
78
|
+
WEEK: "week",
|
|
79
|
+
MONTH: "month",
|
|
80
|
+
YEAR: "year"
|
|
81
|
+
};
|
|
82
|
+
const billingInterval = tenureType && billingIntervalMap[tenureType] ? billingIntervalMap[tenureType] : "month";
|
|
83
|
+
const periodStart = subscription.start_time ? new Date(subscription.start_time) : /* @__PURE__ */ new Date();
|
|
84
|
+
const periodEnd = (() => {
|
|
85
|
+
if (!subscription.start_time) {
|
|
86
|
+
return /* @__PURE__ */ new Date();
|
|
87
|
+
}
|
|
88
|
+
const start = new Date(subscription.start_time);
|
|
89
|
+
const end = new Date(start);
|
|
90
|
+
switch (billingInterval) {
|
|
91
|
+
case "day":
|
|
92
|
+
end.setDate(end.getDate() + 1);
|
|
93
|
+
break;
|
|
94
|
+
case "week":
|
|
95
|
+
end.setDate(end.getDate() + 7);
|
|
96
|
+
break;
|
|
97
|
+
case "month":
|
|
98
|
+
end.setMonth(end.getMonth() + 1);
|
|
99
|
+
break;
|
|
100
|
+
case "year":
|
|
101
|
+
end.setFullYear(end.getFullYear() + 1);
|
|
102
|
+
break;
|
|
103
|
+
default:
|
|
104
|
+
end.setMonth(end.getMonth() + 1);
|
|
105
|
+
}
|
|
106
|
+
return end;
|
|
107
|
+
})();
|
|
56
108
|
return {
|
|
57
109
|
id: subscription.id,
|
|
58
110
|
customer: { email: subscription.subscriber?.email_address ?? "" },
|
|
59
111
|
status,
|
|
60
112
|
item_id: subscription.plan_id,
|
|
61
|
-
current_period_start:
|
|
62
|
-
current_period_end:
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
billing_interval: "month",
|
|
113
|
+
current_period_start: periodStart,
|
|
114
|
+
current_period_end: periodEnd,
|
|
115
|
+
metadata: omitInternalMetadata(JSON.parse(subscription?.customId ?? "{}")),
|
|
116
|
+
billing_interval: billingInterval,
|
|
66
117
|
amount: 0,
|
|
67
118
|
currency: "USD",
|
|
68
|
-
custom_fields: null
|
|
119
|
+
custom_fields: null,
|
|
120
|
+
requires_action: false,
|
|
121
|
+
payment_url: null
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
var paykitInvoice$InboundSchema = (order) => {
|
|
125
|
+
return {
|
|
126
|
+
id: order.id,
|
|
127
|
+
amount_paid: parseFloat(order.purchaseUnits?.[0]?.amount?.value || "0"),
|
|
128
|
+
currency: order.purchaseUnits?.[0]?.amount?.currencyCode || "USD",
|
|
129
|
+
customer: paykitPayee$InboundSchema(order.payer ?? {}),
|
|
130
|
+
status: "paid",
|
|
131
|
+
paid_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
132
|
+
metadata: omitInternalMetadata(
|
|
133
|
+
JSON.parse(order.purchaseUnits?.[0]?.customId ?? "{}")
|
|
134
|
+
),
|
|
135
|
+
custom_fields: null,
|
|
136
|
+
subscription_id: null,
|
|
137
|
+
billing_mode: "one_time",
|
|
138
|
+
line_items: [{ id: order.purchaseUnits?.[0]?.items?.[0]?.sku || "", quantity: 1 }]
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
var paykitPaymentWebhook$InboundSchema = (resource) => {
|
|
142
|
+
const statusMap = {
|
|
143
|
+
CREATED: "pending",
|
|
144
|
+
SAVED: "pending",
|
|
145
|
+
APPROVED: "requires_capture",
|
|
146
|
+
VOIDED: "canceled",
|
|
147
|
+
COMPLETED: "succeeded",
|
|
148
|
+
PAYER_ACTION_REQUIRED: "requires_action"
|
|
149
|
+
};
|
|
150
|
+
const status = statusMap[resource.status ?? "CREATED"] ?? "pending";
|
|
151
|
+
const payer = resource.payer;
|
|
152
|
+
let customer = { email: "" };
|
|
153
|
+
if (payer?.payer_id) {
|
|
154
|
+
customer = payer.payer_id;
|
|
155
|
+
} else if (payer?.email_address) {
|
|
156
|
+
customer = { email: payer.email_address };
|
|
157
|
+
}
|
|
158
|
+
const purchaseUnits = resource.purchase_units ?? [];
|
|
159
|
+
const firstUnit = purchaseUnits[0] ?? {};
|
|
160
|
+
const amount = firstUnit.amount;
|
|
161
|
+
const items = firstUnit.items ?? [];
|
|
162
|
+
const metadata = (() => {
|
|
163
|
+
try {
|
|
164
|
+
const customId = firstUnit?.custom_id;
|
|
165
|
+
return customId ? JSON.parse(customId) : {};
|
|
166
|
+
} catch {
|
|
167
|
+
return {};
|
|
168
|
+
}
|
|
169
|
+
})();
|
|
170
|
+
const itemId = items[0]?.sku ?? firstUnit.reference_id ?? "";
|
|
171
|
+
return {
|
|
172
|
+
id: resource.id,
|
|
173
|
+
status,
|
|
174
|
+
customer,
|
|
175
|
+
amount: parseFloat(amount?.value ?? "0"),
|
|
176
|
+
currency: amount?.currency_code ?? "USD",
|
|
177
|
+
metadata: omitInternalMetadata(metadata),
|
|
178
|
+
item_id: itemId,
|
|
179
|
+
requires_action: false,
|
|
180
|
+
payment_url: null
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
var paykitPaymentCaptureWebhook$InboundSchema = (capture) => {
|
|
184
|
+
const statusMap = {
|
|
185
|
+
COMPLETED: "succeeded",
|
|
186
|
+
PENDING: "pending",
|
|
187
|
+
REFUNDED: "succeeded",
|
|
188
|
+
// Refunded but was successful
|
|
189
|
+
PARTIALLY_REFUNDED: "succeeded"
|
|
190
|
+
};
|
|
191
|
+
const status = statusMap[capture.status ?? "PENDING"] ?? "pending";
|
|
192
|
+
const supplementaryData = capture.supplementary_data;
|
|
193
|
+
const relatedIds = supplementaryData?.related_ids;
|
|
194
|
+
const orderId = relatedIds?.order_id ?? capture.id;
|
|
195
|
+
const metadata = (() => {
|
|
196
|
+
try {
|
|
197
|
+
const customId = capture.custom_id;
|
|
198
|
+
return customId ? JSON.parse(customId) : {};
|
|
199
|
+
} catch {
|
|
200
|
+
return {};
|
|
201
|
+
}
|
|
202
|
+
})();
|
|
203
|
+
const amount = capture.amount;
|
|
204
|
+
return {
|
|
205
|
+
id: orderId,
|
|
206
|
+
status,
|
|
207
|
+
customer: { email: "" },
|
|
208
|
+
// Not available in capture event
|
|
209
|
+
amount: parseFloat(amount?.value ?? "0"),
|
|
210
|
+
currency: amount?.currency_code ?? "USD",
|
|
211
|
+
metadata: omitInternalMetadata(metadata),
|
|
212
|
+
item_id: "",
|
|
213
|
+
// Not available in capture event
|
|
214
|
+
requires_action: false,
|
|
215
|
+
payment_url: null
|
|
216
|
+
};
|
|
217
|
+
};
|
|
218
|
+
var paykitRefundWebhook$InboundSchema = (refund) => {
|
|
219
|
+
const amount = refund.amount;
|
|
220
|
+
const refundAmount = amount?.total ? Math.abs(parseFloat(amount.total)) : 0;
|
|
221
|
+
return {
|
|
222
|
+
id: refund.id,
|
|
223
|
+
amount: refundAmount,
|
|
224
|
+
currency: amount?.currency ?? "USD",
|
|
225
|
+
metadata: omitInternalMetadata({}),
|
|
226
|
+
// No metadata in webhook refund
|
|
227
|
+
reason: null
|
|
228
|
+
// No reason in webhook refund
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
var paykitSubscriptionWebhook$InboundSchema = (agreement) => {
|
|
232
|
+
const state = agreement.state ?? "Pending";
|
|
233
|
+
const statusMap = {
|
|
234
|
+
Pending: "pending",
|
|
235
|
+
Active: "active",
|
|
236
|
+
Suspended: "past_due",
|
|
237
|
+
Cancelled: "canceled",
|
|
238
|
+
Expired: "canceled",
|
|
239
|
+
Completed: "active"
|
|
240
|
+
};
|
|
241
|
+
const status = statusMap[state] ?? "pending";
|
|
242
|
+
const plan = agreement.plan;
|
|
243
|
+
const paymentDefinitions = plan?.payment_definitions ?? [];
|
|
244
|
+
const regularDefinition = paymentDefinitions.find((def) => def.type === "REGULAR") ?? paymentDefinitions[0];
|
|
245
|
+
const frequency = regularDefinition?.frequency ?? "Month";
|
|
246
|
+
const billingIntervalMap = {
|
|
247
|
+
Day: "day",
|
|
248
|
+
Days: "day",
|
|
249
|
+
Week: "week",
|
|
250
|
+
Weeks: "week",
|
|
251
|
+
Month: "month",
|
|
252
|
+
Months: "month",
|
|
253
|
+
Year: "year",
|
|
254
|
+
Years: "year"
|
|
255
|
+
};
|
|
256
|
+
const billingInterval = billingIntervalMap[frequency] ?? "month";
|
|
257
|
+
const startDate = agreement.start_date ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
258
|
+
const periodStart = new Date(startDate);
|
|
259
|
+
const periodEnd = (() => {
|
|
260
|
+
const end = new Date(periodStart);
|
|
261
|
+
switch (billingInterval) {
|
|
262
|
+
case "day":
|
|
263
|
+
end.setDate(end.getDate() + 1);
|
|
264
|
+
break;
|
|
265
|
+
case "week":
|
|
266
|
+
end.setDate(end.getDate() + 7);
|
|
267
|
+
break;
|
|
268
|
+
case "month":
|
|
269
|
+
end.setMonth(end.getMonth() + 1);
|
|
270
|
+
break;
|
|
271
|
+
case "year":
|
|
272
|
+
end.setFullYear(end.getFullYear() + 1);
|
|
273
|
+
break;
|
|
274
|
+
default:
|
|
275
|
+
end.setMonth(end.getMonth() + 1);
|
|
276
|
+
}
|
|
277
|
+
return end;
|
|
278
|
+
})();
|
|
279
|
+
const payer = agreement.payer;
|
|
280
|
+
const payerInfo = payer?.payer_info;
|
|
281
|
+
const email = payerInfo?.email ?? "";
|
|
282
|
+
const planId = plan?.id ?? agreement.id;
|
|
283
|
+
const amount = regularDefinition?.amount;
|
|
284
|
+
const subscriptionAmount = amount?.value ? parseFloat(amount.value) : 0;
|
|
285
|
+
const currency = plan?.curr_code ?? "USD";
|
|
286
|
+
const metadata = (() => {
|
|
287
|
+
try {
|
|
288
|
+
const description = agreement.description;
|
|
289
|
+
return description ? JSON.parse(description) : {};
|
|
290
|
+
} catch {
|
|
291
|
+
return {};
|
|
292
|
+
}
|
|
293
|
+
})();
|
|
294
|
+
return {
|
|
295
|
+
id: agreement.id,
|
|
296
|
+
customer: { email },
|
|
297
|
+
status,
|
|
298
|
+
item_id: planId,
|
|
299
|
+
current_period_start: periodStart,
|
|
300
|
+
current_period_end: periodEnd,
|
|
301
|
+
metadata: omitInternalMetadata(metadata),
|
|
302
|
+
billing_interval: billingInterval,
|
|
303
|
+
amount: subscriptionAmount,
|
|
304
|
+
currency,
|
|
305
|
+
custom_fields: null,
|
|
306
|
+
requires_action: false,
|
|
307
|
+
payment_url: null
|
|
69
308
|
};
|
|
70
309
|
};
|
|
71
310
|
|
|
72
|
-
export { paykitCheckout$InboundSchema, paykitPayment$InboundSchema, paykitRefund$InboundSchema, paykitSubscription$InboundSchema };
|
|
311
|
+
export { paykitCheckout$InboundSchema, paykitInvoice$InboundSchema, paykitPayee$InboundSchema, paykitPayment$InboundSchema, paykitPaymentCaptureWebhook$InboundSchema, paykitPaymentWebhook$InboundSchema, paykitRefund$InboundSchema, paykitRefundWebhook$InboundSchema, paykitSubscription$InboundSchema, paykitSubscriptionWebhook$InboundSchema };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paykit-sdk/paypal",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "PayPal provider for PayKit",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -20,13 +20,14 @@
|
|
|
20
20
|
"tsup": "^8.5.0",
|
|
21
21
|
"typescript": "^5.9.2",
|
|
22
22
|
"zod": "^3.24.2",
|
|
23
|
-
"@paykit-sdk/core": "1.1.
|
|
23
|
+
"@paykit-sdk/core": "1.1.102"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@paypal/paypal-server-sdk": "^1.1.0"
|
|
26
|
+
"@paypal/paypal-server-sdk": "^1.1.0",
|
|
27
|
+
"esbuild": "^0.25.11"
|
|
27
28
|
},
|
|
28
29
|
"peerDependencies": {
|
|
29
|
-
"@paykit-sdk/core": ">=1.1.
|
|
30
|
+
"@paykit-sdk/core": ">=1.1.102"
|
|
30
31
|
},
|
|
31
32
|
"publishConfig": {
|
|
32
33
|
"access": "public"
|