Implement the rest of the WC_Data methods for creating, updating, reading, etc.

This commit is contained in:
Justin Shreve 2016-02-08 08:26:57 -08:00
parent c87c99f847
commit 214f077a91
3 changed files with 118 additions and 37 deletions

View File

@ -30,7 +30,6 @@ if ( ! defined( 'ABSPATH' ) ) {
*
* These fields are accepted by all payment tokens:
* default - boolean Optional - Indicates this is the default payment token for a user
* type - string Required - WC core ships with 'cc' or 'echeck' but other values can be used for custom payment token types
* 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
@ -40,9 +39,10 @@ if ( ! defined( 'ABSPATH' ) ) {
* @param array $data Core token data
* @param array $meta Meta token data
*/
public function __construct( $id, $data, $meta ) {
public function __construct( $id = 0, $data = array(), $meta = array() ) {
$this->id = $id;
$this->data = $data;
$this->data['type'] = $this->type;
$this->meta = $meta;
}
@ -50,7 +50,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* Returns the payment token ID
*
* @since 2.6.0
* @return string Token ID
* @return ID Token ID
*/
public function get_id() {
return $this->id;
@ -83,27 +83,17 @@ if ( ! defined( 'ABSPATH' ) ) {
* @return string Payment token type
*/
public function get_type() {
return isset( $this->data['type'] ) ? $this->data['type'] : null;
}
/**
* Set the payment token type
*
* @since 2.6.0
* @param string $type
*/
public function set_type( $type ) {
$this->data['type'] = $type;
return isset( $this->data['type'] ) ? $this->data['type'] : '';
}
/**
* Returns the user ID associated with the token or false if this token is not associated
*
* @since 2.6.0
* @return mixed User ID if this token is associated with a user or false.
* @return int User ID if this token is associated with a user or 0 if no user is associated
*/
public function get_user_id() {
return ( $this->data['user_id'] > 0 ) ? $this->data['user_id'] : false;
return ( $this->data['user_id'] > 0 ) ? $this->data['user_id'] : 0;
}
/**
@ -143,7 +133,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @return boolean True if the token is default
*/
public function is_default() {
return isset( $this->data['is_default'] ) && $this->data['is_default'];
return ! empty( $this->data['is_default'] );
}
/**
@ -153,7 +143,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @param boolean $is_default True or false
*/
public function set_default( $is_default ) {
$this->data['is_default'] = $is_default;
$this->data['is_default'] = (bool) $is_default;
}
/**
@ -167,32 +157,112 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Get this payment token from the database.
* @param int $token_id Token ID
* @return class Database row
* Validate basic token info (token and type are required)
*
* @since 2.6.0
* @return boolean True if the passed data is valid
*/
public function validate() {
if ( empty( $this->data['token'] ) ) {
return false;
}
if ( empty( $this->data['type'] ) ) {
return false;
}
return true;
}
/**
* Get a token from the database
*
* @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 ) ) ) {
$this->id = $token->token_id;
$token = (array) $token;
unset( $token['token_id'] );
$this->data = $token;
$meta = get_metadata( 'payment_token', $token_id );
$passed_meta = array();
if ( ! empty( $meta ) ) {
foreach( $meta as $meta_key => $meta_value ) {
$passed_meta[ $meta_key ] = $meta_value[0];
}
}
$this->meta = $passed_meta;
}
}
/**
* Update a payment token
*
* @since 2.6.0
* @return True on success, false if validation failed and a payment token could not be updated
*/
public function update() {
if ( false === $this->validate() ) {
return false;
}
global $wpdb;
$wpdb->update( $wpdb->prefix . 'woocommerce_payment_tokens', $this->data, array( 'token_id' => $this->get_id() ) );
foreach ( $this->meta as $meta_key => $meta_value ) {
update_metadata( 'payment_token', $this->get_id(), $meta_key, $meta_value );
}
do_action( 'woocommerce_payment_token_updated', $this->get_id() );
return true;
}
/**
* Create a new payment token in the database
*
* @since 2.6.0
* @return True on success, false if validation failed and a payment token could not be created
*/
public function create() {
if ( false === $this->validate() ) {
return false;
}
global $wpdb;
$wpdb->insert( $wpdb->prefix . 'woocommerce_payment_tokens', $this->data );
$this->id = $token_id = $wpdb->insert_id;
foreach ( $this->meta as $meta_key => $meta_value ) {
add_metadata( 'payment_token', $token_id, $meta_key, $meta_value, true );
}
do_action( 'woocommerce_payment_token_created', $token_id );
return true;
}
/**
* Saves a payment token to the database - does not require you to know if this is a new token or an update token
*
* @since 2.6.0
* @return True on success, false if validation failed and a payment token could not be saved
*/
public function save() {
if ( $this->get_id() > 0 ) {
return $this->update();
} else {
return $this->create();
}
}
/**
* Remove a payment token from the database
* @param int $token_id Token ID
*/
public function delete() {
global $wpdb;
$this->read( $this->get_id() ); // Make sure we have a token to return after deletion
$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 );

View File

@ -18,6 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class WC_Payment_Token_CC extends WC_Payment_Token {
/** @protected string Token Type String */
protected $type = 'CC';
/**
* Validate credit card payment tokens
*
@ -28,24 +31,26 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
* card_type - string Card type (visa, mastercard, etc)
*
* @since 2.6.0
* @param array $args Data to validate
* @return boolean True if the passed data is valid
* @return boolean True if the passed data is valid
*/
public static function validate( $args ) {
if ( empty( $args['meta']['last4'] ) ) {
public function validate() {
if ( false === parent::validate() ) {
return false;
}
if ( empty( $args['meta']['expiry_year'] ) ) {
if ( empty( $this->meta['last4'] ) ) {
return false;
}
if ( empty( $args['meta']['expiry_month'] ) ) {
if ( empty( $this->meta['expiry_year'] ) ) {
return false;
}
if ( empty ( $args['meta']['card_type'] ) ) {
if ( empty( $this->meta['expiry_month'] ) ) {
return false;
}
if ( empty ( $this->meta['card_type'] ) ) {
return false;
}

View File

@ -17,18 +17,24 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class WC_Payment_Token_eCheck extends WC_Payment_Token {
/** @protected string Token Type String */
protected $type = 'eCheck';
/**
* Validate eCheck payment tokens.
*
* These fields are required by all credit card payment tokens:
* These fields are required by all eCheck payment tokens:
* last4 - string Last 4 digits of the check
*
* @since 2.6.0
* @param array $args Data to validate
* @return boolean True if the passed data is valid
* @return boolean True if the passed data is valid
*/
public static function validate( $args ) {
if ( empty( $args['meta']['last4'] ) ) {
public static function validate() {
if ( false === parent::validate() ) {
return false;
}
if ( empty( $this->meta['last4'] ) ) {
return false;
}
return true;