@easypayment/medusa-payment-paypal 1.2.3 → 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 +387 -133
- package/.medusa/server/src/admin/index.mjs +387 -133
- 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 +11 -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 +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,836 +1,906 @@
|
|
|
1
|
-
import { MedusaError } from "@medusajs/framework/utils"
|
|
2
|
-
import type {
|
|
3
|
-
AuthorizePaymentInput,
|
|
4
|
-
AuthorizePaymentOutput,
|
|
5
|
-
CapturePaymentInput,
|
|
6
|
-
CapturePaymentOutput,
|
|
7
|
-
CancelPaymentInput,
|
|
8
|
-
CancelPaymentOutput,
|
|
9
|
-
GetPaymentStatusInput,
|
|
10
|
-
GetPaymentStatusOutput,
|
|
11
|
-
InitiatePaymentInput,
|
|
12
|
-
InitiatePaymentOutput,
|
|
13
|
-
RefundPaymentInput,
|
|
14
|
-
RefundPaymentOutput,
|
|
15
|
-
RetrievePaymentInput,
|
|
16
|
-
RetrievePaymentOutput,
|
|
17
|
-
UpdatePaymentInput,
|
|
18
|
-
UpdatePaymentOutput,
|
|
19
|
-
} from "@medusajs/framework/types"
|
|
20
|
-
import { formatAmountForPayPal, toAmountNumber } from "../utils/amounts"
|
|
21
|
-
import {
|
|
22
|
-
assertPayPalCurrencySupported,
|
|
23
|
-
normalizeCurrencyCode,
|
|
24
|
-
} from "../utils/currencies"
|
|
25
|
-
import { PAYPAL_PARTNER_ATTRIBUTION_ID } from "../utils/partner"
|
|
26
|
-
import { paypalFetch, paypalFetchWithRetry } from "../utils/paypal-fetch"
|
|
27
|
-
import {
|
|
28
|
-
extractCaptureStatus,
|
|
29
|
-
isCaptureCompleted,
|
|
30
|
-
isRefundFailureStatus,
|
|
31
|
-
} from "./status-utils"
|
|
32
|
-
import { PayPalProviderBase } from "./base-provider"
|
|
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
|
-
const
|
|
117
|
-
if (
|
|
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
|
-
capture_id: capture.id,
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
order,
|
|
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
|
-
|
|
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
|
-
...(input.data || {}),
|
|
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
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
if (authorizationId) {
|
|
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
|
-
|
|
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
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
const
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
)
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
1
|
+
import { MedusaError } from "@medusajs/framework/utils"
|
|
2
|
+
import type {
|
|
3
|
+
AuthorizePaymentInput,
|
|
4
|
+
AuthorizePaymentOutput,
|
|
5
|
+
CapturePaymentInput,
|
|
6
|
+
CapturePaymentOutput,
|
|
7
|
+
CancelPaymentInput,
|
|
8
|
+
CancelPaymentOutput,
|
|
9
|
+
GetPaymentStatusInput,
|
|
10
|
+
GetPaymentStatusOutput,
|
|
11
|
+
InitiatePaymentInput,
|
|
12
|
+
InitiatePaymentOutput,
|
|
13
|
+
RefundPaymentInput,
|
|
14
|
+
RefundPaymentOutput,
|
|
15
|
+
RetrievePaymentInput,
|
|
16
|
+
RetrievePaymentOutput,
|
|
17
|
+
UpdatePaymentInput,
|
|
18
|
+
UpdatePaymentOutput,
|
|
19
|
+
} from "@medusajs/framework/types"
|
|
20
|
+
import { formatAmountForPayPal, toAmountNumber } from "../utils/amounts"
|
|
21
|
+
import {
|
|
22
|
+
assertPayPalCurrencySupported,
|
|
23
|
+
normalizeCurrencyCode,
|
|
24
|
+
} from "../utils/currencies"
|
|
25
|
+
import { PAYPAL_PARTNER_ATTRIBUTION_ID } from "../utils/partner"
|
|
26
|
+
import { paypalFetch, paypalFetchWithRetry } from "../utils/paypal-fetch"
|
|
27
|
+
import {
|
|
28
|
+
extractCaptureStatus,
|
|
29
|
+
isCaptureCompleted,
|
|
30
|
+
isRefundFailureStatus,
|
|
31
|
+
} from "./status-utils"
|
|
32
|
+
import { PayPalProviderBase } from "./base-provider"
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* PayPal wallet (Smart Buttons) payment provider — `pp_paypal_paypal`.
|
|
36
|
+
*
|
|
37
|
+
* The storefront creates and approves the PayPal order through the
|
|
38
|
+
* `/store/paypal/create-order` + `/store/paypal/capture-order` routes; this
|
|
39
|
+
* provider then authorizes/captures/refunds/cancels against the order id
|
|
40
|
+
* stored on the payment session.
|
|
41
|
+
*/
|
|
42
|
+
class PayPalPaymentProvider extends PayPalProviderBase {
|
|
43
|
+
static identifier = "paypal"
|
|
44
|
+
|
|
45
|
+
protected readonly sessionPrefix = "pp"
|
|
46
|
+
protected readonly idempotencyPrefix = "pp"
|
|
47
|
+
|
|
48
|
+
async initiatePayment(
|
|
49
|
+
input: InitiatePaymentInput
|
|
50
|
+
): Promise<InitiatePaymentOutput> {
|
|
51
|
+
const providerId = (input.data as Record<string, any> | undefined)
|
|
52
|
+
?.provider_id
|
|
53
|
+
try {
|
|
54
|
+
const currencyOverride = await this.resolveCurrencyOverride()
|
|
55
|
+
const currencyCode = normalizeCurrencyCode(
|
|
56
|
+
input.currency_code || currencyOverride || "EUR"
|
|
57
|
+
)
|
|
58
|
+
assertPayPalCurrencySupported({
|
|
59
|
+
currencyCode,
|
|
60
|
+
paypalCurrencyOverride: currencyOverride,
|
|
61
|
+
})
|
|
62
|
+
return {
|
|
63
|
+
id: this.generateSessionId(),
|
|
64
|
+
data: {
|
|
65
|
+
...(input.data || {}),
|
|
66
|
+
...(providerId ? { provider_id: providerId } : {}),
|
|
67
|
+
amount: input.amount,
|
|
68
|
+
currency_code: currencyCode,
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
} catch (error) {
|
|
72
|
+
await this.recordFailure("initiate_failed", {
|
|
73
|
+
error: this.serializeError(error),
|
|
74
|
+
currency_code: input.currency_code,
|
|
75
|
+
amount: input.amount,
|
|
76
|
+
provider_id: providerId,
|
|
77
|
+
data: input.data ?? null,
|
|
78
|
+
})
|
|
79
|
+
throw error
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
|
|
84
|
+
const currencyOverride = await this.resolveCurrencyOverride()
|
|
85
|
+
const currencyCode = normalizeCurrencyCode(
|
|
86
|
+
input.currency_code || currencyOverride || "EUR"
|
|
87
|
+
)
|
|
88
|
+
assertPayPalCurrencySupported({
|
|
89
|
+
currencyCode,
|
|
90
|
+
paypalCurrencyOverride: currencyOverride,
|
|
91
|
+
})
|
|
92
|
+
const providerId = (input.data as Record<string, any> | undefined)
|
|
93
|
+
?.provider_id
|
|
94
|
+
return {
|
|
95
|
+
data: {
|
|
96
|
+
...(input.data || {}),
|
|
97
|
+
...this.invalidateStaleOrder(input, currencyCode),
|
|
98
|
+
...(providerId ? { provider_id: providerId } : {}),
|
|
99
|
+
amount: input.amount,
|
|
100
|
+
currency_code: currencyCode,
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async authorizePayment(
|
|
106
|
+
input: AuthorizePaymentInput
|
|
107
|
+
): Promise<AuthorizePaymentOutput> {
|
|
108
|
+
const { data } = await this.normalizePaymentData(input)
|
|
109
|
+
const existingPayPal = (data.paypal || {}) as Record<string, any>
|
|
110
|
+
|
|
111
|
+
// Session already carries a settled capture: trust it without a network
|
|
112
|
+
// round-trip. The stored capture object is only written after a COMPLETED
|
|
113
|
+
// gate (capture-order route / capturePayment), but re-verify its status
|
|
114
|
+
// here so a PENDING/DECLINED capture patched in by a webhook is never
|
|
115
|
+
// booked as captured money.
|
|
116
|
+
const storedCaptureStatus = extractCaptureStatus(existingPayPal.capture)
|
|
117
|
+
if (storedCaptureStatus === "COMPLETED") {
|
|
118
|
+
console.info(
|
|
119
|
+
"[PayPal] authorizePayment: session already captured (COMPLETED)"
|
|
120
|
+
)
|
|
121
|
+
return {
|
|
122
|
+
status: "captured",
|
|
123
|
+
data: {
|
|
124
|
+
...(input.data || {}),
|
|
125
|
+
captured_at: (data as any).captured_at || new Date().toISOString(),
|
|
126
|
+
},
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const orderId = String(existingPayPal.order_id || data.order_id || "")
|
|
131
|
+
if (orderId) {
|
|
132
|
+
try {
|
|
133
|
+
console.info(
|
|
134
|
+
"[PayPal] authorizePayment: fetching live order status for",
|
|
135
|
+
orderId
|
|
136
|
+
)
|
|
137
|
+
const order = await this.getOrderDetails(orderId)
|
|
138
|
+
const capture = order?.purchase_units?.[0]?.payments?.captures?.[0]
|
|
139
|
+
const authorization =
|
|
140
|
+
order?.purchase_units?.[0]?.payments?.authorizations?.[0]
|
|
141
|
+
|
|
142
|
+
// Derive the returned status from what actually happened at PayPal —
|
|
143
|
+
// NOT from the mere presence of a capture/authorization id and NOT
|
|
144
|
+
// from the merchant's configured paymentAction. A PENDING capture
|
|
145
|
+
// (eCheck / pending review) must not be booked as captured, and a
|
|
146
|
+
// DENIED/DECLINED one must fail the authorization outright.
|
|
147
|
+
if (capture?.id) {
|
|
148
|
+
const captureStatus = this.mapCaptureStatus(capture?.status)
|
|
149
|
+
if (captureStatus === "captured") {
|
|
150
|
+
return {
|
|
151
|
+
status: "captured",
|
|
152
|
+
data: {
|
|
153
|
+
...(input.data || {}),
|
|
154
|
+
paypal: {
|
|
155
|
+
...existingPayPal,
|
|
156
|
+
order_id: orderId,
|
|
157
|
+
order,
|
|
158
|
+
authorization_id:
|
|
159
|
+
authorization?.id || existingPayPal.authorization_id,
|
|
160
|
+
capture_id: capture.id,
|
|
161
|
+
capture,
|
|
162
|
+
},
|
|
163
|
+
captured_at: new Date().toISOString(),
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (captureStatus === "pending") {
|
|
168
|
+
// Funds are in flight (eCheck etc.): place the order as authorized
|
|
169
|
+
// so checkout completes, but never mark it captured — the
|
|
170
|
+
// CAPTURE.COMPLETED webhook settles it later.
|
|
171
|
+
return {
|
|
172
|
+
status: "authorized",
|
|
173
|
+
data: {
|
|
174
|
+
...(input.data || {}),
|
|
175
|
+
paypal: {
|
|
176
|
+
...existingPayPal,
|
|
177
|
+
order_id: orderId,
|
|
178
|
+
order,
|
|
179
|
+
capture_id: capture.id,
|
|
180
|
+
},
|
|
181
|
+
authorized_at: new Date().toISOString(),
|
|
182
|
+
},
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (captureStatus === "error" || captureStatus === "canceled") {
|
|
186
|
+
return {
|
|
187
|
+
status: "error",
|
|
188
|
+
data: {
|
|
189
|
+
...(input.data || {}),
|
|
190
|
+
paypal: { ...existingPayPal, order_id: orderId, order },
|
|
191
|
+
},
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (authorization?.id) {
|
|
197
|
+
const authStatus = this.mapAuthorizationStatus(authorization?.status)
|
|
198
|
+
if (authStatus === "authorized") {
|
|
199
|
+
return {
|
|
200
|
+
status: "authorized",
|
|
201
|
+
data: {
|
|
202
|
+
...(input.data || {}),
|
|
203
|
+
paypal: {
|
|
204
|
+
...existingPayPal,
|
|
205
|
+
order_id: orderId,
|
|
206
|
+
order,
|
|
207
|
+
authorization_id: authorization.id,
|
|
208
|
+
},
|
|
209
|
+
authorized_at: new Date().toISOString(),
|
|
210
|
+
},
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (authStatus === "error" || authStatus === "canceled") {
|
|
214
|
+
return {
|
|
215
|
+
status: "error",
|
|
216
|
+
data: {
|
|
217
|
+
...(input.data || {}),
|
|
218
|
+
paypal: { ...existingPayPal, order_id: orderId, order },
|
|
219
|
+
},
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Only a buyer-APPROVED order counts as authorized. CREATED/SAVED
|
|
225
|
+
// means the buyer never approved the payment — treating those as
|
|
226
|
+
// authorized would let a cart complete with no funds authorized at
|
|
227
|
+
// PayPal at all.
|
|
228
|
+
if (String(order?.status || "").toUpperCase() === "APPROVED") {
|
|
229
|
+
console.info(
|
|
230
|
+
"[PayPal] authorizePayment: order approved, marking authorized"
|
|
231
|
+
)
|
|
232
|
+
return {
|
|
233
|
+
status: "authorized",
|
|
234
|
+
data: {
|
|
235
|
+
...(input.data || {}),
|
|
236
|
+
paypal: {
|
|
237
|
+
...existingPayPal,
|
|
238
|
+
order_id: orderId,
|
|
239
|
+
order,
|
|
240
|
+
},
|
|
241
|
+
authorized_at: new Date().toISOString(),
|
|
242
|
+
},
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
} catch (e: any) {
|
|
246
|
+
console.warn(
|
|
247
|
+
"[PayPal] authorizePayment: order lookup failed:",
|
|
248
|
+
e?.message
|
|
249
|
+
)
|
|
250
|
+
// Fall back to the session's own record when PayPal is unreachable so a
|
|
251
|
+
// transient lookup error doesn't fail a checkout that already has a
|
|
252
|
+
// real authorization/capture behind it. `captured_at` is only ever
|
|
253
|
+
// stamped after a COMPLETED gate; a bare capture_id (e.g. patched in by
|
|
254
|
+
// a webhook) or authorization id maps to "authorized" — the order is
|
|
255
|
+
// placed without claiming settled funds.
|
|
256
|
+
if ((data as any).captured_at) {
|
|
257
|
+
return {
|
|
258
|
+
status: "captured",
|
|
259
|
+
data: {
|
|
260
|
+
...(input.data || {}),
|
|
261
|
+
captured_at: (data as any).captured_at,
|
|
262
|
+
},
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (
|
|
266
|
+
existingPayPal.capture_id ||
|
|
267
|
+
existingPayPal.authorization_id ||
|
|
268
|
+
(data as any).authorized_at
|
|
269
|
+
) {
|
|
270
|
+
return {
|
|
271
|
+
status: "authorized",
|
|
272
|
+
data: {
|
|
273
|
+
...(input.data || {}),
|
|
274
|
+
authorized_at:
|
|
275
|
+
(data as any).authorized_at || new Date().toISOString(),
|
|
276
|
+
},
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// No usable PayPal order on the session: nothing can be authorized. The
|
|
283
|
+
// previous fallback created a brand-new PayPal order here and immediately
|
|
284
|
+
// called /v2/checkout/orders/{id}/authorize on it — but an order no buyer
|
|
285
|
+
// has approved can never be authorized (PayPal rejects it with
|
|
286
|
+
// ORDER_NOT_APPROVED), so that path only produced confusing 422s and
|
|
287
|
+
// orphaned PayPal orders. Fail with a clear, actionable error instead.
|
|
288
|
+
await this.recordFailure("authorize_failed", {
|
|
289
|
+
cart_id: data.cart_id,
|
|
290
|
+
payment_collection_id: data.payment_collection_id,
|
|
291
|
+
message: "no approved PayPal order on session",
|
|
292
|
+
})
|
|
293
|
+
throw new MedusaError(
|
|
294
|
+
MedusaError.Types.NOT_ALLOWED,
|
|
295
|
+
"No approved PayPal payment was found for this session. The buyer must approve the payment with PayPal before the cart can be completed."
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async retrievePayment(
|
|
300
|
+
input: RetrievePaymentInput
|
|
301
|
+
): Promise<RetrievePaymentOutput> {
|
|
302
|
+
const data = (input.data || {}) as Record<string, any>
|
|
303
|
+
const paypalData = (data.paypal || {}) as Record<string, any>
|
|
304
|
+
const orderId = String(paypalData.order_id || data.order_id || "")
|
|
305
|
+
if (!orderId) {
|
|
306
|
+
return { data: { ...(input.data || {}) } }
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const order = await this.getOrderDetails(orderId)
|
|
310
|
+
const capture = order?.purchase_units?.[0]?.payments?.captures?.[0]
|
|
311
|
+
const authorization =
|
|
312
|
+
order?.purchase_units?.[0]?.payments?.authorizations?.[0]
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
data: {
|
|
316
|
+
...(input.data || {}),
|
|
317
|
+
paypal: {
|
|
318
|
+
...((input.data || {}).paypal as Record<string, unknown>),
|
|
319
|
+
order,
|
|
320
|
+
authorization_id: authorization?.id || paypalData.authorization_id,
|
|
321
|
+
capture_id: capture?.id || paypalData.capture_id,
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async getPaymentStatus(
|
|
328
|
+
input: GetPaymentStatusInput
|
|
329
|
+
): Promise<GetPaymentStatusOutput> {
|
|
330
|
+
const data = (input.data || {}) as Record<string, any>
|
|
331
|
+
const paypalData = (data.paypal || {}) as Record<string, any>
|
|
332
|
+
const orderId = String(paypalData.order_id || data.order_id || "")
|
|
333
|
+
if (!orderId) {
|
|
334
|
+
return { status: "pending", data: { ...(input.data || {}) } }
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
try {
|
|
338
|
+
const order = await this.getOrderDetails(orderId)
|
|
339
|
+
const capture = order?.purchase_units?.[0]?.payments?.captures?.[0]
|
|
340
|
+
const authorization =
|
|
341
|
+
order?.purchase_units?.[0]?.payments?.authorizations?.[0]
|
|
342
|
+
const mappedStatus =
|
|
343
|
+
this.mapCaptureStatus(capture?.status) ||
|
|
344
|
+
this.mapAuthorizationStatus(authorization?.status) ||
|
|
345
|
+
this.mapOrderStatus(order?.status) ||
|
|
346
|
+
"pending"
|
|
347
|
+
|
|
348
|
+
await this.recordSuccess("status_success")
|
|
349
|
+
return {
|
|
350
|
+
status: mappedStatus,
|
|
351
|
+
data: {
|
|
352
|
+
...(input.data || {}),
|
|
353
|
+
paypal: {
|
|
354
|
+
...((input.data || {}).paypal as Record<string, unknown>),
|
|
355
|
+
order,
|
|
356
|
+
authorization_id: authorization?.id || paypalData.authorization_id,
|
|
357
|
+
capture_id: capture?.id || paypalData.capture_id,
|
|
358
|
+
},
|
|
359
|
+
},
|
|
360
|
+
}
|
|
361
|
+
} catch (error: any) {
|
|
362
|
+
await this.recordFailure("status_failed", {
|
|
363
|
+
order_id: orderId,
|
|
364
|
+
message: error?.message,
|
|
365
|
+
})
|
|
366
|
+
throw error
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async capturePayment(
|
|
371
|
+
input: CapturePaymentInput
|
|
372
|
+
): Promise<CapturePaymentOutput> {
|
|
373
|
+
const data = (input.data || {}) as Record<string, any>
|
|
374
|
+
const paypalData = (data.paypal || {}) as Record<string, any>
|
|
375
|
+
const orderId = String(paypalData.order_id || data.order_id || "")
|
|
376
|
+
let authorizationId = String(
|
|
377
|
+
paypalData.authorization_id || data.authorization_id || ""
|
|
378
|
+
)
|
|
379
|
+
if (!orderId) {
|
|
380
|
+
throw new Error("PayPal order_id is required to capture payment")
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (paypalData.capture_id || paypalData.capture) {
|
|
384
|
+
return {
|
|
385
|
+
data: {
|
|
386
|
+
...(input.data || {}),
|
|
387
|
+
paypal: {
|
|
388
|
+
...((input.data || {}).paypal as Record<string, unknown>),
|
|
389
|
+
capture_id: paypalData.capture_id,
|
|
390
|
+
capture: paypalData.capture,
|
|
391
|
+
},
|
|
392
|
+
captured_at: new Date().toISOString(),
|
|
393
|
+
},
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const { amount: sessionAmount, currencyCode } =
|
|
398
|
+
await this.normalizePaymentData(input)
|
|
399
|
+
// The requested (possibly partial) capture amount lives on the Medusa
|
|
400
|
+
// capture row, not in the provider input — resolve it so partial captures
|
|
401
|
+
// don't charge the full session total.
|
|
402
|
+
const amount = await this.resolveRequestedCaptureAmount(
|
|
403
|
+
input,
|
|
404
|
+
sessionAmount
|
|
405
|
+
)
|
|
406
|
+
// Include the amount in the idempotency suffix: PayPal deduplicates by
|
|
407
|
+
// PayPal-Request-Id, so two sequential partial captures of the same order
|
|
408
|
+
// that share an upstream idempotency_key would otherwise collide and the
|
|
409
|
+
// second capture would silently return the first one's result.
|
|
410
|
+
const requestId = this.getIdempotencyKey(
|
|
411
|
+
input,
|
|
412
|
+
`capture-${orderId}-${amount}`
|
|
413
|
+
)
|
|
414
|
+
let debugId: string | null = null
|
|
415
|
+
|
|
416
|
+
try {
|
|
417
|
+
const { accessToken, base } = await this.getPayPalAccessToken()
|
|
418
|
+
const order = await this.getOrderDetails(orderId).catch(() => null)
|
|
419
|
+
const existingCapture =
|
|
420
|
+
order?.purchase_units?.[0]?.payments?.captures?.[0]
|
|
421
|
+
if (existingCapture?.id && isCaptureCompleted(existingCapture)) {
|
|
422
|
+
return {
|
|
423
|
+
data: {
|
|
424
|
+
...(input.data || {}),
|
|
425
|
+
paypal: {
|
|
426
|
+
...((input.data || {}).paypal as Record<string, unknown>),
|
|
427
|
+
capture_id: existingCapture.id,
|
|
428
|
+
capture: existingCapture,
|
|
429
|
+
},
|
|
430
|
+
captured_at: new Date().toISOString(),
|
|
431
|
+
},
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const resolvedIntent = String(
|
|
436
|
+
order?.intent || paypalData.order?.intent || data.intent || ""
|
|
437
|
+
).toUpperCase()
|
|
438
|
+
|
|
439
|
+
if (!authorizationId && resolvedIntent === "AUTHORIZE") {
|
|
440
|
+
const authorizeResp = await paypalFetch(
|
|
441
|
+
`${base}/v2/checkout/orders/${encodeURIComponent(orderId)}/authorize`,
|
|
442
|
+
{
|
|
443
|
+
method: "POST",
|
|
444
|
+
headers: {
|
|
445
|
+
Authorization: `Bearer ${accessToken}`,
|
|
446
|
+
"Content-Type": "application/json",
|
|
447
|
+
"PayPal-Request-Id": `${requestId}-auth`,
|
|
448
|
+
"PayPal-Partner-Attribution-Id": PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
449
|
+
},
|
|
450
|
+
}
|
|
451
|
+
)
|
|
452
|
+
const authorizeText = await authorizeResp.text()
|
|
453
|
+
debugId = authorizeResp.headers.get("paypal-debug-id")
|
|
454
|
+
if (!authorizeResp.ok) {
|
|
455
|
+
throw new Error(
|
|
456
|
+
`PayPal authorize order error (${authorizeResp.status}): ${authorizeText}${
|
|
457
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
458
|
+
}`
|
|
459
|
+
)
|
|
460
|
+
}
|
|
461
|
+
const authorization = JSON.parse(authorizeText)
|
|
462
|
+
authorizationId =
|
|
463
|
+
authorization?.purchase_units?.[0]?.payments?.authorizations?.[0]?.id
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
const isFinalCapture =
|
|
467
|
+
paypalData.is_final_capture ??
|
|
468
|
+
data.is_final_capture ??
|
|
469
|
+
data.final_capture ??
|
|
470
|
+
undefined
|
|
471
|
+
const captureValue =
|
|
472
|
+
amount > 0 ? formatAmountForPayPal(amount, currencyCode || "EUR") : null
|
|
473
|
+
|
|
474
|
+
// `amount` and `is_final_capture` are only valid on the authorizations
|
|
475
|
+
// capture endpoint. The orders capture endpoint always captures the FULL
|
|
476
|
+
// order and silently ignores an `amount` body — so sending a partial
|
|
477
|
+
// amount there would over-capture while we record the (smaller) requested
|
|
478
|
+
// value. Route partial captures to the authorization, and fail closed if a
|
|
479
|
+
// partial capture is attempted against a capture-intent order.
|
|
480
|
+
let capturePayload: Record<string, unknown>
|
|
481
|
+
let captureUrl: string
|
|
482
|
+
if (authorizationId) {
|
|
483
|
+
captureUrl = `${base}/v2/payments/authorizations/${encodeURIComponent(authorizationId)}/capture`
|
|
484
|
+
capturePayload = {
|
|
485
|
+
...(captureValue
|
|
486
|
+
? {
|
|
487
|
+
amount: {
|
|
488
|
+
currency_code: currencyCode || "EUR",
|
|
489
|
+
value: captureValue,
|
|
490
|
+
},
|
|
491
|
+
}
|
|
492
|
+
: {}),
|
|
493
|
+
...(typeof isFinalCapture === "boolean"
|
|
494
|
+
? { is_final_capture: isFinalCapture }
|
|
495
|
+
: {}),
|
|
496
|
+
}
|
|
497
|
+
} else {
|
|
498
|
+
captureUrl = `${base}/v2/checkout/orders/${encodeURIComponent(orderId)}/capture`
|
|
499
|
+
capturePayload = {}
|
|
500
|
+
const orderTotal = order?.purchase_units?.[0]?.amount?.value
|
|
501
|
+
if (captureValue && orderTotal && captureValue !== String(orderTotal)) {
|
|
502
|
+
throw new Error(
|
|
503
|
+
`PayPal partial capture (${captureValue} ${currencyCode || "EUR"}) is not supported for ` +
|
|
504
|
+
`capture-intent orders (order total ${orderTotal}). Create the order with intent ` +
|
|
505
|
+
`AUTHORIZE to capture a partial amount.`
|
|
506
|
+
)
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// Retry transient 5xx/429/timeout: the PayPal-Request-Id makes the
|
|
511
|
+
// capture idempotent, so a retry after a network blip re-uses the same
|
|
512
|
+
// capture instead of double-charging.
|
|
513
|
+
const ppResp = await paypalFetchWithRetry(captureUrl, {
|
|
514
|
+
method: "POST",
|
|
515
|
+
headers: {
|
|
516
|
+
Authorization: `Bearer ${accessToken}`,
|
|
517
|
+
"Content-Type": "application/json",
|
|
518
|
+
"PayPal-Request-Id": requestId,
|
|
519
|
+
"PayPal-Partner-Attribution-Id": PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
520
|
+
},
|
|
521
|
+
body: JSON.stringify(capturePayload),
|
|
522
|
+
})
|
|
523
|
+
|
|
524
|
+
const ppText = await ppResp.text()
|
|
525
|
+
debugId = ppResp.headers.get("paypal-debug-id")
|
|
526
|
+
if (!ppResp.ok) {
|
|
527
|
+
throw new Error(
|
|
528
|
+
`PayPal capture error (${ppResp.status}): ${ppText}${
|
|
529
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
530
|
+
}`
|
|
531
|
+
)
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
const capture = JSON.parse(ppText)
|
|
535
|
+
|
|
536
|
+
// A 2xx response does NOT mean the funds were captured. PayPal returns 201
|
|
537
|
+
// for captures that are PENDING (e.g. pending review / eCheck), DECLINED,
|
|
538
|
+
// or FAILED. Recording any of these as "captured" books money that never
|
|
539
|
+
// settled, so only a COMPLETED capture is treated as success.
|
|
540
|
+
const captureStatus = extractCaptureStatus(capture)
|
|
541
|
+
if (captureStatus !== "COMPLETED") {
|
|
542
|
+
throw new Error(
|
|
543
|
+
`PayPal capture did not complete (status=${captureStatus || "UNKNOWN"}). ` +
|
|
544
|
+
`The payment was not captured.${debugId ? ` debug_id=${debugId}` : ""}`
|
|
545
|
+
)
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
const captureId =
|
|
549
|
+
capture?.id || capture?.purchase_units?.[0]?.payments?.captures?.[0]?.id
|
|
550
|
+
const existingCaptures = Array.isArray(paypalData.captures)
|
|
551
|
+
? paypalData.captures
|
|
552
|
+
: []
|
|
553
|
+
const captureEntry = {
|
|
554
|
+
id: captureId,
|
|
555
|
+
status: capture?.status,
|
|
556
|
+
amount: capture?.amount,
|
|
557
|
+
raw: capture,
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
await this.recordSuccess("capture_success")
|
|
561
|
+
await this.recordPaymentEvent("capture", {
|
|
562
|
+
order_id: orderId,
|
|
563
|
+
capture_id: captureId,
|
|
564
|
+
authorization_id: authorizationId || undefined,
|
|
565
|
+
amount,
|
|
566
|
+
currency_code: currencyCode,
|
|
567
|
+
request_id: requestId,
|
|
568
|
+
})
|
|
569
|
+
|
|
570
|
+
return {
|
|
571
|
+
data: {
|
|
572
|
+
...(input.data || {}),
|
|
573
|
+
paypal: {
|
|
574
|
+
...((input.data || {}).paypal as Record<string, unknown>),
|
|
575
|
+
order_id: orderId,
|
|
576
|
+
capture_id: captureId,
|
|
577
|
+
capture,
|
|
578
|
+
authorization_id: authorizationId || paypalData.authorization_id,
|
|
579
|
+
captures: [...existingCaptures, captureEntry],
|
|
580
|
+
},
|
|
581
|
+
captured_at: new Date().toISOString(),
|
|
582
|
+
},
|
|
583
|
+
}
|
|
584
|
+
} catch (error: any) {
|
|
585
|
+
await this.recordFailure("capture_failed", {
|
|
586
|
+
order_id: orderId,
|
|
587
|
+
request_id: requestId,
|
|
588
|
+
debug_id: debugId,
|
|
589
|
+
message: error?.message,
|
|
590
|
+
})
|
|
591
|
+
throw error instanceof MedusaError
|
|
592
|
+
? error
|
|
593
|
+
: new MedusaError(
|
|
594
|
+
MedusaError.Types.INVALID_DATA,
|
|
595
|
+
error?.message || "PayPal capture failed."
|
|
596
|
+
)
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
async refundPayment(input: RefundPaymentInput): Promise<RefundPaymentOutput> {
|
|
601
|
+
const data = (input.data || {}) as Record<string, any>
|
|
602
|
+
const paypalData = (data.paypal || {}) as Record<string, any>
|
|
603
|
+
const captureId = String(paypalData.capture_id || data.capture_id || "")
|
|
604
|
+
const refundReason = String(
|
|
605
|
+
paypalData.refund_reason || data.refund_reason || data.reason || ""
|
|
606
|
+
).trim()
|
|
607
|
+
const refundReasonCode = String(
|
|
608
|
+
paypalData.refund_reason_code ||
|
|
609
|
+
data.refund_reason_code ||
|
|
610
|
+
data.reason_code ||
|
|
611
|
+
""
|
|
612
|
+
).trim()
|
|
613
|
+
if (!captureId) {
|
|
614
|
+
throw new MedusaError(
|
|
615
|
+
MedusaError.Types.INVALID_DATA,
|
|
616
|
+
"PayPal capture_id is required to refund payment. No capture found in session data."
|
|
617
|
+
)
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// Medusa passes the refund amount as a BigNumberInput (BigNumber instance or
|
|
621
|
+
// serialized { value, precision }); coerce it to a number so partial refunds
|
|
622
|
+
// send the correct value instead of silently refunding the full capture.
|
|
623
|
+
const refundAmount = toAmountNumber(input.amount)
|
|
624
|
+
|
|
625
|
+
// Include the amount in the idempotency suffix so two sequential partial
|
|
626
|
+
// refunds of the same capture are not deduplicated by PayPal into one.
|
|
627
|
+
const requestId = this.getIdempotencyKey(
|
|
628
|
+
input,
|
|
629
|
+
`refund-${captureId}-${refundAmount}`
|
|
630
|
+
)
|
|
631
|
+
|
|
632
|
+
const currencyOverride = await this.resolveCurrencyOverride()
|
|
633
|
+
const currencyCode = normalizeCurrencyCode(
|
|
634
|
+
data.currency_code || currencyOverride || "EUR"
|
|
635
|
+
)
|
|
636
|
+
|
|
637
|
+
const refundValue =
|
|
638
|
+
refundAmount > 0
|
|
639
|
+
? formatAmountForPayPal(refundAmount, currencyCode)
|
|
640
|
+
: null
|
|
641
|
+
|
|
642
|
+
let debugId: string | null = null
|
|
643
|
+
|
|
644
|
+
try {
|
|
645
|
+
const { accessToken, base } = await this.getPayPalAccessToken()
|
|
646
|
+
const refundPayload: Record<string, any> = refundValue
|
|
647
|
+
? {
|
|
648
|
+
amount: {
|
|
649
|
+
currency_code: currencyCode || "EUR",
|
|
650
|
+
value: refundValue,
|
|
651
|
+
},
|
|
652
|
+
}
|
|
653
|
+
: {}
|
|
654
|
+
|
|
655
|
+
if (refundReason) {
|
|
656
|
+
// PayPal rejects a note_to_payer longer than 255 characters with a 422
|
|
657
|
+
// (which would otherwise fail the whole refund); truncate defensively.
|
|
658
|
+
refundPayload.note_to_payer = refundReason.slice(0, 255)
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// Retry transient 5xx/429/timeout: the PayPal-Request-Id (which includes
|
|
662
|
+
// the refund amount) makes the refund idempotent, so a retry after a
|
|
663
|
+
// network blip re-uses the same refund instead of double-refunding.
|
|
664
|
+
const ppResp = await paypalFetchWithRetry(
|
|
665
|
+
`${base}/v2/payments/captures/${encodeURIComponent(captureId)}/refund`,
|
|
666
|
+
{
|
|
667
|
+
method: "POST",
|
|
668
|
+
headers: {
|
|
669
|
+
Authorization: `Bearer ${accessToken}`,
|
|
670
|
+
"Content-Type": "application/json",
|
|
671
|
+
"PayPal-Request-Id": requestId,
|
|
672
|
+
"PayPal-Partner-Attribution-Id": PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
673
|
+
},
|
|
674
|
+
body: JSON.stringify(refundPayload),
|
|
675
|
+
}
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
const ppText = await ppResp.text()
|
|
679
|
+
debugId = ppResp.headers.get("paypal-debug-id")
|
|
680
|
+
if (!ppResp.ok) {
|
|
681
|
+
throw new Error(
|
|
682
|
+
`PayPal refund error (${ppResp.status}): ${ppText}${
|
|
683
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
684
|
+
}`
|
|
685
|
+
)
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
const refund = JSON.parse(ppText)
|
|
689
|
+
|
|
690
|
+
// As with captures, a 2xx response does not guarantee the refund stuck.
|
|
691
|
+
// FAILED / CANCELLED / DENIED refunds also return 2xx and must not be
|
|
692
|
+
// recorded as a successful refund. PENDING is accepted: PayPal processes
|
|
693
|
+
// refunds asynchronously and a pending refund will settle.
|
|
694
|
+
const refundStatus = String(refund?.status || "").toUpperCase()
|
|
695
|
+
if (isRefundFailureStatus(refundStatus)) {
|
|
696
|
+
throw new Error(
|
|
697
|
+
`PayPal refund did not succeed (status=${refundStatus}). The refund was not issued.` +
|
|
698
|
+
(debugId ? ` debug_id=${debugId}` : "")
|
|
699
|
+
)
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
const existingRefunds = Array.isArray(paypalData.refunds)
|
|
703
|
+
? paypalData.refunds
|
|
704
|
+
: []
|
|
705
|
+
const refundEntry = {
|
|
706
|
+
id: refund?.id,
|
|
707
|
+
status: refund?.status,
|
|
708
|
+
amount: refund?.amount,
|
|
709
|
+
reason: refundReason || refund?.note_to_payer,
|
|
710
|
+
reason_code: refundReasonCode || refund?.reason_code,
|
|
711
|
+
raw: refund,
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
await this.recordSuccess("refund_success")
|
|
715
|
+
await this.recordPaymentEvent("refund", {
|
|
716
|
+
capture_id: captureId,
|
|
717
|
+
refund_id: refund?.id,
|
|
718
|
+
amount: refundAmount,
|
|
719
|
+
currency_code: currencyCode,
|
|
720
|
+
request_id: requestId,
|
|
721
|
+
reason: refundReason,
|
|
722
|
+
reason_code: refundReasonCode,
|
|
723
|
+
})
|
|
724
|
+
|
|
725
|
+
return {
|
|
726
|
+
data: {
|
|
727
|
+
...(input.data || {}),
|
|
728
|
+
paypal: {
|
|
729
|
+
...((input.data || {}).paypal as Record<string, unknown>),
|
|
730
|
+
refund_id: refund?.id,
|
|
731
|
+
refund_status: refund?.status,
|
|
732
|
+
refund_reason: refundReason || refund?.note_to_payer,
|
|
733
|
+
refund_reason_code: refundReasonCode || refund?.reason_code,
|
|
734
|
+
refunds: [...existingRefunds, refundEntry],
|
|
735
|
+
refund,
|
|
736
|
+
},
|
|
737
|
+
refunded_at: new Date().toISOString(),
|
|
738
|
+
},
|
|
739
|
+
}
|
|
740
|
+
} catch (error: any) {
|
|
741
|
+
await this.recordFailure("refund_failed", {
|
|
742
|
+
capture_id: captureId,
|
|
743
|
+
request_id: requestId,
|
|
744
|
+
debug_id: debugId,
|
|
745
|
+
message: error?.message,
|
|
746
|
+
})
|
|
747
|
+
throw error instanceof MedusaError
|
|
748
|
+
? error
|
|
749
|
+
: new MedusaError(
|
|
750
|
+
MedusaError.Types.INVALID_DATA,
|
|
751
|
+
error?.message || "PayPal refund failed."
|
|
752
|
+
)
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
async cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput> {
|
|
757
|
+
const data = (input.data || {}) as Record<string, any>
|
|
758
|
+
const paypalData = (data.paypal || {}) as Record<string, any>
|
|
759
|
+
const orderId = String(paypalData.order_id || data.order_id || "")
|
|
760
|
+
const captureId = String(paypalData.capture_id || data.capture_id || "")
|
|
761
|
+
const storedAuthorizationId = String(
|
|
762
|
+
paypalData.authorization_id || data.authorization_id || ""
|
|
763
|
+
)
|
|
764
|
+
let debugId: string | null = null
|
|
765
|
+
|
|
766
|
+
try {
|
|
767
|
+
const order = orderId ? await this.getOrderDetails(orderId) : null
|
|
768
|
+
const intent = String(order?.intent || "").toUpperCase()
|
|
769
|
+
const authorizationId =
|
|
770
|
+
order?.purchase_units?.[0]?.payments?.authorizations?.[0]?.id ||
|
|
771
|
+
storedAuthorizationId
|
|
772
|
+
|
|
773
|
+
if (intent === "AUTHORIZE" && authorizationId) {
|
|
774
|
+
const { accessToken, base } = await this.getPayPalAccessToken()
|
|
775
|
+
const requestId = this.getIdempotencyKey(
|
|
776
|
+
input,
|
|
777
|
+
`void-${authorizationId}`
|
|
778
|
+
)
|
|
779
|
+
|
|
780
|
+
const resp = await paypalFetch(
|
|
781
|
+
`${base}/v2/payments/authorizations/${encodeURIComponent(authorizationId)}/void`,
|
|
782
|
+
{
|
|
783
|
+
method: "POST",
|
|
784
|
+
headers: {
|
|
785
|
+
Authorization: `Bearer ${accessToken}`,
|
|
786
|
+
"Content-Type": "application/json",
|
|
787
|
+
"PayPal-Request-Id": requestId,
|
|
788
|
+
"PayPal-Partner-Attribution-Id": PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
789
|
+
},
|
|
790
|
+
}
|
|
791
|
+
)
|
|
792
|
+
|
|
793
|
+
if (!resp.ok) {
|
|
794
|
+
const text = await resp.text()
|
|
795
|
+
debugId = resp.headers.get("paypal-debug-id")
|
|
796
|
+
throw new Error(
|
|
797
|
+
`PayPal void error (${resp.status}): ${text}${
|
|
798
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
799
|
+
}`
|
|
800
|
+
)
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
await this.recordSuccess("void_success")
|
|
804
|
+
await this.recordPaymentEvent("void", {
|
|
805
|
+
order_id: orderId,
|
|
806
|
+
authorization_id: authorizationId,
|
|
807
|
+
})
|
|
808
|
+
} else if (captureId) {
|
|
809
|
+
const { accessToken, base } = await this.getPayPalAccessToken()
|
|
810
|
+
const requestId = this.getIdempotencyKey(
|
|
811
|
+
input,
|
|
812
|
+
`cancel-refund-${captureId}`
|
|
813
|
+
)
|
|
814
|
+
|
|
815
|
+
const resp = await paypalFetch(
|
|
816
|
+
`${base}/v2/payments/captures/${encodeURIComponent(captureId)}/refund`,
|
|
817
|
+
{
|
|
818
|
+
method: "POST",
|
|
819
|
+
headers: {
|
|
820
|
+
Authorization: `Bearer ${accessToken}`,
|
|
821
|
+
"Content-Type": "application/json",
|
|
822
|
+
"PayPal-Request-Id": requestId,
|
|
823
|
+
"PayPal-Partner-Attribution-Id": PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
824
|
+
},
|
|
825
|
+
body: JSON.stringify({}),
|
|
826
|
+
}
|
|
827
|
+
)
|
|
828
|
+
|
|
829
|
+
if (!resp.ok) {
|
|
830
|
+
const text = await resp.text()
|
|
831
|
+
debugId = resp.headers.get("paypal-debug-id")
|
|
832
|
+
throw new Error(
|
|
833
|
+
`PayPal refund error (${resp.status}): ${text}${
|
|
834
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
835
|
+
}`
|
|
836
|
+
)
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
const refund = await resp.json().catch(() => ({}))
|
|
840
|
+
|
|
841
|
+
// As with refundPayment: a 2xx response does not guarantee the refund
|
|
842
|
+
// stuck — FAILED / CANCELLED / DENIED refunds also return 2xx. Booking
|
|
843
|
+
// `canceled_at` for a refund that never went through would record a
|
|
844
|
+
// cancellation while the merchant keeps the funds.
|
|
845
|
+
const cancelRefundStatus = String(refund?.status || "").toUpperCase()
|
|
846
|
+
if (isRefundFailureStatus(cancelRefundStatus)) {
|
|
847
|
+
throw new Error(
|
|
848
|
+
`PayPal cancel-refund did not succeed (status=${cancelRefundStatus}). The payment was not canceled.`
|
|
849
|
+
)
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
const existingRefunds = Array.isArray(paypalData.refunds)
|
|
853
|
+
? paypalData.refunds
|
|
854
|
+
: []
|
|
855
|
+
const refundEntry = {
|
|
856
|
+
id: refund?.id,
|
|
857
|
+
status: refund?.status,
|
|
858
|
+
amount: refund?.amount,
|
|
859
|
+
raw: refund,
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
await this.recordSuccess("cancel_refund_success")
|
|
863
|
+
|
|
864
|
+
return {
|
|
865
|
+
data: {
|
|
866
|
+
...(input.data || {}),
|
|
867
|
+
paypal: {
|
|
868
|
+
...((input.data || {}).paypal as Record<string, unknown>),
|
|
869
|
+
order: order || undefined,
|
|
870
|
+
authorization_id: authorizationId || storedAuthorizationId,
|
|
871
|
+
capture_id: captureId || paypalData.capture_id,
|
|
872
|
+
refund_id: refund?.id,
|
|
873
|
+
refund_status: refund?.status,
|
|
874
|
+
refunds: [...existingRefunds, refundEntry],
|
|
875
|
+
},
|
|
876
|
+
canceled_at: new Date().toISOString(),
|
|
877
|
+
},
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
return {
|
|
882
|
+
data: {
|
|
883
|
+
...(input.data || {}),
|
|
884
|
+
paypal: {
|
|
885
|
+
...((input.data || {}).paypal as Record<string, unknown>),
|
|
886
|
+
order: order || undefined,
|
|
887
|
+
authorization_id: authorizationId || storedAuthorizationId,
|
|
888
|
+
capture_id: captureId || paypalData.capture_id,
|
|
889
|
+
},
|
|
890
|
+
canceled_at: new Date().toISOString(),
|
|
891
|
+
},
|
|
892
|
+
}
|
|
893
|
+
} catch (error: any) {
|
|
894
|
+
await this.recordFailure("cancel_failed", {
|
|
895
|
+
order_id: orderId,
|
|
896
|
+
capture_id: captureId,
|
|
897
|
+
debug_id: debugId,
|
|
898
|
+
message: error?.message,
|
|
899
|
+
})
|
|
900
|
+
throw error
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
export default PayPalPaymentProvider
|
|
906
|
+
export { PayPalPaymentProvider }
|