BTCardNonce.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #import "BTCardNonce_Internal.h"
  2. #import "BTAuthenticationInsight_Internal.h"
  3. @implementation BTCardNonce
  4. - (instancetype)initWithNonce:(NSString *)nonce
  5. description:(NSString *)description
  6. cardNetwork:(BTCardNetwork)cardNetwork
  7. expirationMonth:(NSString *)expirationMonth
  8. expirationYear:(NSString *)expirationYear
  9. cardholderName:(NSString *)cardholderName
  10. lastTwo:(NSString *)lastTwo
  11. lastFour:(NSString *)lastFour
  12. isDefault:(BOOL)isDefault
  13. cardJSON:(BTJSON *)cardJSON
  14. authInsightJSON:(BTJSON *)authInsightJSON {
  15. self = [super initWithNonce:nonce
  16. localizedDescription:description
  17. type:[BTCardNonce typeStringFromCardNetwork:cardNetwork]
  18. isDefault:isDefault];
  19. if (self) {
  20. _cardNetwork = cardNetwork;
  21. _expirationMonth = expirationMonth;
  22. _expirationYear = expirationYear;
  23. _cardholderName = cardholderName;
  24. _lastTwo = lastTwo;
  25. _lastFour = lastFour;
  26. _binData = [[BTBinData alloc] initWithJSON:cardJSON[@"binData"]];
  27. if ([cardJSON[@"details"][@"bin"] asString]) {
  28. _bin = [cardJSON[@"details"][@"bin"] asString];
  29. } else if ([cardJSON[@"bin"] asString]) {
  30. _bin = [cardJSON[@"bin"] asString];
  31. }
  32. _threeDSecureInfo = [[BTThreeDSecureInfo alloc] initWithJSON:cardJSON[@"threeDSecureInfo"]];
  33. if (authInsightJSON) {
  34. _authenticationInsight = [[BTAuthenticationInsight alloc] initWithJSON:authInsightJSON];
  35. }
  36. }
  37. return self;
  38. }
  39. + (NSString *)typeStringFromCardNetwork:(BTCardNetwork)cardNetwork {
  40. switch (cardNetwork) {
  41. case BTCardNetworkAMEX:
  42. return @"AMEX";
  43. case BTCardNetworkDinersClub:
  44. return @"DinersClub";
  45. case BTCardNetworkDiscover:
  46. return @"Discover";
  47. case BTCardNetworkMasterCard:
  48. return @"MasterCard";
  49. case BTCardNetworkVisa:
  50. return @"Visa";
  51. case BTCardNetworkJCB:
  52. return @"JCB";
  53. case BTCardNetworkLaser:
  54. return @"Laser";
  55. case BTCardNetworkMaestro:
  56. return @"Maestro";
  57. case BTCardNetworkUnionPay:
  58. return @"UnionPay";
  59. case BTCardNetworkHiper:
  60. return @"Hiper";
  61. case BTCardNetworkHipercard:
  62. return @"Hipercard";
  63. case BTCardNetworkSolo:
  64. return @"Solo";
  65. case BTCardNetworkSwitch:
  66. return @"Switch";
  67. case BTCardNetworkUKMaestro:
  68. return @"UKMaestro";
  69. case BTCardNetworkUnknown:
  70. default:
  71. return @"Unknown";
  72. }
  73. }
  74. + (BTCardNetwork)cardNetworkFromGatewayCardType:(NSString *)string {
  75. // Normalize the card network string in cardJSON to be lowercase so that our enum mapping is case insensitive
  76. BTJSON *cardType = [[BTJSON alloc] initWithValue:string.lowercaseString];
  77. return [cardType asEnum:@{
  78. @"american express": @(BTCardNetworkAMEX),
  79. @"diners club": @(BTCardNetworkDinersClub),
  80. @"unionpay": @(BTCardNetworkUnionPay),
  81. @"discover": @(BTCardNetworkDiscover),
  82. @"maestro": @(BTCardNetworkMaestro),
  83. @"mastercard": @(BTCardNetworkMasterCard),
  84. @"jcb": @(BTCardNetworkJCB),
  85. @"hiper": @(BTCardNetworkHiper),
  86. @"hipercard": @(BTCardNetworkHipercard),
  87. @"laser": @(BTCardNetworkLaser),
  88. @"solo": @(BTCardNetworkSolo),
  89. @"switch": @(BTCardNetworkSwitch),
  90. @"uk maestro": @(BTCardNetworkUKMaestro),
  91. @"visa": @(BTCardNetworkVisa),}
  92. orDefault:BTCardNetworkUnknown];
  93. }
  94. + (instancetype)cardNonceWithJSON:(BTJSON *)cardJSON {
  95. BTJSON *authInsightJson;
  96. if ([cardJSON[@"authenticationInsight"] asDictionary]) {
  97. authInsightJson = cardJSON[@"authenticationInsight"];
  98. }
  99. return [[self.class alloc] initWithNonce:[cardJSON[@"nonce"] asString]
  100. description:[cardJSON[@"description"] asString]
  101. cardNetwork:[self.class cardNetworkFromGatewayCardType:[cardJSON[@"details"][@"cardType"] asString]]
  102. expirationMonth:[cardJSON[@"details"][@"expirationMonth"] asString]
  103. expirationYear:[cardJSON[@"details"][@"expirationYear"] asString]
  104. cardholderName:[cardJSON[@"details"][@"cardholderName"] asString]
  105. lastTwo:[cardJSON[@"details"][@"lastTwo"] asString]
  106. lastFour:[cardJSON[@"details"][@"lastFour"] asString]
  107. isDefault:[cardJSON[@"default"] isTrue]
  108. cardJSON:cardJSON
  109. authInsightJSON:authInsightJson];
  110. }
  111. + (instancetype)cardNonceWithGraphQLJSON:(BTJSON *)json {
  112. NSString *lastFour = @"";
  113. if ([json[@"creditCard"][@"last4"] asString]) {
  114. lastFour = [json[@"creditCard"][@"last4"] asString];
  115. }
  116. NSString *lastTwo = lastFour.length == 4 ? [lastFour substringFromIndex:2] : @"";
  117. NSString *description = lastTwo.length > 0 ? [NSString stringWithFormat:@"ending in %@", lastTwo] : @"";
  118. BTJSON *authInsightJson;
  119. if ([json[@"authenticationInsight"] asDictionary]) {
  120. authInsightJson = json[@"authenticationInsight"];
  121. }
  122. return [[self.class alloc] initWithNonce:[json[@"token"] asString]
  123. description:description
  124. cardNetwork:[self.class cardNetworkFromGatewayCardType:[json[@"creditCard"][@"brand"] asString]]
  125. expirationMonth:[json[@"creditCard"][@"expirationMonth"] asString]
  126. expirationYear:[json[@"creditCard"][@"expirationYear"] asString]
  127. cardholderName:[json[@"creditCard"][@"cardholderName"] asString]
  128. lastTwo:lastTwo
  129. lastFour:lastFour
  130. isDefault:NO
  131. cardJSON:json[@"creditCard"]
  132. authInsightJSON:authInsightJson];
  133. }
  134. @end