Skip to content

fix(ios): present the Apple Pay sheet in its own window - #54

Merged
jkasprzyk17 merged 1 commit into
mainfrom
fix/ios-apple-pay-sheet-window
Oct 27, 2026
Merged

jkasprzyk17 merged 1 commit into
mainfrom
fix/ios-apple-pay-sheet-window

Conversation

@jkasprzyk17

@jkasprzyk17 jkasprzyk17 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • iOS presents Apple Pay with PKPaymentAuthorizationController, which shows the sheet in its own window, instead of presenting PKPaymentAuthorizationViewController from the app's top view controller. A view controller that is presenting or dismissing can no longer refuse the sheet or close it.
  • When PassKit reports that it could not show the sheet (present completes with false), startPayment resolves with status: 'failed', code: 'NOT_AVAILABLE' and error: 'Unable to present payment authorization', and the next call opens a new sheet. Before, the promise never settled and every later call returned IN_PROGRESS until the app restarted.
  • presentationWindow(for:) picks a window from a foreground scene. It is required on visionOS, which the podspec targets, and it accepts .foregroundInactive scenes, which the old lookup skipped.
  • The presentation sits behind a small PaymentSheet protocol, so XCTest can inject a sheet that PassKit refuses.
  • Docs: a troubleshooting entry for "Unable to present payment authorization", the iOS NOT_AVAILABLE cause in payment-flow.md#error-codes, and CHANGELOG.

Test plan

  • cd package && bun run typecheck
  • cd package && bun run lint
  • cd package && bun run test:ci (135 tests)
  • cd package && bun run check:docs (no snippet changes)
  • cd package && bun run check:pack
  • xcodebuild test ... -scheme NitroPay-Unit-Tests on this commit (60 tests, 6 new: refused sheet, presented sheet, request PassKit can't show, refusal answered off the main thread, the sheet is held until release, and startPayment end to end with a refused sheet followed by a second payment)
  • Mutation check, before the rebase: dropping the settle on a refused sheet fails 3 of the new tests
  • Example app on the iOS 26.5 simulator, before and after the fix (see Notes). Run before rebasing onto feat: report a typed status and error code on every payment result #52 and feat!: add JS wrappers for the pay buttons #53, which only changed how failures are reported.
  • bun run docs:build
  • visionOS: swiftc -typecheck against the xrsimulator SDK of the delegate and PaymentSheet signatures: fails without presentationWindow(for:), passes with it
  • Full visionOS pod build (not run)
  • ./gradlew :gmisoftware_react-native-pay:testDebugUnitTest (not run; no Kotlin changes)
  • cd example && bunx expo run:android (not run; iOS only)

Risk

  • iOS presents the sheet through a different PassKit API. The delegate methods map one to one, and success, cancel and onAuthorize decline were checked on the simulator, but not on a device.
  • present(completion:) and dismiss(completion:) don't document their thread, so both hop to the main queue. A sheet that is cancelled at once still reports CANCELLED.
  • PKPaymentAuthorizationViewController(paymentRequest:) != nil is kept as a pre-flight check, because the controller's initializer never fails. A request PassKit can't handle still fails with "Unable to create payment authorization".

Notes

  • Reproduction. An Alert button does not reproduce it, because the alert's handler runs after the alert has closed. Calling startPayment() in the same tick as closing a React Native <Modal> does. On the old code, UIKit presented the payment view controller on the dismissing modal and closed it with the modal 0.5 s later (PKPaymentAuthorizationCoordinator - dismissWithCompletion), with no delegate callback. The promise was still pending after a minute, isPaymentInProgress() stayed true, the next startPayment() returned "A payment is already in progress", and a blank sheet blocked the screen. With this change the Apple Pay sheet stays up in its own window and the payment settles normally. While a modal opens, both now appear; before, UIKit refused the app's modal.
  • PassKit's present → false can't be triggered on the simulator, so that path is covered only by XCTest.
  • Not verified: the .foregroundInactive case from the issue (the code accepts such scenes, but nothing exercised it).
  • No presentation watchdog. The Swift present() async -> Bool wrapper implies the completion always fires. A watchdog that settled the payment without also dismissing the controller would leave a sheet that appears late without a payment behind it.

Closes #20


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

startPayment presented PKPaymentAuthorizationViewController from the
app's top view controller. When that view controller was being
dismissed, for example while a modal closed, UIKit closed the sheet
with it or refused to show it, and no delegate method ran. The promise
never settled, and every later call returned "A payment is already in
progress" until the app restarted.

Present with PKPaymentAuthorizationController instead, which has its
own window, and settle the payment with "Unable to present payment
authorization" when PassKit reports that it could not show the sheet.
Implement presentationWindow(for:), which visionOS requires.

Fixes #20
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 9d16e4fc-178a-425e-b156-83e062419b73
Commits

Reviewing files that changed from the base of the PR and between 7d336b6 and af73ba7.

Files selected for processing: 5
  • CHANGELOG.md
  • docs/docs/payment-flow.md
  • docs/docs/troubleshooting.md
  • package/ios/HybridPaymentHandler.swift
  • package/ios/Tests/PaymentSheetPresenterTests.swift

Included review availability: This review used your included allowance. 1 included review remains after this review. Your included PR review attempts over the past 7 days set your current allowance at 3 reviews per hour.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Apple Pay now presents its payment sheet in a separate window. If the sheet cannot be shown, the payment returns a NOT_AVAILABLE failure, and a later attempt can open a new sheet.
  • Documentation
    • Clarified when this failure may occur and added troubleshooting guidance. The guidance explains that no sheet appeared and no payment was charged, and suggests what details to include when reporting an issue.

The iOS payment handler now presents Apple Pay through PKPaymentAuthorizationController. Failed sheet presentation settles with a NOT_AVAILABLE result, and the handler releases the retained sheet. Documentation, changelog entries, and tests describe and cover this behavior.

Changes: iOS payment presentation

Layer / File(s) Summary
Payment sheet presenter and PassKit callbacks
package/ios/HybridPaymentHandler.swift
A PaymentSheetPresenter creates and retains the sheet, reports creation or presentation failures, and releases the sheet on request. PaymentDelegate now uses controller callbacks and selects a presentation window.
Handler integration and failure recovery
package/ios/HybridPaymentHandler.swift, CHANGELOG.md, docs/docs/payment-flow.md, docs/docs/troubleshooting.md
HybridPaymentHandler uses the presenter and releases the retained sheet when handling a result. The changelog and docs describe the NOT_AVAILABLE failure and subsequent call behavior.
Presenter and retry tests
package/ios/Tests/PaymentSheetPresenterTests.swift
Tests cover sheet creation and presentation outcomes, main-thread delivery, retention and release, and repeated failed payment attempts.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant HybridPaymentHandler
  participant PaymentSheetPresenter
  participant PKPaymentAuthorizationController
  HybridPaymentHandler->>PaymentSheetPresenter: present payment sheet
  PaymentSheetPresenter->>PKPaymentAuthorizationController: create and present sheet
  PKPaymentAuthorizationController-->>PaymentSheetPresenter: presentation result
  PaymentSheetPresenter-->>HybridPaymentHandler: report presentation failure
Loading

Possibly related PRs

Suggested reviewers: piotr-graczyk-dev

Merge Risk: Minimal · up to af73b

Failed sheet presentation settles the payment and allows another attempt. No actionable merge blocker is established; device behavior remains unverified.

Pre-merge checks: Passed 5 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 15.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surv… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 5 passed
Check name Status Explanation
Title check Passed The title "fix(ios): present the Apple Pay sheet in its own window" uses a conventional type prefix and states the main change. The PR switches iOS Apple Pay presentation to PKPaymentAuthorizationCont…
Description check Passed The description is detailed and on topic. It explains the switch to PKPaymentAuthorizationController, the NOT_AVAILABLE failure path, presentationWindow(for:) changes, the PaymentSheet protocol for XC…
Linked Issues check Passed Issue #20 is the only active direct target. Its coding requirements are met at head af73ba7.

1. Suggested fix 1, switch to PKPaymentAuthorizationController: met. PaymentSheetPresenter.present calls…
Out of Scope Changes check Passed Every changed file traces to #20 or supports its implementation:
- package/ios/HybridPaymentHandler.swift: the controller migration, PaymentSheet and PaymentSheetPresenter (testability and reten…
Security Check Passed No medium, high, or critical vulnerability was introduced. The PR changes Apple Pay sheet presentation and failure handling only. Payment requests still pass through the existing merchant, amount, net…
Full details: Title check

Explanation

The title "fix(ios): present the Apple Pay sheet in its own window" uses a conventional type prefix and states the main change. The PR switches iOS Apple Pay presentation to PKPaymentAuthorizationController, which shows the sheet in its own window. The title is 55 characters, slightly over the requested 50-character target, but it is concise and descriptive.

Full details: Description check

Explanation

The description is detailed and on topic. It explains the switch to PKPaymentAuthorizationController, the NOT_AVAILABLE failure path, presentationWindow(for:) changes, the PaymentSheet protocol for XCTest, docs and CHANGELOG updates, the test plan, risks, and reproduction notes. It closes issue #20.

Full details: Linked Issues check

Explanation

Issue #20 is the only active direct target. Its coding requirements are met at head af73ba7.

  1. Suggested fix 1, switch to PKPaymentAuthorizationController: met. PaymentSheetPresenter.present calls PKPaymentAuthorizationController through the PaymentSheet protocol. PaymentDelegate implements the controller delegate methods. performPayment no longer uses getRootViewController or topViewController.
  2. Settle the Promise when PassKit reports false: met. sheet.present { presented in ... } calls onFailure on the main queue with notAvailable and "Unable to present payment authorization". onFailure calls handlePaymentResult, which releases the sheet and settles pendingPayment. XCTest testSettlesThePaymentAndLetsTheNextOneStart drives two startPayment calls with a refused sheet. Each settles as failed, isPaymentInProgress() is false, and the second call reaches PassKit.
  3. Presenter failure injected through a protocol in XCTest: met. PaymentSheet and FakeSheet exist. PaymentSheetPresenterTests covers refusal, creation failure, presented sheet, off-main refusal, and sheet retention.
  4. Criterion: no path leaves paymentCompletion set without a pending sheet. Met by code paths. A nil sheet calls onFailure synchronously. A refused sheet calls onFailure after PassKit reports. A presented sheet is held until handlePaymentResult, which releases it.
  5. Related .foregroundInactive concern: code accepts .foregroundInactive in presentationWindow(for:). No test exercises this case. The PR states this. The code evidence supports the requirement; the runtime behavior is unverified.

Residual limits, not failures:

  • Settling depends on PassKit calling the present completion. No watchdog exists. Suggested fix 1 does not require one.
  • The reproduction in the issue no longer triggers a refusal. The sheet now appears. This is a manual check, and manual checks are not coding requirements.
  • The about 1 s timing is not measured in tests.
  • Device behavior is not verified. The PR states this.
Full details: Out of Scope Changes check

Explanation

Every changed file traces to #20 or supports its implementation:

  • package/ios/HybridPaymentHandler.swift: the controller migration, PaymentSheet and PaymentSheetPresenter (testability and retention), presentationWindow(for:) (visionOS and .foregroundInactive from #20), and the init(sheetPresenter:) injection point. The removed root view controller lookup is the code #20 asks to replace.
  • package/ios/Tests/PaymentSheetPresenterTests.swift: tests for the refused-sheet requirement. Required by #20 testing section.
  • CHANGELOG.md, docs/docs/troubleshooting.md, docs/docs/payment-flow.md: document the new NOT_AVAILABLE iOS path and the error text. Supporting documentation for the behavior change.

The PKPaymentAuthorizationViewController(paymentRequest:) != nil pre-flight check keeps the existing creation-failure behavior. It supports the requirement and does not add scope.

The PR description says it was rebased onto #52 and #53. The changed-file list in the raw summary does not include unrelated files. The full file list was truncated at 16000 bytes. Changes from #52 and #53 are not in this review set, so this verdict covers only the listed files.

Full details: Security Check

Explanation

No medium, high, or critical vulnerability was introduced. The PR changes Apple Pay sheet presentation and failure handling only. Payment requests still pass through the existing merchant, amount, network, and capability validation. The new window lookup stays within the app's own foreground scenes, and the code adds no network sink, executable deserialization, secret logging, or permission bypass. No prior security findings were supplied.

Full details: Docstring Coverage

Explanation

Docstring coverage is 15.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 20 functions across 2 files. (3 skipped: 3 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Billing warning: we have not been able to collect payment for this subscription for more than 72 hours. Please update the payment method or pay any pending invoices in Billing to avoid service interruption.

Comment @coderabbitai help to get the list of available commands.

@jkasprzyk17
jkasprzyk17 merged commit 05b782c into main Oct 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iOS: a refused presentation may leave the handler stuck in "payment in progress"

1 participant