VisaCheckoutCard.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Braintree;
  3. /**
  4. * Braintree VisaCheckoutCard module
  5. * Creates and manages Braintree VisaCheckoutCards
  6. *
  7. * <b>== More information ==</b>
  8. *
  9. * 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}
  10. *
  11. * @package Braintree
  12. * @category Resources
  13. *
  14. * @property-read string $billingAddress
  15. * @property-read string $bin
  16. * @property-read string $callId
  17. * @property-read string $cardType
  18. * @property-read string $cardholderName
  19. * @property-read string $commercial
  20. * @property-read string $countryOfIssuance
  21. * @property-read string $createdAt
  22. * @property-read string $customerId
  23. * @property-read string $customerLocation
  24. * @property-read string $debit
  25. * @property-read string $default
  26. * @property-read string $durbinRegulated
  27. * @property-read string $expirationDate
  28. * @property-read string $expirationMonth
  29. * @property-read string $expirationYear
  30. * @property-read string $expired
  31. * @property-read string $healthcare
  32. * @property-read string $imageUrl
  33. * @property-read string $issuingBank
  34. * @property-read string $last4
  35. * @property-read string $maskedNumber
  36. * @property-read string $payroll
  37. * @property-read string $prepaid
  38. * @property-read string $productId
  39. * @property-read string $subscriptions
  40. * @property-read string $token
  41. * @property-read string $uniqueNumberIdentifier
  42. * @property-read string $updatedAt
  43. */
  44. class VisaCheckoutCard extends Base
  45. {
  46. /* instance methods */
  47. /**
  48. * returns false if default is null or false
  49. *
  50. * @return boolean
  51. */
  52. public function isDefault()
  53. {
  54. return $this->default;
  55. }
  56. /**
  57. * checks whether the card is expired based on the current date
  58. *
  59. * @return boolean
  60. */
  61. public function isExpired()
  62. {
  63. return $this->expired;
  64. }
  65. /**
  66. * sets instance properties from an array of values
  67. *
  68. * @access protected
  69. * @param array $creditCardAttribs array of creditcard data
  70. * @return void
  71. */
  72. protected function _initialize($creditCardAttribs)
  73. {
  74. // set the attributes
  75. $this->_attributes = $creditCardAttribs;
  76. // map each address into its own object
  77. $billingAddress = isset($creditCardAttribs['billingAddress']) ?
  78. Address::factory($creditCardAttribs['billingAddress']) :
  79. null;
  80. $subscriptionArray = [];
  81. if (isset($creditCardAttribs['subscriptions'])) {
  82. foreach ($creditCardAttribs['subscriptions'] AS $subscription) {
  83. $subscriptionArray[] = Subscription::factory($subscription);
  84. }
  85. }
  86. $this->_set('subscriptions', $subscriptionArray);
  87. $this->_set('billingAddress', $billingAddress);
  88. $this->_set('expirationDate', $this->expirationMonth . '/' . $this->expirationYear);
  89. $this->_set('maskedNumber', $this->bin . '******' . $this->last4);
  90. if(isset($creditCardAttribs['verifications']) && count($creditCardAttribs['verifications']) > 0) {
  91. $verifications = $creditCardAttribs['verifications'];
  92. usort($verifications, [$this, '_compareCreatedAtOnVerifications']);
  93. $this->_set('verification', CreditCardVerification::factory($verifications[0]));
  94. }
  95. }
  96. private function _compareCreatedAtOnVerifications($verificationAttrib1, $verificationAttrib2)
  97. {
  98. return ($verificationAttrib2['createdAt'] < $verificationAttrib1['createdAt']) ? -1 : 1;
  99. }
  100. /**
  101. * returns false if comparing object is not a VisaCheckoutCard,
  102. * or is a VisaCheckoutCard with a different id
  103. *
  104. * @param object $otherVisaCheckoutCard customer to compare against
  105. * @return boolean
  106. */
  107. public function isEqual($otherVisaCheckoutCard)
  108. {
  109. return !($otherVisaCheckoutCard instanceof self) ? false : $this->token === $otherVisaCheckoutCard->token;
  110. }
  111. /**
  112. * create a printable representation of the object as:
  113. * ClassName[property=value, property=value]
  114. * @return string
  115. */
  116. public function __toString()
  117. {
  118. return __CLASS__ . '[' .
  119. Util::attributesToString($this->_attributes) .']';
  120. }
  121. /**
  122. * factory method: returns an instance of VisaCheckoutCard
  123. * to the requesting method, with populated properties
  124. *
  125. * @ignore
  126. * @return VisaCheckoutCard
  127. */
  128. public static function factory($attributes)
  129. {
  130. $defaultAttributes = [
  131. 'bin' => '',
  132. 'expirationMonth' => '',
  133. 'expirationYear' => '',
  134. 'last4' => '',
  135. ];
  136. $instance = new self();
  137. $instance->_initialize(array_merge($defaultAttributes, $attributes));
  138. return $instance;
  139. }
  140. }
  141. class_alias('Braintree\VisaCheckoutCard', 'Braintree_VisaCheckoutCard');