Make sure the is_default/default token ability works correctly. Also adds a new method for getting a users default token. Fixes up some docblocks.

This commit is contained in:
Justin Shreve 2016-03-01 11:37:06 -08:00
parent 0985292a79
commit 49c45c6dc1
6 changed files with 137 additions and 86 deletions

View File

@ -5,7 +5,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* WooCommerce Payment Token
* WooCommerce Payment Token.
*
* Representation of a general payment token to be extended by individuals types of tokens
* examples: Credit Card, eCheck.
@ -18,15 +18,15 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
abstract class WC_Payment_Token implements WC_Data {
/** @protected int Token ID */
/** @protected int Token ID. */
protected $id;
/** @protected array Core Token Data (stored in the payment_tokens table) */
/** @protected array Core Token Data (stored in the payment_tokens table). */
protected $data;
/** @protected array Meta Token Data (extra data associated with a payment token, stored in the payment_token_meta table) */
/** @protected array Meta Token Data (extra data associated with a payment token, stored in the payment_token_meta table). */
protected $meta;
/**
* Initialize a payment token
* Initialize a payment token.
*
* These fields are accepted by all payment tokens:
* default - boolean Optional - Indicates this is the default payment token for a user
@ -47,8 +47,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Returns the payment token ID
*
* Returns the payment token ID.
* @since 2.6.0
* @return ID Token ID
*/
@ -57,8 +56,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Returns the raw payment token
*
* Returns the raw payment token.
* @since 2.6.0
* @return string Raw token
*/
@ -67,8 +65,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Set the raw payment token
*
* Set the raw payment token.
* @since 2.6.0
* @param string $token
*/
@ -77,8 +74,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Returns the type of this payment token (CC, eCheck, or something else)
*
* Returns the type of this payment token (CC, eCheck, or something else).
* @since 2.6.0
* @return string Payment Token Type (CC, eCheck)
*/
@ -87,8 +83,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Returns the user ID associated with the token or false if this token is not associated
*
* Returns the user ID associated with the token or false if this token is not associated.
* @since 2.6.0
* @return int User ID if this token is associated with a user or 0 if no user is associated
*/
@ -97,8 +92,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Set the user ID for the user associated with this order
*
* Set the user ID for the user associated with this order.
* @since 2.6.0
* @param int $user_id
*/
@ -107,8 +101,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Returns the ID of the gateway associated with this payment token
*
* Returns the ID of the gateway associated with this payment token.
* @since 2.6.0
* @return string Gateway ID
*/
@ -117,8 +110,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Set the gateway ID
*
* Set the gateway ID.
* @since 2.6.0
* @param string $gateway_id
*/
@ -127,8 +119,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Returns if the token is marked as default
*
* Returns if the token is marked as default.
* @since 2.6.0
* @return boolean True if the token is default
*/
@ -137,8 +128,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Marks the payment as default or non-default
*
* Marks the payment as default or non-default.
* @since 2.6.0
* @param boolean $is_default True or false
*/
@ -147,8 +137,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Returns a dump of the token data (combined data and meta)
*
* Returns a dump of the token data (combined data and meta).
* @since 2.6.0
* @return mixed array representation
*/
@ -157,8 +146,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Validate basic token info (token and type are required)
*
* Validate basic token info (token and type are required).
* @since 2.6.0
* @return boolean True if the passed data is valid
*/
@ -175,8 +163,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Get a token from the database
*
* Get a token from the database.
* @since 2.6.0
* @param int $token_id Token ID
*/
@ -199,8 +186,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Update a payment token
*
* Update a payment token.
* @since 2.6.0
* @return True on success, false if validation failed and a payment token could not be updated
*/
@ -221,8 +207,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Create a new payment token in the database
*
* 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
*/
@ -233,6 +218,14 @@ if ( ! defined( 'ABSPATH' ) ) {
global $wpdb;
// Are there any other tokens? If not, set this token as default
if ( ! $this->is_default() && is_user_logged_in() ) {
$default_token = WC_Payment_Tokens::get_customer_default_token( get_current_user_id() );
if ( is_null( $default_token ) ) {
$this->set_default( true );
}
}
$wpdb->insert( $wpdb->prefix . 'woocommerce_payment_tokens', $this->data );
$this->id = $token_id = $wpdb->insert_id;
foreach ( $this->meta as $meta_key => $meta_value ) {
@ -244,8 +237,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Saves a payment token to the database - does not require you to know if this is a new token or an update token
*
* 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
*/
@ -258,7 +250,8 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Remove a payment token from the database
* Remove a payment token from the database.
* @since 2.6.0
*/
public function delete() {
global $wpdb;

View File

@ -168,7 +168,14 @@ class WC_Payment_Gateways {
return;
}
$current = WC()->session->get( 'chosen_payment_method' );
if ( is_user_logged_in() ) {
$default_token = WC_Payment_Tokens::get_customer_default_token( get_current_user_id() );
if ( ! is_null( $default_token ) ) {
$default_token_gateway = $default_token->get_gateway_id();
}
}
$current = ( isset( $default_token_gateway ) ? $default_token_gateway : WC()->session->get( 'chosen_payment_method' ) );
if ( $current && isset( $gateways[ $current ] ) ) {
$current_gateway = $gateways[ $current ];

View File

@ -1,11 +1,10 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
/**
* WooCommerce Payment Tokens
* WooCommerce Payment Tokens.
*
* An API for storing and managing tokens for gateways and customers.
*
@ -18,7 +17,7 @@ if ( ! defined( 'ABSPATH' ) ) {
class WC_Payment_Tokens {
/**
* Returns an array of payment token objects associated with the passed customer ID
* Returns an array of payment token objects associated with the passed customer ID.
* @since 2.6.0
* @param int $customer_id Customer ID
* @param string $gateway Optional Gateway ID for getting tokens for a specific gateway
@ -54,9 +53,33 @@ class WC_Payment_Tokens {
}
/**
* Returns an array of payment token objects associated with the passed order ID
*
* @since 2.6
* Returns a customers default token or NULL if there is no default token.
* @since 2.6.0
* @param int $customer_id
* @return WC_Payment_Token|null
*/
public static function get_customer_default_token( $customer_id ) {
if ( $customer_id < 1 ) {
return null;
}
global $wpdb;
$token = $wpdb->get_row( $wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE user_id = %d AND is_default = 1",
$customer_id
) );
if ( $token ) {
return self::get( $token->token_id, $token );
} else {
return null;
}
}
/**
* Returns an array of payment token objects associated with the passed order ID.
* @since 2.6.0
* @param int $order_id Order ID
* @return array Array of token objects
*/
@ -95,9 +118,8 @@ class WC_Payment_Tokens {
}
/**
* Get a token object by ID
*
* @since 2.6
* Get a token object by ID.
* @since 2.6.0
* @param int $token_id Token ID
* @return WC_Payment_Token|null Returns a valid payment token or null if no token can be found
*/
@ -109,7 +131,7 @@ class WC_Payment_Tokens {
$token_id
) );
// Still empty? Token doesn't exist? Don't continue
if ( empty( $token_result) ) {
if ( empty( $token_result ) ) {
return null;
}
}
@ -127,9 +149,8 @@ class WC_Payment_Tokens {
}
/**
* Remove a payment token from the database by ID
*
* @since 2.6
* Remove a payment token from the database by ID.
* @since 2.6.0
* @param WC_Payment_Token $token_id Token ID
*/
public static function delete( $token_id ) {
@ -142,9 +163,8 @@ class WC_Payment_Tokens {
}
/**
* Loops through all of a users payment tokens and sets is_default to false for all but a specific token
*
* @since 2.6
* Loops through all of a users payment tokens and sets is_default to false for all but a specific token.
* @since 2.6.0
* @param int $user_id User to set a default for
* @param int $token_id The ID of the token that should be default
*/
@ -161,9 +181,8 @@ class WC_Payment_Tokens {
}
/**
* Returns what type (credit card, echeck, etc) of token a token is by ID
*
* @since 2.6
* Returns what type (credit card, echeck, etc) of token a token is by ID.
* @since 2.6.0
* @param int $token_id Token ID
* @return string Type
*/

View File

@ -5,10 +5,9 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* WooCommerce Credit Card Payment Token
* WooCommerce Credit Card Payment Token.
*
* Representation of a payment token for credit cards
* Extends from WC_Payment_Token_eCheck since both types contain last4 digits.
* Representation of a payment token for credit cards.
*
* @class WC_Payment_Token_CC
* @since 2.6.0
@ -18,11 +17,11 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class WC_Payment_Token_CC extends WC_Payment_Token {
/** @protected string Token Type String */
/** @protected string Token Type String. */
protected $type = 'CC';
/**
* Validate credit card payment tokens
* Validate credit card payment tokens.
*
* These fields are required by all credit card payment tokens:
* expiry_month - string Expiration date (MM) for the card
@ -66,8 +65,7 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
}
/**
* Returns the card type (mastercard, visa, ...)
*
* Returns the card type (mastercard, visa, ...).
* @since 2.6.0
* @return string Card type
*/
@ -76,8 +74,7 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
}
/**
* Set the card type (mastercard, visa, ...)
*
* Set the card type (mastercard, visa, ...).
* @since 2.6.0
* @param string $type
*/
@ -86,8 +83,7 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
}
/**
* Returns the card expiration year (YYYY)
*
* Returns the card expiration year (YYYY).
* @since 2.6.0
* @return string Expiration year
*/
@ -96,8 +92,7 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
}
/**
* Set the expiration year for the card (YYYY format)
*
* Set the expiration year for the card (YYYY format).
* @since 2.6.0
* @param string $year
*/
@ -106,8 +101,7 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
}
/**
* Returns the card expiration month (MM)
*
* Returns the card expiration month (MM).
* @since 2.6.0
* @return string Expiration month
*/
@ -116,8 +110,7 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
}
/**
* Set the expiration month for the card (MM format)
*
* Set the expiration month for the card (MM format).
* @since 2.6.0
* @param string $month
*/
@ -126,8 +119,7 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
}
/**
* Returns the last four digits
*
* Returns the last four digits.
* @since 2.6.0
* @return string Last 4 digits
*/
@ -136,8 +128,7 @@ class WC_Payment_Token_CC extends WC_Payment_Token {
}
/**
* Set the last four digits
*
* Set the last four digits.
* @since 2.6.0
* @param string $last4
*/

View File

@ -5,9 +5,9 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* WooCommerce eCheck Payment Token
* WooCommerce eCheck Payment Token.
*
* Representation of a payment token for eChecks
* Representation of a payment token for eChecks.
*
* @class WC_Payment_Token_eCheck
* @since 2.6.0
@ -41,8 +41,7 @@ class WC_Payment_Token_eCheck extends WC_Payment_Token {
}
/**
* Returns the last four digits
*
* Returns the last four digits.
* @since 2.6.0
* @return string Last 4 digits
*/
@ -51,8 +50,7 @@ class WC_Payment_Token_eCheck extends WC_Payment_Token {
}
/**
* Set the last four digits
*
* Set the last four digits.
* @since 2.6.0
* @param string $last4
*/

View File

@ -65,6 +65,49 @@ class Payment_Tokens extends \WC_Unit_Test_Case {
}
}
/**
* Test getting a customers default token.
* @since 2.6.0
*/
function test_wc_get_customer_default_token() {
$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token->set_gateway_id( 'simplify_commerce' );
$token->save();
$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token->set_default( true );
$token->set_gateway_id( 'paypal' );
$token->save();
$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( 1 ) );
$default_token = \WC_Payment_Tokens::get_customer_default_token( 1 );
$this->assertEquals( 'paypal', $default_token->get_gateway_id() );
}
/**
* Test getting a customers default token, when there is no default token.
* @since 2.6.0
*/
function test_wc_get_customer_default_token_returns_null_when_no_default_token() {
$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token->set_gateway_id( 'simplify_commerce' );
$token->save();
$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token->set_gateway_id( 'paypal' );
$token->save();
$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( 1 ) );
$default_token = \WC_Payment_Tokens::get_customer_default_token( 1 );
$this->assertNull( $default_token );
}
/**
* Test getting a token by ID.
* @since 2.6.0