@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,370 +1,416 @@
|
|
|
1
|
-
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
-
import type { Logger } from "@medusajs/framework/types"
|
|
3
|
-
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
|
4
|
-
import { randomUUID } from "crypto"
|
|
5
|
-
import type PayPalModuleService from "../../../../modules/paypal/service"
|
|
6
|
-
import { getPayPalApiBase } from "../../../../modules/paypal/utils/paypal-auth"
|
|
7
|
-
import { PAYPAL_PARTNER_ATTRIBUTION_ID as BN_CODE } from "../../../../modules/paypal/utils/partner"
|
|
8
|
-
import { paypalFetch } from "../../../../modules/paypal/utils/paypal-fetch"
|
|
9
|
-
import { extractCaptureStatus } from "../../../../modules/paypal/payment-provider/status-utils"
|
|
10
|
-
import {
|
|
11
|
-
findPayPalSessionForCart,
|
|
12
|
-
getStoredPayPalOrderId,
|
|
13
|
-
updatePayPalSessionData,
|
|
14
|
-
} from "../../../../modules/paypal/utils/payment-session"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
*
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
*
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
return null
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
message: "
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
return
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
)
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
)
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
1
|
+
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import type { Logger } from "@medusajs/framework/types"
|
|
3
|
+
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
|
4
|
+
import { randomUUID } from "crypto"
|
|
5
|
+
import type PayPalModuleService from "../../../../modules/paypal/service"
|
|
6
|
+
import { getPayPalApiBase } from "../../../../modules/paypal/utils/paypal-auth"
|
|
7
|
+
import { PAYPAL_PARTNER_ATTRIBUTION_ID as BN_CODE } from "../../../../modules/paypal/utils/partner"
|
|
8
|
+
import { paypalFetch } from "../../../../modules/paypal/utils/paypal-fetch"
|
|
9
|
+
import { extractCaptureStatus } from "../../../../modules/paypal/payment-provider/status-utils"
|
|
10
|
+
import {
|
|
11
|
+
findPayPalSessionForCart,
|
|
12
|
+
getStoredPayPalOrderId,
|
|
13
|
+
updatePayPalSessionData,
|
|
14
|
+
} from "../../../../modules/paypal/utils/payment-session"
|
|
15
|
+
|
|
16
|
+
/** Shape of a PayPal order id — rejects anything that is not one before it hits the API. */
|
|
17
|
+
const PAYPAL_ORDER_ID_RE = /^[A-Z0-9]{10,25}$/
|
|
18
|
+
|
|
19
|
+
type Body = {
|
|
20
|
+
cart_id: string
|
|
21
|
+
order_id: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function resolveIdempotencyKey(
|
|
25
|
+
req: MedusaRequest,
|
|
26
|
+
suffix: string,
|
|
27
|
+
fallback: string
|
|
28
|
+
) {
|
|
29
|
+
const header =
|
|
30
|
+
req.headers["idempotency-key"] ||
|
|
31
|
+
req.headers["Idempotency-Key"] ||
|
|
32
|
+
req.headers["x-idempotency-key"] ||
|
|
33
|
+
req.headers["X-Idempotency-Key"]
|
|
34
|
+
const key = Array.isArray(header) ? header[0] : header
|
|
35
|
+
if (key && String(key).trim()) {
|
|
36
|
+
return `${String(key).trim()}-${suffix}`
|
|
37
|
+
}
|
|
38
|
+
return fallback || `pp-${suffix}-${randomUUID()}`
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Persist the capture onto the cart's PayPal session. Returns `true` when the
|
|
43
|
+
* data was written (or there was nothing to write because no session exists),
|
|
44
|
+
* and `false` when the write ultimately failed after retries. The caller must
|
|
45
|
+
* NOT silently treat a `false` here as success: the funds were captured at
|
|
46
|
+
* PayPal, so a persistence failure has to be recorded/alerted (the webhook and
|
|
47
|
+
* paypal-complete's live re-derivation are the reconciliation backstop).
|
|
48
|
+
*/
|
|
49
|
+
async function attachPayPalCaptureToSession(
|
|
50
|
+
cartId: string,
|
|
51
|
+
orderId: string,
|
|
52
|
+
capture: any,
|
|
53
|
+
scope: any
|
|
54
|
+
): Promise<boolean> {
|
|
55
|
+
const session = await findPayPalSessionForCart(cartId, scope)
|
|
56
|
+
if (!session) {
|
|
57
|
+
console.warn(
|
|
58
|
+
"[PayPal] attachPayPalCaptureToSession: no session found for cart",
|
|
59
|
+
cartId
|
|
60
|
+
)
|
|
61
|
+
return true
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const captureId =
|
|
65
|
+
capture?.purchase_units?.[0]?.payments?.captures?.[0]?.id || capture?.id
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
await updatePayPalSessionData(
|
|
69
|
+
session.session_id,
|
|
70
|
+
{
|
|
71
|
+
paypal: {
|
|
72
|
+
...((session.session_data || {}).paypal || {}),
|
|
73
|
+
order_id: orderId,
|
|
74
|
+
capture_id: captureId,
|
|
75
|
+
capture,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
scope
|
|
79
|
+
)
|
|
80
|
+
return true
|
|
81
|
+
} catch (e: unknown) {
|
|
82
|
+
console.error(
|
|
83
|
+
"[PayPal] attachPayPalCaptureToSession failed:",
|
|
84
|
+
e instanceof Error ? e.message : e
|
|
85
|
+
)
|
|
86
|
+
return false
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Authorize-intent counterpart of `attachPayPalCaptureToSession`; same contract. */
|
|
91
|
+
async function attachPayPalAuthorizationToSession(
|
|
92
|
+
cartId: string,
|
|
93
|
+
orderId: string,
|
|
94
|
+
authorization: any,
|
|
95
|
+
scope: any
|
|
96
|
+
): Promise<boolean> {
|
|
97
|
+
const session = await findPayPalSessionForCart(cartId, scope)
|
|
98
|
+
if (!session) {
|
|
99
|
+
console.warn(
|
|
100
|
+
"[PayPal] attachPayPalAuthorizationToSession: no session found for cart",
|
|
101
|
+
cartId
|
|
102
|
+
)
|
|
103
|
+
return true
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const authorizationId =
|
|
107
|
+
authorization?.purchase_units?.[0]?.payments?.authorizations?.[0]?.id
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
await updatePayPalSessionData(
|
|
111
|
+
session.session_id,
|
|
112
|
+
{
|
|
113
|
+
paypal: {
|
|
114
|
+
...((session.session_data || {}).paypal || {}),
|
|
115
|
+
order_id: orderId,
|
|
116
|
+
authorization_id: authorizationId,
|
|
117
|
+
authorization,
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
scope
|
|
121
|
+
)
|
|
122
|
+
return true
|
|
123
|
+
} catch (e: unknown) {
|
|
124
|
+
console.error(
|
|
125
|
+
"[PayPal] attachPayPalAuthorizationToSession failed:",
|
|
126
|
+
e instanceof Error ? e.message : e
|
|
127
|
+
)
|
|
128
|
+
return false
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Return the session's existing capture ONLY when it demonstrably COMPLETED.
|
|
134
|
+
*
|
|
135
|
+
* The session can carry a capture that never settled: the webhook processor
|
|
136
|
+
* patches `capture_id` onto the session for every capture event, including
|
|
137
|
+
* PAYMENT.CAPTURE.DENIED and PENDING. Blindly short-circuiting on any stored
|
|
138
|
+
* capture would report success to the storefront for a payment that was
|
|
139
|
+
* declined — the buyer would then hit "payment processed but order could not
|
|
140
|
+
* be finalized" instead of being able to retry.
|
|
141
|
+
*
|
|
142
|
+
* - stored capture object with a known status: COMPLETED → return it,
|
|
143
|
+
* anything else → null (fall through and attempt a real capture).
|
|
144
|
+
* - bare `capture_id` (or a capture object whose status can't be read):
|
|
145
|
+
* verify against the live PayPal order. If PayPal can't be reached, return
|
|
146
|
+
* the stored data — the previous fail-safe behavior — since attempting a
|
|
147
|
+
* fresh capture against an unreachable PayPal would fail anyway.
|
|
148
|
+
*/
|
|
149
|
+
async function getExistingCompletedCapture(
|
|
150
|
+
cartId: string,
|
|
151
|
+
orderId: string,
|
|
152
|
+
scope: any,
|
|
153
|
+
fetchLiveOrder: () => Promise<any>
|
|
154
|
+
) {
|
|
155
|
+
try {
|
|
156
|
+
const session = await findPayPalSessionForCart(cartId, scope)
|
|
157
|
+
if (!session) return null
|
|
158
|
+
|
|
159
|
+
const paypalData = (session.session_data || {}).paypal || {}
|
|
160
|
+
const existingOrderId = String(paypalData.order_id || "")
|
|
161
|
+
if (existingOrderId && existingOrderId !== orderId) return null
|
|
162
|
+
|
|
163
|
+
const stored = paypalData.capture
|
|
164
|
+
if (stored) {
|
|
165
|
+
const storedStatus = extractCaptureStatus(stored)
|
|
166
|
+
if (storedStatus === "COMPLETED") return stored
|
|
167
|
+
if (storedStatus) return null
|
|
168
|
+
// no readable status on the stored object — verify live below
|
|
169
|
+
}
|
|
170
|
+
if (!stored && !paypalData.capture_id) return null
|
|
171
|
+
|
|
172
|
+
let order: any
|
|
173
|
+
try {
|
|
174
|
+
order = await fetchLiveOrder()
|
|
175
|
+
} catch {
|
|
176
|
+
return stored || { id: paypalData.capture_id }
|
|
177
|
+
}
|
|
178
|
+
const liveCapture = order?.purchase_units?.[0]?.payments?.captures?.[0]
|
|
179
|
+
if (liveCapture && extractCaptureStatus(liveCapture) === "COMPLETED") {
|
|
180
|
+
return liveCapture
|
|
181
|
+
}
|
|
182
|
+
return null
|
|
183
|
+
} catch {
|
|
184
|
+
return null
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* POST /store/paypal/capture-order — capture (or, for authorize-intent
|
|
190
|
+
* merchants, authorize) the PayPal order the buyer just approved.
|
|
191
|
+
*
|
|
192
|
+
* Fail-closed: the supplied `order_id` must equal the id create-order stored
|
|
193
|
+
* on this cart's session, and only a COMPLETED capture is reported as success.
|
|
194
|
+
* Responds `{ capture }` or `{ authorization }` with the raw PayPal resource.
|
|
195
|
+
*/
|
|
196
|
+
export async function POST(req: MedusaRequest, res: MedusaResponse) {
|
|
197
|
+
const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
|
|
198
|
+
const logger = req.scope.resolve<Logger>(ContainerRegistrationKeys.LOGGER)
|
|
199
|
+
const requestId = randomUUID()
|
|
200
|
+
const { scope } = req
|
|
201
|
+
let debugId: string | null = null
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
const body = (req.body || {}) as Body
|
|
205
|
+
const cartId = body.cart_id
|
|
206
|
+
const orderId = body.order_id
|
|
207
|
+
|
|
208
|
+
if (!cartId || !orderId) {
|
|
209
|
+
return res
|
|
210
|
+
.status(400)
|
|
211
|
+
.json({ message: "cart_id and order_id are required" })
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (typeof cartId !== "string" || !cartId.startsWith("cart_")) {
|
|
215
|
+
return res.status(400).json({ message: "Invalid cart_id format" })
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (!PAYPAL_ORDER_ID_RE.test(orderId)) {
|
|
219
|
+
return res.status(400).json({ message: "Invalid order_id format" })
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Bind the capture to the cart's own PayPal session, fail-closed: the
|
|
223
|
+
// order_id MUST be the one this cart's session created (stored by
|
|
224
|
+
// create-order). This prevents capturing an arbitrary, caller-supplied
|
|
225
|
+
// order_id against the merchant account.
|
|
226
|
+
const session = await findPayPalSessionForCart(cartId, scope)
|
|
227
|
+
const sessionOrderId = getStoredPayPalOrderId(session?.session_data)
|
|
228
|
+
if (!session || !sessionOrderId) {
|
|
229
|
+
return res.status(409).json({
|
|
230
|
+
message:
|
|
231
|
+
"No PayPal order is associated with this cart's payment session",
|
|
232
|
+
})
|
|
233
|
+
}
|
|
234
|
+
if (sessionOrderId !== orderId) {
|
|
235
|
+
return res.status(400).json({
|
|
236
|
+
message: "order_id does not match the payment session for this cart",
|
|
237
|
+
})
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const creds = await paypal.getActiveCredentials()
|
|
241
|
+
// Cached app access token (single-flight refresh) — avoids an OAuth
|
|
242
|
+
// round-trip on every capture-order call.
|
|
243
|
+
const base = getPayPalApiBase(creds.environment)
|
|
244
|
+
const accessToken = await paypal.getAppAccessToken()
|
|
245
|
+
|
|
246
|
+
const fetchLiveOrder = async () => {
|
|
247
|
+
const resp = await paypalFetch(
|
|
248
|
+
`${base}/v2/checkout/orders/${encodeURIComponent(orderId)}`,
|
|
249
|
+
{
|
|
250
|
+
method: "GET",
|
|
251
|
+
headers: {
|
|
252
|
+
Authorization: `Bearer ${accessToken}`,
|
|
253
|
+
"Content-Type": "application/json",
|
|
254
|
+
},
|
|
255
|
+
}
|
|
256
|
+
)
|
|
257
|
+
if (!resp.ok) {
|
|
258
|
+
throw new Error(`PayPal get order error (${resp.status})`)
|
|
259
|
+
}
|
|
260
|
+
return resp.json()
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const existingCapture = await getExistingCompletedCapture(
|
|
264
|
+
cartId,
|
|
265
|
+
orderId,
|
|
266
|
+
scope,
|
|
267
|
+
fetchLiveOrder
|
|
268
|
+
)
|
|
269
|
+
if (existingCapture) {
|
|
270
|
+
return res.json({ capture: existingCapture })
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const settings = await paypal.getSettings().catch(() => ({}))
|
|
274
|
+
const data =
|
|
275
|
+
settings && typeof settings === "object" && "data" in settings
|
|
276
|
+
? ((settings as { data?: Record<string, any> }).data ?? {})
|
|
277
|
+
: {}
|
|
278
|
+
const additionalSettings = (data.additional_settings || {}) as Record<
|
|
279
|
+
string,
|
|
280
|
+
any
|
|
281
|
+
>
|
|
282
|
+
const paymentAction =
|
|
283
|
+
typeof additionalSettings.paymentAction === "string"
|
|
284
|
+
? additionalSettings.paymentAction
|
|
285
|
+
: "capture"
|
|
286
|
+
|
|
287
|
+
// The suffix must include the order id: PayPal deduplicates by
|
|
288
|
+
// PayPal-Request-Id, so a client reusing one Idempotency-Key header across
|
|
289
|
+
// two different orders would otherwise get order A's cached capture back
|
|
290
|
+
// for order B. Named `paypalRequestId` so it never shadows `requestId`
|
|
291
|
+
// (the log/response correlation UUID declared at the top of POST).
|
|
292
|
+
const paypalRequestId = resolveIdempotencyKey(
|
|
293
|
+
req,
|
|
294
|
+
`capture-order-${orderId}`,
|
|
295
|
+
`pp-capture-${orderId}`
|
|
296
|
+
)
|
|
297
|
+
const safeOrderId = encodeURIComponent(orderId)
|
|
298
|
+
const endpoint =
|
|
299
|
+
paymentAction === "authorize"
|
|
300
|
+
? `${base}/v2/checkout/orders/${safeOrderId}/authorize`
|
|
301
|
+
: `${base}/v2/checkout/orders/${safeOrderId}/capture`
|
|
302
|
+
|
|
303
|
+
const ppResp = await paypalFetch(endpoint, {
|
|
304
|
+
method: "POST",
|
|
305
|
+
headers: {
|
|
306
|
+
Authorization: `Bearer ${accessToken}`,
|
|
307
|
+
"Content-Type": "application/json",
|
|
308
|
+
"PayPal-Request-Id": paypalRequestId,
|
|
309
|
+
"PayPal-Partner-Attribution-Id": BN_CODE,
|
|
310
|
+
},
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
const ppText = await ppResp.text()
|
|
314
|
+
debugId = ppResp.headers.get("paypal-debug-id")
|
|
315
|
+
if (!ppResp.ok) {
|
|
316
|
+
throw new Error(
|
|
317
|
+
`PayPal capture error (${ppResp.status}): ${ppText}${debugId ? ` debug_id=${debugId}` : ""}`
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const payload = JSON.parse(ppText)
|
|
322
|
+
let persisted = true
|
|
323
|
+
if (paymentAction === "authorize") {
|
|
324
|
+
persisted = await attachPayPalAuthorizationToSession(
|
|
325
|
+
cartId,
|
|
326
|
+
orderId,
|
|
327
|
+
payload,
|
|
328
|
+
req.scope
|
|
329
|
+
)
|
|
330
|
+
} else {
|
|
331
|
+
// A 2xx capture response does NOT guarantee the funds settled: PayPal
|
|
332
|
+
// returns 201 for PENDING (pending review / eCheck), DECLINED and FAILED
|
|
333
|
+
// captures too. Returning those to the storefront as a successful capture
|
|
334
|
+
// would let it finalize the cart for a payment that never completed, so
|
|
335
|
+
// only a COMPLETED capture is reported as success — the webhook will
|
|
336
|
+
// reconcile a later PENDING→COMPLETED transition.
|
|
337
|
+
const captureStatus = extractCaptureStatus(payload)
|
|
338
|
+
if (captureStatus !== "COMPLETED") {
|
|
339
|
+
throw new Error(
|
|
340
|
+
`PayPal capture did not complete (status=${captureStatus || "UNKNOWN"})${
|
|
341
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
342
|
+
}`
|
|
343
|
+
)
|
|
344
|
+
}
|
|
345
|
+
persisted = await attachPayPalCaptureToSession(
|
|
346
|
+
cartId,
|
|
347
|
+
orderId,
|
|
348
|
+
payload,
|
|
349
|
+
req.scope
|
|
350
|
+
)
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// The PayPal operation itself succeeded. If we could not persist it onto the
|
|
354
|
+
// Medusa session, the money is (authorized/)captured but Medusa doesn't yet
|
|
355
|
+
// record it — record a metric and log CRITICAL so it's observable/alertable.
|
|
356
|
+
// We still return success to the storefront: re-capturing would hit
|
|
357
|
+
// ORDER_ALREADY_CAPTURED, and both the webhook and paypal-complete's live
|
|
358
|
+
// PayPal re-derivation reconcile the session afterward.
|
|
359
|
+
if (!persisted) {
|
|
360
|
+
logger.error(
|
|
361
|
+
`[paypal] CRITICAL: ${
|
|
362
|
+
paymentAction === "authorize" ? "authorization" : "capture"
|
|
363
|
+
} succeeded at PayPal but could not be persisted to the session (request_id=${requestId}, cart_id=${cartId}, order_id=${orderId})`
|
|
364
|
+
)
|
|
365
|
+
try {
|
|
366
|
+
await paypal.recordMetric(
|
|
367
|
+
paymentAction === "authorize"
|
|
368
|
+
? "authorize_order_persist_failed"
|
|
369
|
+
: "capture_order_persist_failed"
|
|
370
|
+
)
|
|
371
|
+
} catch {
|
|
372
|
+
// Metrics are best-effort.
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
try {
|
|
377
|
+
await paypal.recordMetric(
|
|
378
|
+
paymentAction === "authorize"
|
|
379
|
+
? "authorize_order_success"
|
|
380
|
+
: "capture_order_success"
|
|
381
|
+
)
|
|
382
|
+
} catch {
|
|
383
|
+
// Metrics are best-effort; the capture already succeeded.
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return paymentAction === "authorize"
|
|
387
|
+
? res.json({ authorization: payload })
|
|
388
|
+
: res.json({ capture: payload })
|
|
389
|
+
} catch (e: any) {
|
|
390
|
+
const body = (req.body || {}) as Body
|
|
391
|
+
logger.error(
|
|
392
|
+
`[paypal] capture-order failed (request_id=${requestId}, cart_id=${
|
|
393
|
+
body.cart_id ?? "n/a"
|
|
394
|
+
}, order_id=${body.order_id ?? "n/a"}, debug_id=${debugId ?? "n/a"}): ${
|
|
395
|
+
e?.message ?? String(e)
|
|
396
|
+
}`,
|
|
397
|
+
e instanceof Error ? e : undefined
|
|
398
|
+
)
|
|
399
|
+
try {
|
|
400
|
+
await paypal.recordAuditEvent("capture_order_failed", {
|
|
401
|
+
cart_id: body.cart_id,
|
|
402
|
+
order_id: body.order_id,
|
|
403
|
+
debug_id: debugId,
|
|
404
|
+
request_id: requestId,
|
|
405
|
+
message: e?.message || String(e),
|
|
406
|
+
})
|
|
407
|
+
await paypal.recordMetric("capture_order_failed")
|
|
408
|
+
} catch {
|
|
409
|
+
// Audit/metric writes are best-effort.
|
|
410
|
+
}
|
|
411
|
+
return res.status(500).json({
|
|
412
|
+
message: "Failed to capture PayPal order",
|
|
413
|
+
request_id: requestId,
|
|
414
|
+
})
|
|
415
|
+
}
|
|
416
|
+
}
|