Skip to content

feat(apm)(POCP-1406): handle manual finalization states and add authorize/capture - #296

Draft
lukasz-k-bieszczad-cko wants to merge 1 commit into
feat/apm-sessions-buy-and-tokenizefrom
feat/apm-sessions-manual-finalization
Draft

lukasz-k-bieszczad-cko wants to merge 1 commit into
feat/apm-sessions-buy-and-tokenizefrom
feat/apm-sessions-manual-finalization

Conversation

@lukasz-k-bieszczad-cko

@lukasz-k-bieszczad-cko lukasz-k-bieszczad-cko commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

On APM sessions with manual finalization (auth and capture split, or sale), the api answers with states src/apm didn't know: AUTHORIZATION_PENDING, CUSTOMER_ACTIONS_COMPLETED and AUTHORIZED. API.ts kept polling only on PENDING, and Page.ts rendered only REDIRECT, NEXT_STEP_REQUIRED, SUCCESS and PENDING. 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_PENDING behaves exactly like PENDING: polling, the confirmation.timeout check, requiresAction, and the Pending view, rendered once when the state enters pending.
  • AUTHORIZED and CUSTOMER_ACTIONS_COMPLETED are final for the widget, like SUCCESS: polling stops, the Success view renders with the same text, and with success.enabled: false the success event fires immediately.
  • The success event is now { trigger, paymentState, availableActions?, customerTokenId? }. paymentState is SUCCESS, AUTHORIZED or CUSTOMER_ACTIONS_COMPLETED. availableActions (AUTHORIZE / CAPTURE) is set only when the merchant still has to finalize.
  • New headless methods apm.authorize(onDone, onError) and apm.capture(onDone, onError):
    • Request: POST /invoices/{id}/authorize or /capture, with the client secret when set.
    • Result: onDone({ paymentState, availableActions?, customerTokenId? }) from the response's apm payload, or onError({ code, message }).
    • Headless: they don't render, emit events or poll, and they work after cleanUp(). success stays the single final event.
    • Checks, which send no request:
      • processout-js.apm.finalization-not-supported outside the authorization flow;
      • processout-js.apm.finalization-in-progress while a call is running;
      • processout-js.apm.action-not-available when the action isn't in the invoice's last known available_actions. That list is bound to its invoice, reset on initialise, and protected against late answers.
  • Bug fix: the Success view used autoDismissDuration and manualDismissDuration (seconds, as documented) as milliseconds, so the auto success fired after 3 ms instead of 3 s. It now waits seconds.
  • Docs: a "Manual Finalization" section in the migration guide.
  • Example: examples/apm shows Authorize and Capture buttons when success carries availableActions.

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

  • I bumped the version of the project using yarn bump-version
  • I have checked the code for any potential issues
  • I tested my changes in the browser

Notes

  • Behaviour changes merchants may notice:
    • success always carries paymentState.
    • The default auto-dismiss now fires success after 3 s, not 3 ms.
  • authorize and capture work only for payments on APM sessions. On the older module/apm path, /capture expects a gway_req_ source, so the call fails and the api error goes to onError. The api also rejects /capture from the browser when the invoice has require_backend_capture.
  • If authorize or capture answers PENDING or AUTHORIZATION_PENDING, that state goes to onDone and the SDK doesn't poll. The merchant follows up with webhooks.
  • Until the api reports manual finalization support for these processout.js versions, this can't be tested end to end against the api.
  • Pre-existing, not changed here:
    • Page.onFailure doesn't update Page.state.
    • ActionHandler's onabort retry calls a missing this.request (src/processout/processout.ts:423).

Jira Issue

POCP-1406

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/apm/API.ts Resolved
Comment thread src/apm/API.ts Resolved
@lukasz-k-bieszczad-cko lukasz-k-bieszczad-cko changed the title feat(apm): handle manual finalization states and add authorize/capture feat(apm)(POCP-1406): handle manual finalization states and add authorize/capture Dec 3, 2026
@lukasz-k-bieszczad-cko
lukasz-k-bieszczad-cko force-pushed the feat/apm-sessions-manual-finalization branch from a0bb568 to acd9ec4 Compare December 3, 2026 14:51
`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
lukasz-k-bieszczad-cko force-pushed the feat/apm-sessions-manual-finalization branch from acd9ec4 to b394293 Compare December 3, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants