@paykit-sdk/paypal 1.0.5 → 1.0.7

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