@easypayment/medusa-payment-paypal 1.2.3 → 1.2.5
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 +392 -138
- package/.medusa/server/src/admin/index.mjs +392 -138
- 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 +11 -1
- 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 +80 -26
- 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 +55 -8
- 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 +24 -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 +296 -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 +14 -7
- 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 +40 -198
- 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 +9 -2
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.js +29 -181
- package/.medusa/server/src/modules/paypal/payment-provider/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/status-utils.d.ts +21 -0
- package/.medusa/server/src/modules/paypal/payment-provider/status-utils.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/status-utils.js +54 -0
- package/.medusa/server/src/modules/paypal/payment-provider/status-utils.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 +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 +45 -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 +65 -0
- package/README.md +184 -184
- package/package.json +7 -4
- 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 +69 -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 +611 -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 +184 -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 +700 -275
- package/src/modules/paypal/payment-provider/card-service.ts +693 -855
- package/src/modules/paypal/payment-provider/index.ts +14 -15
- package/src/modules/paypal/payment-provider/service.ts +673 -836
- package/src/modules/paypal/payment-provider/status-utils.ts +116 -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 +315 -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,539 +1,611 @@
|
|
|
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 { getCurrencyExponent } from "../../../../modules/paypal/utils/amounts"
|
|
6
|
-
import {
|
|
7
|
-
assertPayPalCurrencySupported,
|
|
8
|
-
normalizeCurrencyCode,
|
|
9
|
-
} from "../../../../modules/paypal/utils/currencies"
|
|
10
|
-
import { getPayPalApiBase } from "../../../../modules/paypal/utils/paypal-auth"
|
|
11
|
-
import { PAYPAL_PARTNER_ATTRIBUTION_ID as BN_CODE } from "../../../../modules/paypal/utils/partner"
|
|
12
|
-
import { paypalFetch } from "../../../../modules/paypal/utils/paypal-fetch"
|
|
13
|
-
import type PayPalModuleService from "../../../../modules/paypal/service"
|
|
14
|
-
import {
|
|
15
|
-
findPayPalSessionForCart,
|
|
16
|
-
getStoredPayPalOrderId,
|
|
17
|
-
updatePayPalSessionData,
|
|
18
|
-
} from "../../../../modules/paypal/utils/payment-session"
|
|
19
|
-
|
|
20
|
-
type Body = {
|
|
21
|
-
cart_id: string
|
|
22
|
-
is_card_payment?: boolean
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function resolveIdempotencyKey(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
// the id
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
await
|
|
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
|
-
const
|
|
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
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
)
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const
|
|
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
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
const
|
|
294
|
-
const
|
|
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
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
//
|
|
434
|
-
//
|
|
435
|
-
//
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
const
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
},
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
}
|
|
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 { getCurrencyExponent } from "../../../../modules/paypal/utils/amounts"
|
|
6
|
+
import {
|
|
7
|
+
assertPayPalCurrencySupported,
|
|
8
|
+
normalizeCurrencyCode,
|
|
9
|
+
} from "../../../../modules/paypal/utils/currencies"
|
|
10
|
+
import { getPayPalApiBase } from "../../../../modules/paypal/utils/paypal-auth"
|
|
11
|
+
import { PAYPAL_PARTNER_ATTRIBUTION_ID as BN_CODE } from "../../../../modules/paypal/utils/partner"
|
|
12
|
+
import { paypalFetch } from "../../../../modules/paypal/utils/paypal-fetch"
|
|
13
|
+
import type PayPalModuleService from "../../../../modules/paypal/service"
|
|
14
|
+
import {
|
|
15
|
+
findPayPalSessionForCart,
|
|
16
|
+
getStoredPayPalOrderId,
|
|
17
|
+
updatePayPalSessionData,
|
|
18
|
+
} from "../../../../modules/paypal/utils/payment-session"
|
|
19
|
+
|
|
20
|
+
type Body = {
|
|
21
|
+
cart_id: string
|
|
22
|
+
is_card_payment?: boolean
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function resolveIdempotencyKey(
|
|
26
|
+
req: MedusaRequest,
|
|
27
|
+
suffix: string,
|
|
28
|
+
fallback: string
|
|
29
|
+
) {
|
|
30
|
+
const header =
|
|
31
|
+
req.headers["idempotency-key"] ||
|
|
32
|
+
req.headers["Idempotency-Key"] ||
|
|
33
|
+
req.headers["x-idempotency-key"] ||
|
|
34
|
+
req.headers["X-Idempotency-Key"]
|
|
35
|
+
const key = Array.isArray(header) ? header[0] : header
|
|
36
|
+
if (key && String(key).trim()) {
|
|
37
|
+
// The suffix must include the cart id (callers pass it): PayPal caches by
|
|
38
|
+
// PayPal-Request-Id, so a client reusing one idempotency-key header across
|
|
39
|
+
// two carts would otherwise get cart A's cached order (A's amount) back
|
|
40
|
+
// for cart B.
|
|
41
|
+
return `${String(key).trim()}-${suffix}`
|
|
42
|
+
}
|
|
43
|
+
return fallback || `pp-${suffix}-${randomUUID()}`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Persist the PayPal order id onto the cart's PayPal payment session so that
|
|
47
|
+
// capture-order can bind the capture to the order this cart created. Resolved
|
|
48
|
+
// through the Payment module service (the previous direct-container resolve of
|
|
49
|
+
// "payment_collection"/"payment_session" did not exist in the request scope, so
|
|
50
|
+
// the id was silently never stored and every capture failed fail-closed).
|
|
51
|
+
async function attachPayPalOrderToSession(
|
|
52
|
+
req: MedusaRequest,
|
|
53
|
+
cartId: string,
|
|
54
|
+
orderId: string
|
|
55
|
+
) {
|
|
56
|
+
const session = await findPayPalSessionForCart(cartId, req.scope)
|
|
57
|
+
if (!session) {
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
await updatePayPalSessionData(
|
|
61
|
+
session.session_id,
|
|
62
|
+
{
|
|
63
|
+
paypal: {
|
|
64
|
+
...(session.session_data.paypal || {}),
|
|
65
|
+
order_id: orderId,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
req.scope
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function getExistingPayPalOrderId(req: MedusaRequest, cartId: string) {
|
|
73
|
+
const session = await findPayPalSessionForCart(cartId, req.scope)
|
|
74
|
+
return getStoredPayPalOrderId(session?.session_data)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Storefront return URL from env (`STOREFRONT_URL` / `STORE_URL`), if set. */
|
|
78
|
+
function resolveReturnUrl() {
|
|
79
|
+
const configured = process.env.STOREFRONT_URL || process.env.STORE_URL
|
|
80
|
+
if (!configured) {
|
|
81
|
+
return undefined
|
|
82
|
+
}
|
|
83
|
+
return `${configured.replace(/\/$/, "")}/checkout`
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Storefront cancel URL from env (`STOREFRONT_URL` / `STORE_URL`), if set. */
|
|
87
|
+
function resolveCancelUrl() {
|
|
88
|
+
const configured = process.env.STOREFRONT_URL || process.env.STORE_URL
|
|
89
|
+
if (!configured) {
|
|
90
|
+
return undefined
|
|
91
|
+
}
|
|
92
|
+
return `${configured.replace(/\/$/, "")}/cart`
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* POST /store/paypal/create-order — create (or reuse) the PayPal order for a
|
|
97
|
+
* cart and store its id on the cart's PayPal payment session.
|
|
98
|
+
*
|
|
99
|
+
* Returns `{ id }` — the PayPal order id the storefront hands to the JS SDK.
|
|
100
|
+
* Line items and an amount breakdown are sent when `sendItemDetails` is on,
|
|
101
|
+
* with rounding drift reconciled so PayPal's strict total checks pass.
|
|
102
|
+
*/
|
|
103
|
+
export async function POST(req: MedusaRequest, res: MedusaResponse) {
|
|
104
|
+
const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
|
|
105
|
+
const logger = req.scope.resolve<Logger>(ContainerRegistrationKeys.LOGGER)
|
|
106
|
+
const requestId = randomUUID()
|
|
107
|
+
let debugId: string | null = null
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
const body = (req.body || {}) as Body
|
|
111
|
+
const cartId = body.cart_id
|
|
112
|
+
const isCardPayment = !!body.is_card_payment
|
|
113
|
+
|
|
114
|
+
if (!cartId || typeof cartId !== "string") {
|
|
115
|
+
return res.status(400).json({ message: "cart_id is required" })
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!cartId.startsWith("cart_")) {
|
|
119
|
+
return res.status(400).json({ message: "Invalid cart_id format" })
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// A stored order id is only reused after verifying it still matches the
|
|
123
|
+
// cart (amount/currency/state) — see below, once the cart is loaded. Buyers
|
|
124
|
+
// who back out of the PayPal popup and change their cart would otherwise be
|
|
125
|
+
// charged the stale total.
|
|
126
|
+
const existingOrderId = await getExistingPayPalOrderId(req, cartId)
|
|
127
|
+
|
|
128
|
+
const query = req.scope.resolve("query")
|
|
129
|
+
|
|
130
|
+
// NOTE: do not add `shipping_methods.*` here. Medusa's totals decorator
|
|
131
|
+
// computes shipping from whatever shipping-method rows are loaded, so a
|
|
132
|
+
// partial selection (e.g. `shipping_methods.id`, without `amount`) makes
|
|
133
|
+
// `total`/`shipping_subtotal` come back WITHOUT shipping and the buyer is
|
|
134
|
+
// under-charged. Left unrequested, the query computes every total below
|
|
135
|
+
// (including `shipping_subtotal`) from the full cart.
|
|
136
|
+
const { data } = await query.graph({
|
|
137
|
+
entity: "cart",
|
|
138
|
+
fields: [
|
|
139
|
+
"id",
|
|
140
|
+
"total",
|
|
141
|
+
"subtotal",
|
|
142
|
+
"item_subtotal",
|
|
143
|
+
"shipping_subtotal",
|
|
144
|
+
"shipping_total",
|
|
145
|
+
"tax_total",
|
|
146
|
+
"discount_total",
|
|
147
|
+
"discount_subtotal",
|
|
148
|
+
"currency_code",
|
|
149
|
+
"region.currency_code",
|
|
150
|
+
"items.title",
|
|
151
|
+
"items.quantity",
|
|
152
|
+
"items.unit_price",
|
|
153
|
+
"items.subtotal",
|
|
154
|
+
"items.tax_total",
|
|
155
|
+
"items.total",
|
|
156
|
+
],
|
|
157
|
+
filters: { id: cartId },
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
const cart = (data?.[0] as any) || null
|
|
161
|
+
|
|
162
|
+
if (!cart) {
|
|
163
|
+
return res.status(404).json({ message: "Cart not found" })
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const creds = await paypal.getActiveCredentials()
|
|
167
|
+
|
|
168
|
+
type PayPalSettingsResponse = {
|
|
169
|
+
data?: {
|
|
170
|
+
additional_settings?: Record<string, unknown>
|
|
171
|
+
api_details?: Record<string, unknown>
|
|
172
|
+
advanced_card_payments?: Record<string, unknown>
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const settings = await paypal
|
|
176
|
+
.getSettings()
|
|
177
|
+
.catch((): PayPalSettingsResponse => ({}))
|
|
178
|
+
const settingsData = (settings.data || {}) as Record<string, any>
|
|
179
|
+
const additionalSettings = settingsData.additional_settings || {}
|
|
180
|
+
const apiDetails = settingsData.api_details || {}
|
|
181
|
+
const advancedCardSettings = (settingsData.advanced_card_payments ||
|
|
182
|
+
{}) as Record<string, any>
|
|
183
|
+
|
|
184
|
+
const threeDsRaw =
|
|
185
|
+
typeof advancedCardSettings.threeDS === "string"
|
|
186
|
+
? advancedCardSettings.threeDS
|
|
187
|
+
: "when_required"
|
|
188
|
+
|
|
189
|
+
const threeDsMethod: string | null = isCardPayment
|
|
190
|
+
? threeDsRaw === "always"
|
|
191
|
+
? "SCA_ALWAYS"
|
|
192
|
+
: "SCA_WHEN_REQUIRED"
|
|
193
|
+
: null
|
|
194
|
+
const configuredCurrency =
|
|
195
|
+
typeof apiDetails.currency_code === "string"
|
|
196
|
+
? normalizeCurrencyCode(apiDetails.currency_code)
|
|
197
|
+
: normalizeCurrencyCode(process.env.PAYPAL_CURRENCY || "EUR")
|
|
198
|
+
|
|
199
|
+
const currency = normalizeCurrencyCode(
|
|
200
|
+
cart.region?.currency_code || cart.currency_code || configuredCurrency
|
|
201
|
+
)
|
|
202
|
+
assertPayPalCurrencySupported({
|
|
203
|
+
currencyCode: currency,
|
|
204
|
+
paypalCurrencyOverride: configuredCurrency,
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
const exponent = getCurrencyExponent(currency)
|
|
208
|
+
const totalMajor = Number(cart.total || 0)
|
|
209
|
+
const value = totalMajor.toFixed(exponent)
|
|
210
|
+
|
|
211
|
+
// Reuse the stored PayPal order only when it still matches the cart: same
|
|
212
|
+
// amount, same currency, and still in a reusable (pre-terminal) state. On
|
|
213
|
+
// mismatch fall through and create a fresh order (which replaces the
|
|
214
|
+
// stored id on the session). If PayPal can't be reached for the check,
|
|
215
|
+
// reuse the stored id — matching the previous behavior rather than
|
|
216
|
+
// blocking checkout on a transient error.
|
|
217
|
+
if (existingOrderId) {
|
|
218
|
+
try {
|
|
219
|
+
const checkBase = getPayPalApiBase(creds.environment)
|
|
220
|
+
const checkToken = await paypal.getAppAccessToken()
|
|
221
|
+
const checkResp = await paypalFetch(
|
|
222
|
+
`${checkBase}/v2/checkout/orders/${encodeURIComponent(existingOrderId)}`,
|
|
223
|
+
{
|
|
224
|
+
method: "GET",
|
|
225
|
+
headers: {
|
|
226
|
+
Authorization: `Bearer ${checkToken}`,
|
|
227
|
+
"Content-Type": "application/json",
|
|
228
|
+
},
|
|
229
|
+
}
|
|
230
|
+
)
|
|
231
|
+
if (checkResp.ok) {
|
|
232
|
+
const existingOrder = await checkResp.json()
|
|
233
|
+
const existingAmount = existingOrder?.purchase_units?.[0]?.amount
|
|
234
|
+
const existingStatus = String(
|
|
235
|
+
existingOrder?.status || ""
|
|
236
|
+
).toUpperCase()
|
|
237
|
+
const reusableStatus = [
|
|
238
|
+
"CREATED",
|
|
239
|
+
"APPROVED",
|
|
240
|
+
"PAYER_ACTION_REQUIRED",
|
|
241
|
+
].includes(existingStatus)
|
|
242
|
+
const amountMatches =
|
|
243
|
+
String(existingAmount?.value || "") === value &&
|
|
244
|
+
String(existingAmount?.currency_code || "").toUpperCase() ===
|
|
245
|
+
currency
|
|
246
|
+
if (reusableStatus && amountMatches) {
|
|
247
|
+
return res.json({ id: existingOrderId })
|
|
248
|
+
}
|
|
249
|
+
if (existingStatus === "COMPLETED") {
|
|
250
|
+
// Already paid — never mint a second order for this cart.
|
|
251
|
+
return res.json({ id: existingOrderId })
|
|
252
|
+
}
|
|
253
|
+
logger.info(
|
|
254
|
+
`[paypal] create-order: stored order ${existingOrderId} is stale (status=${existingStatus}, amount=${existingAmount?.value} ${existingAmount?.currency_code} vs cart ${value} ${currency}) — creating a fresh order (request_id=${requestId})`
|
|
255
|
+
)
|
|
256
|
+
} else if (checkResp.status !== 404) {
|
|
257
|
+
// Transient PayPal-side failure: reuse rather than block checkout.
|
|
258
|
+
return res.json({ id: existingOrderId })
|
|
259
|
+
}
|
|
260
|
+
// 404: the stored order no longer exists (e.g. environment switched) —
|
|
261
|
+
// fall through and create a fresh one.
|
|
262
|
+
} catch {
|
|
263
|
+
return res.json({ id: existingOrderId })
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const paymentActionRaw =
|
|
268
|
+
typeof additionalSettings.paymentAction === "string"
|
|
269
|
+
? additionalSettings.paymentAction
|
|
270
|
+
: "capture"
|
|
271
|
+
const paymentAction =
|
|
272
|
+
paymentActionRaw === "authorize" ? "AUTHORIZE" : "CAPTURE"
|
|
273
|
+
const brandName =
|
|
274
|
+
typeof additionalSettings.brandName === "string"
|
|
275
|
+
? additionalSettings.brandName
|
|
276
|
+
: undefined
|
|
277
|
+
const landingPageRaw =
|
|
278
|
+
typeof additionalSettings.landingPage === "string"
|
|
279
|
+
? additionalSettings.landingPage
|
|
280
|
+
: undefined
|
|
281
|
+
const landingPage =
|
|
282
|
+
landingPageRaw === "login"
|
|
283
|
+
? "LOGIN"
|
|
284
|
+
: landingPageRaw === "billing"
|
|
285
|
+
? "BILLING"
|
|
286
|
+
: landingPageRaw === "no_preference"
|
|
287
|
+
? "NO_PREFERENCE"
|
|
288
|
+
: undefined
|
|
289
|
+
const requireInstantPayment =
|
|
290
|
+
typeof additionalSettings.requireInstantPayment === "boolean"
|
|
291
|
+
? additionalSettings.requireInstantPayment
|
|
292
|
+
: undefined
|
|
293
|
+
const sendItemDetails = additionalSettings.sendItemDetails !== false
|
|
294
|
+
const statementName =
|
|
295
|
+
typeof additionalSettings.creditCardStatementName === "string"
|
|
296
|
+
? additionalSettings.creditCardStatementName.trim()
|
|
297
|
+
: ""
|
|
298
|
+
const invoicePrefix =
|
|
299
|
+
typeof additionalSettings.invoicePrefix === "string"
|
|
300
|
+
? additionalSettings.invoicePrefix
|
|
301
|
+
: ""
|
|
302
|
+
const cartIdSuffix = cart.id.slice(-12).toUpperCase()
|
|
303
|
+
const invoiceId = `${invoicePrefix}${cartIdSuffix}`.trim() || cart.id
|
|
304
|
+
const returnUrl =
|
|
305
|
+
typeof apiDetails.storefront_url === "string" &&
|
|
306
|
+
apiDetails.storefront_url.trim()
|
|
307
|
+
? `${apiDetails.storefront_url.replace(/\/$/, "")}/checkout`
|
|
308
|
+
: resolveReturnUrl()
|
|
309
|
+
const cancelUrl =
|
|
310
|
+
typeof apiDetails.storefront_url === "string" &&
|
|
311
|
+
apiDetails.storefront_url.trim()
|
|
312
|
+
? `${apiDetails.storefront_url.replace(/\/$/, "")}/cart`
|
|
313
|
+
: resolveCancelUrl()
|
|
314
|
+
|
|
315
|
+
const applicationContext: Record<string, any> = {
|
|
316
|
+
...(brandName ? { brand_name: brandName } : {}),
|
|
317
|
+
...(landingPage ? { landing_page: landingPage } : {}),
|
|
318
|
+
...(requireInstantPayment
|
|
319
|
+
? { payment_method_preference: "IMMEDIATE_PAYMENT_REQUIRED" }
|
|
320
|
+
: {}),
|
|
321
|
+
...(returnUrl ? { return_url: returnUrl } : {}),
|
|
322
|
+
...(cancelUrl ? { cancel_url: cancelUrl } : {}),
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Medusa v2's `cart.subtotal` is items PLUS shipping (pre-tax, pre-discount)
|
|
326
|
+
// and its `total` is `subtotal + tax_total - discount_subtotal`. The PayPal
|
|
327
|
+
// breakdown needs the item-only subtotal for `item_total` — using
|
|
328
|
+
// `cart.subtotal` here made every cart with a shipping charge grow a bogus
|
|
329
|
+
// "Line Item Amount Offset" item worth the shipping fee plus a matching
|
|
330
|
+
// fake `shipping_discount` to make the numbers balance.
|
|
331
|
+
// Likewise `shipping_total` includes the tax on shipping, which
|
|
332
|
+
// `tax_total` already carries — PayPal's `shipping` must be the pre-tax
|
|
333
|
+
// `shipping_subtotal` or the breakdown is short by the shipping tax.
|
|
334
|
+
const shippingMajor = Number(
|
|
335
|
+
cart.shipping_subtotal ?? cart.shipping_total ?? 0
|
|
336
|
+
)
|
|
337
|
+
const subtotalMajor =
|
|
338
|
+
cart.item_subtotal !== undefined && cart.item_subtotal !== null
|
|
339
|
+
? Number(cart.item_subtotal || 0)
|
|
340
|
+
: Number(cart.subtotal || 0) - shippingMajor
|
|
341
|
+
const taxMajor = Number(cart.tax_total || 0)
|
|
342
|
+
// `discount_subtotal` is the pre-tax discount actually subtracted from the
|
|
343
|
+
// total; `discount_total` also carries the discount's tax share.
|
|
344
|
+
const discountMajor = Number(
|
|
345
|
+
cart.discount_subtotal ?? cart.discount_total ?? 0
|
|
346
|
+
)
|
|
347
|
+
const lineItems = Array.isArray((cart as any).items)
|
|
348
|
+
? (cart as any).items
|
|
349
|
+
: []
|
|
350
|
+
|
|
351
|
+
const purchaseItemsRaw = sendItemDetails
|
|
352
|
+
? lineItems
|
|
353
|
+
.map((item: any) => {
|
|
354
|
+
const quantity = Number(item?.quantity || 0)
|
|
355
|
+
const lineSubtotalMajor = Number(
|
|
356
|
+
item?.subtotal ?? Number(item?.unit_price || 0) * quantity
|
|
357
|
+
)
|
|
358
|
+
const unitAmount =
|
|
359
|
+
quantity > 0
|
|
360
|
+
? parseFloat((lineSubtotalMajor / quantity).toFixed(exponent))
|
|
361
|
+
: 0
|
|
362
|
+
|
|
363
|
+
if (
|
|
364
|
+
!quantity ||
|
|
365
|
+
Number.isNaN(quantity) ||
|
|
366
|
+
Number.isNaN(unitAmount)
|
|
367
|
+
) {
|
|
368
|
+
return null
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return {
|
|
372
|
+
quantity,
|
|
373
|
+
unitAmount,
|
|
374
|
+
paypalItem: {
|
|
375
|
+
name: String(item?.title || "Item").slice(0, 127),
|
|
376
|
+
quantity: String(Math.max(1, quantity)),
|
|
377
|
+
unit_amount: {
|
|
378
|
+
currency_code: currency,
|
|
379
|
+
value: unitAmount.toFixed(exponent),
|
|
380
|
+
},
|
|
381
|
+
},
|
|
382
|
+
}
|
|
383
|
+
})
|
|
384
|
+
.filter(Boolean)
|
|
385
|
+
: []
|
|
386
|
+
|
|
387
|
+
const roundedItemSum = purchaseItemsRaw.reduce(
|
|
388
|
+
(sum: number, item: any) => sum + item.unitAmount * item.quantity,
|
|
389
|
+
0
|
|
390
|
+
)
|
|
391
|
+
const roundedItemSumFixed = parseFloat(roundedItemSum.toFixed(exponent))
|
|
392
|
+
|
|
393
|
+
const finalPurchaseItems = purchaseItemsRaw.map(
|
|
394
|
+
(item: any) => item.paypalItem
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
// Reconcile rounding drift so the items we send sum to the cart subtotal.
|
|
398
|
+
if (finalPurchaseItems.length > 0) {
|
|
399
|
+
const diff = parseFloat(
|
|
400
|
+
(subtotalMajor - roundedItemSumFixed).toFixed(exponent)
|
|
401
|
+
)
|
|
402
|
+
if (Math.abs(diff) > 0.000001) {
|
|
403
|
+
finalPurchaseItems.push({
|
|
404
|
+
name: "Line Item Amount Offset",
|
|
405
|
+
quantity: "1",
|
|
406
|
+
unit_amount: {
|
|
407
|
+
currency_code: currency,
|
|
408
|
+
value: diff.toFixed(exponent),
|
|
409
|
+
},
|
|
410
|
+
})
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Only send line items (and an item_total) when we actually have them.
|
|
415
|
+
const sendItems = finalPurchaseItems.length > 0
|
|
416
|
+
|
|
417
|
+
// item_total MUST equal the exact sum of (unit_amount * quantity) of the
|
|
418
|
+
// items we send; otherwise PayPal rejects with ITEM_TOTAL_MISMATCH.
|
|
419
|
+
const itemsTotalMajor = sendItems
|
|
420
|
+
? parseFloat(
|
|
421
|
+
finalPurchaseItems
|
|
422
|
+
.reduce(
|
|
423
|
+
(sum: number, it: any) =>
|
|
424
|
+
sum + Number(it.unit_amount.value) * Number(it.quantity),
|
|
425
|
+
0
|
|
426
|
+
)
|
|
427
|
+
.toFixed(exponent)
|
|
428
|
+
)
|
|
429
|
+
: 0
|
|
430
|
+
|
|
431
|
+
const discountValue = discountMajor
|
|
432
|
+
|
|
433
|
+
// Build a breakdown only when we send line items. A breakdown whose
|
|
434
|
+
// item_total has no matching items (or items with no item_total) is
|
|
435
|
+
// rejected by PayPal, so with no items we send just the order total.
|
|
436
|
+
const breakdown: Record<string, any> = {}
|
|
437
|
+
if (sendItems) {
|
|
438
|
+
if (itemsTotalMajor > 0) {
|
|
439
|
+
breakdown.item_total = {
|
|
440
|
+
currency_code: currency,
|
|
441
|
+
value: itemsTotalMajor.toFixed(exponent),
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (shippingMajor > 0) {
|
|
445
|
+
breakdown.shipping = {
|
|
446
|
+
currency_code: currency,
|
|
447
|
+
value: shippingMajor.toFixed(exponent),
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (taxMajor > 0) {
|
|
451
|
+
breakdown.tax_total = {
|
|
452
|
+
currency_code: currency,
|
|
453
|
+
value: taxMajor.toFixed(exponent),
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
if (discountValue > 0) {
|
|
457
|
+
breakdown.discount = {
|
|
458
|
+
currency_code: currency,
|
|
459
|
+
value: discountValue.toFixed(exponent),
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const breakdownSum = parseFloat(
|
|
464
|
+
(itemsTotalMajor + shippingMajor + taxMajor - discountValue).toFixed(
|
|
465
|
+
exponent
|
|
466
|
+
)
|
|
467
|
+
)
|
|
468
|
+
|
|
469
|
+
if (Math.abs(breakdownSum - totalMajor) > 0.000001) {
|
|
470
|
+
const gap = parseFloat((totalMajor - breakdownSum).toFixed(exponent))
|
|
471
|
+
|
|
472
|
+
if (gap > 0) {
|
|
473
|
+
breakdown.tax_total = {
|
|
474
|
+
currency_code: currency,
|
|
475
|
+
value: parseFloat(
|
|
476
|
+
(Number(breakdown.tax_total?.value || 0) + gap).toFixed(exponent)
|
|
477
|
+
).toFixed(exponent),
|
|
478
|
+
}
|
|
479
|
+
} else {
|
|
480
|
+
breakdown.shipping_discount = {
|
|
481
|
+
currency_code: currency,
|
|
482
|
+
value: Math.abs(gap).toFixed(exponent),
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
logger.debug(
|
|
489
|
+
`[paypal] create-order line items (request_id=${requestId}): send_item_details=${sendItemDetails}, cart_items=${lineItems.length}, sent_items=${finalPurchaseItems.length}, items_total=${itemsTotalMajor}, subtotal=${subtotalMajor}, total=${totalMajor}`
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
// Use the module's cached app access token (single-flight refresh, ~9h TTL)
|
|
493
|
+
// instead of minting a fresh OAuth token on every create-order call.
|
|
494
|
+
const base = getPayPalApiBase(creds.environment)
|
|
495
|
+
const accessToken = await paypal.getAppAccessToken()
|
|
496
|
+
|
|
497
|
+
// Deterministic-per-(cart, amount, currency) idempotency key for the PayPal
|
|
498
|
+
// call. The amount/currency MUST be part of the key: PayPal replays the
|
|
499
|
+
// cached response for a reused PayPal-Request-Id, so a key derived from the
|
|
500
|
+
// cart id alone would return the ORIGINAL order (at the original total)
|
|
501
|
+
// when the buyer changes the cart and a fresh order is minted — silently
|
|
502
|
+
// re-charging the stale amount and defeating the staleness check above.
|
|
503
|
+
// Kept distinct from `requestId` (the log/response correlation id declared
|
|
504
|
+
// at the top of POST) so the two never shadow each other.
|
|
505
|
+
const paypalRequestId = resolveIdempotencyKey(
|
|
506
|
+
req,
|
|
507
|
+
`create-order-${cart.id}-${value}-${currency}`,
|
|
508
|
+
`pp-create-${cart.id}-${value}-${currency}`
|
|
509
|
+
)
|
|
510
|
+
|
|
511
|
+
const ppResp = await paypalFetch(`${base}/v2/checkout/orders`, {
|
|
512
|
+
method: "POST",
|
|
513
|
+
headers: {
|
|
514
|
+
Authorization: `Bearer ${accessToken}`,
|
|
515
|
+
"Content-Type": "application/json",
|
|
516
|
+
"PayPal-Request-Id": paypalRequestId,
|
|
517
|
+
"PayPal-Partner-Attribution-Id": BN_CODE,
|
|
518
|
+
},
|
|
519
|
+
body: JSON.stringify({
|
|
520
|
+
intent: paymentAction,
|
|
521
|
+
purchase_units: [
|
|
522
|
+
{
|
|
523
|
+
reference_id: "default",
|
|
524
|
+
// Set custom_id on the purchase unit (not just the order) so PayPal
|
|
525
|
+
// propagates the cart id onto capture/refund webhook resources,
|
|
526
|
+
// letting webhooks resolve the session directly without a scan.
|
|
527
|
+
custom_id: cart.id,
|
|
528
|
+
invoice_id: invoiceId,
|
|
529
|
+
...(statementName
|
|
530
|
+
? { soft_descriptor: statementName.slice(0, 22) }
|
|
531
|
+
: {}),
|
|
532
|
+
amount: {
|
|
533
|
+
currency_code: currency,
|
|
534
|
+
value,
|
|
535
|
+
...(Object.keys(breakdown).length > 0 ? { breakdown } : {}),
|
|
536
|
+
},
|
|
537
|
+
...(finalPurchaseItems.length > 0
|
|
538
|
+
? { items: finalPurchaseItems }
|
|
539
|
+
: {}),
|
|
540
|
+
},
|
|
541
|
+
],
|
|
542
|
+
...(threeDsMethod
|
|
543
|
+
? {
|
|
544
|
+
payment_source: {
|
|
545
|
+
card: {
|
|
546
|
+
attributes: {
|
|
547
|
+
verification: {
|
|
548
|
+
method: threeDsMethod,
|
|
549
|
+
},
|
|
550
|
+
},
|
|
551
|
+
},
|
|
552
|
+
},
|
|
553
|
+
}
|
|
554
|
+
: {}),
|
|
555
|
+
custom_id: cart.id,
|
|
556
|
+
...(Object.keys(applicationContext).length > 0
|
|
557
|
+
? { application_context: applicationContext }
|
|
558
|
+
: {}),
|
|
559
|
+
}),
|
|
560
|
+
})
|
|
561
|
+
|
|
562
|
+
const ppText = await ppResp.text()
|
|
563
|
+
debugId = ppResp.headers.get("paypal-debug-id")
|
|
564
|
+
if (!ppResp.ok) {
|
|
565
|
+
throw new Error(
|
|
566
|
+
`PayPal create order error (${ppResp.status}): ${ppText}${
|
|
567
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
568
|
+
}`
|
|
569
|
+
)
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const order = JSON.parse(ppText)
|
|
573
|
+
|
|
574
|
+
await attachPayPalOrderToSession(req, cart.id, order.id)
|
|
575
|
+
|
|
576
|
+
try {
|
|
577
|
+
await paypal.recordMetric("create_order_success")
|
|
578
|
+
} catch {
|
|
579
|
+
// Metrics are best-effort; never fail the order because of them.
|
|
580
|
+
}
|
|
581
|
+
return res.json({ id: order.id })
|
|
582
|
+
} catch (e: any) {
|
|
583
|
+
const body = (req.body || {}) as Body
|
|
584
|
+
logger.error(
|
|
585
|
+
`[paypal] create-order failed (request_id=${requestId}, cart_id=${
|
|
586
|
+
body.cart_id ?? "n/a"
|
|
587
|
+
}, debug_id=${debugId ?? "n/a"}): ${e?.message ?? String(e)}`,
|
|
588
|
+
e instanceof Error ? e : undefined
|
|
589
|
+
)
|
|
590
|
+
try {
|
|
591
|
+
await paypal.recordAuditEvent("create_order_failed", {
|
|
592
|
+
cart_id: body.cart_id,
|
|
593
|
+
debug_id: debugId,
|
|
594
|
+
request_id: requestId,
|
|
595
|
+
message: e?.message || String(e),
|
|
596
|
+
})
|
|
597
|
+
await paypal.recordMetric("create_order_failed")
|
|
598
|
+
} catch {
|
|
599
|
+
// Audit/metric writes are best-effort.
|
|
600
|
+
}
|
|
601
|
+
const rawMessage = e?.message || ""
|
|
602
|
+
const isCurrencyError =
|
|
603
|
+
rawMessage.includes("PayPal does not support currency") ||
|
|
604
|
+
rawMessage.includes("PayPal is configured for")
|
|
605
|
+
const status = isCurrencyError ? 400 : 500
|
|
606
|
+
const message = isCurrencyError
|
|
607
|
+
? rawMessage
|
|
608
|
+
: "Failed to create PayPal order"
|
|
609
|
+
return res.status(status).json({ message, request_id: requestId })
|
|
610
|
+
}
|
|
611
|
+
}
|