fix: stop caching the QPay access token past its real expiry - #99
Merged
Conversation
QPay returns `expires_in` on /v2/auth/token as an ABSOLUTE UNIX TIMESTAMP, not a lifetime in seconds — a live response carries 1788603222, which is 2026-09-05T10:13:42Z, roughly a day out. setAuthToken() read it as a duration and cached the token for `expires_in - 60` SECONDS: 1,788,603,162s, about 56.7 years. The first token minted after deploy was therefore pinned in the cache permanently, and once QPay expired it server-side (~24h later) every storefront call kept sending that dead bearer token. QPay answered NO_CREDENTIALS / "Хандах эрхгүй байна. Нэвтрэнэ үү." for correct credentials, which broke checkout invoice creation, the capture-qpay callback, checkout status polling, and the ebarimt receipt path. Mint a fresh token per authentication, as before the caching was added. That costs one round trip per QPay call, which is what the caching was saving — not a trade worth making on a payment path. Reintroducing reuse requires deriving the TTL from the epoch and keying on the credentials so rotation invalidates it. The test added with the caching mocked expires_in as 3600 — a duration, the same wrong assumption as the code — so it agreed with the bug and passed. It is replaced with regression coverage that uses a real epoch-shaped response and asserts each authentication re-mints.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #99 +/- ##
===========================================
Coverage 100.00% 100.00%
+ Complexity 1775 1772 -3
===========================================
Files 135 135
Lines 7785 7778 -7
===========================================
- Hits 7785 7778 -7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merged
roncodes
added a commit
that referenced
this pull request
Sep 4, 2026
Cuts the patch release carrying the QPay authentication fix (#99) and the network model repairs and testing seeder split (#98). The release/v* workflow gate is deliberately NOT changed here — #97 owns that, with fleetbase/fleetbase#641 behind it. Both must land before this branch is merged, or the tag job either skips (old gate) or is refused by the reusable workflow (gate updated, reusable workflow not yet).
roncodes
force-pushed
the
release/v0.4.21
branch
from
September 4, 2026 10:44
54965e0 to
9bbc7b5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Impact
QPay checkout is broken in production.
/storefront/v1/checkouts/before?...&gateway=qpayreturns an invoice payload of:{"invoice":{"error":"NO_CREDENTIALS","message":"Хандах эрхгүй байна. Нэвтрэнэ үү."}, ...}The credentials are valid — authenticating directly against
POST https://merchant.qpay.mn/v2/auth/tokenwith the same username/password succeeds and returns a token.Cause
Introduced by me in #90 (
ee672a4), as part of an efficiency cleanup that added access-token caching toQPay::setAuthToken().QPay returns
expires_inas an absolute UNIX timestamp, not a lifetime in seconds. A live response carries1788603222=2026-09-05T10:13:42Z, roughly 24 hours out. The shipped code read it as a duration:So the first token minted after deploy was cached effectively forever. About a day later QPay expired it server-side, and from then on every storefront request sent
Authorization: Bearer <dead token>and gotNO_CREDENTIALSback. Nothing re-mints, so it stays broken.Everything that authenticates to QPay is affected: checkout invoice creation, the
capture-qpaycallback, checkout status polling, and the eBarimt receipt path inOrderController. Stripe and cash checkout are unaffected.Why CI did not catch it
The test that shipped alongside the caching mocked
{"access_token":"cached-token","expires_in":3600}— a duration, i.e. the same wrong assumption the code made. The mock agreed with the bug, so it passed with full coverage.Fix
Mint a fresh token on every authentication, as it worked before the caching was added. That is one extra round trip per QPay call — exactly what the caching saved — which is not a trade worth making on a payment path.
Reintroducing reuse later requires deriving the TTL from the epoch (
expires_in - time() - 60), clamping it, and keying on the credentials so a password rotation invalidates the entry.The misleading test is replaced with regression coverage that uses a real epoch-shaped response and asserts each authentication re-mints a token, plus a case for a token response that carries no
access_token.Immediate mitigation (no deploy needed)
Dropping the cached entry makes the next request re-authenticate and restores checkout right away — but only for ~24h, until the token expires again. This PR is the durable fix.
(That key is
md5('https://merchant.qpay.mn/v2/|OLIMAX_APP'); a different merchant username hashes differently.)Validation
server/tests/Unit/Support/QPayTest.php— 16 tests, 115 assertions, 0 failuresphp-cs-fixer --dry-runclean on both changed files