Skip to content

Commit

Permalink
Refactor out fatalerror in pollingviewcontroller (stripe#3505)
Browse files Browse the repository at this point in the history
… action

PollingViewController takes a `STPPaymentHandlerActionParams ` and
fatalErrors if it's not a `STPPaymentHandlerPaymentIntentActionParams` -
just make it take a `STPPaymentHandlerPaymentIntentActionParams` instead

## Testing
Compiler, existing tests

## Changelog
Not user facing
  • Loading branch information
yuki-stripe authored Apr 10, 2024
1 parent f628dbc commit 692ef55
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ class AuthenticationContext: NSObject, PaymentSheetAuthenticationContext {
presentingViewController.present(authenticationViewController, animated: true, completion: nil)
}

func presentPollingVCForAction(action: STPPaymentHandlerActionParams, type: STPPaymentMethodType, safariViewController: SFSafariViewController?) {
func presentPollingVCForAction(action: STPPaymentHandlerPaymentIntentActionParams, type: STPPaymentMethodType, safariViewController: SFSafariViewController?) {
let pollingVC = PollingViewController(currentAction: action, viewModel: PollingViewModel(paymentMethodType: type),
appearance: self.appearance, safariViewController: safariViewController)
presentingViewController.present(pollingVC, animated: true, completion: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ extension BottomSheetViewController: PaymentSheetAuthenticationContext {
self.removeBlurEffect(animated: true, completion: completion)
}

func presentPollingVCForAction(action: STPPaymentHandlerActionParams, type: STPPaymentMethodType, safariViewController: SFSafariViewController?) {
func presentPollingVCForAction(action: STPPaymentHandlerPaymentIntentActionParams, type: STPPaymentMethodType, safariViewController: SFSafariViewController?) {
let pollingVC = PollingViewController(currentAction: action, viewModel: PollingViewModel(paymentMethodType: type),
appearance: self.appearance, safariViewController: safariViewController)
pushContentViewController(pollingVC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ class PollingViewController: UIViewController {
// MARK: State

private var oneSecondTimer: Timer?
private let currentAction: STPPaymentHandlerActionParams
private let currentAction: STPPaymentHandlerPaymentIntentActionParams
private let appearance: PaymentSheet.Appearance
private let viewModel: PollingViewModel
private let safariViewController: SFSafariViewController?

private lazy var intentPoller: IntentStatusPoller = {
guard let currentAction = currentAction as? STPPaymentHandlerPaymentIntentActionParams else { fatalError() }

let intentPoller = IntentStatusPoller(retryInterval: viewModel.retryInterval,
intentRetriever: currentAction.apiClient,
clientSecret: currentAction.paymentIntent.clientSecret)
Expand Down Expand Up @@ -161,7 +159,7 @@ class PollingViewController: UIViewController {

// MARK: Overrides

init(currentAction: STPPaymentHandlerActionParams, viewModel: PollingViewModel, appearance: PaymentSheet.Appearance, safariViewController: SFSafariViewController? = nil) {
init(currentAction: STPPaymentHandlerPaymentIntentActionParams, viewModel: PollingViewModel, appearance: PaymentSheet.Appearance, safariViewController: SFSafariViewController? = nil) {
self.currentAction = currentAction
self.appearance = appearance
self.viewModel = viewModel
Expand Down Expand Up @@ -350,8 +348,6 @@ extension PollingViewController: SheetNavigationBarDelegate {

extension PollingViewController: IntentStatusPollerDelegate {
func didUpdate(paymentIntent: STPPaymentIntent) {
guard let currentAction = currentAction as? STPPaymentHandlerPaymentIntentActionParams else { return }

if paymentIntent.status == .succeeded {
setErrorStateWorkItem.cancel() // cancel the error work item incase it was scheduled
currentAction.paymentIntent = paymentIntent // update the local copy of the intent with the latest from the server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ extension PaymentSheet_LPM_ConfirmFlowTests: PaymentSheetAuthenticationContext {
completion?()
}

func presentPollingVCForAction(action: STPPaymentHandlerActionParams, type: STPPaymentMethodType, safariViewController: SFSafariViewController?) {
func presentPollingVCForAction(action: STPPaymentHandlerPaymentIntentActionParams, type: STPPaymentMethodType, safariViewController: SFSafariViewController?) {
guard let currentAction = action as? STPPaymentHandlerPaymentIntentActionParams else { return }
// Simulate that the intent transitioned to succeeded
// If we don't update the status to succeeded, completing the action with .succeeded may fail due to invalid state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,10 @@ public class STPPaymentHandler: NSObject {
case .BLIKAuthorize:
// The customer must authorize the transaction in their banking app within 1 minute
if let presentingVC = currentAction.authenticationContext as? PaymentSheetAuthenticationContext {
guard let currentAction = currentAction as? STPPaymentHandlerPaymentIntentActionParams else {
currentAction.complete(with: .failed, error: _error(for: .unexpectedErrorCode, loggingSafeErrorMessage: "Handling BLIKAuthorize next action with SetupIntent is not supported"))
return
}
// If we are using PaymentSheet, PollingViewController will poll Stripe to determine success and complete the currentAction
presentingVC.presentPollingVCForAction(action: currentAction, type: .blik, safariViewController: nil)
} else {
Expand All @@ -1282,6 +1286,10 @@ public class STPPaymentHandler: NSObject {
case .upiAwaitNotification:
// The customer must authorize the transaction in their banking app within 5 minutes
if let presentingVC = currentAction.authenticationContext as? PaymentSheetAuthenticationContext {
guard let currentAction = currentAction as? STPPaymentHandlerPaymentIntentActionParams else {
currentAction.complete(with: .failed, error: _error(for: .unexpectedErrorCode, loggingSafeErrorMessage: "Handling upiAwaitNotification next action with SetupIntent is not supported"))
return
}
// If we are using PaymentSheet, PollingViewController will poll Stripe to determine success and complete the currentAction
presentingVC.presentPollingVCForAction(action: currentAction, type: .UPI, safariViewController: nil)
} else {
Expand Down Expand Up @@ -1315,7 +1323,10 @@ public class STPPaymentHandler: NSObject {
currentAction.complete(with: .failed, error: _error(for: .unsupportedAuthenticationErrorCode, loggingSafeErrorMessage: "PayNow is not supported outside of PaymentSheet."))
return
}

guard let currentAction = currentAction as? STPPaymentHandlerPaymentIntentActionParams else {
currentAction.complete(with: .failed, error: self._error(for: .unexpectedErrorCode, loggingSafeErrorMessage: "Handling payNowDisplayQrCode next action with SetupIntent is not supported"))
return
}
_handleRedirect(to: hostedInstructionsURL, fallbackURL: hostedInstructionsURL, return: returnURL) { safariViewController in
// Present the polling view controller behind the web view so we can start polling right away
presentingVC.presentPollingVCForAction(action: currentAction, type: .paynow, safariViewController: safariViewController)
Expand All @@ -1341,6 +1352,10 @@ public class STPPaymentHandler: NSObject {
currentAction.complete(with: .failed, error: _error(for: .unsupportedAuthenticationErrorCode, loggingSafeErrorMessage: "PromptPay is not supported outside of PaymentSheet."))
return
}
guard let currentAction = currentAction as? STPPaymentHandlerPaymentIntentActionParams else {
currentAction.complete(with: .failed, error: self._error(for: .unexpectedErrorCode, loggingSafeErrorMessage: "Handling promptpayDisplayQrCode next action with SetupIntent is not supported"))
return
}

_handleRedirect(to: hostedInstructionsURL, fallbackURL: hostedInstructionsURL, return: returnURL) { safariViewController in
// Present the polling view controller behind the web view so we can start polling right away
Expand Down Expand Up @@ -2384,7 +2399,7 @@ extension STPPaymentHandler {
@_spi(STP) public protocol PaymentSheetAuthenticationContext: STPAuthenticationContext {
func present(_ authenticationViewController: UIViewController, completion: @escaping () -> Void)
func dismiss(_ authenticationViewController: UIViewController, completion: (() -> Void)?)
func presentPollingVCForAction(action: STPPaymentHandlerActionParams, type: STPPaymentMethodType, safariViewController: SFSafariViewController?)
func presentPollingVCForAction(action: STPPaymentHandlerPaymentIntentActionParams, type: STPPaymentMethodType, safariViewController: SFSafariViewController?)
}

@_spi(STP) public protocol FormSpecPaymentHandler {
Expand Down

0 comments on commit 692ef55

Please sign in to comment.