PPOTCheckoutRequest.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // PPOTCheckoutRequest.m
  3. // PayPalOneTouch
  4. //
  5. // Copyright © 2015 PayPal, Inc. All rights reserved.
  6. //
  7. #import "PPOTRequest_Internal.h"
  8. #import "PPOTCheckoutAppSwitchRequest.h"
  9. #import "PPOTCheckoutBrowserSwitchRequest.h"
  10. #import "PPOTConfiguration.h"
  11. #if __has_include("PayPalUtils.h")
  12. #import "PPOTDevice.h"
  13. #import "PPOTMacros.h"
  14. #else
  15. #import <PayPalUtils/PPOTDevice.h>
  16. #import <PayPalUtils/PPOTMacros.h>
  17. #endif
  18. #pragma mark - PPOTCheckoutRequest implementation
  19. @implementation PPOTCheckoutRequest
  20. - (instancetype)initWithApprovalURL:(NSURL *)approvalURL
  21. pairingId:(NSString *)pairingId
  22. clientID:(NSString *)clientID
  23. environment:(NSString *)environment
  24. callbackURLScheme:(NSString *)callbackURLScheme {
  25. if (!approvalURL
  26. || ([environment isEqualToString:PPRequestEnvironmentProduction]
  27. && ![approvalURL.absoluteString hasPrefix:@"https://"])) {
  28. PPSDKLog(@"invalid approval URL, or scheme is not https:");
  29. return nil;
  30. }
  31. NSDictionary *queryDictionary = [PPOTAppSwitchUtil parseQueryString:[approvalURL query]];
  32. NSString *hermesToken = queryDictionary[kPPOTAppSwitchHermesTokenKey];
  33. if (!hermesToken) {
  34. hermesToken = queryDictionary[kPPOTAppSwitchHermesBATokenKey];
  35. }
  36. if (!hermesToken) {
  37. PPSDKLog(@"approval URL lacks a Hermes token");
  38. return nil;
  39. }
  40. self = [super initWithClientID:clientID
  41. environment:environment
  42. callbackURLScheme:callbackURLScheme];
  43. if (self) {
  44. _approvalURL = approvalURL;
  45. _pairingId = pairingId;
  46. }
  47. return self;
  48. }
  49. + (instancetype)requestWithApprovalURL:(NSURL *)approvalURL
  50. clientID:(NSString *)clientID
  51. environment:(NSString *)environment
  52. callbackURLScheme:(NSString *)callbackURLScheme {
  53. return [PPOTCheckoutRequest requestWithApprovalURL:approvalURL
  54. pairingId:nil
  55. clientID:clientID
  56. environment:environment
  57. callbackURLScheme:callbackURLScheme];
  58. }
  59. + (instancetype)requestWithApprovalURL:(NSURL *)approvalURL
  60. pairingId:(NSString *)pairingId
  61. clientID:(NSString *)clientID
  62. environment:(NSString *)environment
  63. callbackURLScheme:(NSString *)callbackURLScheme {
  64. PPOTCheckoutRequest *request = [[[self class] alloc] initWithApprovalURL:approvalURL
  65. pairingId:pairingId
  66. clientID:clientID
  67. environment:environment
  68. callbackURLScheme:callbackURLScheme];
  69. return request;
  70. }
  71. #pragma mark - add subclass-specific info to appSwitchRequest
  72. - (PPOTSwitchRequest *)getAppSwitchRequestForConfigurationRecipe:(PPOTConfigurationRecipe *)configurationRecipe {
  73. PPOTCheckoutSwitchRequest *appSwitchRequest = nil;
  74. switch (configurationRecipe.target) {
  75. case PPOTRequestTargetOnDeviceApplication: {
  76. appSwitchRequest = [[PPOTCheckoutAppSwitchRequest alloc] initWithProtocolVersion:configurationRecipe.protocolVersion
  77. appGuid:[PPOTDevice appropriateIdentifier]
  78. clientID:self.clientID
  79. environment:self.environment
  80. callbackURLScheme:self.callbackURLScheme
  81. pairingId:self.pairingId];
  82. break;
  83. }
  84. case PPOTRequestTargetBrowser: {
  85. PPOTCheckoutBrowserSwitchRequest *browserSwitchRequest =
  86. [[PPOTCheckoutBrowserSwitchRequest alloc] initWithProtocolVersion:configurationRecipe.protocolVersion
  87. appGuid:[PPOTDevice appropriateIdentifier]
  88. clientID:self.clientID
  89. environment:self.environment
  90. callbackURLScheme:self.callbackURLScheme
  91. pairingId:self.pairingId];
  92. appSwitchRequest = browserSwitchRequest;
  93. break;
  94. }
  95. default: {
  96. break;
  97. }
  98. }
  99. if (appSwitchRequest) {
  100. appSwitchRequest.targetAppURLScheme = configurationRecipe.targetAppURLScheme;
  101. appSwitchRequest.responseType = PPAppSwitchResponseTypeWeb;
  102. appSwitchRequest.approvalURL = [self.approvalURL absoluteString];
  103. }
  104. return appSwitchRequest;
  105. }
  106. #pragma mark - configuration methods
  107. - (void)getAppropriateConfigurationRecipe:(void (^)(PPOTConfigurationRecipe *configurationRecipe))completionBlock {
  108. PPAssert(completionBlock, @"getAppropriateConfigurationRecipe: completionBlock is required");
  109. PPOTConfiguration *currentConfiguration = [PPOTConfiguration getCurrentConfiguration];
  110. PPOTConfigurationCheckoutRecipe *bestConfigurationRecipe = nil;
  111. for (PPOTConfigurationCheckoutRecipe *configurationRecipe in currentConfiguration.prioritizedCheckoutRecipes) {
  112. if (![self isConfigurationRecipeTargetSupported:configurationRecipe] ||
  113. ![self isConfigurationRecipeLocaleSupported:configurationRecipe]) {
  114. continue;
  115. }
  116. bestConfigurationRecipe = configurationRecipe;
  117. break;
  118. }
  119. completionBlock(bestConfigurationRecipe);
  120. }
  121. @end