2016-02-02 17:08:32 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* WooCommerce Credit Card Payment Token.
|
2016-02-02 17:08:32 +00:00
|
|
|
*
|
2016-03-01 19:37:06 +00:00
|
|
|
* Representation of a payment token for credit cards.
|
2016-02-02 17:08:32 +00:00
|
|
|
*
|
|
|
|
* @class WC_Payment_Token_CC
|
2017-03-14 19:21:51 +00:00
|
|
|
* @version 3.0.0
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @category PaymentTokens
|
|
|
|
* @package WooCommerce/PaymentTokens
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2016-02-04 20:45:27 +00:00
|
|
|
class WC_Payment_Token_CC extends WC_Payment_Token {
|
2016-02-02 17:08:32 +00:00
|
|
|
|
2016-03-01 19:37:06 +00:00
|
|
|
/** @protected string Token Type String. */
|
2016-02-08 16:26:57 +00:00
|
|
|
protected $type = 'CC';
|
|
|
|
|
2017-03-14 19:21:51 +00:00
|
|
|
/**
|
|
|
|
* Stores Credit Card payment token data.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $extra_data = array(
|
|
|
|
'last4' => '',
|
|
|
|
'expiry_year' => '',
|
|
|
|
'expiry_month' => '',
|
|
|
|
'card_type' => '',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get type to display to user.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2017-03-15 15:21:02 +00:00
|
|
|
* @param string $deprecated Deprecated since WooCommerce 3.0
|
2017-03-14 19:21:51 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-03-15 15:21:02 +00:00
|
|
|
public function get_display_name( $deprecated = '' ) {
|
2017-03-14 19:21:51 +00:00
|
|
|
/* translators: 1: credit card type 2: last 4 digits 3: expiry month 4: expiry year */
|
|
|
|
$display = sprintf(
|
|
|
|
__( '%1$s ending in %2$s (expires %3$s/%4$s)', 'woocommerce' ),
|
2017-03-15 15:21:02 +00:00
|
|
|
wc_get_credit_card_type_label( $this->get_card_type() ),
|
|
|
|
$this->get_last4(),
|
|
|
|
$this->get_expiry_month(),
|
|
|
|
substr( $this->get_expiry_year(), 2 )
|
2017-03-14 19:21:51 +00:00
|
|
|
);
|
|
|
|
return $display;
|
|
|
|
}
|
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
/**
|
|
|
|
* Hook prefix
|
|
|
|
*
|
2017-03-14 19:21:51 +00:00
|
|
|
* @since 3.0.0
|
2016-11-14 14:20:41 +00:00
|
|
|
*/
|
|
|
|
protected function get_hook_prefix() {
|
|
|
|
return 'woocommerce_payment_token_cc_get_';
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:04:45 +00:00
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Validate credit card payment tokens.
|
2016-02-02 17:08:32 +00:00
|
|
|
*
|
|
|
|
* These fields are required by all credit card payment tokens:
|
2016-02-04 16:23:41 +00:00
|
|
|
* expiry_month - string Expiration date (MM) for the card
|
|
|
|
* expiry_year - string Expiration date (YYYY) for the card
|
|
|
|
* last4 - string Last 4 digits of the card
|
|
|
|
* card_type - string Card type (visa, mastercard, etc)
|
2016-02-02 17:08:32 +00:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2016-02-08 16:26:57 +00:00
|
|
|
* @return boolean True if the passed data is valid
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
2016-02-08 16:26:57 +00:00
|
|
|
public function validate() {
|
|
|
|
if ( false === parent::validate() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-04 20:45:27 +00:00
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
if ( ! $this->get_last4( 'edit' ) ) {
|
2016-02-02 17:08:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
if ( ! $this->get_expiry_year( 'edit' ) ) {
|
2016-02-02 17:08:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
if ( ! $this->get_expiry_month( 'edit' ) ) {
|
2016-02-02 17:08:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
if ( ! $this->get_card_type( 'edit' ) ) {
|
2016-02-02 17:08:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
if ( 4 !== strlen( $this->get_expiry_year( 'edit' ) ) ) {
|
2016-02-08 16:36:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
if ( 2 !== strlen( $this->get_expiry_month( 'edit' ) ) ) {
|
2016-02-08 16:36:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-02 17:08:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Returns the card type (mastercard, visa, ...).
|
2016-11-14 14:20:41 +00:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $context
|
2016-02-02 17:08:32 +00:00
|
|
|
* @return string Card type
|
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function get_card_type( $context = 'view' ) {
|
2017-03-14 19:21:51 +00:00
|
|
|
return $this->get_prop( 'card_type', $context );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Set the card type (mastercard, visa, ...).
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $type
|
|
|
|
*/
|
|
|
|
public function set_card_type( $type ) {
|
2017-03-14 19:21:51 +00:00
|
|
|
$this->set_prop( 'card_type', $type );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Returns the card expiration year (YYYY).
|
2016-11-14 14:20:41 +00:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $context
|
2016-02-02 17:08:32 +00:00
|
|
|
* @return string Expiration year
|
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function get_expiry_year( $context = 'view' ) {
|
2017-03-14 19:21:51 +00:00
|
|
|
return $this->get_prop( 'expiry_year', $context );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Set the expiration year for the card (YYYY format).
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $year
|
|
|
|
*/
|
|
|
|
public function set_expiry_year( $year ) {
|
2017-03-14 19:21:51 +00:00
|
|
|
$this->set_prop( 'expiry_year', $year );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Returns the card expiration month (MM).
|
2016-11-14 14:20:41 +00:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $context
|
2016-02-02 17:08:32 +00:00
|
|
|
* @return string Expiration month
|
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function get_expiry_month( $context = 'view' ) {
|
2017-03-14 19:21:51 +00:00
|
|
|
return $this->get_prop( 'expiry_month', $context );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-01 11:17:10 +00:00
|
|
|
* Set the expiration month for the card (formats into MM format).
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $month
|
|
|
|
*/
|
|
|
|
public function set_expiry_month( $month ) {
|
2017-03-14 19:21:51 +00:00
|
|
|
$this->set_prop( 'expiry_month', str_pad( $month, 2, '0', STR_PAD_LEFT ) );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 21:24:29 +00:00
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Returns the last four digits.
|
2016-11-14 14:20:41 +00:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $context
|
2016-02-04 21:24:29 +00:00
|
|
|
* @return string Last 4 digits
|
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function get_last4( $context = 'view' ) {
|
2017-03-14 19:21:51 +00:00
|
|
|
return $this->get_prop( 'last4', $context );
|
2016-02-04 21:24:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Set the last four digits.
|
2016-02-04 21:24:29 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $last4
|
|
|
|
*/
|
|
|
|
public function set_last4( $last4 ) {
|
2017-03-14 19:21:51 +00:00
|
|
|
$this->set_prop( 'last4', $last4 );
|
2016-02-04 21:24:29 +00:00
|
|
|
}
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|