Prepare a list of payment methods for output on the payment methods "my account" page.

This commit is contained in:
Justin Shreve 2016-02-09 12:19:34 -08:00
parent 8f27a3b3af
commit 5d8967c7d7
8 changed files with 247 additions and 22 deletions

View File

@ -4,16 +4,6 @@ if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* WooCommerce Payment Token Meta API - set table name
*/
function wc_payment_token_metadata_wpdbfix() {
global $wpdb;
$wpdb->payment_tokenmeta = $wpdb->prefix . 'woocommerce_payment_tokenmeta';
$wpdb->tables[] = 'woocommerce_payment_tokenmeta';
}
add_action( 'init', 'wc_payment_token_metadata_wpdbfix', 0 );
/**
* WooCommerce Payment Token
*
@ -63,7 +53,7 @@ add_action( 'init', 'wc_payment_token_metadata_wpdbfix', 0 );
* @return ID Token ID
*/
public function get_id() {
return $this->id;
return absint( $this->id );
}
/**
@ -103,7 +93,7 @@ add_action( 'init', 'wc_payment_token_metadata_wpdbfix', 0 );
* @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 ( isset( $this->data['user_id'] ) && $this->data['user_id'] > 0 ) ? $this->data['user_id'] : 0;
return ( isset( $this->data['user_id'] ) && $this->data['user_id'] > 0 ) ? absint( $this->data['user_id'] ) : 0;
}
/**

View File

@ -201,6 +201,25 @@ class WC_Settings_Payment_Gateways extends WC_Settings_Page {
'desc_tip' => true,
),
array(
'title' => __( 'Delete Payment Method', 'woocommerce' ),
'desc' => __( 'Endpoint for the delete payment method page', 'woocommerce' ),
'id' => 'woocommerce_myaccount_delete_payment_method_endpoint',
'type' => 'text',
'default' => 'delete-payment-method',
'desc_tip' => true,
),
array(
'title' => __( 'Set Default Payment Method', 'woocommerce' ),
'desc' => __( 'Endpoint for the setting a default payment page', 'woocommerce' ),
'id' => 'woocommerce_myaccount_set_default_payment_method_endpoint',
'type' => 'text',
'default' => 'set-default-payment-method',
'desc_tip' => true,
),
array(
'type' => 'sectionend',
'id' => 'checkout_endpoint_options',

View File

@ -19,6 +19,7 @@ class WC_Payment_Tokens {
/**
* Returns an array of payment token objects associated with the passed customer ID
* @since 2.6
* @param int $customer_id Customer ID
* @return array Array of token objects
*/
@ -51,6 +52,8 @@ class WC_Payment_Tokens {
/**
* Returns an array of payment token objects associated with the passed order ID
*
* @since 2.6
* @param int $order_id Order ID
* @return array Array of token objects
*/
@ -90,6 +93,8 @@ class WC_Payment_Tokens {
/**
* Get a token object by ID
*
* @since 2.6
* @param int $token_id Token ID
* @return WC_Payment_Token|null Returns a valid payment token or null if no token can be found
*/
@ -120,6 +125,8 @@ class WC_Payment_Tokens {
/**
* Remove a payment token from the database by ID
*
* @since 2.6
* @param WC_Payment_Token $token_id Token ID
*/
public static function delete( $token_id ) {
@ -131,8 +138,29 @@ 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
* @param int $user_id User to set a default for
* @param int $token_id The ID of the token that should be default
*/
public static function set_users_default( $user_id, $token_id ) {
$users_tokens = self::get_customer_tokens( $user_id );
foreach ( $users_tokens as $token ) {
if ( $token_id === $token->get_id() ) {
$token->set_default( true );
} else {
$token->set_default( false );
}
$token->update();
}
}
/**
* Returns what type (credit card, echeck, etc) of token a token is by ID
*
* @since 2.6
* @param int $token_id Token ID
* @return string Type
*/

View File

@ -65,17 +65,18 @@ class WC_Query {
// Checkout actions.
'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),
'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),
// My account actions.
'orders' => get_option( 'woocommerce_myaccount_orders_endpoint', 'orders' ),
'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ),
'downloads' => get_option( 'woocommerce_myaccount_downloads_endpoint', 'downloads' ),
'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ),
'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ),
'payment-methods' => get_option( 'woocommerce_myaccount_payment_methods_endpoint', 'payment-methods' ),
'lost-password' => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ),
'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ),
'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ),
'orders' => get_option( 'woocommerce_myaccount_orders_endpoint', 'orders' ),
'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ),
'downloads' => get_option( 'woocommerce_myaccount_downloads_endpoint', 'downloads' ),
'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ),
'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ),
'payment-methods' => get_option( 'woocommerce_myaccount_payment_methods_endpoint', 'payment-methods' ),
'lost-password' => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ),
'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ),
'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ),
'delete-payment-method' => get_option( 'woocommerce_myaccount_delete_payment_method_endpoint', 'delete-payment-method' ),
'set-default-payment-method' => get_option( 'woocommerce_myaccount_set_default_payment_method_endpoint', 'set-default-payment-method' ),
);
}

View File

@ -344,4 +344,59 @@ class WC_Shortcode_My_Account {
}
}
/**
* Deletes a payment method from a users list and displays a message to the user
*
* @since 2.6
* @param int $id Payment Token ID
*/
private static function delete_payment_method( $id ) {
$token = WC_Payment_Tokens::get( $id );
if ( is_null( $token ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
return false;
}
if ( get_current_user_id() !== $token->get_user_id() ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
return false;
}
if ( false === wp_verify_nonce( $_REQUEST['_wpnonce'], 'delete-payment-method-' . $id ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
return false;
}
WC_Payment_Tokens::delete( $id );
}
/**
* Sets a payment method as default and displays a message to the user
*
* @since 2.6
* @param int $id Payment Token ID
*/
private static function set_default_payment_method( $id ) {
$token = WC_Payment_Tokens::get( $id );
if ( is_null( $token ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
return false;
}
if ( get_current_user_id() !== $token->get_user_id() ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
return false;
}
if ( false === wp_verify_nonce( $_REQUEST['_wpnonce'], 'set-default-payment-method-' . $id ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
return false;
}
WC_Payment_Tokens::set_users_default( $token->get_user_id(), $id );
}
}

View File

@ -208,3 +208,93 @@ function wc_get_account_payment_methods_columns() {
'actions' => ' ',
) );
}
/**
* Returns an array of a user's saved payments list for output on the account tab.
*
* @since 2.6
* @param array $list List of payment methods passed from wc_get_customer_saved_methods_list()
* @param int $customer_id The customer to fetch payment methods for
* @return array Filtered list of customers payment methods
*/
function wc_get_account_saved_payment_methods_list( $list, $customer_id ) {
$payment_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id );
foreach ( $payment_tokens as $payment_token ) {
$delete_url = wc_get_endpoint_url( 'delete-payment-method', $payment_token->get_id() );
$delete_url = wp_nonce_url( $delete_url, 'delete-payment-method-' . $payment_token->get_id() );
$set_default_url = wc_get_endpoint_url( 'set-default-payment-method', $payment_token->get_id() );
$set_default_url = wp_nonce_url( $set_default_url, 'set-default-payment-method-' . $payment_token->get_id() );
$type = strtolower( $payment_token->get_type() );
$list[ $type ][] = array(
'method' => array(
'gateway' => $payment_token->get_gateway_id(),
),
'expires' => esc_html__( 'N/A', 'woocommerce' ),
'is_default' => $payment_token->is_default(),
'actions' => array(
'delete' => array(
'url' => $delete_url,
'name' => esc_html__( 'Delete', 'woocommerce' ),
),
),
);
$key = key( array_slice( $list[ $type ], -1, 1, true ) );
if ( ! $payment_token->is_default() ) {
$list[ $type ][$key]['actions']['default'] = array(
'url' => $set_default_url,
'name' => esc_html__( 'Make Default', 'woocommerce' ),
);
}
$list[ $type ][ $key ] = apply_filters( 'woocommerce_payment_methods_list_item', $list[ $type ][ $key ], $payment_token );
}
return $list;
}
add_filter( 'woocommerce_saved_payment_methods_list', 'wc_get_account_saved_payment_methods_list', 10, 2 );
/**
* Controls the output for credit cards on the my account page.
*
* @since 2.6
* @param array $item Individual list item from woocommerce_saved_payment_methods_list
* @param WC_Payment_Token $payment_token The payment token associated with this method entry
* @return array Filtered item
*/
function wc_get_account_saved_payment_methods_list_item_cc( $item, $payment_token ) {
if ( 'cc' !== strtolower( $payment_token->get_type() ) ) {
return $item;
}
$card_type = $payment_token->get_card_type();
$item['method']['last4'] = $payment_token->get_last4();
$item['method']['brand'] = ( ! empty( $card_type ) ? ucfirst( $card_type ) : esc_html__( 'Credit Card', 'woocommerce' ) );
$item['expires'] = $payment_token->get_expiry_month() . '/' . substr( $payment_token->get_expiry_year(), -2 );
return $item;
}
add_filter( 'woocommerce_payment_methods_list_item', 'wc_get_account_saved_payment_methods_list_item_cc', 10, 2 );
/**
* Controls the output for eChecks on the my account page.
*
* @since 2.6
* @param array $item Individual list item from woocommerce_saved_payment_methods_list
* @param WC_Payment_Token $payment_token The payment token associated with this method entry
* @return array Filtered item
*/
function wc_get_account_saved_payment_methods_list_item_echeck( $item, $payment_token ) {
if ( 'echeck' !== strtolower( $payment_token->get_type() ) ) {
return $item;
}
$item['method']['last4'] = $payment_token->get_last4();
$item['method']['brand'] = esc_html__( 'eCheck', 'woocommerce' );
return $item;
}
add_filter( 'woocommerce_payment_methods_list_item', 'wc_get_account_saved_payment_methods_list_item_echeck', 10, 2 );

View File

@ -76,4 +76,36 @@ class Payment_Tokens extends \WC_Unit_Test_Case {
$this->assertEquals( 'CC', \WC_Payment_Tokens::get_token_type_by_id( $token_id ) );
}
/**
* Test setting a users default token
*
* @since 2.6
*/
function test_wc_payment_tokens_set_users_default() {
$token = \WC_Helper_Payment_Token::create_cc_token();
$token_id = $token->get_id();
$token->set_user_id( 1 );
$token->save();
$token2 = \WC_Helper_Payment_Token::create_cc_token();
$token_id_2 = $token2->get_id();
$token2->set_user_id( 1 );
$token2->save();
$this->assertFalse( $token->is_default() );
$this->assertFalse( $token2->is_default() );
\WC_Payment_Tokens::set_users_default( 1, $token_id_2 );
$token->read( $token_id );
$token2->read( $token_id_2 );
$this->assertFalse( $token->is_default() );
$this->assertTrue( $token2->is_default() );
\WC_Payment_Tokens::set_users_default( 1, $token_id );
$token->read( $token_id );
$token2->read( $token_id_2 );
$this->assertTrue( $token->is_default() );
$this->assertFalse( $token2->is_default() );
}
}

View File

@ -167,6 +167,7 @@ final class WooCommerce {
add_action( 'init', array( $this, 'init' ), 0 );
add_action( 'init', array( 'WC_Shortcodes', 'init' ) );
add_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
add_action( 'init', array( $this, 'payment_token_metadata_wpdbfix' ), 0 );
}
/**
@ -468,6 +469,15 @@ final class WooCommerce {
}
}
/**
* WooCommerce Payment Token Meta API - set table name
*/
function payment_token_metadata_wpdbfix() {
global $wpdb;
$wpdb->payment_tokenmeta = $wpdb->prefix . 'woocommerce_payment_tokenmeta';
$wpdb->tables[] = 'woocommerce_payment_tokenmeta';
}
/**
* Get Checkout Class.
* @return WC_Checkout