BTThreeDSecureAuthenticateJWT.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #import "BTThreeDSecureAuthenticateJWT.h"
  2. #import "BTPaymentFlowDriver+ThreeDSecure_Internal.h"
  3. #import "BTThreeDSecureResult_Internal.h"
  4. #if __has_include(<Braintree/BraintreeThreeDSecure.h>) // CocoaPods
  5. #import <Braintree/BraintreeCard.h>
  6. #import <Braintree/BTAPIClient_Internal.h>
  7. #elif SWIFT_PACKAGE // SPM
  8. #import <BraintreeCard/BraintreeCard.h>
  9. #import "../BraintreeCore/BTAPIClient_Internal.h"
  10. #else // Carthage
  11. #import <BraintreeCard/BraintreeCard.h>
  12. #import <BraintreeCore/BTAPIClient_Internal.h>
  13. #endif
  14. @implementation BTThreeDSecureAuthenticateJWT
  15. + (void)authenticateJWT:(NSString *)jwt
  16. withAPIClient:(BTAPIClient *)apiClient
  17. forLookupResult:(BTThreeDSecureResult *)lookupResult
  18. success:(BTThreeDSecureV2ProviderSuccessHandler)successHandler
  19. failure:(BTThreeDSecureV2ProviderFailureHandler)failureHandler {
  20. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.upgrade-payment-method.started"];
  21. if (!lookupResult.tokenizedCard.nonce) {
  22. NSError *error = [NSError errorWithDomain:BTThreeDSecureFlowErrorDomain
  23. code:BTThreeDSecureFlowErrorTypeFailedAuthentication
  24. userInfo:@{NSLocalizedDescriptionKey: @"Tokenized card nonce is required"}];
  25. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.upgrade-payment-method.errored"];
  26. if (failureHandler != nil) {
  27. failureHandler(error);
  28. }
  29. return;
  30. }
  31. NSString *urlSafeNonce = [lookupResult.tokenizedCard.nonce stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  32. NSDictionary *requestParameters = @{@"jwt": jwt, @"paymentMethodNonce": lookupResult.tokenizedCard.nonce};
  33. [apiClient POST:[NSString stringWithFormat:@"v1/payment_methods/%@/three_d_secure/authenticate_from_jwt", urlSafeNonce]
  34. parameters:requestParameters
  35. completion:^(BTJSON *body, __unused NSHTTPURLResponse *response, NSError *error) {
  36. if (error) {
  37. if (error.code == NETWORK_CONNECTION_LOST_CODE) {
  38. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.network-connection.failure"];
  39. }
  40. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.upgrade-payment-method.errored"];
  41. if (failureHandler != nil) {
  42. failureHandler(error);
  43. }
  44. return;
  45. }
  46. BTThreeDSecureResult *result = [[BTThreeDSecureResult alloc] initWithJSON:body];
  47. if (result.tokenizedCard && !result.errorMessage) {
  48. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.upgrade-payment-method.succeeded"];
  49. } else {
  50. [apiClient sendAnalyticsEvent:@"ios.three-d-secure.verification-flow.upgrade-payment-method.failure.returned-lookup-nonce"];
  51. // If authentication wasn't successful, add the BTCardNonce from the lookup result to the authentication result
  52. // so that merchants can transact with the lookup nonce if desired.
  53. result.tokenizedCard = lookupResult.tokenizedCard;
  54. }
  55. if (successHandler != nil) {
  56. successHandler(result);
  57. }
  58. }];
  59. }
  60. @end