2016-02-02 17:08:32 +00:00
|
|
|
<?php
|
2018-03-05 11:54:54 +00:00
|
|
|
/**
|
|
|
|
* Abstract payment tokens
|
|
|
|
*
|
2021-03-05 09:19:58 +00:00
|
|
|
* Generic payment tokens functionality which can be extended by individual types of payment tokens.
|
2018-03-05 11:54:54 +00:00
|
|
|
*
|
|
|
|
* @class WC_Payment_Token
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Abstracts
|
2018-03-05 11:54:54 +00:00
|
|
|
*/
|
|
|
|
|
2016-02-02 17:08:32 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2016-11-14 14:20:41 +00:00
|
|
|
exit;
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 11:54:54 +00:00
|
|
|
require_once WC_ABSPATH . 'includes/legacy/abstract-wc-legacy-payment-token.php';
|
2017-02-16 16:39:56 +00:00
|
|
|
|
2016-02-02 17:08:32 +00:00
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* WooCommerce Payment Token.
|
2016-02-02 17:08:32 +00:00
|
|
|
*
|
|
|
|
* Representation of a general payment token to be extended by individuals types of tokens
|
|
|
|
* examples: Credit Card, eCheck.
|
|
|
|
*
|
2018-03-05 11:54:54 +00:00
|
|
|
* @class WC_Payment_Token
|
2017-03-14 19:21:51 +00:00
|
|
|
* @version 3.0.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @since 2.6.0
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Abstracts
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
2018-03-05 11:54:54 +00:00
|
|
|
abstract class WC_Payment_Token extends WC_Legacy_Payment_Token {
|
2016-02-02 17:08:32 +00:00
|
|
|
|
2016-06-06 19:04:45 +00:00
|
|
|
/**
|
|
|
|
* Token Data (stored in the payment_tokens table).
|
2018-03-05 11:54:54 +00:00
|
|
|
*
|
2016-06-06 19:04:45 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $data = array(
|
2018-03-05 11:54:54 +00:00
|
|
|
'gateway_id' => '',
|
|
|
|
'token' => '',
|
|
|
|
'is_default' => false,
|
|
|
|
'user_id' => 0,
|
|
|
|
'type' => '',
|
2016-06-06 19:04:45 +00:00
|
|
|
);
|
2016-03-18 18:24:06 +00:00
|
|
|
|
2017-03-14 19:21:51 +00:00
|
|
|
/**
|
|
|
|
* Token Type (CC, eCheck, or a custom type added by an extension).
|
|
|
|
* Set by child classes.
|
2018-03-05 11:54:54 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2017-03-14 19:21:51 +00:00
|
|
|
*/
|
|
|
|
protected $type = '';
|
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
/**
|
|
|
|
* Initialize a payment token.
|
|
|
|
*
|
|
|
|
* These fields are accepted by all payment tokens:
|
|
|
|
* is_default - boolean Optional - Indicates this is the default payment token for a user
|
|
|
|
* token - string Required - The actual token to store
|
|
|
|
* gateway_id - string Required - Identifier for the gateway this token is associated with
|
|
|
|
* user_id - int Optional - ID for the user this token is associated with. 0 if this token is not associated with a user
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param mixed $token Token.
|
2016-11-14 14:20:41 +00:00
|
|
|
*/
|
2016-03-18 18:24:06 +00:00
|
|
|
public function __construct( $token = '' ) {
|
2017-03-14 19:21:51 +00:00
|
|
|
parent::__construct( $token );
|
2016-11-14 14:20:41 +00:00
|
|
|
|
2016-03-18 18:24:06 +00:00
|
|
|
if ( is_numeric( $token ) ) {
|
2016-11-14 14:20:41 +00:00
|
|
|
$this->set_id( $token );
|
2016-09-02 02:40:36 +00:00
|
|
|
} elseif ( is_object( $token ) ) {
|
2016-03-23 00:36:24 +00:00
|
|
|
$token_id = $token->get_id();
|
|
|
|
if ( ! empty( $token_id ) ) {
|
2016-11-14 14:20:41 +00:00
|
|
|
$this->set_id( $token->get_id() );
|
2016-03-23 00:36:24 +00:00
|
|
|
}
|
2016-11-14 14:20:41 +00:00
|
|
|
} else {
|
|
|
|
$this->set_object_read( true );
|
2016-03-18 18:24:06 +00:00
|
|
|
}
|
2016-11-14 14:20:41 +00:00
|
|
|
|
|
|
|
$this->data_store = WC_Data_Store::load( 'payment-token' );
|
|
|
|
if ( $this->get_id() > 0 ) {
|
|
|
|
$this->data_store->read( $this );
|
2016-03-18 18:24:06 +00:00
|
|
|
}
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
/*
|
2018-03-05 11:54:54 +00:00
|
|
|
*--------------------------------------------------------------------------
|
|
|
|
* Getters
|
|
|
|
*--------------------------------------------------------------------------
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2016-11-14 14:20:41 +00:00
|
|
|
* Returns the raw payment token.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param string $context Context in which to call this.
|
2016-11-14 14:20:41 +00:00
|
|
|
* @return string Raw token
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function get_token( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'token', $context );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Returns the type of this payment token (CC, eCheck, or something else).
|
2017-03-14 19:21:51 +00:00
|
|
|
* Overwritten by child classes.
|
2016-11-14 14:20:41 +00:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param string $deprecated Deprecated since WooCommerce 3.0.
|
2016-02-08 16:36:27 +00:00
|
|
|
* @return string Payment Token Type (CC, eCheck)
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
2017-03-14 19:21:51 +00:00
|
|
|
public function get_type( $deprecated = '' ) {
|
|
|
|
return $this->type;
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 10:55:11 +00:00
|
|
|
/**
|
|
|
|
* Get type to display to user.
|
2016-11-14 14:20:41 +00:00
|
|
|
* Get's overwritten by child classes.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param string $deprecated Deprecated since WooCommerce 3.0.
|
2016-05-26 10:55:11 +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
|
|
|
return $this->get_type();
|
2016-05-26 10:55:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 17:08:32 +00:00
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Returns the user ID associated with the token or false if this token is not associated.
|
2016-11-14 14:20:41 +00:00
|
|
|
*
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param string $context In what context to execute this.
|
2016-02-08 16:26:57 +00:00
|
|
|
* @return int User ID if this token is associated with a user or 0 if no user is associated
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function get_user_id( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'user_id', $context );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 14:20:41 +00:00
|
|
|
* Returns the ID of the gateway associated with this payment token.
|
|
|
|
*
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param string $context In what context to execute this.
|
2016-11-14 14:20:41 +00:00
|
|
|
* @return string Gateway ID
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function get_gateway_id( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'gateway_id', $context );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Returns the ID of the gateway associated with this payment token.
|
2016-11-14 14:20:41 +00:00
|
|
|
*
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param string $context In what context to execute this.
|
2016-02-02 17:08:32 +00:00
|
|
|
* @return string Gateway ID
|
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function get_is_default( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'is_default', $context );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Setters
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2016-02-02 17:08:32 +00:00
|
|
|
/**
|
2016-11-14 14:20:41 +00:00
|
|
|
* Set the raw payment token.
|
|
|
|
*
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param string $token Payment token.
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function set_token( $token ) {
|
|
|
|
$this->set_prop( 'token', $token );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 14:20:41 +00:00
|
|
|
* Set the user ID for the user associated with this order.
|
|
|
|
*
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param int $user_id User ID.
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function set_user_id( $user_id ) {
|
|
|
|
$this->set_prop( 'user_id', absint( $user_id ) );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 15:37:03 +00:00
|
|
|
/**
|
2016-11-14 14:20:41 +00:00
|
|
|
* Set the gateway ID.
|
|
|
|
*
|
2016-02-08 16:26:57 +00:00
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param string $gateway_id Gateway ID.
|
2016-02-08 15:37:03 +00:00
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function set_gateway_id( $gateway_id ) {
|
2016-11-17 20:07:58 +00:00
|
|
|
$this->set_prop( 'gateway_id', $gateway_id );
|
2016-02-08 15:37:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 16:26:57 +00:00
|
|
|
/**
|
2016-11-14 14:20:41 +00:00
|
|
|
* Marks the payment as default or non-default.
|
2018-03-05 11:54:54 +00:00
|
|
|
*
|
2016-02-08 16:26:57 +00:00
|
|
|
* @since 2.6.0
|
2018-03-05 11:54:54 +00:00
|
|
|
* @param boolean $is_default True or false.
|
2016-02-08 16:26:57 +00:00
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function set_default( $is_default ) {
|
|
|
|
$this->set_prop( 'is_default', (bool) $is_default );
|
2016-02-08 16:26:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 14:20:41 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Other Methods
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2016-02-08 16:26:57 +00:00
|
|
|
/**
|
2016-11-14 14:20:41 +00:00
|
|
|
* Returns if the token is marked as default.
|
|
|
|
*
|
2016-02-08 16:26:57 +00:00
|
|
|
* @since 2.6.0
|
2016-11-14 14:20:41 +00:00
|
|
|
* @return boolean True if the token is default
|
2016-02-08 16:26:57 +00:00
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function is_default() {
|
|
|
|
return (bool) $this->get_prop( 'is_default', 'view' );
|
2016-02-08 15:37:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 16:26:57 +00:00
|
|
|
/**
|
2016-11-14 14:20:41 +00:00
|
|
|
* Validate basic token info (token and type are required).
|
2018-03-05 11:54:54 +00:00
|
|
|
*
|
2016-02-08 16:26:57 +00:00
|
|
|
* @since 2.6.0
|
2016-11-14 14:20:41 +00:00
|
|
|
* @return boolean True if the passed data is valid
|
2016-02-08 16:26:57 +00:00
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function validate() {
|
|
|
|
$token = $this->get_prop( 'token', 'edit' );
|
|
|
|
if ( empty( $token ) ) {
|
2016-02-08 16:26:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-02-08 15:37:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|