PPOTPersistentRequestData.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // PPOTPersistentRequestData.m
  3. // PayPalOneTouch
  4. //
  5. // Copyright © 2015 PayPal, Inc. All rights reserved.
  6. //
  7. #import "PPOTPersistentRequestData.h"
  8. #import "PPOTOAuth2SwitchRequest.h"
  9. #import "PPOTConfiguration.h"
  10. #if __has_include("PayPalUtils.h")
  11. #import "PPOTMacros.h"
  12. #import "PPOTSimpleKeychain.h"
  13. #else
  14. #import <PayPalUtils/PPOTMacros.h>
  15. #import <PayPalUtils/PPOTSimpleKeychain.h>
  16. #endif
  17. #define kPPOTCoderKeyRequestDataConfigurationRecipe CARDIO_STR(@"configuration_recipe")
  18. #define kPPOTCoderKeyRequestDataEnvironment CARDIO_STR(@"environment")
  19. #define kPPOTCoderKeyRequestDataClientID CARDIO_STR(@"client_id")
  20. #define kPPOTCoderKeyRequestDataDataDictionary CARDIO_STR(@"data_dictionary")
  21. #define kPPOTKeychainRequestSpecificData CARDIO_STR(@"PayPal_OTC_RequestData")
  22. @implementation PPOTPersistentRequestData
  23. #pragma mark - initializer
  24. - (instancetype)initWithConfigurationRecipe:(PPOTConfigurationRecipe *)configurationRecipe
  25. withRequest:(PPOTSwitchRequest *)request {
  26. if ((self = [super init])) {
  27. _configurationRecipe = configurationRecipe;
  28. _environment = request.environment;
  29. _clientID = request.clientID;
  30. _requestData = [NSMutableDictionary dictionary];
  31. [request addDataToPersistentRequestDataDictionary:_requestData];
  32. }
  33. return self;
  34. }
  35. #pragma mark - NSCoding
  36. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  37. if ((self = [super init])) {
  38. _configurationRecipe = [aDecoder decodeObjectForKey:kPPOTCoderKeyRequestDataConfigurationRecipe];
  39. _environment = [aDecoder decodeObjectForKey:kPPOTCoderKeyRequestDataEnvironment];
  40. _clientID = [aDecoder decodeObjectForKey:kPPOTCoderKeyRequestDataClientID];
  41. _requestData = [aDecoder decodeObjectForKey:kPPOTCoderKeyRequestDataDataDictionary];
  42. }
  43. return self;
  44. }
  45. - (void)encodeWithCoder:(NSCoder *)aCoder {
  46. [aCoder encodeObject:self.configurationRecipe forKey:kPPOTCoderKeyRequestDataConfigurationRecipe];
  47. [aCoder encodeObject:self.environment forKey:kPPOTCoderKeyRequestDataEnvironment];
  48. [aCoder encodeObject:self.clientID forKey:kPPOTCoderKeyRequestDataClientID];
  49. [aCoder encodeObject:self.requestData forKey:kPPOTCoderKeyRequestDataDataDictionary];
  50. }
  51. #pragma mark - keychain
  52. + (PPOTPersistentRequestData *)fetch {
  53. return [PPOTSimpleKeychain unarchiveObjectWithDataForKey:kPPOTKeychainRequestSpecificData];
  54. }
  55. + (void)storeWithConfigurationRecipe:(PPOTConfigurationRecipe *)configurationRecipe
  56. withRequest:(PPOTSwitchRequest *)request {
  57. PPOTPersistentRequestData *persistentRequestData = [[PPOTPersistentRequestData alloc]
  58. initWithConfigurationRecipe:configurationRecipe withRequest:request];
  59. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:persistentRequestData];
  60. [PPOTSimpleKeychain setData:data forKey:kPPOTKeychainRequestSpecificData];
  61. }
  62. + (void)remove {
  63. [PPOTSimpleKeychain setData:nil forKey:kPPOTKeychainRequestSpecificData];
  64. }
  65. @end