Repository navigation
fix(ios): present the Apple Pay sheet in its own window - #54
Conversation
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
|
No actionable comments were generated in the recent review.
|
| 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:
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
Possibly related PRs
- gmi-software/react-native-pay#45: Changes payment state and settlement handling in
HybridPaymentHandler, which relates to clearing state after presentation failure. - gmi-software/react-native-pay#52: Defines typed payment outcomes and uses
NOT_AVAILABLEfor iOS presentation failures.
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: 
5 · 
1
-
Failed checks: 1 warning Passed checks: 5 passed-
Check name Status Explanation Title check 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 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 Issue #20is the only active direct target. Its coding requirements are met at head af73ba7.
1. Suggested fix 1, switch to PKPaymentAuthorizationController: met.PaymentSheetPresenter.presentcalls…Out of Scope Changes check Every changed file traces to #20or supports its implementation:
-package/ios/HybridPaymentHandler.swift: the controller migration,PaymentSheetandPaymentSheetPresenter(testability and reten…Security Check 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
#20is the only active direct target. Its coding requirements are met at head af73ba7.- Suggested fix 1, switch to PKPaymentAuthorizationController: met.
PaymentSheetPresenter.presentcallsPKPaymentAuthorizationControllerthrough thePaymentSheetprotocol.PaymentDelegateimplements the controller delegate methods.performPaymentno longer usesgetRootViewControllerortopViewController. - Settle the Promise when PassKit reports
false: met.sheet.present { presented in ... }callsonFailureon the main queue withnotAvailableand "Unable to present payment authorization".onFailurecallshandlePaymentResult, which releases the sheet and settlespendingPayment.XCTesttestSettlesThePaymentAndLetsTheNextOneStartdrives twostartPaymentcalls with a refused sheet. Each settles asfailed,isPaymentInProgress()isfalse, and the second call reaches PassKit. - Presenter failure injected through a protocol in XCTest: met.
PaymentSheetandFakeSheetexist.PaymentSheetPresenterTestscovers refusal, creation failure, presented sheet, off-main refusal, and sheet retention. - Criterion: no path leaves
paymentCompletionset without a pending sheet. Met by code paths. A nil sheet callsonFailuresynchronously. A refused sheet callsonFailureafter PassKit reports. A presented sheet is held untilhandlePaymentResult, which releases it. - Related
.foregroundInactiveconcern: code accepts.foregroundInactiveinpresentationWindow(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
presentcompletion. 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 stiming is not measured in tests. - Device behavior is not verified. The PR states this.
- Suggested fix 1, switch to PKPaymentAuthorizationController: met.
Full details: Out of Scope Changes check
-
Explanation
Every changed file traces to
#20or supports its implementation:package/ios/HybridPaymentHandler.swift: the controller migration,PaymentSheetandPaymentSheetPresenter(testability and retention),presentationWindow(for:)(visionOS and.foregroundInactivefrom#20), and theinit(sheetPresenter:)injection point. The removed root view controller lookup is the code#20asks to replace.package/ios/Tests/PaymentSheetPresenterTests.swift: tests for the refused-sheet requirement. Required by#20testing section.CHANGELOG.md,docs/docs/troubleshooting.md,docs/docs/payment-flow.md: document the newNOT_AVAILABLEiOS path and the error text. Supporting documentation for the behavior change.
The
PKPaymentAuthorizationViewController(paymentRequest:) != nilpre-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
#52and#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#52and#53are 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.
Summary
PKPaymentAuthorizationController, which shows the sheet in its own window, instead of presentingPKPaymentAuthorizationViewControllerfrom the app's top view controller. A view controller that is presenting or dismissing can no longer refuse the sheet or close it.presentcompletes withfalse),startPaymentresolves withstatus: 'failed',code: 'NOT_AVAILABLE'anderror: 'Unable to present payment authorization', and the next call opens a new sheet. Before, the promise never settled and every later call returnedIN_PROGRESSuntil the app restarted.presentationWindow(for:)picks a window from a foreground scene. It is required on visionOS, which the podspec targets, and it accepts.foregroundInactivescenes, which the old lookup skipped.PaymentSheetprotocol, so XCTest can inject a sheet that PassKit refuses.NOT_AVAILABLEcause inpayment-flow.md#error-codes, and CHANGELOG.Test plan
cd package && bun run typecheckcd package && bun run lintcd package && bun run test:ci(135 tests)cd package && bun run check:docs(no snippet changes)cd package && bun run check:packxcodebuild test ... -scheme NitroPay-Unit-Testson 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, andstartPaymentend to end with a refused sheet followed by a second payment)bun run docs:buildswiftc -typecheckagainst thexrsimulatorSDK of the delegate andPaymentSheetsignatures: fails withoutpresentationWindow(for:), passes with it./gradlew :gmisoftware_react-native-pay:testDebugUnitTest(not run; no Kotlin changes)cd example && bunx expo run:android(not run; iOS only)Risk
onAuthorizedecline were checked on the simulator, but not on a device.present(completion:)anddismiss(completion:)don't document their thread, so both hop to the main queue. A sheet that is cancelled at once still reportsCANCELLED.PKPaymentAuthorizationViewController(paymentRequest:) != nilis 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
Alertbutton does not reproduce it, because the alert's handler runs after the alert has closed. CallingstartPayment()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()stayedtrue, the nextstartPayment()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.present→falsecan't be triggered on the simulator, so that path is covered only by XCTest..foregroundInactivecase from the issue (the code accepts such scenes, but nothing exercised it).present() async -> Boolwrapper 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
Need help on this PR? Tag
@codesmith-botwith what you need.