BTThreeDSecureAuthenticateJWT.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #import "BTThreeDSecureAuthenticateJWT.h"
  2. #import "BTPaymentFlowDriver+ThreeDSecure_Internal.h"
  3. #if __has_include("BTAPIClient_Internal.h")
  4. #import "BTAPIClient_Internal.h"
  5. #else
  6. #import <BraintreeCore/BTAPIClient_Internal.h>
  7. #endif
  8. @implementation BTThreeDSecureAuthenticateJWT
  9. + (void)authenticateJWT:(NSString *)jwt
  10. withAPIClient:(BTAPIClient *)apiClient
  11. forLookupResult:(BTThreeDSecureLookup *)lookupResult
  12. success:(BTThreeDSecureV2ProviderSuccessHandler)successHandler
  13. failure:(BTThreeDSecureV2ProviderFailureHandler)failureHandler {
  14. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.upgrade-payment-method.started"];
  15. if (! lookupResult.threeDSecureResult.tokenizedCard.nonce) {
  16. NSError *error = [NSError errorWithDomain:BTThreeDSecureFlowErrorDomain
  17. code:BTThreeDSecureFlowErrorTypeFailedAuthentication
  18. userInfo:@{NSLocalizedDescriptionKey: @"Tokenized card nonce is required"}];
  19. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.upgrade-payment-method.errored"];
  20. failureHandler(error);
  21. return;
  22. }
  23. NSString *urlSafeNonce = [lookupResult.threeDSecureResult.tokenizedCard.nonce stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  24. NSDictionary *requestParameters = @{@"jwt": jwt, @"paymentMethodNonce": lookupResult.threeDSecureResult.tokenizedCard.nonce};
  25. [apiClient POST:[NSString stringWithFormat:@"v1/payment_methods/%@/three_d_secure/authenticate_from_jwt", urlSafeNonce]
  26. parameters:requestParameters
  27. completion:^(BTJSON *body, __unused NSHTTPURLResponse *response, NSError *error) {
  28. if (error) {
  29. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.upgrade-payment-method.errored"];
  30. failureHandler(error);
  31. return;
  32. }
  33. #pragma clang diagnostic push
  34. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  35. BTThreeDSecureResult *result = [[BTThreeDSecureResult alloc] initWithJSON:body];
  36. if (result.errorMessage) {
  37. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.upgrade-payment-method.failure.returned-lookup-nonce"];
  38. lookupResult.threeDSecureResult.tokenizedCard.threeDSecureInfo.errorMessage = result.errorMessage;
  39. successHandler(lookupResult.threeDSecureResult);
  40. } else {
  41. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.upgrade-payment-method.succeeded"];
  42. successHandler(result);
  43. }
  44. #pragma clang diagnostic pop
  45. }];
  46. }
  47. @end