protected function get_users_token() { $customer_token = null; if (is_user_logged_in()) { $tokens = WC_Payment_Tokens::get_customer_tokens(get_current_user_id()); foreach ($tokens as $token) { if ($token->get_gateway_id() === $this->id) { $customer_token = $token; break; } } } return $customer_token; }
/** * 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; }
/** * Returns a users saved tokens for this gateway. * @since 2.6.0 * @return array */ public function get_tokens() { if (sizeof($this->tokens) > 0) { return $this->tokens; } if (is_user_logged_in() && $this->supports('tokenization')) { $this->tokens = WC_Payment_Tokens::get_customer_tokens(get_current_user_id(), $this->id); } return $this->tokens; }
/** * 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()); }
/** * 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); }