class-wc-payment-token-cc.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Class WC_Payment_Token_CC file.
  4. *
  5. * @package WooCommerce\PaymentTokens
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit; // Exit if accessed directly.
  9. }
  10. /**
  11. * WooCommerce Credit Card Payment Token.
  12. *
  13. * Representation of a payment token for credit cards.
  14. *
  15. * @class WC_Payment_Token_CC
  16. * @version 3.0.0
  17. * @since 2.6.0
  18. * @package WooCommerce/PaymentTokens
  19. */
  20. class WC_Payment_Token_CC extends WC_Payment_Token {
  21. /**
  22. * Token Type String.
  23. *
  24. * @var string
  25. */
  26. protected $type = 'CC';
  27. /**
  28. * Stores Credit Card payment token data.
  29. *
  30. * @var array
  31. */
  32. protected $extra_data = array(
  33. 'last4' => '',
  34. 'expiry_year' => '',
  35. 'expiry_month' => '',
  36. 'card_type' => '',
  37. );
  38. /**
  39. * Get type to display to user.
  40. *
  41. * @since 2.6.0
  42. * @param string $deprecated Deprecated since WooCommerce 3.0.
  43. * @return string
  44. */
  45. public function get_display_name( $deprecated = '' ) {
  46. $display = sprintf(
  47. /* translators: 1: credit card type 2: last 4 digits 3: expiry month 4: expiry year */
  48. __( '%1$s ending in %2$s (expires %3$s/%4$s)', 'woocommerce' ),
  49. wc_get_credit_card_type_label( $this->get_card_type() ),
  50. $this->get_last4(),
  51. $this->get_expiry_month(),
  52. substr( $this->get_expiry_year(), 2 )
  53. );
  54. return $display;
  55. }
  56. /**
  57. * Hook prefix
  58. *
  59. * @since 3.0.0
  60. */
  61. protected function get_hook_prefix() {
  62. return 'woocommerce_payment_token_cc_get_';
  63. }
  64. /**
  65. * Validate credit card payment tokens.
  66. *
  67. * These fields are required by all credit card payment tokens:
  68. * expiry_month - string Expiration date (MM) for the card
  69. * expiry_year - string Expiration date (YYYY) for the card
  70. * last4 - string Last 4 digits of the card
  71. * card_type - string Card type (visa, mastercard, etc)
  72. *
  73. * @since 2.6.0
  74. * @return boolean True if the passed data is valid
  75. */
  76. public function validate() {
  77. if ( false === parent::validate() ) {
  78. return false;
  79. }
  80. if ( ! $this->get_last4( 'edit' ) ) {
  81. return false;
  82. }
  83. if ( ! $this->get_expiry_year( 'edit' ) ) {
  84. return false;
  85. }
  86. if ( ! $this->get_expiry_month( 'edit' ) ) {
  87. return false;
  88. }
  89. if ( ! $this->get_card_type( 'edit' ) ) {
  90. return false;
  91. }
  92. if ( 4 !== strlen( $this->get_expiry_year( 'edit' ) ) ) {
  93. return false;
  94. }
  95. if ( 2 !== strlen( $this->get_expiry_month( 'edit' ) ) ) {
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * Returns the card type (mastercard, visa, ...).
  102. *
  103. * @since 2.6.0
  104. * @param string $context What the value is for. Valid values are view and edit.
  105. * @return string Card type
  106. */
  107. public function get_card_type( $context = 'view' ) {
  108. return $this->get_prop( 'card_type', $context );
  109. }
  110. /**
  111. * Set the card type (mastercard, visa, ...).
  112. *
  113. * @since 2.6.0
  114. * @param string $type Credit card type (mastercard, visa, ...).
  115. */
  116. public function set_card_type( $type ) {
  117. $this->set_prop( 'card_type', $type );
  118. }
  119. /**
  120. * Returns the card expiration year (YYYY).
  121. *
  122. * @since 2.6.0
  123. * @param string $context What the value is for. Valid values are view and edit.
  124. * @return string Expiration year
  125. */
  126. public function get_expiry_year( $context = 'view' ) {
  127. return $this->get_prop( 'expiry_year', $context );
  128. }
  129. /**
  130. * Set the expiration year for the card (YYYY format).
  131. *
  132. * @since 2.6.0
  133. * @param string $year Credit card expiration year.
  134. */
  135. public function set_expiry_year( $year ) {
  136. $this->set_prop( 'expiry_year', $year );
  137. }
  138. /**
  139. * Returns the card expiration month (MM).
  140. *
  141. * @since 2.6.0
  142. * @param string $context What the value is for. Valid values are view and edit.
  143. * @return string Expiration month
  144. */
  145. public function get_expiry_month( $context = 'view' ) {
  146. return $this->get_prop( 'expiry_month', $context );
  147. }
  148. /**
  149. * Set the expiration month for the card (formats into MM format).
  150. *
  151. * @since 2.6.0
  152. * @param string $month Credit card expiration month.
  153. */
  154. public function set_expiry_month( $month ) {
  155. $this->set_prop( 'expiry_month', str_pad( $month, 2, '0', STR_PAD_LEFT ) );
  156. }
  157. /**
  158. * Returns the last four digits.
  159. *
  160. * @since 2.6.0
  161. * @param string $context What the value is for. Valid values are view and edit.
  162. * @return string Last 4 digits
  163. */
  164. public function get_last4( $context = 'view' ) {
  165. return $this->get_prop( 'last4', $context );
  166. }
  167. /**
  168. * Set the last four digits.
  169. *
  170. * @since 2.6.0
  171. * @param string $last4 Credit card last four digits.
  172. */
  173. public function set_last4( $last4 ) {
  174. $this->set_prop( 'last4', $last4 );
  175. }
  176. }