@easypayment/medusa-payment-paypal 1.2.2 → 1.2.4
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 +390 -136
- package/.medusa/server/src/admin/index.mjs +390 -136
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.d.ts +1 -0
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.js +5 -1
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/disconnect/route.d.ts +1 -0
- package/.medusa/server/src/api/admin/paypal/disconnect/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/disconnect/route.js +1 -0
- package/.medusa/server/src/api/admin/paypal/disconnect/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/environment/route.d.ts +2 -0
- package/.medusa/server/src/api/admin/paypal/environment/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/environment/route.js +2 -0
- package/.medusa/server/src/api/admin/paypal/environment/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.d.ts +9 -0
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.js +9 -4
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.d.ts +7 -1
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.js +9 -2
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.d.ts +1 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.js +1 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.d.ts +1 -0
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.js +10 -3
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/settings/route.d.ts +2 -0
- package/.medusa/server/src/api/admin/paypal/settings/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/settings/route.js +9 -2
- package/.medusa/server/src/api/admin/paypal/settings/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/status/route.d.ts +4 -0
- package/.medusa/server/src/api/admin/paypal/status/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/status/route.js +9 -1
- package/.medusa/server/src/api/admin/paypal/status/route.js.map +1 -1
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.d.ts +4 -0
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.js +5 -0
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts +8 -0
- package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/capture-order/route.js +23 -3
- package/.medusa/server/src/api/store/paypal/capture-order/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/config/route.d.ts +5 -0
- package/.medusa/server/src/api/store/paypal/config/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/config/route.js +12 -5
- 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 +8 -0
- 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 +54 -20
- package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/onboard-return/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/onboard-return/route.js +19 -19
- package/.medusa/server/src/api/store/paypal/onboard-return/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/settings/route.d.ts +1 -0
- package/.medusa/server/src/api/store/paypal/settings/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/settings/route.js +4 -1
- package/.medusa/server/src/api/store/paypal/settings/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/webhook/route.d.ts +7 -0
- package/.medusa/server/src/api/store/paypal/webhook/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/webhook/route.js +34 -3
- package/.medusa/server/src/api/store/paypal/webhook/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal-complete/route.d.ts +8 -0
- package/.medusa/server/src/api/store/paypal-complete/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal-complete/route.js +33 -6
- package/.medusa/server/src/api/store/paypal-complete/route.js.map +1 -1
- package/.medusa/server/src/api/utils/rate-limit.d.ts +4 -0
- package/.medusa/server/src/api/utils/rate-limit.d.ts.map +1 -1
- package/.medusa/server/src/api/utils/rate-limit.js +9 -4
- package/.medusa/server/src/api/utils/rate-limit.js.map +1 -1
- package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts +5 -0
- package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts.map +1 -1
- package/.medusa/server/src/jobs/paypal-webhook-retry.js +13 -2
- package/.medusa/server/src/jobs/paypal-webhook-retry.js.map +1 -1
- package/.medusa/server/src/modules/paypal/index.d.ts +1 -0
- package/.medusa/server/src/modules/paypal/index.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/index.js +1 -0
- package/.medusa/server/src/modules/paypal/index.js.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20260115120000_create_paypal_connection.js +22 -22
- package/.medusa/server/src/modules/paypal/migrations/20260123090000_create_paypal_settings.js +11 -11
- package/.medusa/server/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.js +18 -18
- package/.medusa/server/src/modules/paypal/migrations/20260401090000_create_paypal_metric.js +16 -16
- package/.medusa/server/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.js +20 -20
- package/.medusa/server/src/modules/paypal/migrations/20261101090000_remove_paypal_reconciliation_status.js +14 -14
- package/.medusa/server/src/modules/paypal/migrations/20261201090000_remove_paypal_audit_log.js +15 -15
- package/.medusa/server/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.js.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270102090000_add_paypal_seller_email.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270102090000_add_paypal_seller_email.js.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270103090000_add_paypal_seller_merchant_id.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270103090000_add_paypal_seller_merchant_id.js.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270201000000_add_webhook_dead_letter.js +6 -6
- package/.medusa/server/src/modules/paypal/models/paypal_connection.d.ts +5 -0
- package/.medusa/server/src/modules/paypal/models/paypal_connection.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_connection.js +5 -0
- package/.medusa/server/src/modules/paypal/models/paypal_connection.js.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_metric.d.ts +4 -0
- package/.medusa/server/src/modules/paypal/models/paypal_metric.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_metric.js +4 -0
- package/.medusa/server/src/modules/paypal/models/paypal_metric.js.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_settings.d.ts +5 -0
- package/.medusa/server/src/modules/paypal/models/paypal_settings.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_settings.js +5 -0
- package/.medusa/server/src/modules/paypal/models/paypal_settings.js.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.d.ts +7 -0
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.js +7 -0
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.js.map +1 -1
- 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.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts +14 -6
- 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 +55 -32
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/index.d.ts +6 -1
- package/.medusa/server/src/modules/paypal/payment-provider/index.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/index.js +6 -4
- package/.medusa/server/src/modules/paypal/payment-provider/index.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts +8 -0
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.js +39 -12
- package/.medusa/server/src/modules/paypal/payment-provider/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/service.d.ts +72 -2
- package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/service.js +159 -33
- package/.medusa/server/src/modules/paypal/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.d.ts +2 -0
- package/.medusa/server/src/modules/paypal/types/config.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.js +5 -2
- package/.medusa/server/src/modules/paypal/types/config.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/amounts.d.ts +1 -0
- package/.medusa/server/src/modules/paypal/utils/amounts.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/amounts.js +15 -5
- package/.medusa/server/src/modules/paypal/utils/amounts.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.js +3 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/currencies.d.ts +9 -0
- package/.medusa/server/src/modules/paypal/utils/currencies.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/currencies.js +13 -0
- package/.medusa/server/src/modules/paypal/utils/currencies.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/payment-session.d.ts +12 -0
- package/.medusa/server/src/modules/paypal/utils/payment-session.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/payment-session.js +4 -3
- package/.medusa/server/src/modules/paypal/utils/payment-session.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-auth.d.ts +1 -0
- package/.medusa/server/src/modules/paypal/utils/paypal-auth.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-auth.js +1 -0
- package/.medusa/server/src/modules/paypal/utils/paypal-auth.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts +11 -0
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js +17 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/provider-ids.d.ts +6 -0
- package/.medusa/server/src/modules/paypal/utils/provider-ids.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/provider-ids.js +6 -0
- package/.medusa/server/src/modules/paypal/utils/provider-ids.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/secret-crypto.d.ts +2 -0
- package/.medusa/server/src/modules/paypal/utils/secret-crypto.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/secret-crypto.js +6 -1
- package/.medusa/server/src/modules/paypal/utils/secret-crypto.js.map +1 -1
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts +43 -0
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/webhook-processor.js +57 -5
- package/.medusa/server/src/modules/paypal/webhook-processor.js.map +1 -1
- package/.medusa/server/src/providers/paypal/index.d.ts +1 -0
- package/.medusa/server/src/providers/paypal/index.d.ts.map +1 -1
- package/.medusa/server/src/providers/paypal/index.js +1 -0
- package/.medusa/server/src/providers/paypal/index.js.map +1 -1
- package/.medusa/server/src/providers/paypal_card/index.d.ts +1 -0
- package/.medusa/server/src/providers/paypal_card/index.d.ts.map +1 -1
- package/.medusa/server/src/providers/paypal_card/index.js +1 -0
- package/.medusa/server/src/providers/paypal_card/index.js.map +1 -1
- package/.medusa/server/src/subscribers/paypal-order-invoice.d.ts +6 -0
- package/.medusa/server/src/subscribers/paypal-order-invoice.d.ts.map +1 -1
- package/.medusa/server/src/subscribers/paypal-order-invoice.js +9 -1
- package/.medusa/server/src/subscribers/paypal-order-invoice.js.map +1 -1
- package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts +1 -0
- package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts.map +1 -1
- package/.medusa/server/src/subscribers/paypal-webhook-process.js +17 -4
- package/.medusa/server/src/subscribers/paypal-webhook-process.js.map +1 -1
- package/CHANGELOG.md +19 -0
- package/README.md +184 -175
- package/package.json +24 -5
- package/src/admin/index.ts +1 -1
- package/src/admin/routes/settings/paypal/_components/FieldRow.tsx +38 -29
- package/src/admin/routes/settings/paypal/_components/SectionCard.tsx +29 -26
- package/src/admin/routes/settings/paypal/_components/Tabs.tsx +49 -48
- package/src/admin/routes/settings/paypal/_components/Toast.tsx +63 -52
- package/src/admin/routes/settings/paypal/_utils/adminFetch.ts +52 -32
- package/src/admin/routes/settings/paypal/additional-settings/page.tsx +246 -139
- package/src/admin/routes/settings/paypal/advanced-card-payments/page.tsx +208 -123
- package/src/admin/routes/settings/paypal/apple-pay/page.tsx +9 -5
- package/src/admin/routes/settings/paypal/connection/page.tsx +974 -908
- package/src/admin/routes/settings/paypal/google-pay/page.tsx +9 -5
- package/src/admin/routes/settings/paypal/page.tsx +14 -13
- package/src/admin/routes/settings/paypal/pay-later-messaging/page.tsx +9 -5
- package/src/admin/routes/settings/paypal/paypal-settings/page.tsx +304 -273
- package/src/api/admin/payment-collections/[id]/payment-sessions/route.ts +48 -39
- package/src/api/admin/paypal/disconnect/route.ts +15 -14
- package/src/api/admin/paypal/environment/route.ts +34 -32
- package/src/api/admin/paypal/onboard-complete/route.ts +61 -50
- package/src/api/admin/paypal/onboarding-link/route.ts +91 -76
- package/src/api/admin/paypal/onboarding-status/route.ts +21 -20
- package/src/api/admin/paypal/save-credentials/route.ts +48 -37
- package/src/api/admin/paypal/settings/route.ts +63 -45
- package/src/api/admin/paypal/status/route.ts +29 -17
- package/src/api/hooks/paypal/onboard-return/route.ts +15 -15
- package/src/api/hooks/paypal/webhook/route.ts +17 -17
- package/src/api/middlewares.ts +61 -61
- package/src/api/store/payment-collections/[id]/payment-sessions/route.ts +63 -54
- package/src/api/store/paypal/capture-order/route.ts +416 -370
- package/src/api/store/paypal/config/route.ts +121 -115
- package/src/api/store/paypal/create-order/route.ts +586 -539
- package/src/api/store/paypal/onboard-return/route.ts +116 -113
- package/src/api/store/paypal/settings/route.ts +29 -19
- package/src/api/store/paypal/webhook/route.ts +380 -314
- package/src/api/store/paypal-complete/route.ts +157 -128
- package/src/api/utils/rate-limit.ts +143 -129
- package/src/index.ts +1 -1
- package/src/jobs/paypal-webhook-retry.ts +257 -241
- package/src/modules/paypal/index.ts +9 -8
- package/src/modules/paypal/migrations/20260115120000_create_paypal_connection.ts +33 -33
- package/src/modules/paypal/migrations/20260123090000_create_paypal_settings.ts +22 -22
- package/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.ts +29 -29
- package/src/modules/paypal/migrations/20260401090000_create_paypal_metric.ts +27 -27
- package/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.ts +31 -31
- package/src/modules/paypal/migrations/20261101090000_remove_paypal_reconciliation_status.ts +25 -25
- package/src/modules/paypal/migrations/20261201090000_remove_paypal_audit_log.ts +26 -26
- package/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.ts +15 -11
- package/src/modules/paypal/migrations/20270102090000_add_paypal_seller_email.ts +15 -11
- package/src/modules/paypal/migrations/20270103090000_add_paypal_seller_merchant_id.ts +15 -11
- package/src/modules/paypal/migrations/20270201000000_add_webhook_dead_letter.ts +17 -17
- package/src/modules/paypal/migrations/20270202000000_align_nullable_defaults.ts +44 -44
- package/src/modules/paypal/migrations/20270301000000_add_webhook_transmission_id_index.ts +15 -15
- package/src/modules/paypal/models/paypal_connection.ts +28 -23
- package/src/modules/paypal/models/paypal_metric.ts +13 -9
- package/src/modules/paypal/models/paypal_settings.ts +13 -8
- package/src/modules/paypal/models/paypal_webhook_event.ts +26 -19
- package/src/modules/paypal/payment-provider/base-provider.ts +287 -275
- package/src/modules/paypal/payment-provider/card-service.ts +927 -855
- package/src/modules/paypal/payment-provider/index.ts +14 -15
- package/src/modules/paypal/payment-provider/service.ts +906 -836
- package/src/modules/paypal/payment-provider/status-utils.ts +59 -59
- package/src/modules/paypal/payment-provider/webhook-utils.ts +109 -109
- package/src/modules/paypal/service.ts +362 -115
- package/src/modules/paypal/types/config.ts +70 -65
- package/src/modules/paypal/utils/amounts.ts +123 -107
- package/src/modules/paypal/utils/core-workflow.ts +46 -46
- package/src/modules/paypal/utils/credential-resolver.ts +275 -271
- package/src/modules/paypal/utils/currencies.ts +97 -84
- package/src/modules/paypal/utils/partner.ts +8 -8
- package/src/modules/paypal/utils/payment-session.ts +144 -138
- package/src/modules/paypal/utils/paypal-auth.ts +6 -5
- package/src/modules/paypal/utils/paypal-fetch.ts +197 -181
- package/src/modules/paypal/utils/provider-ids.ts +21 -15
- package/src/modules/paypal/utils/secret-crypto.ts +226 -197
- package/src/modules/paypal/utils/webhook-verify.ts +61 -61
- package/src/modules/paypal/webhook-processor.ts +641 -573
- package/src/providers/paypal/index.ts +9 -8
- package/src/providers/paypal_card/index.ts +9 -8
- package/src/subscribers/paypal-order-invoice.ts +180 -173
- package/src/subscribers/paypal-webhook-process.ts +147 -131
- package/tsconfig.json +21 -31
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure mapping/validation helpers for PayPal capture & refund resource statuses.
|
|
3
|
-
*
|
|
4
|
-
* Kept free of any container/IO dependencies so the money-flow decisions (what
|
|
5
|
-
* counts as a settled capture, when a refund must be rejected, how a PayPal
|
|
6
|
-
* status maps onto a Medusa payment status) can be unit-tested in isolation.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/** Map a PayPal capture status onto a Medusa payment status. */
|
|
10
|
-
export function mapPayPalCaptureStatus(
|
|
11
|
-
status?: string
|
|
12
|
-
): "captured" | "pending" | "error" | "canceled" | null {
|
|
13
|
-
const normalized = String(status || "").toUpperCase()
|
|
14
|
-
if (!normalized) return null
|
|
15
|
-
if (normalized === "COMPLETED") return "captured"
|
|
16
|
-
if (normalized === "PENDING") return "pending"
|
|
17
|
-
if (["DENIED", "DECLINED", "FAILED"].includes(normalized)) return "error"
|
|
18
|
-
// A partially-refunded capture is still a captured payment — only a portion of
|
|
19
|
-
// the funds was returned. Mapping it to "canceled" would wrongly unwind a live
|
|
20
|
-
// capture, so it must stay "captured". A full refund/reversal does unwind it.
|
|
21
|
-
if (normalized === "PARTIALLY_REFUNDED") return "captured"
|
|
22
|
-
if (["REFUNDED", "REVERSED"].includes(normalized)) return "canceled"
|
|
23
|
-
return null
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Effective status of a PayPal capture resource.
|
|
28
|
-
*
|
|
29
|
-
* The orders-capture response nests the capture under
|
|
30
|
-
* `purchase_units[].payments.captures[]` — the order's own top-level status can
|
|
31
|
-
* read COMPLETED while the capture is still PENDING — whereas the
|
|
32
|
-
* authorizations-capture response is the capture object directly. Prefer the
|
|
33
|
-
* nested capture status, falling back to the top level.
|
|
34
|
-
*/
|
|
35
|
-
export function extractCaptureStatus(resource: any): string {
|
|
36
|
-
if (!resource || typeof resource !== "object") return ""
|
|
37
|
-
const nested = resource?.purchase_units?.[0]?.payments?.captures?.[0]?.status
|
|
38
|
-
return String(nested ?? resource.status ?? "").toUpperCase()
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* True only when a capture resource is COMPLETED. A 2xx HTTP response does NOT
|
|
43
|
-
* imply this: PayPal returns 201 for PENDING (pending review / eCheck),
|
|
44
|
-
* DECLINED, and FAILED captures too.
|
|
45
|
-
*/
|
|
46
|
-
export function isCaptureCompleted(resource: any): boolean {
|
|
47
|
-
return extractCaptureStatus(resource) === "COMPLETED"
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Refund statuses that mean the refund did NOT go through and must never be
|
|
52
|
-
* recorded as a successful refund. PENDING is intentionally excluded: PayPal
|
|
53
|
-
* processes refunds asynchronously and a pending refund settles later.
|
|
54
|
-
*/
|
|
55
|
-
export function isRefundFailureStatus(status?: string): boolean {
|
|
56
|
-
return ["FAILED", "CANCELLED", "CANCELED", "DENIED"].includes(
|
|
57
|
-
String(status || "").toUpperCase()
|
|
58
|
-
)
|
|
59
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Pure mapping/validation helpers for PayPal capture & refund resource statuses.
|
|
3
|
+
*
|
|
4
|
+
* Kept free of any container/IO dependencies so the money-flow decisions (what
|
|
5
|
+
* counts as a settled capture, when a refund must be rejected, how a PayPal
|
|
6
|
+
* status maps onto a Medusa payment status) can be unit-tested in isolation.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Map a PayPal capture status onto a Medusa payment status. */
|
|
10
|
+
export function mapPayPalCaptureStatus(
|
|
11
|
+
status?: string
|
|
12
|
+
): "captured" | "pending" | "error" | "canceled" | null {
|
|
13
|
+
const normalized = String(status || "").toUpperCase()
|
|
14
|
+
if (!normalized) return null
|
|
15
|
+
if (normalized === "COMPLETED") return "captured"
|
|
16
|
+
if (normalized === "PENDING") return "pending"
|
|
17
|
+
if (["DENIED", "DECLINED", "FAILED"].includes(normalized)) return "error"
|
|
18
|
+
// A partially-refunded capture is still a captured payment — only a portion of
|
|
19
|
+
// the funds was returned. Mapping it to "canceled" would wrongly unwind a live
|
|
20
|
+
// capture, so it must stay "captured". A full refund/reversal does unwind it.
|
|
21
|
+
if (normalized === "PARTIALLY_REFUNDED") return "captured"
|
|
22
|
+
if (["REFUNDED", "REVERSED"].includes(normalized)) return "canceled"
|
|
23
|
+
return null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Effective status of a PayPal capture resource.
|
|
28
|
+
*
|
|
29
|
+
* The orders-capture response nests the capture under
|
|
30
|
+
* `purchase_units[].payments.captures[]` — the order's own top-level status can
|
|
31
|
+
* read COMPLETED while the capture is still PENDING — whereas the
|
|
32
|
+
* authorizations-capture response is the capture object directly. Prefer the
|
|
33
|
+
* nested capture status, falling back to the top level.
|
|
34
|
+
*/
|
|
35
|
+
export function extractCaptureStatus(resource: any): string {
|
|
36
|
+
if (!resource || typeof resource !== "object") return ""
|
|
37
|
+
const nested = resource?.purchase_units?.[0]?.payments?.captures?.[0]?.status
|
|
38
|
+
return String(nested ?? resource.status ?? "").toUpperCase()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* True only when a capture resource is COMPLETED. A 2xx HTTP response does NOT
|
|
43
|
+
* imply this: PayPal returns 201 for PENDING (pending review / eCheck),
|
|
44
|
+
* DECLINED, and FAILED captures too.
|
|
45
|
+
*/
|
|
46
|
+
export function isCaptureCompleted(resource: any): boolean {
|
|
47
|
+
return extractCaptureStatus(resource) === "COMPLETED"
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Refund statuses that mean the refund did NOT go through and must never be
|
|
52
|
+
* recorded as a successful refund. PENDING is intentionally excluded: PayPal
|
|
53
|
+
* processes refunds asynchronously and a pending refund settles later.
|
|
54
|
+
*/
|
|
55
|
+
export function isRefundFailureStatus(status?: string): boolean {
|
|
56
|
+
return ["FAILED", "CANCELLED", "CANCELED", "DENIED"].includes(
|
|
57
|
+
String(status || "").toUpperCase()
|
|
58
|
+
)
|
|
59
|
+
}
|
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ProviderWebhookPayload,
|
|
3
|
-
WebhookActionResult,
|
|
4
|
-
} from "@medusajs/framework/types"
|
|
5
|
-
|
|
6
|
-
const EVENT_ACTIONS: Record<string, WebhookActionResult["action"]> = {
|
|
7
|
-
"CHECKOUT.ORDER.CREATED": "pending",
|
|
8
|
-
"CHECKOUT.ORDER.SAVED": "pending",
|
|
9
|
-
"CHECKOUT.ORDER.APPROVED": "authorized",
|
|
10
|
-
"CHECKOUT.ORDER.PAYER_ACTION_REQUIRED": "pending",
|
|
11
|
-
"CHECKOUT.ORDER.CANCELLED": "canceled",
|
|
12
|
-
"CHECKOUT.ORDER.EXPIRED": "failed",
|
|
13
|
-
"CHECKOUT.ORDER.VOIDED": "canceled",
|
|
14
|
-
"CHECKOUT.ORDER.COMPLETED": "captured",
|
|
15
|
-
"PAYMENT.AUTHORIZATION.CREATED": "authorized",
|
|
16
|
-
"PAYMENT.AUTHORIZATION.DENIED": "failed",
|
|
17
|
-
"PAYMENT.AUTHORIZATION.EXPIRED": "failed",
|
|
18
|
-
"PAYMENT.AUTHORIZATION.PENDING": "pending",
|
|
19
|
-
"PAYMENT.AUTHORIZATION.VOIDED": "canceled",
|
|
20
|
-
"PAYMENT.CAPTURE.COMPLETED": "captured",
|
|
21
|
-
"PAYMENT.CAPTURE.DENIED": "failed",
|
|
22
|
-
"PAYMENT.CAPTURE.PENDING": "pending",
|
|
23
|
-
"PAYMENT.CAPTURE.REFUNDED": "canceled",
|
|
24
|
-
"PAYMENT.CAPTURE.REVERSED": "canceled",
|
|
25
|
-
"PAYMENT.REFUND.COMPLETED": "canceled",
|
|
26
|
-
"PAYMENT.REFUND.DENIED": "failed",
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function resolveSessionId(resource: any): string | undefined {
|
|
30
|
-
return (
|
|
31
|
-
resource?.custom_id ||
|
|
32
|
-
resource?.purchase_units?.[0]?.custom_id ||
|
|
33
|
-
resource?.purchase_units?.[0]?.payments?.captures?.[0]?.custom_id ||
|
|
34
|
-
resource?.purchase_units?.[0]?.payments?.authorizations?.[0]?.custom_id ||
|
|
35
|
-
resource?.purchase_units?.[0]?.payments?.refunds?.[0]?.custom_id
|
|
36
|
-
)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function resolveAmount(resource: any): number | undefined {
|
|
40
|
-
const amount =
|
|
41
|
-
resource?.amount ||
|
|
42
|
-
resource?.purchase_units?.[0]?.amount ||
|
|
43
|
-
resource?.purchase_units?.[0]?.payments?.captures?.[0]?.amount ||
|
|
44
|
-
resource?.purchase_units?.[0]?.payments?.authorizations?.[0]?.amount ||
|
|
45
|
-
resource?.purchase_units?.[0]?.payments?.refunds?.[0]?.amount ||
|
|
46
|
-
resource?.seller_receivable_breakdown?.gross_amount
|
|
47
|
-
const value = amount?.value ?? amount?.amount?.value
|
|
48
|
-
if (value === undefined || value === null) {
|
|
49
|
-
return undefined
|
|
50
|
-
}
|
|
51
|
-
const parsed = Number(value)
|
|
52
|
-
return Number.isFinite(parsed) ? parsed : undefined
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function resolveCurrencyCode(resource: any): string | undefined {
|
|
56
|
-
const amount =
|
|
57
|
-
resource?.amount ||
|
|
58
|
-
resource?.purchase_units?.[0]?.amount ||
|
|
59
|
-
resource?.purchase_units?.[0]?.payments?.captures?.[0]?.amount ||
|
|
60
|
-
resource?.purchase_units?.[0]?.payments?.authorizations?.[0]?.amount
|
|
61
|
-
return amount?.currency_code || undefined
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function resolveEventType(payload: ProviderWebhookPayload["payload"]) {
|
|
65
|
-
const raw = payload as { event_type?: string; eventType?: string }
|
|
66
|
-
return raw?.event_type || raw?.eventType
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Maps a PayPal webhook payload to the generic Medusa payment-provider webhook
|
|
71
|
-
* result (the `getWebhookActionAndData` hook on AbstractPaymentProvider).
|
|
72
|
-
*
|
|
73
|
-
* NOTE: the authoritative webhook handling for this plugin is the dedicated
|
|
74
|
-
* `/store/paypal/webhook` route (signature verification, replay window,
|
|
75
|
-
* dedup, dead-letter retry). This helper exists only for the framework's
|
|
76
|
-
* generic `/hooks/payment/:provider` path. The `session_id` returned here is
|
|
77
|
-
* derived from the PayPal resource `custom_id`, which this plugin sets to the
|
|
78
|
-
* cart id (see create-order) — so consumers must resolve the cart, not treat
|
|
79
|
-
* it as a payment-session id directly.
|
|
80
|
-
*/
|
|
81
|
-
export function getPayPalWebhookActionAndData(
|
|
82
|
-
payload: ProviderWebhookPayload["payload"]
|
|
83
|
-
): WebhookActionResult {
|
|
84
|
-
const eventType = resolveEventType(payload)
|
|
85
|
-
if (!eventType) {
|
|
86
|
-
return { action: "not_supported" }
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const action = EVENT_ACTIONS[eventType]
|
|
90
|
-
if (!action) {
|
|
91
|
-
return { action: "not_supported" }
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const resource = (payload as { resource?: unknown })?.resource
|
|
95
|
-
const sessionId = resolveSessionId(resource)
|
|
96
|
-
const amount = resolveAmount(resource)
|
|
97
|
-
|
|
98
|
-
if (!sessionId || amount === undefined) {
|
|
99
|
-
return { action: "not_supported" }
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
action,
|
|
104
|
-
data: {
|
|
105
|
-
session_id: sessionId,
|
|
106
|
-
amount,
|
|
107
|
-
},
|
|
108
|
-
}
|
|
109
|
-
}
|
|
1
|
+
import type {
|
|
2
|
+
ProviderWebhookPayload,
|
|
3
|
+
WebhookActionResult,
|
|
4
|
+
} from "@medusajs/framework/types"
|
|
5
|
+
|
|
6
|
+
const EVENT_ACTIONS: Record<string, WebhookActionResult["action"]> = {
|
|
7
|
+
"CHECKOUT.ORDER.CREATED": "pending",
|
|
8
|
+
"CHECKOUT.ORDER.SAVED": "pending",
|
|
9
|
+
"CHECKOUT.ORDER.APPROVED": "authorized",
|
|
10
|
+
"CHECKOUT.ORDER.PAYER_ACTION_REQUIRED": "pending",
|
|
11
|
+
"CHECKOUT.ORDER.CANCELLED": "canceled",
|
|
12
|
+
"CHECKOUT.ORDER.EXPIRED": "failed",
|
|
13
|
+
"CHECKOUT.ORDER.VOIDED": "canceled",
|
|
14
|
+
"CHECKOUT.ORDER.COMPLETED": "captured",
|
|
15
|
+
"PAYMENT.AUTHORIZATION.CREATED": "authorized",
|
|
16
|
+
"PAYMENT.AUTHORIZATION.DENIED": "failed",
|
|
17
|
+
"PAYMENT.AUTHORIZATION.EXPIRED": "failed",
|
|
18
|
+
"PAYMENT.AUTHORIZATION.PENDING": "pending",
|
|
19
|
+
"PAYMENT.AUTHORIZATION.VOIDED": "canceled",
|
|
20
|
+
"PAYMENT.CAPTURE.COMPLETED": "captured",
|
|
21
|
+
"PAYMENT.CAPTURE.DENIED": "failed",
|
|
22
|
+
"PAYMENT.CAPTURE.PENDING": "pending",
|
|
23
|
+
"PAYMENT.CAPTURE.REFUNDED": "canceled",
|
|
24
|
+
"PAYMENT.CAPTURE.REVERSED": "canceled",
|
|
25
|
+
"PAYMENT.REFUND.COMPLETED": "canceled",
|
|
26
|
+
"PAYMENT.REFUND.DENIED": "failed",
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function resolveSessionId(resource: any): string | undefined {
|
|
30
|
+
return (
|
|
31
|
+
resource?.custom_id ||
|
|
32
|
+
resource?.purchase_units?.[0]?.custom_id ||
|
|
33
|
+
resource?.purchase_units?.[0]?.payments?.captures?.[0]?.custom_id ||
|
|
34
|
+
resource?.purchase_units?.[0]?.payments?.authorizations?.[0]?.custom_id ||
|
|
35
|
+
resource?.purchase_units?.[0]?.payments?.refunds?.[0]?.custom_id
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function resolveAmount(resource: any): number | undefined {
|
|
40
|
+
const amount =
|
|
41
|
+
resource?.amount ||
|
|
42
|
+
resource?.purchase_units?.[0]?.amount ||
|
|
43
|
+
resource?.purchase_units?.[0]?.payments?.captures?.[0]?.amount ||
|
|
44
|
+
resource?.purchase_units?.[0]?.payments?.authorizations?.[0]?.amount ||
|
|
45
|
+
resource?.purchase_units?.[0]?.payments?.refunds?.[0]?.amount ||
|
|
46
|
+
resource?.seller_receivable_breakdown?.gross_amount
|
|
47
|
+
const value = amount?.value ?? amount?.amount?.value
|
|
48
|
+
if (value === undefined || value === null) {
|
|
49
|
+
return undefined
|
|
50
|
+
}
|
|
51
|
+
const parsed = Number(value)
|
|
52
|
+
return Number.isFinite(parsed) ? parsed : undefined
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function resolveCurrencyCode(resource: any): string | undefined {
|
|
56
|
+
const amount =
|
|
57
|
+
resource?.amount ||
|
|
58
|
+
resource?.purchase_units?.[0]?.amount ||
|
|
59
|
+
resource?.purchase_units?.[0]?.payments?.captures?.[0]?.amount ||
|
|
60
|
+
resource?.purchase_units?.[0]?.payments?.authorizations?.[0]?.amount
|
|
61
|
+
return amount?.currency_code || undefined
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function resolveEventType(payload: ProviderWebhookPayload["payload"]) {
|
|
65
|
+
const raw = payload as { event_type?: string; eventType?: string }
|
|
66
|
+
return raw?.event_type || raw?.eventType
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Maps a PayPal webhook payload to the generic Medusa payment-provider webhook
|
|
71
|
+
* result (the `getWebhookActionAndData` hook on AbstractPaymentProvider).
|
|
72
|
+
*
|
|
73
|
+
* NOTE: the authoritative webhook handling for this plugin is the dedicated
|
|
74
|
+
* `/store/paypal/webhook` route (signature verification, replay window,
|
|
75
|
+
* dedup, dead-letter retry). This helper exists only for the framework's
|
|
76
|
+
* generic `/hooks/payment/:provider` path. The `session_id` returned here is
|
|
77
|
+
* derived from the PayPal resource `custom_id`, which this plugin sets to the
|
|
78
|
+
* cart id (see create-order) — so consumers must resolve the cart, not treat
|
|
79
|
+
* it as a payment-session id directly.
|
|
80
|
+
*/
|
|
81
|
+
export function getPayPalWebhookActionAndData(
|
|
82
|
+
payload: ProviderWebhookPayload["payload"]
|
|
83
|
+
): WebhookActionResult {
|
|
84
|
+
const eventType = resolveEventType(payload)
|
|
85
|
+
if (!eventType) {
|
|
86
|
+
return { action: "not_supported" }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const action = EVENT_ACTIONS[eventType]
|
|
90
|
+
if (!action) {
|
|
91
|
+
return { action: "not_supported" }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const resource = (payload as { resource?: unknown })?.resource
|
|
95
|
+
const sessionId = resolveSessionId(resource)
|
|
96
|
+
const amount = resolveAmount(resource)
|
|
97
|
+
|
|
98
|
+
if (!sessionId || amount === undefined) {
|
|
99
|
+
return { action: "not_supported" }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
action,
|
|
104
|
+
data: {
|
|
105
|
+
session_id: sessionId,
|
|
106
|
+
amount,
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
}
|