BTThreeDSecureV1BrowserSwitchHelper.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #import "BTThreeDSecureV1BrowserSwitchHelper.h"
  2. static NSString *const BTThreeDSecureAssetsPath = @"/mobile/three-d-secure-redirect/0.2.0";
  3. @implementation BTThreeDSecureV1BrowserSwitchHelper
  4. + (NSURL *)urlWithScheme:(NSString *)appReturnURLScheme
  5. assetsURL:(NSString *)assetsURL
  6. threeDSecureRequest:(BTThreeDSecureRequest *)threeDSecureRequest
  7. threeDSecureLookup:(BTThreeDSecureLookup *)threeDSecureLookup {
  8. NSString *rfc3986UnreservedCharacters = @"-._~";
  9. NSMutableCharacterSet *unreservedCharacters = NSMutableCharacterSet.alphanumericCharacterSet;
  10. [unreservedCharacters addCharactersInString:rfc3986UnreservedCharacters];
  11. NSURLComponents *redirectURLComponents = [[NSURLComponents alloc] init];
  12. redirectURLComponents.scheme = appReturnURLScheme;
  13. redirectURLComponents.host = @"x-callback-url";
  14. redirectURLComponents.path = @"/braintree/threedsecure";
  15. // Trailing question mark is required so that we can append 3DS result to redirect URL.
  16. NSString *redirectURL = [NSString stringWithFormat:@"%@?", redirectURLComponents.URL.absoluteString];
  17. NSURLComponents *returnURLComponents = [NSURLComponents componentsWithString:assetsURL];
  18. returnURLComponents.path = [BTThreeDSecureAssetsPath stringByAppendingString:@"/redirect.html"];
  19. NSMutableString *returnURLQuery = [@"" mutableCopy];
  20. if (threeDSecureRequest.v1UICustomization) {
  21. if (threeDSecureRequest.v1UICustomization.redirectButtonText) {
  22. NSString *encodedButtonText = [threeDSecureRequest.v1UICustomization.redirectButtonText stringByAddingPercentEncodingWithAllowedCharacters:unreservedCharacters];
  23. [returnURLQuery appendFormat:@"b=%@&", encodedButtonText];
  24. }
  25. if (threeDSecureRequest.v1UICustomization.redirectDescription) {
  26. NSString *encodedDescription = [threeDSecureRequest.v1UICustomization.redirectDescription stringByAddingPercentEncodingWithAllowedCharacters:unreservedCharacters];
  27. [returnURLQuery appendFormat:@"d=%@&", encodedDescription];
  28. }
  29. }
  30. // redirect_url must be last query parameter in returnUrl
  31. [returnURLQuery appendFormat:@"redirect_url=%@", [redirectURL stringByAddingPercentEncodingWithAllowedCharacters:unreservedCharacters]];
  32. // The return url's query string needs to be encoded
  33. returnURLComponents.percentEncodedQuery = [returnURLQuery stringByAddingPercentEncodingWithAllowedCharacters:unreservedCharacters];
  34. NSString *encodedReturnURL = [returnURLComponents.URL.absoluteString stringByAddingPercentEncodingWithAllowedCharacters:unreservedCharacters];
  35. NSURLComponents *urlComponents = [NSURLComponents componentsWithString:assetsURL];
  36. urlComponents.path = [BTThreeDSecureAssetsPath stringByAppendingString:@"/index.html"];
  37. NSString *encodedAcsURL = [threeDSecureLookup.acsURL.absoluteString stringByAddingPercentEncodingWithAllowedCharacters:unreservedCharacters];
  38. NSString *encodedPAReq = [threeDSecureLookup.PAReq stringByAddingPercentEncodingWithAllowedCharacters:unreservedCharacters];
  39. NSString *encodedMD = [threeDSecureLookup.MD stringByAddingPercentEncodingWithAllowedCharacters:unreservedCharacters];
  40. NSString *encodedTermURL = [threeDSecureLookup.termURL.absoluteString stringByAddingPercentEncodingWithAllowedCharacters:unreservedCharacters];
  41. urlComponents.percentEncodedQuery = [NSString stringWithFormat:@"AcsUrl=%@&PaReq=%@&MD=%@&TermUrl=%@&ReturnUrl=%@",
  42. encodedAcsURL,
  43. encodedPAReq,
  44. encodedMD,
  45. encodedTermURL,
  46. encodedReturnURL];
  47. return urlComponents.URL;
  48. }
  49. @end