Skip to content

Commit

Permalink
Fix PaymentHandler reporting certain payment errors as unexpected errors
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-stripe committed Apr 25, 2024
1 parent efcdbe4 commit 2043798
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1455,9 +1455,9 @@ public class STPPaymentHandler: NSObject {
currentAction.apiClient.retrievePaymentIntent(
withClientSecret: currentAction.paymentIntent.clientSecret,
expand: ["payment_method"]
) { paymentIntent, error in
guard let paymentIntent, let paymentMethod = paymentIntent.paymentMethod, error == nil else {
let error = error ?? self._error(for: .unexpectedErrorCode, loggingSafeErrorMessage: "Missing PaymentIntent or paymentIntent.paymentMethod.")
) { [self] paymentIntent, error in
guard let paymentIntent, error == nil else {
let error = error ?? self._error(for: .unexpectedErrorCode, loggingSafeErrorMessage: "Missing PaymentIntent.")
currentAction.complete(
with: STPPaymentHandlerActionStatus.failed,
error: error as NSError
Expand All @@ -1468,6 +1468,7 @@ public class STPPaymentHandler: NSObject {
// If the transaction is still unexpectedly processing, refresh the PaymentIntent
// This could happen if, for example, a payment is approved in an SFSafariViewController, the user closes the sheet, and the approval races with this fetch.
if
let paymentMethod = paymentIntent.paymentMethod,
!STPPaymentHandler._isProcessingIntentSuccess(for: paymentMethod.type),
paymentIntent.status == .processing && retryCount > 0
{
Expand All @@ -1488,6 +1489,13 @@ public class STPPaymentHandler: NSObject {
forAction: currentAction
)
if requiresAction {
guard let paymentMethod = paymentIntent.paymentMethod else {
currentAction.complete(
with: STPPaymentHandlerActionStatus.failed,
error: self._error(for: .unexpectedErrorCode, loggingSafeErrorMessage: "PaymentIntent requires action but missing PaymentMethod.")
)
return
}
// If the status is still RequiresAction, the user exited from the redirect before the
// payment intent was updated. Consider it a cancel, unless it's a valid terminal next action
if self.isNextActionSuccessState(
Expand Down

0 comments on commit 2043798

Please sign in to comment.