PPOTSwitchRequest.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // PPOTSwitchRequest.m
  3. // PayPalOneTouch
  4. //
  5. // Copyright © 2015 PayPal, Inc. All rights reserved.
  6. //
  7. #import "PPOTSwitchRequest.h"
  8. #if __has_include("PPOTMacros.h")
  9. #import "PPOTMacros.h"
  10. #else
  11. #import <PayPalUtils/PPOTMacros.h>
  12. #endif
  13. #if __has_include("PPDataCollector_Internal.h")
  14. #import "PPDataCollector_Internal.h"
  15. #else
  16. #import <PayPalDataCollector/PPDataCollector_Internal.h>
  17. #endif
  18. @implementation PPOTSwitchRequest
  19. - (instancetype)initWithProtocolVersion:(NSNumber *)protocolVersion
  20. appGuid:(NSString *)appGuid
  21. clientID:(NSString *)clientID
  22. environment:(NSString *)environment
  23. callbackURLScheme:(NSString *)callbackURLScheme {
  24. return [self initWithProtocolVersion:protocolVersion appGuid:appGuid clientID:clientID environment:environment callbackURLScheme:callbackURLScheme pairingId:nil];
  25. }
  26. - (instancetype)initWithProtocolVersion:(NSNumber *)protocolVersion
  27. appGuid:(NSString *)appGuid
  28. clientID:(NSString *)clientID
  29. environment:(NSString *)environment
  30. callbackURLScheme:(NSString *)callbackURLScheme
  31. pairingId:(NSString *)pairingId {
  32. self = [super init];
  33. if (self) {
  34. _protocolVersion = protocolVersion;
  35. _appGuid = appGuid;
  36. _clientID = clientID;
  37. _environment = environment;
  38. _responseType = PPAppSwitchResponseTypeUnknown;
  39. _callbackURLScheme = callbackURLScheme;
  40. _clientMetadataID = [PPDataCollector generateClientMetadataID:pairingId disableBeacon:NO data:nil];
  41. }
  42. return self;
  43. }
  44. - (NSDictionary *)payloadDictionary {
  45. // mangle with environment for name "custom"
  46. NSString *environment = self.environment;
  47. if (![environment isEqualToString:PPRequestEnvironmentProduction] && ![environment isEqualToString:PPRequestEnvironmentNoNetwork] &&
  48. ![environment isEqualToString:PPRequestEnvironmentSandbox]) {
  49. // extract baseURL
  50. NSURL *serviceURL = [NSURL URLWithString:environment];
  51. if (!serviceURL.host.length) {
  52. serviceURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@", environment]];
  53. }
  54. self.customURL = [NSString stringWithFormat:@"%@://%@:%@", serviceURL.scheme, serviceURL.host, serviceURL.port];
  55. environment = kPPOTAppSwitchCustomEnvironmentKey;
  56. }
  57. NSMutableDictionary *payload = [NSMutableDictionary dictionaryWithCapacity:11];
  58. payload[kPPOTAppSwitchProtocolVersionKey] = self.protocolVersion;
  59. payload[kPPOTAppSwitchClientIdKey] = [self.clientID copy];
  60. // use environment or custom
  61. payload[kPPOTAppSwitchEnvironmentKey] = [environment copy];
  62. payload[kPPOTAppSwitchAppNameKey] = FORCE_VALUE_OR_NULL([PPOTAppSwitchUtil bundleName]);
  63. switch (self.responseType) {
  64. case PPAppSwitchResponseTypeToken:
  65. payload[kPPOTAppSwitchResponseTypeKey] = kPPOTAppSwitchResponseTypeToken;
  66. break;
  67. case PPAppSwitchResponseTypeAuthorizationCode:
  68. payload[kPPOTAppSwitchResponseTypeKey] = kPPOTAppSwitchResponseTypeCode;
  69. break;
  70. case PPAppSwitchResponseTypeWeb:
  71. payload[kPPOTAppSwitchResponseTypeKey] = kPPOTAppSwitchResponseTypeWeb;
  72. break;
  73. default:
  74. PPAssert(YES, @"Response type unsupported");
  75. break;
  76. }
  77. if (self.customURL.length) {
  78. payload[kPPOTAppSwitchEnvironmentURLKey] = self.customURL;
  79. }
  80. // dyson pairing id
  81. payload[kPPOTAppSwitchMetadataClientIDKey] = FORCE_VALUE_OR_NULL(self.clientMetadataID);
  82. return payload;
  83. }
  84. // default version of encodedURL (for v1, v2, and v3)
  85. - (NSURL *)encodedURL {
  86. NSDictionary *payload = [self payloadDictionary];
  87. NSURL *url = [PPOTAppSwitchUtil URLAction:kPPOTAppSwitchAuthenticateAction
  88. targetAppURLScheme:self.targetAppURLScheme
  89. callbackURLScheme:self.callbackURLScheme
  90. payload:payload];
  91. return url;
  92. }
  93. - (void)addDataToPersistentRequestDataDictionary:(__attribute__((unused)) NSMutableDictionary *)requestDataDictionary {
  94. // subclasses each call [super] add then add their own relevant data, if any, to be retrieved when the response comes back to us
  95. }
  96. @end