@easypayment/medusa-payment-paypal 1.1.2 → 1.2.1

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.
Files changed (68) hide show
  1. package/.medusa/server/src/admin/index.js +73 -73
  2. package/.medusa/server/src/admin/index.mjs +73 -73
  3. package/.medusa/server/src/api/middlewares.d.ts.map +1 -1
  4. package/.medusa/server/src/api/middlewares.js +16 -2
  5. package/.medusa/server/src/api/middlewares.js.map +1 -1
  6. package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts.map +1 -1
  7. package/.medusa/server/src/api/store/paypal/capture-order/route.js +66 -13
  8. package/.medusa/server/src/api/store/paypal/capture-order/route.js.map +1 -1
  9. package/.medusa/server/src/api/store/paypal/create-order/route.d.ts.map +1 -1
  10. package/.medusa/server/src/api/store/paypal/create-order/route.js +11 -6
  11. package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -1
  12. package/.medusa/server/src/api/utils/rate-limit.d.ts +13 -1
  13. package/.medusa/server/src/api/utils/rate-limit.d.ts.map +1 -1
  14. package/.medusa/server/src/api/utils/rate-limit.js +7 -5
  15. package/.medusa/server/src/api/utils/rate-limit.js.map +1 -1
  16. package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts.map +1 -1
  17. package/.medusa/server/src/jobs/paypal-webhook-retry.js +4 -0
  18. package/.medusa/server/src/jobs/paypal-webhook-retry.js.map +1 -1
  19. package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts +15 -0
  20. package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts.map +1 -1
  21. package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js +26 -0
  22. package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js.map +1 -1
  23. package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts.map +1 -1
  24. package/.medusa/server/src/modules/paypal/payment-provider/card-service.js +244 -169
  25. package/.medusa/server/src/modules/paypal/payment-provider/card-service.js.map +1 -1
  26. package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts +0 -4
  27. package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts.map +1 -1
  28. package/.medusa/server/src/modules/paypal/payment-provider/service.js +8 -25
  29. package/.medusa/server/src/modules/paypal/payment-provider/service.js.map +1 -1
  30. package/.medusa/server/src/modules/paypal/service.d.ts +2 -0
  31. package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -1
  32. package/.medusa/server/src/modules/paypal/service.js +54 -13
  33. package/.medusa/server/src/modules/paypal/service.js.map +1 -1
  34. package/.medusa/server/src/modules/paypal/types/config.d.ts.map +1 -1
  35. package/.medusa/server/src/modules/paypal/types/config.js +13 -0
  36. package/.medusa/server/src/modules/paypal/types/config.js.map +1 -1
  37. package/.medusa/server/src/modules/paypal/utils/amounts.d.ts +1 -1
  38. package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts.map +1 -1
  39. package/.medusa/server/src/modules/paypal/utils/credential-resolver.js +10 -34
  40. package/.medusa/server/src/modules/paypal/utils/credential-resolver.js.map +1 -1
  41. package/.medusa/server/src/modules/paypal/utils/metrics.d.ts +18 -0
  42. package/.medusa/server/src/modules/paypal/utils/metrics.d.ts.map +1 -0
  43. package/.medusa/server/src/modules/paypal/utils/metrics.js +45 -0
  44. package/.medusa/server/src/modules/paypal/utils/metrics.js.map +1 -0
  45. package/.medusa/server/src/modules/paypal/webhook-processor.d.ts +16 -0
  46. package/.medusa/server/src/modules/paypal/webhook-processor.d.ts.map +1 -1
  47. package/.medusa/server/src/modules/paypal/webhook-processor.js +85 -2
  48. package/.medusa/server/src/modules/paypal/webhook-processor.js.map +1 -1
  49. package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts.map +1 -1
  50. package/.medusa/server/src/subscribers/paypal-webhook-process.js +4 -0
  51. package/.medusa/server/src/subscribers/paypal-webhook-process.js.map +1 -1
  52. package/CHANGELOG.md +208 -146
  53. package/README.md +175 -184
  54. package/package.json +91 -88
  55. package/src/api/middlewares.ts +16 -2
  56. package/src/api/store/paypal/capture-order/route.ts +83 -12
  57. package/src/api/store/paypal/create-order/route.ts +11 -7
  58. package/src/api/utils/rate-limit.ts +20 -5
  59. package/src/jobs/paypal-webhook-retry.ts +4 -0
  60. package/src/modules/paypal/payment-provider/base-provider.ts +31 -0
  61. package/src/modules/paypal/payment-provider/card-service.ts +84 -0
  62. package/src/modules/paypal/payment-provider/service.ts +12 -30
  63. package/src/modules/paypal/service.ts +1703 -1651
  64. package/src/modules/paypal/types/config.ts +16 -0
  65. package/src/modules/paypal/utils/credential-resolver.ts +9 -36
  66. package/src/modules/paypal/utils/metrics.ts +48 -0
  67. package/src/modules/paypal/webhook-processor.ts +101 -2
  68. package/src/subscribers/paypal-webhook-process.ts +4 -0
package/CHANGELOG.md CHANGED
@@ -1,146 +1,208 @@
1
- # Changelog
2
-
3
- All notable changes to `@easypayment/medusa-payment-paypal` are documented here.
4
-
5
- ## 1.1.0
6
-
7
- ### Fixed (re-audit)
8
- - **PayPal webhooks and the onboarding return bridge now work without a
9
- publishable API key.** Medusa applies its store publishable-key middleware to
10
- every `/store/*` route, and PayPal cannot send `x-publishable-api-key` — so
11
- webhook deliveries and onboarding return redirects to the old `/store/paypal/*`
12
- paths were rejected before the handlers ran. Both handlers are now also served
13
- from `/hooks/paypal/webhook` and `/hooks/paypal/onboard-return` (no key
14
- guard). New registrations use the `/hooks` URLs, and existing PayPal webhook
15
- registrations pointing at the legacy path are migrated in place (best-effort,
16
- on admin status view) via PayPal's webhook PATCH API. The legacy `/store`
17
- routes remain for deployments that inject the key at their edge.
18
- - **Unapproved orders can no longer be booked as authorized.** `authorizePayment`
19
- treated a PayPal order in `CREATED`/`SAVED` status as authorized, letting a
20
- cart complete (order placed, inventory reserved) with no payment approved at
21
- PayPal at all. Only buyer-`APPROVED` orders now count.
22
- - **PENDING / DECLINED captures are no longer booked as captured.** The
23
- idempotency branches of `authorizePayment` (both providers) keyed off the mere
24
- presence of a capture/authorization id and returned the settings-derived
25
- status. They now derive the status from the actual PayPal resource: a
26
- `COMPLETED` capture → captured; a `PENDING` (eCheck) capture → authorized
27
- (order placed, funds not claimed until the `CAPTURE.COMPLETED` webhook); a
28
- `DENIED`/`DECLINED`/`FAILED` capture → error.
29
- - **Partial captures no longer charge the full amount.** Medusa never passes the
30
- requested capture amount to a provider — only the Medusa capture-row id (as
31
- `context.idempotency_key`). The providers now resolve the requested amount
32
- from that row, so an admin capturing €40 of a €100 authorization no longer
33
- sends a €100 capture to PayPal.
34
- - **Stale PayPal orders are invalidated when the cart changes.** `updatePayment`
35
- now drops the stored `paypal.order_id` when the session amount/currency
36
- changes (before any capture/authorization), and `create-order` verifies a
37
- stored order's amount, currency, and state against the cart before reusing it
38
- — so a buyer who backs out of the PayPal popup and edits their cart is charged
39
- the new total, not the stale one.
40
- - **Card provider no longer 422s on approved capture-intent orders.** Its
41
- `authorizePayment` called `/v2/checkout/orders/{id}/authorize` regardless of
42
- intent, which PayPal rejects (`UNSUPPORTED_INTENT`) for `CAPTURE`-intent
43
- orders — permanently stranding a checkout whose storefront capture call had
44
- failed after 3-D Secure approval. Approved orders now return `authorized` and
45
- the capture is routed by intent; `/authorize` is only called for
46
- `AUTHORIZE`-intent orders. The impossible create-order-then-authorize
47
- fallbacks (both providers) were replaced with clear errors.
48
- - **Webhook processing no longer corrupts session data.** `PAYMENT.CAPTURE.REFUNDED`
49
- / `REVERSED` events carry a *refund* resource — its id was being stored as the
50
- session's `capture_id`. Identifiers are now extracted refund-aware (with the
51
- capture id taken from `related_ids` or the resource's "up" link), and patches
52
- no longer overwrite stored fields (e.g. `order_id`) with null when an event
53
- lacks them.
54
- - **Partial refunds no longer cancel the payment session.** Refund events now
55
- compare the cumulative refunded total against the captured amount and keep the
56
- session status for partial refunds (recording the refund data); only full
57
- refunds transition the session to canceled.
58
- - **Rate limiter can no longer be bypassed via `X-Forwarded-For`.** The client
59
- key now uses the right-most (proxy-appended) XFF entry instead of the
60
- left-most (client-forgeable) one.
61
- - **`paypal-complete` no longer 500s on an empty/non-JSON body** (missing body
62
- guard before destructuring).
63
- - **Client-supplied idempotency keys are now cart-scoped in `create-order`**, so
64
- a reused `Idempotency-Key` header across two carts can no longer return cart
65
- A's cached PayPal order (and amount) for cart B.
66
- - **Circuit breaker is now keyed per host.** Failures of a non-PayPal host (e.g.
67
- the onboarding service) can no longer open the breaker and block live
68
- captures/refunds against a healthy PayPal API.
69
- - **Raw-knex reads now respect soft deletes** (`paypal_connection` /
70
- `paypal_settings` lookups in the credential resolver filter `deleted_at`).
71
- - **Removed the broken `./workflows` package export** (it pointed at a file that
72
- was never built; importing it always failed).
73
-
74
- ### Added (re-audit)
75
- - `GET /store/paypal/config` now emits `disable_buttons` (from
76
- `paypal_settings.disableButtons` / `disable_buttons`), which the UI package
77
- has always read to build the SDK's `disable-funding` list.
78
-
79
-
80
- ### Fixed
81
- - **Transient capture/refund failures now retry.** The `paypalFetchWithRetry`
82
- wrapper (jittered backoff on 5xx/429/timeout) was implemented but never wired
83
- in — capture and refund used the non-retrying path. They now retry safely: the
84
- `PayPal-Request-Id` idempotency key means a retry re-uses the same
85
- capture/refund instead of double-charging.
86
- - **Provider `recordMetric` is now atomic.** The credential-resolver metric path
87
- used by the payment providers (capture/refund/authorize success & failure
88
- counters) now uses a single `INSERT … ON CONFLICT … count = count + 1` upsert
89
- instead of a read-modify-write, so concurrent captures/refunds no longer lose
90
- increments. (The module-service metric path remains best-effort and unchanged.)
91
- - **Capture persisted-but-lost gap is now observable.** After a successful PayPal
92
- capture, session persistence is retried, and if it still fails the route logs
93
- CRITICAL and records a `capture_order_persist_failed` metric instead of
94
- silently swallowing the error (the webhook and `paypal-complete`'s live
95
- re-derivation remain the reconciliation backstop).
96
- - **Card provider partial-capture idempotency key** now includes the amount, so
97
- two sequential partial captures of the same order are no longer deduplicated by
98
- PayPal into one (matching the wallet provider).
99
-
100
- ### Added
101
- - **Asynchronous webhook processing.** The webhook route now verifies, persists,
102
- and acknowledges (200) immediately, then processes off the request path via a
103
- subscriber — so PayPal's ~15s delivery timeout is never tripped by slow
104
- downstream work. The retry cron additionally recovers events left in
105
- `processing` past a staleness threshold, so an event is never lost even if the
106
- event bus drops a message.
107
- - **Opt-in rate limiting** on the public store payment routes (`create-order`,
108
- `capture-order`, `paypal-complete`). Disabled by default (a pass-through) so
109
- existing deployments are unaffected; enable by setting `PAYPAL_RATE_LIMIT_MAX`
110
- (and optionally `PAYPAL_RATE_LIMIT_WINDOW_MS`) once you've confirmed the real
111
- client IP reaches the app (e.g. behind a proxy that forwards `x-forwarded-for`).
112
- - **Encryption-key guardrail.** When `PAYPAL_ENCRYPTION_KEY` is unset, secrets
113
- now emit a loud (production-flagged) warning; set `PAYPAL_ENCRYPTION_STRICT=true`
114
- to fail closed instead of persisting plaintext secrets.
115
- - Additional unit tests: retry/circuit-breaker behavior, the rate limiter, the
116
- atomic metric upsert, and session-persistence retry.
117
-
118
- ### Changed
119
- - **`client_token` is only issued when advanced card fields are enabled** — the
120
- PayPal-buttons flow uses just `client_id`, so an unauthenticated config fetch no
121
- longer hands out a card-fields token it won't use.
122
- - **`postMessage` target origin** for the onboarding return popup is now
123
- configurable (`PAYPAL_ADMIN_ORIGIN`, falling back to the first `ADMIN_CORS`
124
- entry) instead of the `"*"` wildcard.
125
- - **Shared provider base class.** The wallet and advanced-card providers now
126
- extend a common `PayPalProviderBase` for the identical credential/token
127
- handling, order lookups, idempotency keys, amount normalization, and status
128
- mapping — removing a large block of duplicated code. Behavior is unchanged.
129
- - Centralized the PayPal partner-attribution (BN) code into a single constant and
130
- removed the unused `PayPalSellerClient` dead code.
131
-
132
- ## 1.0.2
133
-
134
- ### Fixed
135
- - `toAmountNumber` no longer silently coerces a blank string (`""` / `" "`) or a
136
- blank serialized `BigNumber` value to `0`. `Number("")` is `0`, so a blank amount
137
- could previously turn a partial refund/capture into a full one — it now throws
138
- instead, consistent with the function's existing guards against `NaN`/unparseable
139
- input.
140
-
141
- ### Added
142
- - Unit test suite (vitest) covering the money-critical, IO-free logic:
143
- amount coercion & currency formatting, currency support checks, capture/refund
144
- status mapping, secret encryption/decryption (AES-256-GCM round-trip, tamper &
145
- missing-key handling), webhook signature request composition, the webhook state
146
- machine, retry scheduling, identifier extraction, and provider-id detection.
1
+ # Changelog
2
+
3
+ All notable changes to `@easypayment/medusa-payment-paypal` are documented here.
4
+
5
+ ## 1.2.0 — 2026-09-02
6
+
7
+ ### Fixed
8
+ - **A cart edit can no longer resurrect a stale-amount PayPal order.** The
9
+ create-order `PayPal-Request-Id` now includes the cart total and currency.
10
+ Previously it was derived from the cart id alone, so after the buyer changed
11
+ the cart, the "create a fresh order" path was silently answered from PayPal's
12
+ idempotency cache with the ORIGINAL order at the original total.
13
+ - **`capture-order` no longer reports success for a capture that never
14
+ completed.** The existing-capture short-circuit now requires a demonstrably
15
+ COMPLETED capture; a webhook-patched `capture_id` (which is also written for
16
+ DENIED/PENDING captures) is verified against the live PayPal order before
17
+ being returned as success, so a declined payment surfaces as retryable
18
+ instead of "payment processed but order could not be finalized".
19
+ - **The capture-order idempotency key is now scoped per order.** A client
20
+ reusing one `Idempotency-Key` header across two orders could previously
21
+ receive order A's cached capture for order B. Also fixed the internal
22
+ variable shadowing so the request-correlation UUID appears consistently in
23
+ logs.
24
+ - **Canceling a captured payment no longer records success for a refund that
25
+ failed.** Both providers' `cancelPayment` now reject FAILED/CANCELLED/DENIED
26
+ refund statuses (PayPal returns 2xx for those too), matching the existing
27
+ `refundPayment` gate.
28
+
29
+ ### Added
30
+ - **Webhook-driven cart completion (safety net).** When a
31
+ `PAYMENT.CAPTURE.COMPLETED` webhook arrives for a cart the buyer never
32
+ finalized (closed tab or crash right after paying), the plugin now completes
33
+ the cart server-side so a captured payment always produces an order. Races
34
+ with the storefront's own completion are handled (whoever wins, the other
35
+ treats it as success); genuine failures follow the webhook retry schedule and
36
+ stay visible in the dead-letter queue. Disable with
37
+ `PAYPAL_WEBHOOK_COMPLETE_CART=false`. New metric: `webhook_cart_completed`.
38
+
39
+ ### Hardened
40
+ - **Metric writes are now atomic everywhere.** The module service's
41
+ `recordMetric` uses the same single-statement `INSERT ... ON CONFLICT`
42
+ upsert as the payment providers (shared `utils/metrics.ts`), so concurrent
43
+ routes/webhooks can no longer lose increments to a read-modify-write race.
44
+ - **Settings singleton reads are deterministic.** All readers (module service
45
+ and the providers' raw-SQL credential resolver) now order by `created_at
46
+ ASC`, and a first-boot save race merges into the canonical row instead of
47
+ leaving a divergent duplicate.
48
+ - **Webhook endpoints can be rate-limited** (opt-in, separate from the buyer
49
+ routes): set `PAYPAL_WEBHOOK_RATE_LIMIT_MAX` /
50
+ `PAYPAL_WEBHOOK_RATE_LIMIT_WINDOW_MS`. Deliberately NOT tied to
51
+ `PAYPAL_RATE_LIMIT_MAX` — PayPal delivers from a handful of egress IPs, so a
52
+ buyer-sized cap would throttle legitimate webhook bursts.
53
+ - **Card provider observability parity.** The advanced-card provider now
54
+ records the same audit events and metrics as the wallet provider
55
+ (`capture_success`/`capture_failed`, `refund_*`, `void_success`,
56
+ `cancel_*`, `authorize_failed`), converts capture errors to `MedusaError`
57
+ so real PayPal failure reasons are not masked in production, and preserves
58
+ the `provider_id` passthrough in `updatePayment`.
59
+ - **Startup warning when `PAYPAL_SELLER_NONCE` is auto-generated** — in
60
+ multi-instance deployments a per-process nonce can race the onboarding
61
+ code_verifier; the warning tells operators to pin it.
62
+ - **Single source of truth for the BN code and API base URLs** (removed 4
63
+ duplicated constants and 9 duplicated environment ternaries).
64
+ - `npm run typecheck` (full `tsc --noEmit`, admin UI included) now passes and
65
+ runs in CI; added `@types/react`/`react` dev dependencies.
66
+
67
+ ## 1.1.0
68
+
69
+ ### Fixed (re-audit)
70
+ - **PayPal webhooks and the onboarding return bridge now work without a
71
+ publishable API key.** Medusa applies its store publishable-key middleware to
72
+ every `/store/*` route, and PayPal cannot send `x-publishable-api-key` — so
73
+ webhook deliveries and onboarding return redirects to the old `/store/paypal/*`
74
+ paths were rejected before the handlers ran. Both handlers are now also served
75
+ from `/hooks/paypal/webhook` and `/hooks/paypal/onboard-return` (no key
76
+ guard). New registrations use the `/hooks` URLs, and existing PayPal webhook
77
+ registrations pointing at the legacy path are migrated in place (best-effort,
78
+ on admin status view) via PayPal's webhook PATCH API. The legacy `/store`
79
+ routes remain for deployments that inject the key at their edge.
80
+ - **Unapproved orders can no longer be booked as authorized.** `authorizePayment`
81
+ treated a PayPal order in `CREATED`/`SAVED` status as authorized, letting a
82
+ cart complete (order placed, inventory reserved) with no payment approved at
83
+ PayPal at all. Only buyer-`APPROVED` orders now count.
84
+ - **PENDING / DECLINED captures are no longer booked as captured.** The
85
+ idempotency branches of `authorizePayment` (both providers) keyed off the mere
86
+ presence of a capture/authorization id and returned the settings-derived
87
+ status. They now derive the status from the actual PayPal resource: a
88
+ `COMPLETED` capture → captured; a `PENDING` (eCheck) capture → authorized
89
+ (order placed, funds not claimed until the `CAPTURE.COMPLETED` webhook); a
90
+ `DENIED`/`DECLINED`/`FAILED` capture → error.
91
+ - **Partial captures no longer charge the full amount.** Medusa never passes the
92
+ requested capture amount to a provider — only the Medusa capture-row id (as
93
+ `context.idempotency_key`). The providers now resolve the requested amount
94
+ from that row, so an admin capturing €40 of a €100 authorization no longer
95
+ sends a €100 capture to PayPal.
96
+ - **Stale PayPal orders are invalidated when the cart changes.** `updatePayment`
97
+ now drops the stored `paypal.order_id` when the session amount/currency
98
+ changes (before any capture/authorization), and `create-order` verifies a
99
+ stored order's amount, currency, and state against the cart before reusing it
100
+ — so a buyer who backs out of the PayPal popup and edits their cart is charged
101
+ the new total, not the stale one.
102
+ - **Card provider no longer 422s on approved capture-intent orders.** Its
103
+ `authorizePayment` called `/v2/checkout/orders/{id}/authorize` regardless of
104
+ intent, which PayPal rejects (`UNSUPPORTED_INTENT`) for `CAPTURE`-intent
105
+ orders — permanently stranding a checkout whose storefront capture call had
106
+ failed after 3-D Secure approval. Approved orders now return `authorized` and
107
+ the capture is routed by intent; `/authorize` is only called for
108
+ `AUTHORIZE`-intent orders. The impossible create-order-then-authorize
109
+ fallbacks (both providers) were replaced with clear errors.
110
+ - **Webhook processing no longer corrupts session data.** `PAYMENT.CAPTURE.REFUNDED`
111
+ / `REVERSED` events carry a *refund* resource — its id was being stored as the
112
+ session's `capture_id`. Identifiers are now extracted refund-aware (with the
113
+ capture id taken from `related_ids` or the resource's "up" link), and patches
114
+ no longer overwrite stored fields (e.g. `order_id`) with null when an event
115
+ lacks them.
116
+ - **Partial refunds no longer cancel the payment session.** Refund events now
117
+ compare the cumulative refunded total against the captured amount and keep the
118
+ session status for partial refunds (recording the refund data); only full
119
+ refunds transition the session to canceled.
120
+ - **Rate limiter can no longer be bypassed via `X-Forwarded-For`.** The client
121
+ key now uses the right-most (proxy-appended) XFF entry instead of the
122
+ left-most (client-forgeable) one.
123
+ - **`paypal-complete` no longer 500s on an empty/non-JSON body** (missing body
124
+ guard before destructuring).
125
+ - **Client-supplied idempotency keys are now cart-scoped in `create-order`**, so
126
+ a reused `Idempotency-Key` header across two carts can no longer return cart
127
+ A's cached PayPal order (and amount) for cart B.
128
+ - **Circuit breaker is now keyed per host.** Failures of a non-PayPal host (e.g.
129
+ the onboarding service) can no longer open the breaker and block live
130
+ captures/refunds against a healthy PayPal API.
131
+ - **Raw-knex reads now respect soft deletes** (`paypal_connection` /
132
+ `paypal_settings` lookups in the credential resolver filter `deleted_at`).
133
+ - **Removed the broken `./workflows` package export** (it pointed at a file that
134
+ was never built; importing it always failed).
135
+
136
+ ### Added (re-audit)
137
+ - `GET /store/paypal/config` now emits `disable_buttons` (from
138
+ `paypal_settings.disableButtons` / `disable_buttons`), which the UI package
139
+ has always read to build the SDK's `disable-funding` list.
140
+
141
+
142
+ ### Fixed
143
+ - **Transient capture/refund failures now retry.** The `paypalFetchWithRetry`
144
+ wrapper (jittered backoff on 5xx/429/timeout) was implemented but never wired
145
+ in — capture and refund used the non-retrying path. They now retry safely: the
146
+ `PayPal-Request-Id` idempotency key means a retry re-uses the same
147
+ capture/refund instead of double-charging.
148
+ - **Provider `recordMetric` is now atomic.** The credential-resolver metric path
149
+ used by the payment providers (capture/refund/authorize success & failure
150
+ counters) now uses a single `INSERT … ON CONFLICT … count = count + 1` upsert
151
+ instead of a read-modify-write, so concurrent captures/refunds no longer lose
152
+ increments. (The module-service metric path remains best-effort and unchanged.)
153
+ - **Capture persisted-but-lost gap is now observable.** After a successful PayPal
154
+ capture, session persistence is retried, and if it still fails the route logs
155
+ CRITICAL and records a `capture_order_persist_failed` metric instead of
156
+ silently swallowing the error (the webhook and `paypal-complete`'s live
157
+ re-derivation remain the reconciliation backstop).
158
+ - **Card provider partial-capture idempotency key** now includes the amount, so
159
+ two sequential partial captures of the same order are no longer deduplicated by
160
+ PayPal into one (matching the wallet provider).
161
+
162
+ ### Added
163
+ - **Asynchronous webhook processing.** The webhook route now verifies, persists,
164
+ and acknowledges (200) immediately, then processes off the request path via a
165
+ subscriber — so PayPal's ~15s delivery timeout is never tripped by slow
166
+ downstream work. The retry cron additionally recovers events left in
167
+ `processing` past a staleness threshold, so an event is never lost even if the
168
+ event bus drops a message.
169
+ - **Opt-in rate limiting** on the public store payment routes (`create-order`,
170
+ `capture-order`, `paypal-complete`). Disabled by default (a pass-through) so
171
+ existing deployments are unaffected; enable by setting `PAYPAL_RATE_LIMIT_MAX`
172
+ (and optionally `PAYPAL_RATE_LIMIT_WINDOW_MS`) once you've confirmed the real
173
+ client IP reaches the app (e.g. behind a proxy that forwards `x-forwarded-for`).
174
+ - **Encryption-key guardrail.** When `PAYPAL_ENCRYPTION_KEY` is unset, secrets
175
+ now emit a loud (production-flagged) warning; set `PAYPAL_ENCRYPTION_STRICT=true`
176
+ to fail closed instead of persisting plaintext secrets.
177
+ - Additional unit tests: retry/circuit-breaker behavior, the rate limiter, the
178
+ atomic metric upsert, and session-persistence retry.
179
+
180
+ ### Changed
181
+ - **`client_token` is only issued when advanced card fields are enabled** — the
182
+ PayPal-buttons flow uses just `client_id`, so an unauthenticated config fetch no
183
+ longer hands out a card-fields token it won't use.
184
+ - **`postMessage` target origin** for the onboarding return popup is now
185
+ configurable (`PAYPAL_ADMIN_ORIGIN`, falling back to the first `ADMIN_CORS`
186
+ entry) instead of the `"*"` wildcard.
187
+ - **Shared provider base class.** The wallet and advanced-card providers now
188
+ extend a common `PayPalProviderBase` for the identical credential/token
189
+ handling, order lookups, idempotency keys, amount normalization, and status
190
+ mapping — removing a large block of duplicated code. Behavior is unchanged.
191
+ - Centralized the PayPal partner-attribution (BN) code into a single constant and
192
+ removed the unused `PayPalSellerClient` dead code.
193
+
194
+ ## 1.0.2
195
+
196
+ ### Fixed
197
+ - `toAmountNumber` no longer silently coerces a blank string (`""` / `" "`) or a
198
+ blank serialized `BigNumber` value to `0`. `Number("")` is `0`, so a blank amount
199
+ could previously turn a partial refund/capture into a full one — it now throws
200
+ instead, consistent with the function's existing guards against `NaN`/unparseable
201
+ input.
202
+
203
+ ### Added
204
+ - Unit test suite (vitest) covering the money-critical, IO-free logic:
205
+ amount coercion & currency formatting, currency support checks, capture/refund
206
+ status mapping, secret encryption/decryption (AES-256-GCM round-trip, tamper &
207
+ missing-key handling), webhook signature request composition, the webhook state
208
+ machine, retry scheduling, identifier extraction, and provider-id detection.