Skip to content

Commit

Permalink
Use unexpectedErrorCode for unexpected errors that we should alert on… (
Browse files Browse the repository at this point in the history
stripe#3497)

… in STPPaymentHandler

## Summary
Now that STPPaymentHandler sends start and finish analytics
(stripe#3484) and sends
loggableUserInfo in those analytics
(stripe#3486), this PR:
1. _Finishes_ the confirmation with an error instead of hanging, in
certain cases
2. Corrects errors that were using the wrong/inaccurate error code
3. Adds an "unexpectedErrorCode" case for unexpected errors that should
never happen and indicate a problem with the SDK/API that we should
alert on

## Testing
Relying on existing unit tests 

## Changelog
Not user facing.
  • Loading branch information
yuki-stripe authored Apr 10, 2024
1 parent c9481e0 commit ac4a846
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 501 deletions.
5 changes: 4 additions & 1 deletion Stripe/StripeiOSTests/STPPaymentHandlerFunctionalTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ - (void)testAlipayOpensWebviewAfterNativeURLUnavailable {
__block NSString *clientSecret = @"pi_123_secret_456";

id apiClient = OCMPartialMock(STPAPIClient.sharedClient);
STPPaymentIntent *paymentIntent = [STPFixtures paymentIntent];
NSMutableDictionary *paymentIntentJSON = [[STPTestUtils jsonNamed:@"PaymentIntent"] mutableCopy];
paymentIntentJSON[@"payment_method"] = [STPTestUtils jsonNamed:STPTestJSONPaymentMethodCard];
STPPaymentIntent *paymentIntent = [STPPaymentIntent decodedObjectFromAPIResponse:paymentIntentJSON];

OCMStub([apiClient confirmPaymentIntentWithParams:[OCMArg any] expand:[OCMArg any] completion:[OCMArg any]]).andDo(^(NSInvocation *invocation) {
void (^handler)(STPPaymentIntent *paymentIntent, __unused NSError * _Nullable error);
[invocation getArgument:&handler atIndex:4];
Expand Down
6 changes: 2 additions & 4 deletions Stripe/StripeiOSTests/STPPaymentHandlerFunctionalTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ final class STPPaymentHandlerFunctionalSwiftTest: XCTestCase, STPAuthenticationC
XCTAssertEqual(lastAnalytic?["error_code"] as? String, "unsupportedAuthenticationErrorCode")
XCTAssertEqual(lastAnalytic?["error_details"] as? [String: String], [
"NSLocalizedDescription": "There was an unexpected error -- try again in a few seconds",
"com.stripe.lib:ErrorMessageKey": "The SDK doesn\'t recognize the PaymentIntent action type.",
"STPIntentAction": "unknown",
"com.stripe.lib:ErrorMessageKey": "Unknown authentication action type",
])
paymentHandlerExpectation.fulfill()
}
Expand Down Expand Up @@ -346,8 +345,7 @@ final class STPPaymentHandlerFunctionalSwiftTest: XCTestCase, STPAuthenticationC
XCTAssertEqual(lastAnalytic?["error_code"] as? String, "unsupportedAuthenticationErrorCode")
XCTAssertEqual(lastAnalytic?["error_details"] as? [String: String], [
"NSLocalizedDescription": "There was an unexpected error -- try again in a few seconds",
"com.stripe.lib:ErrorMessageKey": "The SDK doesn\'t recognize the PaymentIntent action type.",
"STPIntentAction": "unknown",
"com.stripe.lib:ErrorMessageKey": "Unknown authentication action type",
])
paymentHandlerExpectation.fulfill()
}
Expand Down
1 change: 1 addition & 0 deletions Stripe/StripeiOSTests/STPPaymentHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class STPPaymentHandlerStubbedTests: STPNetworkStubbingTestCase {
XCTAssertEqual(lastAnalytic?["payment_method_type"] as? String, "card")
XCTAssertEqual(lastAnalytic?["error_type"] as? String, "STPPaymentHandlerErrorDomain")
XCTAssertEqual(lastAnalytic?["error_code"] as? String, "requiresAuthenticationContextErrorCode")
XCTAssertEqual(lastAnalytic?[jsonDict: "error_details"]?["com.stripe.lib:ErrorMessageKey"] as? String, "authenticationPresentingViewController is not in the window hierarchy. You should probably return the top-most view controller instead.")
XCTAssertTrue(status == .failed)
XCTAssertNotNil(paymentIntent)
XCTAssertNotNil(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,8 @@ extension STPPaymentHandler {
action.complete(
with: STPPaymentHandlerActionStatus.failed,
error: _error(
for: .unsupportedAuthenticationErrorCode,
loggingSafeUserInfo: [
"STPIntentAction": action.nextAction()?.description ?? "",
]
for: .unexpectedErrorCode,
loggingSafeErrorMessage: "paymentIntentStatusSpec is redirect_to_url but missing redirect URL"
)
)
}
Expand All @@ -142,10 +140,9 @@ extension STPPaymentHandler {
action.complete(
with: .failed,
error: _error(
for: .intentStatusErrorCode,
loggingSafeUserInfo: [
"STPIntentAction": action.nextAction()?.description ?? "",
]
for: .unexpectedErrorCode,
loggingSafeErrorMessage: "Failed with unknown status spec type \(paymentIntentStatusSpec.type)"

)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ extension STPPaymentHandlerErrorCode: CustomStringConvertible {
return "timedOutErrorCode"
case .unsupportedAuthenticationErrorCode:
return "unsupportedAuthenticationErrorCode"
case .unexpectedErrorCode:
return "unexpectedErrorCode"
case .missingReturnURL:
return "missingReturnURL"
}
}
}
Expand Down
Loading

0 comments on commit ac4a846

Please sign in to comment.