BTThreeDSecureV1BrowserSwitchHelper.m 4.1 KB

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