Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/guides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
25 changes: 21 additions & 4 deletions package/ios/HybridPaymentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down
32 changes: 32 additions & 0 deletions package/ios/Tests/AuthorizationResultBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading