@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,314 +1,380 @@
|
|
|
1
|
-
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
-
import { Modules } from "@medusajs/framework/utils"
|
|
3
|
-
import type { IEventBusModuleService } from "@medusajs/framework/types"
|
|
4
|
-
import type PayPalModuleService from "../../../../modules/paypal/service"
|
|
5
|
-
import { paypalFetch } from "../../../../modules/paypal/utils/paypal-fetch"
|
|
6
|
-
import {
|
|
7
|
-
isAllowedEventType,
|
|
8
|
-
normalizeEventVersion,
|
|
9
|
-
} from "../../../../modules/paypal/webhook-processor"
|
|
10
|
-
import { PAYPAL_WEBHOOK_RECEIVED_EVENT } from "../../../../subscribers/paypal-webhook-process"
|
|
11
|
-
import {
|
|
12
|
-
composeVerifyRequestBody,
|
|
13
|
-
rawBodyToString,
|
|
14
|
-
resolveWebhookEventJson,
|
|
15
|
-
} from "../../../../modules/paypal/utils/webhook-verify"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
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
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
const
|
|
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
|
-
|
|
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
|
-
}
|
|
1
|
+
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import { Modules } from "@medusajs/framework/utils"
|
|
3
|
+
import type { IEventBusModuleService } from "@medusajs/framework/types"
|
|
4
|
+
import type PayPalModuleService from "../../../../modules/paypal/service"
|
|
5
|
+
import { paypalFetch } from "../../../../modules/paypal/utils/paypal-fetch"
|
|
6
|
+
import {
|
|
7
|
+
isAllowedEventType,
|
|
8
|
+
normalizeEventVersion,
|
|
9
|
+
} from "../../../../modules/paypal/webhook-processor"
|
|
10
|
+
import { PAYPAL_WEBHOOK_RECEIVED_EVENT } from "../../../../subscribers/paypal-webhook-process"
|
|
11
|
+
import {
|
|
12
|
+
composeVerifyRequestBody,
|
|
13
|
+
rawBodyToString,
|
|
14
|
+
resolveWebhookEventJson,
|
|
15
|
+
} from "../../../../modules/paypal/utils/webhook-verify"
|
|
16
|
+
|
|
17
|
+
/** Deliveries whose transmission time is older than this are rejected as replays. */
|
|
18
|
+
const REPLAY_WINDOW_MINUTES = (() => {
|
|
19
|
+
const v = Number(process.env.PAYPAL_WEBHOOK_REPLAY_WINDOW_MINUTES)
|
|
20
|
+
return Number.isFinite(v) && v > 0 ? v : 60
|
|
21
|
+
})()
|
|
22
|
+
|
|
23
|
+
/** Case-insensitive single-value header lookup. */
|
|
24
|
+
function getHeader(
|
|
25
|
+
headers: Record<string, string | string[] | undefined>,
|
|
26
|
+
name: string
|
|
27
|
+
): string | undefined {
|
|
28
|
+
const direct = headers[name]
|
|
29
|
+
if (Array.isArray(direct)) return direct[0]
|
|
30
|
+
if (typeof direct === "string") return direct
|
|
31
|
+
const lower = name.toLowerCase()
|
|
32
|
+
const key = Object.keys(headers).find((h) => h.toLowerCase() === lower)
|
|
33
|
+
if (!key) return undefined
|
|
34
|
+
const val = headers[key]
|
|
35
|
+
return Array.isArray(val) ? val[0] : val
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ValidationFail {
|
|
39
|
+
ok: false
|
|
40
|
+
status: number
|
|
41
|
+
message: string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface ValidationPass {
|
|
45
|
+
ok: true
|
|
46
|
+
eventId: string
|
|
47
|
+
eventType: string
|
|
48
|
+
transmissionId: string | null
|
|
49
|
+
transmissionTime: Date | null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Structural checks that run before any DB or PayPal call: required fields,
|
|
54
|
+
* a parseable `paypal-transmission-time`, and the replay window.
|
|
55
|
+
*/
|
|
56
|
+
function validateRequest(req: MedusaRequest): ValidationFail | ValidationPass {
|
|
57
|
+
const payload = (req.body || {}) as Record<string, any>
|
|
58
|
+
const eventId = String(payload?.id || payload?.event_id || "").trim()
|
|
59
|
+
const eventType = String(
|
|
60
|
+
payload?.event_type || payload?.eventType || ""
|
|
61
|
+
).trim()
|
|
62
|
+
|
|
63
|
+
if (!eventId || !eventType) {
|
|
64
|
+
return {
|
|
65
|
+
ok: false,
|
|
66
|
+
status: 400,
|
|
67
|
+
message: "Missing required fields: id and event_type",
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const transmissionTimeHeader = getHeader(
|
|
72
|
+
req.headers,
|
|
73
|
+
"paypal-transmission-time"
|
|
74
|
+
)
|
|
75
|
+
if (!transmissionTimeHeader) {
|
|
76
|
+
return {
|
|
77
|
+
ok: false,
|
|
78
|
+
status: 400,
|
|
79
|
+
message: "Missing required header: paypal-transmission-time",
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const transmissionMs = Date.parse(transmissionTimeHeader)
|
|
84
|
+
if (!Number.isFinite(transmissionMs)) {
|
|
85
|
+
return {
|
|
86
|
+
ok: false,
|
|
87
|
+
status: 400,
|
|
88
|
+
message: "Invalid paypal-transmission-time header value",
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const ageMs = Math.abs(Date.now() - transmissionMs)
|
|
93
|
+
if (ageMs > REPLAY_WINDOW_MINUTES * 60 * 1000) {
|
|
94
|
+
return {
|
|
95
|
+
ok: false,
|
|
96
|
+
status: 400,
|
|
97
|
+
message: `Webhook rejected: outside ${REPLAY_WINDOW_MINUTES}-minute replay window`,
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
ok: true,
|
|
103
|
+
eventId,
|
|
104
|
+
eventType,
|
|
105
|
+
transmissionId: getHeader(req.headers, "paypal-transmission-id") || null,
|
|
106
|
+
transmissionTime: new Date(transmissionMs),
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The registered webhook id for an environment: admin settings first
|
|
112
|
+
* (`api_details.webhook_ids`), then the `PAYPAL_WEBHOOK_ID_*` env override.
|
|
113
|
+
*/
|
|
114
|
+
function resolveWebhookId(
|
|
115
|
+
environment: string,
|
|
116
|
+
settings: Record<string, unknown>
|
|
117
|
+
): string | undefined {
|
|
118
|
+
const ids = (settings?.webhook_ids || {}) as Record<
|
|
119
|
+
string,
|
|
120
|
+
string | undefined
|
|
121
|
+
>
|
|
122
|
+
if (environment === "live") {
|
|
123
|
+
return (
|
|
124
|
+
ids.live ||
|
|
125
|
+
(settings?.webhook_id_live as string) ||
|
|
126
|
+
process.env.PAYPAL_WEBHOOK_ID_LIVE
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
return (
|
|
130
|
+
ids.sandbox ||
|
|
131
|
+
(settings?.webhook_id_sandbox as string) ||
|
|
132
|
+
process.env.PAYPAL_WEBHOOK_ID_SANDBOX
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Verify the delivery with PayPal's `verify-webhook-signature` API. Throws
|
|
138
|
+
* unless PayPal answers `VERIFIED`; also rejects a `paypal-cert-url` that
|
|
139
|
+
* does not point at paypal.com before making the call.
|
|
140
|
+
*/
|
|
141
|
+
async function verifyWebhookSignature(
|
|
142
|
+
paypal: PayPalModuleService,
|
|
143
|
+
environment: string,
|
|
144
|
+
body: Record<string, unknown>,
|
|
145
|
+
headers: Record<string, string | string[] | undefined>,
|
|
146
|
+
rawBody: string | null
|
|
147
|
+
): Promise<void> {
|
|
148
|
+
const settings = await paypal.getSettings().catch(() => ({ data: {} }))
|
|
149
|
+
const webhookId = resolveWebhookId(
|
|
150
|
+
environment,
|
|
151
|
+
(settings?.data as Record<string, unknown>) || {}
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
if (!webhookId) {
|
|
155
|
+
throw new Error(
|
|
156
|
+
`PayPal webhook ID not configured for environment "${environment}". Set PAYPAL_WEBHOOK_ID_${environment.toUpperCase()} or configure it in admin settings.`
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const base =
|
|
161
|
+
environment === "live"
|
|
162
|
+
? "https://api-m.paypal.com"
|
|
163
|
+
: "https://api-m.sandbox.paypal.com"
|
|
164
|
+
|
|
165
|
+
const accessToken = await paypal.getAppAccessToken()
|
|
166
|
+
|
|
167
|
+
const verifyFields = {
|
|
168
|
+
auth_algo: getHeader(headers, "paypal-auth-algo"),
|
|
169
|
+
cert_url: getHeader(headers, "paypal-cert-url"),
|
|
170
|
+
transmission_id: getHeader(headers, "paypal-transmission-id"),
|
|
171
|
+
transmission_sig: getHeader(headers, "paypal-transmission-sig"),
|
|
172
|
+
transmission_time: getHeader(headers, "paypal-transmission-time"),
|
|
173
|
+
webhook_id: webhookId,
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const certUrl = verifyFields.cert_url
|
|
177
|
+
if (
|
|
178
|
+
certUrl &&
|
|
179
|
+
!certUrl.startsWith("https://www.paypal.com/") &&
|
|
180
|
+
!certUrl.startsWith("https://api-m.paypal.com/") &&
|
|
181
|
+
!certUrl.startsWith("https://api-m.sandbox.paypal.com/")
|
|
182
|
+
) {
|
|
183
|
+
throw new Error("Invalid paypal-cert-url: must originate from paypal.com")
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const missing = Object.entries(verifyFields)
|
|
187
|
+
.filter(([k, v]) => k !== "webhook_id" && !v)
|
|
188
|
+
.map(([k]) => k)
|
|
189
|
+
|
|
190
|
+
if (missing.length > 0) {
|
|
191
|
+
throw new Error(
|
|
192
|
+
`Missing required PayPal webhook headers: ${missing.join(", ")}`
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Use the raw bytes PayPal signed for `webhook_event` whenever available
|
|
197
|
+
// (preserved by the route's `bodyParser: { preserveRawBody: true }`); fall
|
|
198
|
+
// back to serializing the parsed body so verification still runs if it isn't.
|
|
199
|
+
const requestBody = composeVerifyRequestBody(
|
|
200
|
+
verifyFields,
|
|
201
|
+
resolveWebhookEventJson(rawBody, body)
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
const resp = await paypalFetch(
|
|
205
|
+
`${base}/v1/notifications/verify-webhook-signature`,
|
|
206
|
+
{
|
|
207
|
+
method: "POST",
|
|
208
|
+
headers: {
|
|
209
|
+
Authorization: `Bearer ${accessToken}`,
|
|
210
|
+
"Content-Type": "application/json",
|
|
211
|
+
},
|
|
212
|
+
body: requestBody,
|
|
213
|
+
}
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
const json = await resp.json().catch(() => ({}))
|
|
217
|
+
const debugId = resp.headers.get("paypal-debug-id") || json?.debug_id
|
|
218
|
+
|
|
219
|
+
if (!resp.ok) {
|
|
220
|
+
throw new Error(
|
|
221
|
+
`PayPal signature verification API error (${resp.status}): ${JSON.stringify(json)}` +
|
|
222
|
+
(debugId ? ` debug_id=${debugId}` : "")
|
|
223
|
+
)
|
|
224
|
+
}
|
|
225
|
+
if (json?.verification_status !== "VERIFIED") {
|
|
226
|
+
throw new Error(
|
|
227
|
+
`PayPal webhook signature not verified. Status: ${json?.verification_status}` +
|
|
228
|
+
(debugId ? ` debug_id=${debugId}` : "")
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* POST /store/paypal/webhook (also mounted at /hooks/paypal/webhook).
|
|
235
|
+
*
|
|
236
|
+
* Validate → dedupe by transmission id → verify signature → persist as
|
|
237
|
+
* "processing" → dedupe by event id → hand off to the async subscriber and
|
|
238
|
+
* acknowledge immediately. All heavy work happens off the request path.
|
|
239
|
+
*/
|
|
240
|
+
export async function POST(req: MedusaRequest, res: MedusaResponse) {
|
|
241
|
+
const paypal = req.scope.resolve<PayPalModuleService>("paypal_onboarding")
|
|
242
|
+
|
|
243
|
+
const validation = validateRequest(req)
|
|
244
|
+
if (!validation.ok) {
|
|
245
|
+
console.warn("[PayPal] webhook: validation failed:", validation.message)
|
|
246
|
+
return res.status(validation.status).json({ message: validation.message })
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const { eventId, eventType, transmissionId, transmissionTime } = validation
|
|
250
|
+
const payload = (req.body || {}) as Record<string, any>
|
|
251
|
+
|
|
252
|
+
if (transmissionId) {
|
|
253
|
+
try {
|
|
254
|
+
const existing = await paypal.listPayPalWebhookEvents(
|
|
255
|
+
{ transmission_id: transmissionId },
|
|
256
|
+
{ take: 1 }
|
|
257
|
+
)
|
|
258
|
+
if ((existing || []).length > 0) {
|
|
259
|
+
console.info("[PayPal] webhook: duplicate transmission_id", {
|
|
260
|
+
transmissionId,
|
|
261
|
+
eventId,
|
|
262
|
+
})
|
|
263
|
+
return res.json({ ok: true, duplicate: true })
|
|
264
|
+
}
|
|
265
|
+
} catch (e: any) {
|
|
266
|
+
console.warn(
|
|
267
|
+
"[PayPal] webhook: transmission_id dedup check failed:",
|
|
268
|
+
e?.message
|
|
269
|
+
)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
try {
|
|
274
|
+
const creds = await paypal.getActiveCredentials()
|
|
275
|
+
const rawBody = rawBodyToString(
|
|
276
|
+
(req as unknown as { rawBody?: unknown }).rawBody
|
|
277
|
+
)
|
|
278
|
+
await verifyWebhookSignature(
|
|
279
|
+
paypal,
|
|
280
|
+
creds.environment,
|
|
281
|
+
payload,
|
|
282
|
+
req.headers,
|
|
283
|
+
rawBody
|
|
284
|
+
)
|
|
285
|
+
} catch (e: any) {
|
|
286
|
+
console.error(
|
|
287
|
+
"[PayPal] webhook: signature verification failed:",
|
|
288
|
+
e?.message
|
|
289
|
+
)
|
|
290
|
+
return res
|
|
291
|
+
.status(401)
|
|
292
|
+
.json({ message: "Webhook signature verification failed" })
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const eventVersion = normalizeEventVersion(payload)
|
|
296
|
+
let recordId: string | null = null
|
|
297
|
+
|
|
298
|
+
try {
|
|
299
|
+
const recordResult = await paypal.createWebhookEventRecord({
|
|
300
|
+
event_id: eventId,
|
|
301
|
+
event_type: eventType,
|
|
302
|
+
payload,
|
|
303
|
+
event_version: eventVersion,
|
|
304
|
+
transmission_id: transmissionId,
|
|
305
|
+
transmission_time: transmissionTime,
|
|
306
|
+
status: "processing",
|
|
307
|
+
attempt_count: 1,
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
if (!recordResult.created) {
|
|
311
|
+
console.info("[PayPal] webhook: duplicate event_id", {
|
|
312
|
+
eventId,
|
|
313
|
+
eventType,
|
|
314
|
+
})
|
|
315
|
+
return res.json({ ok: true, duplicate: true })
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
recordId = recordResult.event?.id ?? null
|
|
319
|
+
} catch (e: any) {
|
|
320
|
+
console.error("[PayPal] webhook: failed to create DB record:", e?.message)
|
|
321
|
+
return res.status(500).json({ message: "Failed to record webhook event" })
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (!isAllowedEventType(eventType)) {
|
|
325
|
+
console.info("[PayPal] webhook: unsupported event type, ignoring", {
|
|
326
|
+
eventType,
|
|
327
|
+
})
|
|
328
|
+
await paypal.recordAuditEvent("webhook_unsupported_event", {
|
|
329
|
+
event_id: eventId,
|
|
330
|
+
event_type: eventType,
|
|
331
|
+
})
|
|
332
|
+
if (recordId) {
|
|
333
|
+
await paypal
|
|
334
|
+
.updateWebhookEventRecord({
|
|
335
|
+
id: recordId,
|
|
336
|
+
status: "ignored",
|
|
337
|
+
processed_at: new Date(),
|
|
338
|
+
})
|
|
339
|
+
.catch(() => {})
|
|
340
|
+
}
|
|
341
|
+
return res.json({ ok: true, ignored: true })
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Process the verified, persisted event asynchronously: emit an event and
|
|
345
|
+
// return 200 immediately so PayPal's ~15s webhook delivery timeout is never
|
|
346
|
+
// tripped by slow downstream work (the audit's synchronous-processing
|
|
347
|
+
// bottleneck). The `paypal-webhook-process` subscriber does the actual work
|
|
348
|
+
// off the request path; if the event bus drops the message, the webhook retry
|
|
349
|
+
// cron recovers events left in "processing" past a staleness threshold, so the
|
|
350
|
+
// event is never lost. The record is already persisted as "processing" above.
|
|
351
|
+
try {
|
|
352
|
+
const eventBus = req.scope.resolve<IEventBusModuleService>(
|
|
353
|
+
Modules.EVENT_BUS
|
|
354
|
+
)
|
|
355
|
+
await eventBus.emit({
|
|
356
|
+
name: PAYPAL_WEBHOOK_RECEIVED_EVENT,
|
|
357
|
+
data: { id: recordId },
|
|
358
|
+
})
|
|
359
|
+
console.info("[PayPal] webhook: accepted for async processing", {
|
|
360
|
+
event_id: eventId,
|
|
361
|
+
event_type: eventType,
|
|
362
|
+
record_id: recordId,
|
|
363
|
+
})
|
|
364
|
+
} catch (e: any) {
|
|
365
|
+
// The event was persisted as "processing"; the retry cron's stale-processing
|
|
366
|
+
// recovery will pick it up even though the emit failed. Ack PayPal anyway so
|
|
367
|
+
// it doesn't re-deliver (which would just dedupe).
|
|
368
|
+
console.error(
|
|
369
|
+
"[PayPal] webhook: failed to enqueue async processing (retry cron will recover)",
|
|
370
|
+
{
|
|
371
|
+
event_id: eventId,
|
|
372
|
+
event_type: eventType,
|
|
373
|
+
record_id: recordId,
|
|
374
|
+
error: e?.message,
|
|
375
|
+
}
|
|
376
|
+
)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return res.json({ ok: true, accepted: true })
|
|
380
|
+
}
|