@easypayment/medusa-payment-paypal 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.medusa/server/src/admin/index.js +390 -136
- package/.medusa/server/src/admin/index.mjs +390 -136
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.d.ts +1 -0
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.js +5 -1
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/disconnect/route.d.ts +1 -0
- package/.medusa/server/src/api/admin/paypal/disconnect/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/disconnect/route.js +1 -0
- package/.medusa/server/src/api/admin/paypal/disconnect/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/environment/route.d.ts +2 -0
- package/.medusa/server/src/api/admin/paypal/environment/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/environment/route.js +2 -0
- package/.medusa/server/src/api/admin/paypal/environment/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.d.ts +9 -0
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.js +9 -4
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.d.ts +7 -1
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.js +9 -2
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.d.ts +1 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.js +1 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.d.ts +1 -0
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.js +10 -3
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/settings/route.d.ts +2 -0
- package/.medusa/server/src/api/admin/paypal/settings/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/settings/route.js +9 -2
- package/.medusa/server/src/api/admin/paypal/settings/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/status/route.d.ts +4 -0
- package/.medusa/server/src/api/admin/paypal/status/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/status/route.js +9 -1
- package/.medusa/server/src/api/admin/paypal/status/route.js.map +1 -1
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.d.ts +4 -0
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.js +5 -0
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts +8 -0
- package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/capture-order/route.js +23 -3
- package/.medusa/server/src/api/store/paypal/capture-order/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/config/route.d.ts +5 -0
- package/.medusa/server/src/api/store/paypal/config/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/config/route.js +12 -5
- package/.medusa/server/src/api/store/paypal/config/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/create-order/route.d.ts +8 -0
- package/.medusa/server/src/api/store/paypal/create-order/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/create-order/route.js +54 -20
- package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/onboard-return/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/onboard-return/route.js +19 -19
- package/.medusa/server/src/api/store/paypal/onboard-return/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/settings/route.d.ts +1 -0
- package/.medusa/server/src/api/store/paypal/settings/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/settings/route.js +4 -1
- package/.medusa/server/src/api/store/paypal/settings/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/webhook/route.d.ts +7 -0
- package/.medusa/server/src/api/store/paypal/webhook/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/webhook/route.js +34 -3
- package/.medusa/server/src/api/store/paypal/webhook/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal-complete/route.d.ts +8 -0
- package/.medusa/server/src/api/store/paypal-complete/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal-complete/route.js +33 -6
- package/.medusa/server/src/api/store/paypal-complete/route.js.map +1 -1
- package/.medusa/server/src/api/utils/rate-limit.d.ts +4 -0
- package/.medusa/server/src/api/utils/rate-limit.d.ts.map +1 -1
- package/.medusa/server/src/api/utils/rate-limit.js +9 -4
- package/.medusa/server/src/api/utils/rate-limit.js.map +1 -1
- package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts +5 -0
- package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts.map +1 -1
- package/.medusa/server/src/jobs/paypal-webhook-retry.js +13 -2
- package/.medusa/server/src/jobs/paypal-webhook-retry.js.map +1 -1
- package/.medusa/server/src/modules/paypal/index.d.ts +1 -0
- package/.medusa/server/src/modules/paypal/index.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/index.js +1 -0
- package/.medusa/server/src/modules/paypal/index.js.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20260115120000_create_paypal_connection.js +22 -22
- package/.medusa/server/src/modules/paypal/migrations/20260123090000_create_paypal_settings.js +11 -11
- package/.medusa/server/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.js +18 -18
- package/.medusa/server/src/modules/paypal/migrations/20260401090000_create_paypal_metric.js +16 -16
- package/.medusa/server/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.js +20 -20
- package/.medusa/server/src/modules/paypal/migrations/20261101090000_remove_paypal_reconciliation_status.js +14 -14
- package/.medusa/server/src/modules/paypal/migrations/20261201090000_remove_paypal_audit_log.js +15 -15
- package/.medusa/server/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.js.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270102090000_add_paypal_seller_email.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270102090000_add_paypal_seller_email.js.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270103090000_add_paypal_seller_merchant_id.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270103090000_add_paypal_seller_merchant_id.js.map +1 -1
- package/.medusa/server/src/modules/paypal/migrations/20270201000000_add_webhook_dead_letter.js +6 -6
- package/.medusa/server/src/modules/paypal/models/paypal_connection.d.ts +5 -0
- package/.medusa/server/src/modules/paypal/models/paypal_connection.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_connection.js +5 -0
- package/.medusa/server/src/modules/paypal/models/paypal_connection.js.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_metric.d.ts +4 -0
- package/.medusa/server/src/modules/paypal/models/paypal_metric.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_metric.js +4 -0
- package/.medusa/server/src/modules/paypal/models/paypal_metric.js.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_settings.d.ts +5 -0
- package/.medusa/server/src/modules/paypal/models/paypal_settings.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_settings.js +5 -0
- package/.medusa/server/src/modules/paypal/models/paypal_settings.js.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.d.ts +7 -0
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.js +7 -0
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts +14 -6
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js +55 -32
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/index.d.ts +6 -1
- package/.medusa/server/src/modules/paypal/payment-provider/index.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/index.js +6 -4
- package/.medusa/server/src/modules/paypal/payment-provider/index.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts +8 -0
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.js +39 -12
- package/.medusa/server/src/modules/paypal/payment-provider/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/service.d.ts +72 -2
- package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/service.js +159 -33
- package/.medusa/server/src/modules/paypal/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.d.ts +2 -0
- package/.medusa/server/src/modules/paypal/types/config.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.js +5 -2
- package/.medusa/server/src/modules/paypal/types/config.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/amounts.d.ts +1 -0
- package/.medusa/server/src/modules/paypal/utils/amounts.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/amounts.js +15 -5
- package/.medusa/server/src/modules/paypal/utils/amounts.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.js +3 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/currencies.d.ts +9 -0
- package/.medusa/server/src/modules/paypal/utils/currencies.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/currencies.js +13 -0
- package/.medusa/server/src/modules/paypal/utils/currencies.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/payment-session.d.ts +12 -0
- package/.medusa/server/src/modules/paypal/utils/payment-session.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/payment-session.js +4 -3
- package/.medusa/server/src/modules/paypal/utils/payment-session.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-auth.d.ts +1 -0
- package/.medusa/server/src/modules/paypal/utils/paypal-auth.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-auth.js +1 -0
- package/.medusa/server/src/modules/paypal/utils/paypal-auth.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts +11 -0
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js +17 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/provider-ids.d.ts +6 -0
- package/.medusa/server/src/modules/paypal/utils/provider-ids.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/provider-ids.js +6 -0
- package/.medusa/server/src/modules/paypal/utils/provider-ids.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/secret-crypto.d.ts +2 -0
- package/.medusa/server/src/modules/paypal/utils/secret-crypto.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/secret-crypto.js +6 -1
- package/.medusa/server/src/modules/paypal/utils/secret-crypto.js.map +1 -1
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts +43 -0
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/webhook-processor.js +57 -5
- package/.medusa/server/src/modules/paypal/webhook-processor.js.map +1 -1
- package/.medusa/server/src/providers/paypal/index.d.ts +1 -0
- package/.medusa/server/src/providers/paypal/index.d.ts.map +1 -1
- package/.medusa/server/src/providers/paypal/index.js +1 -0
- package/.medusa/server/src/providers/paypal/index.js.map +1 -1
- package/.medusa/server/src/providers/paypal_card/index.d.ts +1 -0
- package/.medusa/server/src/providers/paypal_card/index.d.ts.map +1 -1
- package/.medusa/server/src/providers/paypal_card/index.js +1 -0
- package/.medusa/server/src/providers/paypal_card/index.js.map +1 -1
- package/.medusa/server/src/subscribers/paypal-order-invoice.d.ts +6 -0
- package/.medusa/server/src/subscribers/paypal-order-invoice.d.ts.map +1 -1
- package/.medusa/server/src/subscribers/paypal-order-invoice.js +9 -1
- package/.medusa/server/src/subscribers/paypal-order-invoice.js.map +1 -1
- package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts +1 -0
- package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts.map +1 -1
- package/.medusa/server/src/subscribers/paypal-webhook-process.js +17 -4
- package/.medusa/server/src/subscribers/paypal-webhook-process.js.map +1 -1
- package/CHANGELOG.md +19 -0
- package/README.md +184 -175
- package/package.json +24 -5
- package/src/admin/index.ts +1 -1
- package/src/admin/routes/settings/paypal/_components/FieldRow.tsx +38 -29
- package/src/admin/routes/settings/paypal/_components/SectionCard.tsx +29 -26
- package/src/admin/routes/settings/paypal/_components/Tabs.tsx +49 -48
- package/src/admin/routes/settings/paypal/_components/Toast.tsx +63 -52
- package/src/admin/routes/settings/paypal/_utils/adminFetch.ts +52 -32
- package/src/admin/routes/settings/paypal/additional-settings/page.tsx +246 -139
- package/src/admin/routes/settings/paypal/advanced-card-payments/page.tsx +208 -123
- package/src/admin/routes/settings/paypal/apple-pay/page.tsx +9 -5
- package/src/admin/routes/settings/paypal/connection/page.tsx +974 -908
- package/src/admin/routes/settings/paypal/google-pay/page.tsx +9 -5
- package/src/admin/routes/settings/paypal/page.tsx +14 -13
- package/src/admin/routes/settings/paypal/pay-later-messaging/page.tsx +9 -5
- package/src/admin/routes/settings/paypal/paypal-settings/page.tsx +304 -273
- package/src/api/admin/payment-collections/[id]/payment-sessions/route.ts +48 -39
- package/src/api/admin/paypal/disconnect/route.ts +15 -14
- package/src/api/admin/paypal/environment/route.ts +34 -32
- package/src/api/admin/paypal/onboard-complete/route.ts +61 -50
- package/src/api/admin/paypal/onboarding-link/route.ts +91 -76
- package/src/api/admin/paypal/onboarding-status/route.ts +21 -20
- package/src/api/admin/paypal/save-credentials/route.ts +48 -37
- package/src/api/admin/paypal/settings/route.ts +63 -45
- package/src/api/admin/paypal/status/route.ts +29 -17
- package/src/api/hooks/paypal/onboard-return/route.ts +15 -15
- package/src/api/hooks/paypal/webhook/route.ts +17 -17
- package/src/api/middlewares.ts +61 -61
- package/src/api/store/payment-collections/[id]/payment-sessions/route.ts +63 -54
- package/src/api/store/paypal/capture-order/route.ts +416 -370
- package/src/api/store/paypal/config/route.ts +121 -115
- package/src/api/store/paypal/create-order/route.ts +586 -539
- package/src/api/store/paypal/onboard-return/route.ts +116 -113
- package/src/api/store/paypal/settings/route.ts +29 -19
- package/src/api/store/paypal/webhook/route.ts +380 -314
- package/src/api/store/paypal-complete/route.ts +157 -128
- package/src/api/utils/rate-limit.ts +143 -129
- package/src/index.ts +1 -1
- package/src/jobs/paypal-webhook-retry.ts +257 -241
- package/src/modules/paypal/index.ts +9 -8
- package/src/modules/paypal/migrations/20260115120000_create_paypal_connection.ts +33 -33
- package/src/modules/paypal/migrations/20260123090000_create_paypal_settings.ts +22 -22
- package/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.ts +29 -29
- package/src/modules/paypal/migrations/20260401090000_create_paypal_metric.ts +27 -27
- package/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.ts +31 -31
- package/src/modules/paypal/migrations/20261101090000_remove_paypal_reconciliation_status.ts +25 -25
- package/src/modules/paypal/migrations/20261201090000_remove_paypal_audit_log.ts +26 -26
- package/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.ts +15 -11
- package/src/modules/paypal/migrations/20270102090000_add_paypal_seller_email.ts +15 -11
- package/src/modules/paypal/migrations/20270103090000_add_paypal_seller_merchant_id.ts +15 -11
- package/src/modules/paypal/migrations/20270201000000_add_webhook_dead_letter.ts +17 -17
- package/src/modules/paypal/migrations/20270202000000_align_nullable_defaults.ts +44 -44
- package/src/modules/paypal/migrations/20270301000000_add_webhook_transmission_id_index.ts +15 -15
- package/src/modules/paypal/models/paypal_connection.ts +28 -23
- package/src/modules/paypal/models/paypal_metric.ts +13 -9
- package/src/modules/paypal/models/paypal_settings.ts +13 -8
- package/src/modules/paypal/models/paypal_webhook_event.ts +26 -19
- package/src/modules/paypal/payment-provider/base-provider.ts +287 -275
- package/src/modules/paypal/payment-provider/card-service.ts +927 -855
- package/src/modules/paypal/payment-provider/index.ts +14 -15
- package/src/modules/paypal/payment-provider/service.ts +906 -836
- package/src/modules/paypal/payment-provider/status-utils.ts +59 -59
- package/src/modules/paypal/payment-provider/webhook-utils.ts +109 -109
- package/src/modules/paypal/service.ts +362 -115
- package/src/modules/paypal/types/config.ts +70 -65
- package/src/modules/paypal/utils/amounts.ts +123 -107
- package/src/modules/paypal/utils/core-workflow.ts +46 -46
- package/src/modules/paypal/utils/credential-resolver.ts +275 -271
- package/src/modules/paypal/utils/currencies.ts +97 -84
- package/src/modules/paypal/utils/partner.ts +8 -8
- package/src/modules/paypal/utils/payment-session.ts +144 -138
- package/src/modules/paypal/utils/paypal-auth.ts +6 -5
- package/src/modules/paypal/utils/paypal-fetch.ts +197 -181
- package/src/modules/paypal/utils/provider-ids.ts +21 -15
- package/src/modules/paypal/utils/secret-crypto.ts +226 -197
- package/src/modules/paypal/utils/webhook-verify.ts +61 -61
- package/src/modules/paypal/webhook-processor.ts +641 -573
- package/src/providers/paypal/index.ts +9 -8
- package/src/providers/paypal_card/index.ts +9 -8
- package/src/subscribers/paypal-order-invoice.ts +180 -173
- package/src/subscribers/paypal-webhook-process.ts +147 -131
- package/tsconfig.json +21 -31
|
@@ -1,271 +1,275 @@
|
|
|
1
|
-
import { recordMetricAtomic } from "./metrics"
|
|
2
|
-
import { PAYPAL_PARTNER_ATTRIBUTION_ID as BN_CODE } from "./partner"
|
|
3
|
-
import { paypalFetch } from "./paypal-fetch"
|
|
4
|
-
import { decryptSecret, encryptSecret } from "./secret-crypto"
|
|
5
|
-
|
|
6
|
-
const TOKEN_MARGIN_MS = 2 * 60 * 1000
|
|
7
|
-
|
|
8
|
-
type Environment = "sandbox" | "live"
|
|
9
|
-
|
|
10
|
-
interface ActiveCredentials {
|
|
11
|
-
environment: Environment
|
|
12
|
-
client_id: string
|
|
13
|
-
client_secret: string
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface AccessTokenResult {
|
|
17
|
-
accessToken: string
|
|
18
|
-
base: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface ResolvedSettings {
|
|
22
|
-
additionalSettings: Record<string, unknown>
|
|
23
|
-
advancedCardSettings: Record<string, unknown>
|
|
24
|
-
apiDetails: Record<string, unknown>
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Self-contained credential and token management for PayPal payment providers.
|
|
29
|
-
*
|
|
30
|
-
* Payment providers in Medusa v2 run inside the payment module's scoped
|
|
31
|
-
* container, which is isolated from the application container where the
|
|
32
|
-
* PayPal module service lives. This resolver bypasses the container boundary
|
|
33
|
-
* entirely by reading credentials directly from the database via pgConnection
|
|
34
|
-
* (knex), which Medusa injects into every module container.
|
|
35
|
-
*
|
|
36
|
-
* Same source of truth (paypal_connection / paypal_settings tables), zero
|
|
37
|
-
* cross-container dependencies.
|
|
38
|
-
*/
|
|
39
|
-
export class PayPalCredentialResolver {
|
|
40
|
-
private db: any
|
|
41
|
-
private tokenRefreshPromise: Promise<string> | null = null
|
|
42
|
-
|
|
43
|
-
constructor(pgConnection: any) {
|
|
44
|
-
this.db = pgConnection
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
private async getConnectionRow(): Promise<any | null> {
|
|
48
|
-
// Match the module service's soft-delete semantics: this raw knex read
|
|
49
|
-
// bypasses MikroORM's automatic deleted_at filter, so apply it explicitly
|
|
50
|
-
// or a soft-deleted connection's credentials would keep being used.
|
|
51
|
-
const rows = await this.db("paypal_connection")
|
|
52
|
-
.select("*")
|
|
53
|
-
.whereNull("deleted_at")
|
|
54
|
-
.orderBy("created_at", "desc")
|
|
55
|
-
.limit(1)
|
|
56
|
-
|
|
57
|
-
return rows?.[0] ?? null
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Resolve the amount an admin actually requested for a capture.
|
|
62
|
-
*
|
|
63
|
-
* Medusa does NOT pass the requested capture amount to the provider — only
|
|
64
|
-
* `context.idempotency_key`, which is the Medusa `capture` row id. Without
|
|
65
|
-
* this lookup the provider can only capture the full session amount, which
|
|
66
|
-
* over-charges the buyer on partial captures. Returns null when the id
|
|
67
|
-
* doesn't resolve (caller falls back to the session amount).
|
|
68
|
-
*/
|
|
69
|
-
async getRequestedCaptureAmount(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
.
|
|
76
|
-
.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
if (
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return null
|
|
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
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
string,
|
|
235
|
-
unknown
|
|
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
|
-
|
|
1
|
+
import { recordMetricAtomic } from "./metrics"
|
|
2
|
+
import { PAYPAL_PARTNER_ATTRIBUTION_ID as BN_CODE } from "./partner"
|
|
3
|
+
import { paypalFetch } from "./paypal-fetch"
|
|
4
|
+
import { decryptSecret, encryptSecret } from "./secret-crypto"
|
|
5
|
+
|
|
6
|
+
const TOKEN_MARGIN_MS = 2 * 60 * 1000
|
|
7
|
+
|
|
8
|
+
type Environment = "sandbox" | "live"
|
|
9
|
+
|
|
10
|
+
interface ActiveCredentials {
|
|
11
|
+
environment: Environment
|
|
12
|
+
client_id: string
|
|
13
|
+
client_secret: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface AccessTokenResult {
|
|
17
|
+
accessToken: string
|
|
18
|
+
base: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ResolvedSettings {
|
|
22
|
+
additionalSettings: Record<string, unknown>
|
|
23
|
+
advancedCardSettings: Record<string, unknown>
|
|
24
|
+
apiDetails: Record<string, unknown>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Self-contained credential and token management for PayPal payment providers.
|
|
29
|
+
*
|
|
30
|
+
* Payment providers in Medusa v2 run inside the payment module's scoped
|
|
31
|
+
* container, which is isolated from the application container where the
|
|
32
|
+
* PayPal module service lives. This resolver bypasses the container boundary
|
|
33
|
+
* entirely by reading credentials directly from the database via pgConnection
|
|
34
|
+
* (knex), which Medusa injects into every module container.
|
|
35
|
+
*
|
|
36
|
+
* Same source of truth (paypal_connection / paypal_settings tables), zero
|
|
37
|
+
* cross-container dependencies.
|
|
38
|
+
*/
|
|
39
|
+
export class PayPalCredentialResolver {
|
|
40
|
+
private db: any
|
|
41
|
+
private tokenRefreshPromise: Promise<string> | null = null
|
|
42
|
+
|
|
43
|
+
constructor(pgConnection: any) {
|
|
44
|
+
this.db = pgConnection
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private async getConnectionRow(): Promise<any | null> {
|
|
48
|
+
// Match the module service's soft-delete semantics: this raw knex read
|
|
49
|
+
// bypasses MikroORM's automatic deleted_at filter, so apply it explicitly
|
|
50
|
+
// or a soft-deleted connection's credentials would keep being used.
|
|
51
|
+
const rows = await this.db("paypal_connection")
|
|
52
|
+
.select("*")
|
|
53
|
+
.whereNull("deleted_at")
|
|
54
|
+
.orderBy("created_at", "desc")
|
|
55
|
+
.limit(1)
|
|
56
|
+
|
|
57
|
+
return rows?.[0] ?? null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Resolve the amount an admin actually requested for a capture.
|
|
62
|
+
*
|
|
63
|
+
* Medusa does NOT pass the requested capture amount to the provider — only
|
|
64
|
+
* `context.idempotency_key`, which is the Medusa `capture` row id. Without
|
|
65
|
+
* this lookup the provider can only capture the full session amount, which
|
|
66
|
+
* over-charges the buyer on partial captures. Returns null when the id
|
|
67
|
+
* doesn't resolve (caller falls back to the session amount).
|
|
68
|
+
*/
|
|
69
|
+
async getRequestedCaptureAmount(
|
|
70
|
+
captureRowId: string
|
|
71
|
+
): Promise<number | null> {
|
|
72
|
+
try {
|
|
73
|
+
if (!captureRowId || typeof captureRowId !== "string") return null
|
|
74
|
+
const rows = await this.db("capture")
|
|
75
|
+
.select("amount", "raw_amount")
|
|
76
|
+
.where("id", captureRowId)
|
|
77
|
+
.whereNull("deleted_at")
|
|
78
|
+
.limit(1)
|
|
79
|
+
const row = rows?.[0]
|
|
80
|
+
if (!row) return null
|
|
81
|
+
const raw = row.raw_amount
|
|
82
|
+
if (raw && typeof raw === "object" && raw.value !== undefined) {
|
|
83
|
+
const v = Number(raw.value)
|
|
84
|
+
if (Number.isFinite(v) && v > 0) return v
|
|
85
|
+
}
|
|
86
|
+
const v = Number(row.amount)
|
|
87
|
+
return Number.isFinite(v) && v > 0 ? v : null
|
|
88
|
+
} catch {
|
|
89
|
+
return null
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private getEnvCreds(
|
|
94
|
+
row: any,
|
|
95
|
+
env: Environment
|
|
96
|
+
): { clientId?: string; clientSecret?: string } {
|
|
97
|
+
const meta = (row?.metadata || {}) as any
|
|
98
|
+
const creds = meta?.credentials?.[env] || {}
|
|
99
|
+
return {
|
|
100
|
+
clientId: creds.client_id || creds.clientId || undefined,
|
|
101
|
+
clientSecret:
|
|
102
|
+
decryptSecret(creds.client_secret || creds.clientSecret) || undefined,
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async getActiveCredentials(): Promise<ActiveCredentials> {
|
|
107
|
+
const row = await this.getConnectionRow()
|
|
108
|
+
if (!row) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
"PayPal connection not found. Please complete onboarding in the admin panel."
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const env: Environment =
|
|
115
|
+
(row.environment as Environment) === "sandbox" ? "sandbox" : "live"
|
|
116
|
+
const c = this.getEnvCreds(row, env)
|
|
117
|
+
|
|
118
|
+
if (!c.clientId || !c.clientSecret) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`PayPal credentials missing for environment "${env}". Please save credentials in the admin panel.`
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
environment: env,
|
|
126
|
+
client_id: c.clientId,
|
|
127
|
+
client_secret: c.clientSecret,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async getAccessToken(): Promise<AccessTokenResult> {
|
|
132
|
+
const creds = await this.getActiveCredentials()
|
|
133
|
+
const base =
|
|
134
|
+
creds.environment === "live"
|
|
135
|
+
? "https://api-m.paypal.com"
|
|
136
|
+
: "https://api-m.sandbox.paypal.com"
|
|
137
|
+
|
|
138
|
+
const row = await this.getConnectionRow()
|
|
139
|
+
if (row?.app_access_token && row.app_access_token_expires_at) {
|
|
140
|
+
const expiresAt = new Date(row.app_access_token_expires_at)
|
|
141
|
+
if (expiresAt.getTime() - Date.now() > TOKEN_MARGIN_MS) {
|
|
142
|
+
return {
|
|
143
|
+
accessToken: decryptSecret(row.app_access_token) as string,
|
|
144
|
+
base,
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (this.tokenRefreshPromise) {
|
|
150
|
+
const accessToken = await this.tokenRefreshPromise
|
|
151
|
+
return { accessToken, base }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
this.tokenRefreshPromise = this.refreshAccessToken(row, creds).finally(
|
|
155
|
+
() => {
|
|
156
|
+
this.tokenRefreshPromise = null
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
const accessToken = await this.tokenRefreshPromise
|
|
161
|
+
return { accessToken, base }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private async refreshAccessToken(
|
|
165
|
+
row: any,
|
|
166
|
+
creds: ActiveCredentials
|
|
167
|
+
): Promise<string> {
|
|
168
|
+
const base =
|
|
169
|
+
creds.environment === "live"
|
|
170
|
+
? "https://api-m.paypal.com"
|
|
171
|
+
: "https://api-m.sandbox.paypal.com"
|
|
172
|
+
const basic = Buffer.from(
|
|
173
|
+
`${creds.client_id}:${creds.client_secret}`
|
|
174
|
+
).toString("base64")
|
|
175
|
+
|
|
176
|
+
const body = new URLSearchParams()
|
|
177
|
+
body.set("grant_type", "client_credentials")
|
|
178
|
+
|
|
179
|
+
const res = await paypalFetch(`${base}/v1/oauth2/token`, {
|
|
180
|
+
method: "POST",
|
|
181
|
+
headers: {
|
|
182
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
183
|
+
Authorization: `Basic ${basic}`,
|
|
184
|
+
"PayPal-Partner-Attribution-Id": BN_CODE,
|
|
185
|
+
},
|
|
186
|
+
body,
|
|
187
|
+
signal: AbortSignal.timeout(30_000),
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
const json = await res.json().catch(() => ({}))
|
|
191
|
+
if (!res.ok) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
`PayPal client_credentials failed (${res.status}): ${JSON.stringify(json)}`
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const accessToken = String(json.access_token || "")
|
|
198
|
+
if (!accessToken) {
|
|
199
|
+
throw new Error(
|
|
200
|
+
"PayPal client_credentials succeeded but access_token is missing."
|
|
201
|
+
)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const expiresIn = Number(json.expires_in || 3600)
|
|
205
|
+
const newExpiresAt = new Date(Date.now() + expiresIn * 1000)
|
|
206
|
+
|
|
207
|
+
if (row?.id) {
|
|
208
|
+
await this.db("paypal_connection")
|
|
209
|
+
.where("id", row.id)
|
|
210
|
+
.update({
|
|
211
|
+
app_access_token: encryptSecret(accessToken),
|
|
212
|
+
app_access_token_expires_at: newExpiresAt,
|
|
213
|
+
updated_at: new Date(),
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return accessToken
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async getSettings(): Promise<ResolvedSettings> {
|
|
221
|
+
try {
|
|
222
|
+
// Deterministic singleton read (oldest row wins) — must match the module
|
|
223
|
+
// service's getSettings ordering so both containers agree on the same
|
|
224
|
+
// row even if a first-boot race ever created a duplicate.
|
|
225
|
+
const rows = await this.db("paypal_settings")
|
|
226
|
+
.select("data")
|
|
227
|
+
.whereNull("deleted_at")
|
|
228
|
+
.orderBy("created_at", "asc")
|
|
229
|
+
.limit(1)
|
|
230
|
+
|
|
231
|
+
const data = (rows?.[0]?.data || {}) as Record<string, any>
|
|
232
|
+
return {
|
|
233
|
+
additionalSettings: (data.additional_settings || {}) as Record<
|
|
234
|
+
string,
|
|
235
|
+
unknown
|
|
236
|
+
>,
|
|
237
|
+
advancedCardSettings: (data.advanced_card_payments || {}) as Record<
|
|
238
|
+
string,
|
|
239
|
+
unknown
|
|
240
|
+
>,
|
|
241
|
+
apiDetails: (data.api_details || {}) as Record<string, unknown>,
|
|
242
|
+
}
|
|
243
|
+
} catch {
|
|
244
|
+
return {
|
|
245
|
+
additionalSettings: {},
|
|
246
|
+
advancedCardSettings: {},
|
|
247
|
+
apiDetails: {},
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async recordAuditEvent(
|
|
253
|
+
eventType: string,
|
|
254
|
+
metadata?: Record<string, unknown>
|
|
255
|
+
) {
|
|
256
|
+
try {
|
|
257
|
+
console.info(
|
|
258
|
+
JSON.stringify({
|
|
259
|
+
log: "paypal_audit",
|
|
260
|
+
event_type: eventType,
|
|
261
|
+
at: new Date().toISOString(),
|
|
262
|
+
...(metadata ? { metadata } : {}),
|
|
263
|
+
})
|
|
264
|
+
)
|
|
265
|
+
} catch {
|
|
266
|
+
// audit logging must never break the payment flow
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async recordMetric(name: string, metadata?: Record<string, unknown>) {
|
|
271
|
+
// Atomic upsert (single INSERT ... ON CONFLICT statement) so concurrent
|
|
272
|
+
// captures/refunds don't lose metric updates via a read-modify-write race.
|
|
273
|
+
await recordMetricAtomic(this.db, name, metadata)
|
|
274
|
+
}
|
|
275
|
+
}
|