get_customer_default_token() public static method

Returns a customers default token or NULL if there is no default token.
Since: 2.6.0
public static get_customer_default_token ( integer $customer_id ) : WC_Payment_Token | null
$customer_id integer
return WC_Payment_Token | null
 /**
  * Create a new payment token in the database.
  *
  * @since 2.7.0
  * @param WC_Payment_Token $token
  */
 public function create(&$token)
 {
     if (false === $token->validate()) {
         throw new Exception(__('Invalid or missing payment token fields.', 'woocommerce'));
     }
     global $wpdb;
     if (!$token->is_default() && $token->get_user_id() > 0) {
         $default_token = WC_Payment_Tokens::get_customer_default_token($token->get_user_id());
         if (is_null($default_token)) {
             $token->set_default(true);
         }
     }
     $payment_token_data = array('gateway_id' => $token->get_gateway_id('edit'), 'token' => $token->get_token('edit'), 'user_id' => $token->get_user_id('edit'), 'type' => $token->get_type('edit'));
     $wpdb->insert($wpdb->prefix . 'woocommerce_payment_tokens', $payment_token_data);
     $token_id = $wpdb->insert_id;
     $token->set_id($token_id);
     $token->save_meta_data();
     $token->apply_changes();
     // Make sure all other tokens are not set to default
     if ($token->is_default() && $token->get_user_id() > 0) {
         WC_Payment_Tokens::set_users_default($token->get_user_id(), $token_id);
     }
     do_action('woocommerce_payment_token_created', $token_id);
 }
 /**
  * Create a new payment token in the database.
  * @since 2.6.0
  * @return boolean 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;
     // Are there any other tokens? If not, set this token as default
     if (!$this->is_default() && $this->get_user_id() > 0) {
         $default_token = WC_Payment_Tokens::get_customer_default_token($this->get_user_id());
         if (is_null($default_token)) {
             $this->set_default(true);
         }
     }
     $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();
     // 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);
     }
     do_action('woocommerce_payment_token_created', $token_id);
     return true;
 }
 /**
  * Test getting a customers default token, when there no token is expictly set.
  * This should be the "first created".
  * @see WC_Payment_Token::create()
  * @group failing
  * @since 2.6.0
  */
 function test_wc_get_customer_default_token_returns_first_created_when_no_default_token_set()
 {
     $token = WC_Helper_Payment_Token::create_cc_token($this->user_id);
     $token->set_gateway_id('bacs');
     $token->save();
     $token = WC_Helper_Payment_Token::create_cc_token($this->user_id);
     $token->set_gateway_id('paypal');
     $token->save();
     $this->assertCount(2, WC_Payment_Tokens::get_customer_tokens($this->user_id));
     $default_token = WC_Payment_Tokens::get_customer_default_token($this->user_id);
     $this->assertEquals('bacs', $default_token->get_gateway_id());
 }
 /**
  * Set the current, active gateway.
  *
  * @param array $gateway Available payment gateways.
  */
 public function set_current_gateway($gateways)
 {
     // Be on the defensive
     if (!is_array($gateways) || empty($gateways)) {
         return;
     }
     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];
     } else {
         $current_gateway = current($gateways);
     }
     // Ensure we can make a call to set_current() without triggering an error
     if ($current_gateway && is_callable(array($current_gateway, 'set_current'))) {
         $current_gateway->set_current();
     }
 }
 /**
  * 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;
     // 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) {
         add_metadata('payment_token', $token_id, $meta_key, $meta_value, true);
     }
     do_action('woocommerce_payment_token_created', $token_id);
     return true;
 }
Example #6
0
 /**
  * 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);
 }