@easypayment/medusa-payment-paypal 1.1.0 → 1.1.2
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/.medusa/server/src/admin/index.js +73 -73
- package/.medusa/server/src/admin/index.mjs +73 -73
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.d.ts +16 -0
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.d.ts.map +1 -0
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.js +20 -0
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.js.map +1 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.d.ts +18 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.d.ts.map +1 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.js +22 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.js.map +1 -0
- package/.medusa/server/src/api/middlewares.d.ts.map +1 -1
- package/.medusa/server/src/api/middlewares.js +9 -0
- package/.medusa/server/src/api/middlewares.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/config/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/config/route.js +8 -0
- package/.medusa/server/src/api/store/paypal/config/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/create-order/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/create-order/route.js +53 -4
- package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal-complete/route.js +1 -1
- package/.medusa/server/src/api/store/paypal-complete/route.js.map +1 -1
- package/.medusa/server/src/api/utils/rate-limit.d.ts.map +1 -1
- package/.medusa/server/src/api/utils/rate-limit.js +11 -2
- package/.medusa/server/src/api/utils/rate-limit.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts +32 -3
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js +47 -0
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js +172 -117
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.js +132 -126
- package/.medusa/server/src/modules/paypal/payment-provider/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/service.d.ts +5 -0
- package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/service.js +82 -1
- package/.medusa/server/src/modules/paypal/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts +10 -0
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.js +39 -0
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js +45 -28
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js.map +1 -1
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts +11 -0
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/webhook-processor.js +85 -7
- package/.medusa/server/src/modules/paypal/webhook-processor.js.map +1 -1
- package/CHANGELOG.md +73 -0
- package/package.json +4 -5
- package/src/api/hooks/paypal/onboard-return/route.ts +15 -0
- package/src/api/hooks/paypal/webhook/route.ts +17 -0
- package/src/api/middlewares.ts +9 -0
- package/src/api/store/paypal/config/route.ts +8 -0
- package/src/api/store/paypal/create-order/route.ts +64 -4
- package/src/api/store/paypal-complete/route.ts +1 -1
- package/src/api/utils/rate-limit.ts +11 -2
- package/src/modules/paypal/payment-provider/base-provider.ts +52 -0
- package/src/modules/paypal/payment-provider/card-service.ts +193 -136
- package/src/modules/paypal/payment-provider/service.ts +137 -156
- package/src/modules/paypal/service.ts +98 -1
- package/src/modules/paypal/utils/credential-resolver.ts +36 -0
- package/src/modules/paypal/utils/paypal-fetch.ts +58 -29
- package/src/modules/paypal/webhook-processor.ts +94 -8
|
@@ -100,6 +100,19 @@ export function normalizeEventVersion(payload: Record<string, any>): string | nu
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
|
|
103
|
+
/** Pull the capture id out of a refund resource's "up" HATEOAS link. */
|
|
104
|
+
export function extractCaptureIdFromLinks(
|
|
105
|
+
resource: Record<string, any>
|
|
106
|
+
): string | null {
|
|
107
|
+
const links = Array.isArray(resource?.links) ? resource.links : []
|
|
108
|
+
for (const link of links) {
|
|
109
|
+
const href = String(link?.href || "")
|
|
110
|
+
const match = href.match(/\/captures\/([A-Za-z0-9-]+)/)
|
|
111
|
+
if (match) return match[1]
|
|
112
|
+
}
|
|
113
|
+
return null
|
|
114
|
+
}
|
|
115
|
+
|
|
103
116
|
export interface ExtractedIdentifiers {
|
|
104
117
|
orderId: string | null
|
|
105
118
|
captureId: string | null
|
|
@@ -133,7 +146,21 @@ export function extractIdentifiers(
|
|
|
133
146
|
resource?.purchase_units?.[0]?.payments?.captures?.[0]?.id || ""
|
|
134
147
|
).trim() || null
|
|
135
148
|
} else if (isCapture) {
|
|
136
|
-
|
|
149
|
+
// PAYMENT.CAPTURE.REFUNDED / REVERSED carry a *refund* resource (its `id`
|
|
150
|
+
// is the refund id, with an "up" link to the capture) — treating that id
|
|
151
|
+
// as the capture id would corrupt the session's stored capture_id.
|
|
152
|
+
const isRefundShaped =
|
|
153
|
+
eventType === "PAYMENT.CAPTURE.REFUNDED" ||
|
|
154
|
+
eventType === "PAYMENT.CAPTURE.REVERSED"
|
|
155
|
+
if (isRefundShaped) {
|
|
156
|
+
refundId = String(resource?.id || "").trim() || null
|
|
157
|
+
captureId =
|
|
158
|
+
String(related?.capture_id || "").trim() ||
|
|
159
|
+
extractCaptureIdFromLinks(resource) ||
|
|
160
|
+
null
|
|
161
|
+
} else {
|
|
162
|
+
captureId = String(resource?.id || "").trim() || null
|
|
163
|
+
}
|
|
137
164
|
orderId = String(related?.order_id || "").trim() || null
|
|
138
165
|
cartId = String(resource?.custom_id || "").trim() || null
|
|
139
166
|
} else if (isAuthorization) {
|
|
@@ -223,12 +250,14 @@ function mergeRefunds(existing: any[], incoming: any[]): any[] {
|
|
|
223
250
|
async function applyStatusToSession(
|
|
224
251
|
container: MedusaContainer,
|
|
225
252
|
resolved: ResolvedSession,
|
|
226
|
-
status: string,
|
|
253
|
+
status: string | null,
|
|
227
254
|
patch: Record<string, unknown>
|
|
228
255
|
): Promise<void> {
|
|
229
256
|
const paymentModule = container.resolve(Modules.PAYMENT) as any
|
|
230
257
|
|
|
231
|
-
|
|
258
|
+
// A null status means "record the data but keep the current session status"
|
|
259
|
+
// (e.g. a partial refund must not cancel a captured session).
|
|
260
|
+
if (status !== null && !isTransitionAllowed(resolved.sessionStatus, status)) {
|
|
232
261
|
console.info(
|
|
233
262
|
`[PayPal] webhook: skipping disallowed transition ${resolved.sessionStatus} → ${status} for session ${resolved.sessionId}`
|
|
234
263
|
)
|
|
@@ -246,14 +275,22 @@ async function applyStatusToSession(
|
|
|
246
275
|
? mergeRefunds(existingRefunds, incomingRefunds)
|
|
247
276
|
: existingRefunds
|
|
248
277
|
|
|
278
|
+
// Drop undefined/null values from the patch so a webhook that lacks an
|
|
279
|
+
// identifier (e.g. a capture event with no related_ids) can never clobber
|
|
280
|
+
// stored fields like `order_id` with null.
|
|
281
|
+
const cleanPatch: Record<string, unknown> = {}
|
|
282
|
+
for (const [k, v] of Object.entries(patch)) {
|
|
283
|
+
if (v !== undefined && v !== null) cleanPatch[k] = v
|
|
284
|
+
}
|
|
285
|
+
|
|
249
286
|
await paymentModule.updatePaymentSession({
|
|
250
287
|
id: resolved.sessionId,
|
|
251
|
-
status,
|
|
288
|
+
status: status === null ? resolved.sessionStatus : status,
|
|
252
289
|
data: {
|
|
253
290
|
...resolved.sessionData,
|
|
254
291
|
paypal: {
|
|
255
292
|
...existingPaypal,
|
|
256
|
-
...
|
|
293
|
+
...cleanPatch,
|
|
257
294
|
refunds: nextRefunds,
|
|
258
295
|
},
|
|
259
296
|
},
|
|
@@ -261,6 +298,37 @@ async function applyStatusToSession(
|
|
|
261
298
|
}
|
|
262
299
|
|
|
263
300
|
|
|
301
|
+
/**
|
|
302
|
+
* True when a refund resource demonstrably covers less than the captured
|
|
303
|
+
* amount. Uses the refund's cumulative `total_refunded_amount` (falling back
|
|
304
|
+
* to the single refund amount) against the capture amount stored on the
|
|
305
|
+
* session (falling back to the session total). Returns false — i.e. treat as
|
|
306
|
+
* a full refund, matching the previous behavior — whenever the amounts can't
|
|
307
|
+
* be determined.
|
|
308
|
+
*/
|
|
309
|
+
export function isPartialRefund(
|
|
310
|
+
resource: Record<string, any>,
|
|
311
|
+
sessionData: Record<string, any>
|
|
312
|
+
): boolean {
|
|
313
|
+
const refundedRaw =
|
|
314
|
+
resource?.seller_payable_breakdown?.total_refunded_amount?.value ??
|
|
315
|
+
resource?.amount?.value
|
|
316
|
+
const refunded = Number(refundedRaw)
|
|
317
|
+
if (!Number.isFinite(refunded) || refunded <= 0) return false
|
|
318
|
+
|
|
319
|
+
const paypal = (sessionData?.paypal || {}) as Record<string, any>
|
|
320
|
+
const capturedRaw =
|
|
321
|
+
paypal?.capture?.amount?.value ??
|
|
322
|
+
paypal?.capture?.seller_receivable_breakdown?.gross_amount?.value ??
|
|
323
|
+
sessionData?.amount
|
|
324
|
+
const captured = Number(capturedRaw)
|
|
325
|
+
if (!Number.isFinite(captured) || captured <= 0) return false
|
|
326
|
+
|
|
327
|
+
// Tolerance for floating-point noise; amounts are decimal strings from
|
|
328
|
+
// PayPal or the session's stored major-unit amount.
|
|
329
|
+
return refunded + 0.005 < captured
|
|
330
|
+
}
|
|
331
|
+
|
|
264
332
|
export interface ProcessResult {
|
|
265
333
|
orderId: string | null
|
|
266
334
|
captureId: string | null
|
|
@@ -364,10 +432,28 @@ export async function processPayPalWebhookEvent(
|
|
|
364
432
|
]
|
|
365
433
|
: null
|
|
366
434
|
|
|
367
|
-
|
|
368
|
-
|
|
435
|
+
// A PARTIAL refund must not cancel the session: PayPal fires
|
|
436
|
+
// PAYMENT.CAPTURE.REFUNDED / PAYMENT.REFUND.COMPLETED for partial
|
|
437
|
+
// refunds too, and flipping a substantially-paid session to "canceled"
|
|
438
|
+
// misrepresents the payment. Record the refund but keep the status
|
|
439
|
+
// unless the cumulative refunded total covers the captured amount (or
|
|
440
|
+
// the amounts can't be determined, preserving the old full-refund
|
|
441
|
+
// behavior).
|
|
442
|
+
let effectiveStatus: string | null = targetStatus
|
|
443
|
+
const isRefundEvent =
|
|
444
|
+
input.eventType === "PAYMENT.CAPTURE.REFUNDED" ||
|
|
445
|
+
input.eventType === "PAYMENT.CAPTURE.REVERSED" ||
|
|
446
|
+
input.eventType === "PAYMENT.REFUND.COMPLETED"
|
|
447
|
+
if (isRefundEvent && targetStatus === "canceled") {
|
|
448
|
+
if (isPartialRefund(resource, resolved.sessionData)) {
|
|
449
|
+
effectiveStatus = null
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
await applyStatusToSession(container, resolved, effectiveStatus, {
|
|
454
|
+
order_id: orderId ?? undefined,
|
|
369
455
|
capture_id: captureId ?? resolved.sessionData.paypal?.capture_id ?? undefined,
|
|
370
|
-
refund_id: refundId,
|
|
456
|
+
refund_id: refundId ?? undefined,
|
|
371
457
|
refund_status: refundId ? resource?.status : undefined,
|
|
372
458
|
refund_reason: refundReason,
|
|
373
459
|
refund_reason_code: refundReasonCode,
|