Repository navigation
feat(apm)(POCP-1406): handle manual finalization states and add authorize/capture - #296
Draft
lukasz-k-bieszczad-cko wants to merge 1 commit into
Conversation
lukasz-k-bieszczad-cko
force-pushed
the
feat/apm-sessions-manual-finalization
branch
from
December 1, 2026 15:09
74e60a0 to
a0bb568
Compare
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Critical cross-instance context leaks can make stale instances finalize another invoice and assign a late response’s actions to the wrong invoice.
2 open issues
What changed in this PR
Adds manual finalization support to the APM widget so merchants can handle authorization and capture separately.
Changes:
- Handles additional pending and completed states and expands success payloads.
- Adds headless authorize/capture methods, documentation, and example controls.
- Corrects success-dismiss timing to seconds and extends unit coverage.
| File | Description |
|---|---|
| test/support/loadNamespace.ts | Loads page logic for tests. |
| test/apm/api.test.ts | Tests states, finalization, payloads, and timing. |
| src/apm/views/Success.ts | Expands payloads and fixes timer units. |
| src/apm/Page.ts | Renders new pending and completed states. |
| src/apm/index.ts | Exposes authorize and capture methods. |
| src/apm/events/APMEventListener.ts | Adds success-event fields. |
| src/apm/API.ts | Implements finalization requests and state handling. |
| examples/apm/index.html | Adds manual finalization controls. |
| docs/migration-guide-native-apm-to-apm.md | Documents finalization usage and limitations. |
Review effort: Balanced
lukasz-k-bieszczad-cko
force-pushed
the
feat/apm-sessions-manual-finalization
branch
from
December 3, 2026 14:51
a0bb568 to
acd9ec4
Compare
`client.apm.authorization` now handles every state the api returns on
APM sessions for manual finalization:
- `AUTHORIZATION_PENDING` behaves like `PENDING`: polling, timeout and
the Pending view, rendered once on entering a pending state.
- `AUTHORIZED` and `CUSTOMER_ACTIONS_COMPLETED` end the widget like
`SUCCESS`: polling stops and the Success view renders. Before, these
states stopped polling and rendered nothing.
- The `success` event now carries `paymentState` and, when the merchant
still has to finalize, `availableActions`.
Add headless `apm.authorize(onDone, onError)` and
`apm.capture(onDone, onError)`. They call `/invoices/{id}/authorize` or
`/capture`, return the session result from the `apm` payload, and
refuse without a request outside the authorization flow, while a call
is in flight, or when the action is not in the invoice's last known
`available_actions`.
Both methods are bound to the widget instance that made them: once a newer
`APM` instance replaced the global context, they refuse with
`processout-js.apm.stale-instance` and send nothing. `available_actions`
from an answer is kept only while the request's own context is still
current, so a late answer cannot gate another invoice.
Building a new `APM` widget now cancels the previous widget's polling and
retry timers and clears its finalize state, so a leftover timer cannot
post to the new invoice through the shared context.
Fix the Success view auto-dismiss: `autoDismissDuration` and
`manualDismissDuration` are seconds but were used as milliseconds, so
`success` fired after 3 ms instead of 3 s.
Docs and the example page cover the new states and methods.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lukasz-k-bieszczad-cko
force-pushed
the
feat/apm-sessions-manual-finalization
branch
from
December 3, 2026 14:56
acd9ec4 to
b394293
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.

Description
On APM sessions with manual finalization (auth and capture split, or sale), the api answers with states
src/apmdidn't know:AUTHORIZATION_PENDING,CUSTOMER_ACTIONS_COMPLETEDandAUTHORIZED.API.tskept polling only onPENDING, andPage.tsrendered onlyREDIRECT,NEXT_STEP_REQUIRED,SUCCESSandPENDING. So these states stopped polling and rendered nothing: after the redirect, the widget stayed on the redirect view, and no event reached the merchant.Stacked on the buy and tokenize PR.
Solution
AUTHORIZATION_PENDINGbehaves exactly likePENDING: polling, theconfirmation.timeoutcheck,requiresAction, and the Pending view, rendered once when the state enters pending.AUTHORIZEDandCUSTOMER_ACTIONS_COMPLETEDare final for the widget, likeSUCCESS: polling stops, the Success view renders with the same text, and withsuccess.enabled: falsethesuccessevent fires immediately.successevent is now{ trigger, paymentState, availableActions?, customerTokenId? }.paymentStateisSUCCESS,AUTHORIZEDorCUSTOMER_ACTIONS_COMPLETED.availableActions(AUTHORIZE/CAPTURE) is set only when the merchant still has to finalize.apm.authorize(onDone, onError)andapm.capture(onDone, onError):POST /invoices/{id}/authorizeor/capture, with the client secret when set.onDone({ paymentState, availableActions?, customerTokenId? })from the response'sapmpayload, oronError({ code, message }).cleanUp().successstays the single final event.processout-js.apm.finalization-not-supportedoutside the authorization flow;processout-js.apm.finalization-in-progresswhile a call is running;processout-js.apm.action-not-availablewhen the action isn't in the invoice's last knownavailable_actions. That list is bound to its invoice, reset oninitialise, and protected against late answers.autoDismissDurationandmanualDismissDuration(seconds, as documented) as milliseconds, so the autosuccessfired after 3 ms instead of 3 s. It now waits seconds.examples/apmshows Authorize and Capture buttons whensuccesscarriesavailableActions.Demo
Not tested end to end yet. The new states are covered by unit tests: polling, timeout, the rendered view, the event payloads, and the finalize requests, results, errors and checks.
Checklist
yarn bump-versionNotes
successalways carriespaymentState.successafter 3 s, not 3 ms.authorizeandcapturework only for payments on APM sessions. On the oldermodule/apmpath,/captureexpects agway_req_source, so the call fails and the api error goes toonError. The api also rejects/capturefrom the browser when the invoice hasrequire_backend_capture.PENDINGorAUTHORIZATION_PENDING, that state goes toonDoneand the SDK doesn't poll. The merchant follows up with webhooks.Page.onFailuredoesn't updatePage.state.ActionHandler'sonabortretry calls a missingthis.request(src/processout/processout.ts:423).Jira Issue
POCP-1406
🤖 Generated with Claude Code