BTCard.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #import "BTCard_Internal.h"
  2. #if __has_include(<Braintree/BraintreeCard.h>)
  3. #import <Braintree/BraintreeCore.h>
  4. #else
  5. #import <BraintreeCore/BraintreeCore.h>
  6. #endif
  7. @interface BTCard ()
  8. @property (nonatomic, strong, readonly) NSString *cardTokenizationGraphQLMutation;
  9. @end
  10. @implementation BTCard
  11. #pragma mark -
  12. - (NSDictionary *)parameters {
  13. NSMutableDictionary *p = [NSMutableDictionary new];
  14. if (self.number) {
  15. p[@"number"] = self.number;
  16. }
  17. if (self.expirationMonth) {
  18. p[@"expiration_month"] = self.expirationMonth;
  19. }
  20. if (self.expirationYear) {
  21. p[@"expiration_year"] = self.expirationYear;
  22. }
  23. if (self.cardholderName) {
  24. p[@"cardholder_name"] = self.cardholderName;
  25. }
  26. if (self.cvv) {
  27. p[@"cvv"] = self.cvv;
  28. }
  29. NSMutableDictionary *billingAddressDictionary = [NSMutableDictionary new];
  30. if (self.firstName) {
  31. billingAddressDictionary[@"first_name"] = self.firstName;
  32. }
  33. if (self.lastName) {
  34. billingAddressDictionary[@"last_name"] = self.lastName;
  35. }
  36. if (self.company) {
  37. billingAddressDictionary[@"company"] = self.company;
  38. }
  39. if (self.postalCode) {
  40. billingAddressDictionary[@"postal_code"] = self.postalCode;
  41. }
  42. if (self.streetAddress) {
  43. billingAddressDictionary[@"street_address"] = self.streetAddress;
  44. }
  45. if (self.extendedAddress) {
  46. billingAddressDictionary[@"extended_address"] = self.extendedAddress;
  47. }
  48. if (self.locality) {
  49. billingAddressDictionary[@"locality"] = self.locality;
  50. }
  51. if (self.region) {
  52. billingAddressDictionary[@"region"] = self.region;
  53. }
  54. if (self.countryName) {
  55. billingAddressDictionary[@"country_name"] = self.countryName;
  56. }
  57. if (self.countryCodeAlpha2) {
  58. billingAddressDictionary[@"country_code_alpha2"] = self.countryCodeAlpha2;
  59. }
  60. if (self.countryCodeAlpha3) {
  61. billingAddressDictionary[@"country_code_alpha3"] = self.countryCodeAlpha3;
  62. }
  63. if (self.countryCodeNumeric) {
  64. billingAddressDictionary[@"country_code_numeric"] = self.countryCodeNumeric;
  65. }
  66. if (billingAddressDictionary.count > 0) {
  67. p[@"billing_address"] = [billingAddressDictionary copy];
  68. }
  69. p[@"options"] = @{@"validate" : @(self.shouldValidate)};
  70. return [p copy];
  71. }
  72. - (NSDictionary *)graphQLParameters {
  73. NSMutableDictionary *inputDictionary = [NSMutableDictionary new];
  74. NSMutableDictionary *cardDictionary = [NSMutableDictionary new];
  75. inputDictionary[@"creditCard"] = cardDictionary;
  76. if (self.number) {
  77. cardDictionary[@"number"] = self.number;
  78. }
  79. if (self.expirationMonth) {
  80. cardDictionary[@"expirationMonth"] = self.expirationMonth;
  81. }
  82. if (self.expirationYear) {
  83. cardDictionary[@"expirationYear"] = self.expirationYear;
  84. }
  85. if (self.cvv) {
  86. cardDictionary[@"cvv"] = self.cvv;
  87. }
  88. if (self.cardholderName) {
  89. cardDictionary[@"cardholderName"] = self.cardholderName;
  90. }
  91. NSMutableDictionary *billingAddressDictionary = [NSMutableDictionary new];
  92. if (self.firstName) {
  93. billingAddressDictionary[@"firstName"] = self.firstName;
  94. }
  95. if (self.lastName) {
  96. billingAddressDictionary[@"lastName"] = self.lastName;
  97. }
  98. if (self.company) {
  99. billingAddressDictionary[@"company"] = self.company;
  100. }
  101. if (self.postalCode) {
  102. billingAddressDictionary[@"postalCode"] = self.postalCode;
  103. }
  104. if (self.streetAddress) {
  105. billingAddressDictionary[@"streetAddress"] = self.streetAddress;
  106. }
  107. if (self.extendedAddress) {
  108. billingAddressDictionary[@"extendedAddress"] = self.extendedAddress;
  109. }
  110. if (self.locality) {
  111. billingAddressDictionary[@"locality"] = self.locality;
  112. }
  113. if (self.region) {
  114. billingAddressDictionary[@"region"] = self.region;
  115. }
  116. if (self.countryName) {
  117. billingAddressDictionary[@"countryName"] = self.countryName;
  118. }
  119. if (self.countryCodeAlpha2) {
  120. billingAddressDictionary[@"countryCodeAlpha2"] = self.countryCodeAlpha2;
  121. }
  122. if (self.countryCodeAlpha3) {
  123. billingAddressDictionary[@"countryCode"] = self.countryCodeAlpha3;
  124. }
  125. if (self.countryCodeNumeric) {
  126. billingAddressDictionary[@"countryCodeNumeric"] = self.countryCodeNumeric;
  127. }
  128. if (billingAddressDictionary.count > 0) {
  129. cardDictionary[@"billingAddress"] = [billingAddressDictionary copy];
  130. }
  131. inputDictionary[@"options"] = @{@"validate" : @(self.shouldValidate)};
  132. NSMutableDictionary *variables = [@{ @"input": [inputDictionary copy] } mutableCopy];
  133. if (self.authenticationInsightRequested) {
  134. variables[@"authenticationInsightInput"] = self.merchantAccountID ? @{ @"merchantAccountId": self.merchantAccountID } : @{};
  135. }
  136. return @{
  137. @"operationName": @"TokenizeCreditCard",
  138. @"query": self.cardTokenizationGraphQLMutation,
  139. @"variables": variables
  140. };
  141. }
  142. - (NSString *)cardTokenizationGraphQLMutation {
  143. NSMutableString *mutation = [@"mutation TokenizeCreditCard($input: TokenizeCreditCardInput!" mutableCopy];
  144. if (self.authenticationInsightRequested) {
  145. [mutation appendString:@", $authenticationInsightInput: AuthenticationInsightInput!"];
  146. }
  147. [mutation appendString:@""
  148. ") {"
  149. " tokenizeCreditCard(input: $input) {"
  150. " token"
  151. " creditCard {"
  152. " brand"
  153. " expirationMonth"
  154. " expirationYear"
  155. " cardholderName"
  156. " last4"
  157. " bin"
  158. " binData {"
  159. " prepaid"
  160. " healthcare"
  161. " debit"
  162. " durbinRegulated"
  163. " commercial"
  164. " payroll"
  165. " issuingBank"
  166. " countryOfIssuance"
  167. " productId"
  168. " }"
  169. " }"
  170. ];
  171. if (self.authenticationInsightRequested) {
  172. [mutation appendString:@""
  173. " authenticationInsight(input: $authenticationInsightInput) {"
  174. " customerAuthenticationRegulationEnvironment"
  175. " }"
  176. ];
  177. }
  178. [mutation appendString:@""
  179. " }"
  180. "}"
  181. ];
  182. return mutation;
  183. }
  184. @end