From 40a460bf23d950e993dc869377bcbaf6ceb1d761 Mon Sep 17 00:00:00 2001 From: Jakub Kasprzyk Date: Tue, 27 Oct 2026 15:03:06 +0100 Subject: [PATCH] fix(ios): let the payer retry after onAuthorize reports a failure PassKit treats a failure that carries no PassKit error as final: the sheet showed "Payment Failed" and closed, although the docs promise a retry from the same sheet. Complete a failure, a rejected promise and the 25-second timeout with PKPaymentError.unknownError and the error text, which PassKit lets the payer retry. Each retry calls onAuthorize with a new token. On the iOS 26.5 simulator, nil, an empty array and an NSError from another domain all end in PKPaymentAuthorizationStateFatalError, while unknownError ends in the non-fatal PKPaymentAuthorizationStateError. --- CHANGELOG.md | 7 ++++ docs/docs/guides/testing.md | 2 +- package/ios/HybridPaymentHandler.swift | 25 ++++++++++++--- .../AuthorizationResultBuilderTests.swift | 32 +++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 package/ios/Tests/AuthorizationResultBuilderTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 99627e3..c98985d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -160,6 +160,13 @@ All notable changes to this project are documented in this file. cannot refuse or close it. If PassKit still cannot show it, `startPayment` resolves with `status: 'failed'`, `code: 'NOT_AVAILABLE'` and `error: 'Unable to present payment authorization'`, and the next call opens a new sheet. +- **iOS: after `onAuthorize` reports a failure, the payer can retry from the Apple Pay sheet,** as + the docs said. PassKit treats a failure that carries no PassKit error as final, so the sheet + showed "Payment Failed" and closed. A `'failure'`, a rejected promise and the 25-second timeout + now complete the sheet with `PKPaymentError.unknownError` and your `error` text. The payer can + try again, and each retry calls `onAuthorize` with a new token. When the sheet closes, + `startPayment` resolves with the last attempt's result, which for a failure still has + `code: 'PROVIDER_ERROR'`. - **Android: `supportedNetworks` limits the cards the payer can choose.** Android ignored it and always offered Visa, Mastercard, Amex and Discover, so a merchant whose gateway refuses Amex still received Amex tokens, and JCB or Interac could not be enabled. The payment request, the diff --git a/docs/docs/guides/testing.md b/docs/docs/guides/testing.md index 90ce3be..3d877c7 100644 --- a/docs/docs/guides/testing.md +++ b/docs/docs/guides/testing.md @@ -8,7 +8,7 @@ Only `PRODUCTION` with real cards proves that a charge goes through. Each test e | Environment | Verifies | Cannot verify | |---|---|---| -| iOS Simulator | The sheet opens and shows your line items and the "Pay *merchantName*" total. Cancel, [`onAuthorize`](/docs/guides/server-processing#charge-before-the-sheet-closes) success and failure, and the double-tap guard. No card or account needed. | A token your gateway can charge. Apple doesn't document what the Simulator's token contains, and developers report that its `paymentData` is empty. Apple also says the Simulator doesn't support every Apple Pay feature, without listing which. | +| iOS Simulator | The sheet opens and shows your line items and the "Pay *merchantName*" total. Cancel, [`onAuthorize`](/docs/guides/server-processing#charge-before-the-sheet-closes) success and failure, a retry after a failure, and the double-tap guard. No card or account needed. | A token your gateway can charge. Apple doesn't document what the Simulator's token contains, and developers report that its `paymentData` is empty. Apple also says the Simulator doesn't support every Apple Pay feature, without listing which. | | iPhone with a sandbox tester | The full sheet with Apple's test cards, Face ID or passcode, and a token your payment processor can test decrypting. | A completed charge. Apple says sandbox transactions are declined before fulfillment, because the test key doesn't match your production key. | | Android, `TEST` environment | The Google Pay sheet with Google's test cards, the result handling, and `onAuthorize`. No Google Pay merchant ID needed. | Availability: `hasActiveCard` is always `true` in `TEST`. A live charge: `TEST` tokens never carry real card data, so only your gateway's test environment can process them. Your production request: the merchant ID and the `PRODUCTION` checks only run in `PRODUCTION`. | | Android, `PRODUCTION` | Everything, with real cards. | Nothing. It needs [production access](#production-access) first. | diff --git a/package/ios/HybridPaymentHandler.swift b/package/ios/HybridPaymentHandler.swift index 037eae6..ac6858f 100644 --- a/package/ios/HybridPaymentHandler.swift +++ b/package/ios/HybridPaymentHandler.swift @@ -371,6 +371,26 @@ private struct PaymentTokenConverter { } } +// MARK: - Authorization Results + +// Internal, not private, so the NitroPay unit tests can reach it. +enum AuthorizationResultBuilder { + + /// The merchant's outcome for the sheet. PassKit ends the payment on a + /// failure without a PassKit error, but lets the payer try again after + /// `unknownError`, which calls `didAuthorizePayment` with a new token. + static func build(success: Bool, error: String?) -> PKPaymentAuthorizationResult { + guard !success else { + return PKPaymentAuthorizationResult(status: .success, errors: nil) + } + let retryable = PKPaymentError( + .unknownError, + userInfo: [NSLocalizedDescriptionKey: error ?? ErrorMessage.authorizationFailed] + ) + return PKPaymentAuthorizationResult(status: .failure, errors: [retryable]) + } +} + // MARK: - Payment Results private enum PaymentResultFactory { @@ -564,10 +584,7 @@ private class PaymentDelegate: NSObject, PKPaymentAuthorizationControllerDelegat isSettled = true self.isAuthorizing = false self.authorizedResult = result - completion(PKPaymentAuthorizationResult( - status: result.success ? .success : .failure, - errors: nil - )) + completion(AuthorizationResultBuilder.build(success: result.success, error: result.error)) if self.finishedWhileAuthorizing { self.reportResult() } diff --git a/package/ios/Tests/AuthorizationResultBuilderTests.swift b/package/ios/Tests/AuthorizationResultBuilderTests.swift new file mode 100644 index 0000000..53930ba --- /dev/null +++ b/package/ios/Tests/AuthorizationResultBuilderTests.swift @@ -0,0 +1,32 @@ +import PassKit +import XCTest +@testable import NitroPay + +final class AuthorizationResultBuilderTests: XCTestCase { + + func testSuccessCarriesNoErrors() { + let result = AuthorizationResultBuilder.build(success: true, error: nil) + + XCTAssertEqual(result.status, .success) + XCTAssertTrue(result.errors.isEmpty) + } + + // PassKit closes the sheet after a failure with no errors, or with an + // error from another domain; after `unknownError` the payer can retry. + func testFailureLetsThePayerRetry() { + let result = AuthorizationResultBuilder.build(success: false, error: "Card declined") + + XCTAssertEqual(result.status, .failure) + XCTAssertEqual(result.errors.count, 1) + let error = result.errors[0] as NSError + XCTAssertEqual(error.domain, PKPaymentErrorDomain) + XCTAssertEqual(error.code, PKPaymentError.unknownError.rawValue) + XCTAssertEqual(error.localizedDescription, "Card declined") + } + + func testFailureWithoutAMessageUsesTheGenericOne() { + let result = AuthorizationResultBuilder.build(success: false, error: nil) + + XCTAssertEqual((result.errors.first as NSError?)?.localizedDescription, "Payment authorization failed") + } +}