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 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.
|
|
|
|
*
|
|
|
|
* @class WC_Payment_Token
|
|
|
|
* @since 2.6.0
|
|
|
|
* @package WooCommerce/Abstracts
|
|
|
|
* @category Abstract Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2016-03-18 18:24:06 +00:00
|
|
|
abstract class WC_Payment_Token extends WC_Data {
|
2016-02-02 17:08:32 +00:00
|
|
|
|
2016-03-18 18:24:06 +00:00
|
|
|
/**
|
|
|
|
* Token Data (stored in the payment_tokens table).
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_data = array(
|
|
|
|
'id' => 0,
|
|
|
|
'gateway_id' => '',
|
|
|
|
'token' => '',
|
|
|
|
'is_default' => 0,
|
|
|
|
'user_id' => 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Meta type. Payment tokens are a new object type.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_meta_type = 'payment_token';
|
2016-02-02 17:08:32 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Initialize a payment token.
|
2016-02-02 17:08:32 +00:00
|
|
|
*
|
2016-02-04 16:14:19 +00:00
|
|
|
* These fields are accepted by all payment tokens:
|
2016-03-18 18:24:06 +00:00
|
|
|
* is_default - boolean Optional - Indicates this is the default payment token for a user
|
2016-02-04 16:14:19 +00:00
|
|
|
* token - string Required - The actual token to store
|
|
|
|
* gateway_id - string Required - Identifier for the gateway this token is associated with
|
2016-02-04 20:56:29 +00:00
|
|
|
* user_id - int Optional - ID for the user this token is associated with. 0 if this token is not associated with a user
|
2016-02-02 17:08:32 +00:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2016-03-18 18:24:06 +00:00
|
|
|
* @param mixed $token
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
2016-03-18 18:24:06 +00:00
|
|
|
public function __construct( $token = '' ) {
|
|
|
|
if ( is_numeric( $token ) ) {
|
|
|
|
$this->read( $token );
|
2016-03-23 00:36:24 +00:00
|
|
|
} else if ( is_object( $token ) ) {
|
|
|
|
$token_id = $token->get_id();
|
|
|
|
if ( ! empty( $token_id ) ) {
|
|
|
|
$this->read( $token->get_id() );
|
|
|
|
}
|
2016-03-18 18:24:06 +00:00
|
|
|
}
|
|
|
|
// Set token type (cc, echeck)
|
|
|
|
if ( ! empty( $this->type ) ) {
|
|
|
|
$this->_data['type'] = $this->type;
|
|
|
|
}
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Returns the payment token ID.
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
2016-02-08 16:26:57 +00:00
|
|
|
* @return ID Token ID
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
|
|
|
public function get_id() {
|
2016-03-18 18:24:06 +00:00
|
|
|
return absint( $this->_data['id'] );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Returns the raw payment token.
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @return string Raw token
|
|
|
|
*/
|
|
|
|
public function get_token() {
|
2016-03-18 18:24:06 +00:00
|
|
|
return $this->_data['token'];
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Set the raw payment token.
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $token
|
|
|
|
*/
|
|
|
|
public function set_token( $token ) {
|
2016-03-18 18:24:06 +00:00
|
|
|
$this->_data['token'] = $token;
|
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).
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
2016-02-08 16:36:27 +00:00
|
|
|
* @return string Payment Token Type (CC, eCheck)
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
|
|
|
public function get_type() {
|
2016-03-18 18:24:06 +00:00
|
|
|
return isset( $this->_data['type'] ) ? $this->_data['type'] : '';
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 10:55:11 +00:00
|
|
|
/**
|
|
|
|
* Get type to display to user.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-05-26 11:42:29 +00:00
|
|
|
public function get_display_name() {
|
2016-05-26 10:55:11 +00:00
|
|
|
return $this->get_type();
|
|
|
|
}
|
|
|
|
|
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-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
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-02-04 20:56:29 +00:00
|
|
|
public function get_user_id() {
|
2016-03-18 18:24:06 +00:00
|
|
|
return ( isset( $this->_data['user_id'] ) && $this->_data['user_id'] > 0 ) ? absint( $this->_data['user_id'] ) : 0;
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +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
|
2016-02-04 20:56:29 +00:00
|
|
|
* @param int $user_id
|
2016-02-02 17:08:32 +00:00
|
|
|
*/
|
2016-02-04 20:56:29 +00:00
|
|
|
public function set_user_id( $user_id ) {
|
2016-03-18 18:24:06 +00:00
|
|
|
$this->_data['user_id'] = absint( $user_id );
|
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-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @return string Gateway ID
|
|
|
|
*/
|
|
|
|
public function get_gateway_id() {
|
2016-03-18 18:24:06 +00:00
|
|
|
return $this->_data['gateway_id'];
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Set the gateway ID.
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @param string $gateway_id
|
|
|
|
*/
|
|
|
|
public function set_gateway_id( $gateway_id ) {
|
2016-03-18 18:24:06 +00:00
|
|
|
$this->_data['gateway_id'] = $gateway_id;
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Returns if the token is marked as default.
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @return boolean True if the token is default
|
|
|
|
*/
|
|
|
|
public function is_default() {
|
2016-03-18 18:24:06 +00:00
|
|
|
return ! empty( $this->_data['is_default'] );
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Marks the payment as default or non-default.
|
2016-02-02 17:08:32 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @param boolean $is_default True or false
|
|
|
|
*/
|
|
|
|
public function set_default( $is_default ) {
|
2016-03-18 18:24:06 +00:00
|
|
|
$this->_data['is_default'] = (bool) $is_default;
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 15:37:03 +00:00
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Validate basic token info (token and type are required).
|
2016-02-08 16:26:57 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @return boolean True if the passed data is valid
|
2016-02-08 15:37:03 +00:00
|
|
|
*/
|
2016-02-08 16:26:57 +00:00
|
|
|
public function validate() {
|
2016-03-18 18:24:06 +00:00
|
|
|
if ( empty( $this->_data['token'] ) ) {
|
2016-02-08 16:26:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-18 18:24:06 +00:00
|
|
|
if ( empty( $this->_data['type'] ) ) {
|
2016-02-08 16:26:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-08 15:37:03 +00:00
|
|
|
|
2016-02-08 16:26:57 +00:00
|
|
|
return true;
|
2016-02-08 15:37:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 16:26:57 +00:00
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Get a token from the database.
|
2016-02-08 16:26:57 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @param int $token_id Token ID
|
|
|
|
*/
|
|
|
|
public function read( $token_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
if ( $token = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token_id = %d LIMIT 1;", $token_id ) ) ) {
|
2016-03-18 18:24:06 +00:00
|
|
|
$token_id = $token->token_id;
|
2016-02-08 16:26:57 +00:00
|
|
|
$token = (array) $token;
|
|
|
|
unset( $token['token_id'] );
|
2016-03-18 18:24:06 +00:00
|
|
|
$this->_data = $token;
|
|
|
|
$this->_data['id'] = $token_id;
|
|
|
|
$this->read_meta_data();
|
2016-02-08 16:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Update a payment token.
|
2016-02-08 16:26:57 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @return True on success, false if validation failed and a payment token could not be updated
|
|
|
|
*/
|
2016-02-08 15:37:03 +00:00
|
|
|
public function update() {
|
2016-02-08 16:26:57 +00:00
|
|
|
if ( false === $this->validate() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
global $wpdb;
|
2016-02-08 15:37:03 +00:00
|
|
|
|
2016-03-18 18:24:06 +00:00
|
|
|
$payment_token_data = array(
|
|
|
|
'gateway_id' => $this->get_gateway_id(),
|
|
|
|
'token' => $this->get_token(),
|
|
|
|
'user_id' => $this->get_user_id(),
|
|
|
|
'type' => $this->get_type(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$wpdb->update(
|
|
|
|
$wpdb->prefix . 'woocommerce_payment_tokens',
|
|
|
|
$payment_token_data,
|
|
|
|
array( 'token_id' => $this->get_id() )
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->save_meta_data();
|
2016-02-08 16:26:57 +00:00
|
|
|
|
2016-03-10 18:19:08 +00:00
|
|
|
// Make sure all other tokens are not set to default
|
|
|
|
if ( $this->is_default() && $this->get_user_id() > 0 ) {
|
|
|
|
WC_Payment_Tokens::set_users_default( $this->get_user_id(), $this->get_id() );
|
|
|
|
}
|
|
|
|
|
2016-02-08 16:26:57 +00:00
|
|
|
do_action( 'woocommerce_payment_token_updated', $this->get_id() );
|
|
|
|
return true;
|
2016-02-08 15:37:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 16:26:57 +00:00
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Create a new payment token in the database.
|
2016-02-08 16:26:57 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @return True on success, false if validation failed and a payment token could not be created
|
|
|
|
*/
|
2016-02-08 15:37:03 +00:00
|
|
|
public function create() {
|
2016-02-08 16:26:57 +00:00
|
|
|
if ( false === $this->validate() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
global $wpdb;
|
2016-03-01 19:37:06 +00:00
|
|
|
// Are there any other tokens? If not, set this token as default
|
2016-03-10 18:19:08 +00:00
|
|
|
if ( ! $this->is_default() && $this->get_user_id() > 0 ) {
|
|
|
|
$default_token = WC_Payment_Tokens::get_customer_default_token( $this->get_user_id() );
|
2016-03-01 19:37:06 +00:00
|
|
|
if ( is_null( $default_token ) ) {
|
|
|
|
$this->set_default( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 18:24:06 +00:00
|
|
|
$payment_token_data = array(
|
|
|
|
'gateway_id' => $this->get_gateway_id(),
|
|
|
|
'token' => $this->get_token(),
|
|
|
|
'user_id' => $this->get_user_id(),
|
|
|
|
'type' => $this->get_type(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$wpdb->insert( $wpdb->prefix . 'woocommerce_payment_tokens', $payment_token_data );
|
|
|
|
$this->_data['id'] = $token_id = $wpdb->insert_id;
|
|
|
|
$this->save_meta_data();
|
2016-02-08 16:26:57 +00:00
|
|
|
|
2016-03-10 18:19:08 +00:00
|
|
|
// Make sure all other tokens are not set to default
|
|
|
|
if ( $this->is_default() && $this->get_user_id() > 0 ) {
|
|
|
|
WC_Payment_Tokens::set_users_default( $this->get_user_id(), $token_id );
|
|
|
|
}
|
|
|
|
|
2016-02-08 16:26:57 +00:00
|
|
|
do_action( 'woocommerce_payment_token_created', $token_id );
|
|
|
|
return true;
|
2016-02-08 15:37:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 16:26:57 +00:00
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Saves a payment token to the database - does not require you to know if this is a new token or an update token.
|
2016-02-08 16:26:57 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @return True on success, false if validation failed and a payment token could not be saved
|
|
|
|
*/
|
2016-02-08 15:37:03 +00:00
|
|
|
public function save() {
|
2016-02-08 16:26:57 +00:00
|
|
|
if ( $this->get_id() > 0 ) {
|
|
|
|
return $this->update();
|
|
|
|
} else {
|
|
|
|
return $this->create();
|
|
|
|
}
|
2016-02-08 15:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 19:37:06 +00:00
|
|
|
* Remove a payment token from the database.
|
|
|
|
* @since 2.6.0
|
2016-02-08 15:37:03 +00:00
|
|
|
*/
|
|
|
|
public function delete() {
|
|
|
|
global $wpdb;
|
2016-02-08 16:26:57 +00:00
|
|
|
$this->read( $this->get_id() ); // Make sure we have a token to return after deletion
|
2016-02-08 15:37:03 +00:00
|
|
|
$wpdb->delete( $wpdb->prefix . 'woocommerce_payment_tokens', array( 'token_id' => $this->get_id() ), array( '%d' ) );
|
|
|
|
$wpdb->delete( $wpdb->prefix . 'woocommerce_payment_tokenmeta', array( 'payment_token_id' => $this->get_id() ), array( '%d' ) );
|
|
|
|
do_action( 'woocommerce_payment_token_deleted', $this->get_id(), $this );
|
|
|
|
}
|
|
|
|
|
2016-02-02 17:08:32 +00:00
|
|
|
}
|