WC_Payment_Token_CC

WooCommerce Credit Card Payment Token.

Defined (1)

The class is defined in the following location(s).

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