CreditCard.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace Braintree;
  3. /**
  4. * Braintree CreditCard module
  5. * Creates and manages Braintree CreditCards
  6. *
  7. * <b>== More information ==</b>
  8. *
  9. * For more detailed information on CreditCards, see {@link https://developers.braintreepayments.com/reference/response/credit-card/php https://developers.braintreepayments.com/reference/response/credit-card/php}<br />
  10. * For more detailed information on CreditCard verifications, see {@link https://developers.braintreepayments.com/reference/response/credit-card-verification/php https://developers.braintreepayments.com/reference/response/credit-card-verification/php}
  11. *
  12. * @package Braintree
  13. * @category Resources
  14. *
  15. * @property-read string $billingAddress
  16. * @property-read string $bin
  17. * @property-read string $cardType
  18. * @property-read string $cardholderName
  19. * @property-read string $createdAt
  20. * @property-read string $customerId
  21. * @property-read string $expirationDate
  22. * @property-read string $expirationMonth
  23. * @property-read string $expirationYear
  24. * @property-read string $imageUrl
  25. * @property-read string $last4
  26. * @property-read string $maskedNumber
  27. * @property-read string $token
  28. * @property-read string $updatedAt
  29. */
  30. class CreditCard extends Base
  31. {
  32. // Card Type
  33. const AMEX = 'American Express';
  34. const CARTE_BLANCHE = 'Carte Blanche';
  35. const CHINA_UNION_PAY = 'China UnionPay';
  36. const DINERS_CLUB_INTERNATIONAL = 'Diners Club';
  37. const DISCOVER = 'Discover';
  38. const JCB = 'JCB';
  39. const LASER = 'Laser';
  40. const MAESTRO = 'Maestro';
  41. const UK_MAESTRO = 'UK Maestro';
  42. const MASTER_CARD = 'MasterCard';
  43. const SOLO = 'Solo';
  44. const SWITCH_TYPE = 'Switch';
  45. const VISA = 'Visa';
  46. const UNKNOWN = 'Unknown';
  47. // Credit card origination location
  48. const INTERNATIONAL = "international";
  49. const US = "us";
  50. const PREPAID_YES = 'Yes';
  51. const PREPAID_NO = 'No';
  52. const PREPAID_UNKNOWN = 'Unknown';
  53. const PAYROLL_YES = 'Yes';
  54. const PAYROLL_NO = 'No';
  55. const PAYROLL_UNKNOWN = 'Unknown';
  56. const HEALTHCARE_YES = 'Yes';
  57. const HEALTHCARE_NO = 'No';
  58. const HEALTHCARE_UNKNOWN = 'Unknown';
  59. const DURBIN_REGULATED_YES = 'Yes';
  60. const DURBIN_REGULATED_NO = 'No';
  61. const DURBIN_REGULATED_UNKNOWN = 'Unknown';
  62. const DEBIT_YES = 'Yes';
  63. const DEBIT_NO = 'No';
  64. const DEBIT_UNKNOWN = 'Unknown';
  65. const COMMERCIAL_YES = 'Yes';
  66. const COMMERCIAL_NO = 'No';
  67. const COMMERCIAL_UNKNOWN = 'Unknown';
  68. const COUNTRY_OF_ISSUANCE_UNKNOWN = "Unknown";
  69. const ISSUING_BANK_UNKNOWN = "Unknown";
  70. const PRODUCT_ID_UNKNOWN = "Unknown";
  71. /* instance methods */
  72. /**
  73. * returns false if default is null or false
  74. *
  75. * @return boolean
  76. */
  77. public function isDefault()
  78. {
  79. return $this->default;
  80. }
  81. /**
  82. * checks whether the card is expired based on the current date
  83. *
  84. * @return boolean
  85. */
  86. public function isExpired()
  87. {
  88. return $this->expired;
  89. }
  90. /**
  91. * checks whether the card is associated with venmo sdk
  92. *
  93. * @return boolean
  94. */
  95. public function isVenmoSdk()
  96. {
  97. return $this->venmoSdk;
  98. }
  99. /**
  100. * sets instance properties from an array of values
  101. *
  102. * @access protected
  103. * @param array $creditCardAttribs array of creditcard data
  104. * @return void
  105. */
  106. protected function _initialize($creditCardAttribs)
  107. {
  108. // set the attributes
  109. $this->_attributes = $creditCardAttribs;
  110. // map each address into its own object
  111. $billingAddress = isset($creditCardAttribs['billingAddress']) ?
  112. Address::factory($creditCardAttribs['billingAddress']) :
  113. null;
  114. $subscriptionArray = [];
  115. if (isset($creditCardAttribs['subscriptions'])) {
  116. foreach ($creditCardAttribs['subscriptions'] AS $subscription) {
  117. $subscriptionArray[] = Subscription::factory($subscription);
  118. }
  119. }
  120. $this->_set('subscriptions', $subscriptionArray);
  121. $this->_set('billingAddress', $billingAddress);
  122. $this->_set('expirationDate', $this->expirationMonth . '/' . $this->expirationYear);
  123. $this->_set('maskedNumber', $this->bin . '******' . $this->last4);
  124. if(isset($creditCardAttribs['verifications']) && count($creditCardAttribs['verifications']) > 0) {
  125. $verifications = $creditCardAttribs['verifications'];
  126. usort($verifications, [$this, '_compareCreatedAtOnVerifications']);
  127. $this->_set('verification', CreditCardVerification::factory($verifications[0]));
  128. }
  129. }
  130. private function _compareCreatedAtOnVerifications($verificationAttrib1, $verificationAttrib2)
  131. {
  132. return ($verificationAttrib2['createdAt'] < $verificationAttrib1['createdAt']) ? -1 : 1;
  133. }
  134. /**
  135. * returns false if comparing object is not a CreditCard,
  136. * or is a CreditCard with a different id
  137. *
  138. * @param object $otherCreditCard customer to compare against
  139. * @return boolean
  140. */
  141. public function isEqual($otherCreditCard)
  142. {
  143. return !($otherCreditCard instanceof self) ? false : $this->token === $otherCreditCard->token;
  144. }
  145. /**
  146. * create a printable representation of the object as:
  147. * ClassName[property=value, property=value]
  148. * @return string
  149. */
  150. public function __toString()
  151. {
  152. return __CLASS__ . '[' .
  153. Util::attributesToString($this->_attributes) .']';
  154. }
  155. /**
  156. * factory method: returns an instance of CreditCard
  157. * to the requesting method, with populated properties
  158. *
  159. * @ignore
  160. * @return CreditCard
  161. */
  162. public static function factory($attributes)
  163. {
  164. $defaultAttributes = [
  165. 'bin' => '',
  166. 'expirationMonth' => '',
  167. 'expirationYear' => '',
  168. 'last4' => '',
  169. ];
  170. $instance = new self();
  171. $instance->_initialize(array_merge($defaultAttributes, $attributes));
  172. return $instance;
  173. }
  174. // static methods redirecting to gateway
  175. public static function create($attribs)
  176. {
  177. return Configuration::gateway()->creditCard()->create($attribs);
  178. }
  179. public static function createNoValidate($attribs)
  180. {
  181. return Configuration::gateway()->creditCard()->createNoValidate($attribs);
  182. }
  183. public static function createFromTransparentRedirect($queryString)
  184. {
  185. return Configuration::gateway()->creditCard()->createFromTransparentRedirect($queryString);
  186. }
  187. public static function createCreditCardUrl()
  188. {
  189. return Configuration::gateway()->creditCard()->createCreditCardUrl();
  190. }
  191. public static function expired()
  192. {
  193. return Configuration::gateway()->creditCard()->expired();
  194. }
  195. public static function fetchExpired($ids)
  196. {
  197. return Configuration::gateway()->creditCard()->fetchExpired($ids);
  198. }
  199. public static function expiringBetween($startDate, $endDate)
  200. {
  201. return Configuration::gateway()->creditCard()->expiringBetween($startDate, $endDate);
  202. }
  203. public static function fetchExpiring($startDate, $endDate, $ids)
  204. {
  205. return Configuration::gateway()->creditCard()->fetchExpiring($startDate, $endDate, $ids);
  206. }
  207. public static function find($token)
  208. {
  209. return Configuration::gateway()->creditCard()->find($token);
  210. }
  211. public static function fromNonce($nonce)
  212. {
  213. return Configuration::gateway()->creditCard()->fromNonce($nonce);
  214. }
  215. public static function credit($token, $transactionAttribs)
  216. {
  217. return Configuration::gateway()->creditCard()->credit($token, $transactionAttribs);
  218. }
  219. public static function creditNoValidate($token, $transactionAttribs)
  220. {
  221. return Configuration::gateway()->creditCard()->creditNoValidate($token, $transactionAttribs);
  222. }
  223. public static function sale($token, $transactionAttribs)
  224. {
  225. return Configuration::gateway()->creditCard()->sale($token, $transactionAttribs);
  226. }
  227. public static function saleNoValidate($token, $transactionAttribs)
  228. {
  229. return Configuration::gateway()->creditCard()->saleNoValidate($token, $transactionAttribs);
  230. }
  231. public static function update($token, $attributes)
  232. {
  233. return Configuration::gateway()->creditCard()->update($token, $attributes);
  234. }
  235. public static function updateNoValidate($token, $attributes)
  236. {
  237. return Configuration::gateway()->creditCard()->updateNoValidate($token, $attributes);
  238. }
  239. public static function updateCreditCardUrl()
  240. {
  241. return Configuration::gateway()->creditCard()->updateCreditCardUrl();
  242. }
  243. public static function updateFromTransparentRedirect($queryString)
  244. {
  245. return Configuration::gateway()->creditCard()->updateFromTransparentRedirect($queryString);
  246. }
  247. public static function delete($token)
  248. {
  249. return Configuration::gateway()->creditCard()->delete($token);
  250. }
  251. /** @return array */
  252. public static function allCardTypes()
  253. {
  254. return [
  255. CreditCard::AMEX,
  256. CreditCard::CARTE_BLANCHE,
  257. CreditCard::CHINA_UNION_PAY,
  258. CreditCard::DINERS_CLUB_INTERNATIONAL,
  259. CreditCard::DISCOVER,
  260. CreditCard::JCB,
  261. CreditCard::LASER,
  262. CreditCard::MAESTRO,
  263. CreditCard::MASTER_CARD,
  264. CreditCard::SOLO,
  265. CreditCard::SWITCH_TYPE,
  266. CreditCard::VISA,
  267. CreditCard::UNKNOWN
  268. ];
  269. }
  270. }
  271. class_alias('Braintree\CreditCard', 'Braintree_CreditCard');