An API for storing and managing tokens for gateways and customers.
Since: 2.6.0
Author: WooThemes
 /**
  * Update a payment token.
  *
  * @since 2.7.0
  * @param WC_Payment_Token $token
  */
 public function update(&$token)
 {
     if (false === $token->validate()) {
         throw new Exception(__('Invalid or missing payment token fields.', 'woocommerce'));
     }
     global $wpdb;
     $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->update($wpdb->prefix . 'woocommerce_payment_tokens', $payment_token_data, array('token_id' => $token->get_id('edit')));
     $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->get_id());
     }
     do_action('woocommerce_payment_token_updated', $token->get_id());
 }
 /**
  * Process the delete payment method form.
  */
 public static function set_default_payment_method_action()
 {
     global $wp;
     if (isset($wp->query_vars['set-default-payment-method'])) {
         $token_id = absint($wp->query_vars['set-default-payment-method']);
         $token = WC_Payment_Tokens::get($token_id);
         $delete = true;
         if (is_null($token)) {
             wc_add_notice(__('Invalid payment method.', 'woocommerce'), 'error');
             $delete = false;
         }
         if (get_current_user_id() !== $token->get_user_id()) {
             wc_add_notice(__('Invalid payment method.', 'woocommerce'), 'error');
             $delete = false;
         }
         if (false === wp_verify_nonce($_REQUEST['_wpnonce'], 'set-default-payment-method-' . $token_id)) {
             wc_add_notice(__('Invalid payment method.', 'woocommerce'), 'error');
             $delete = false;
         }
         if ($delete) {
             WC_Payment_Tokens::set_users_default($token->get_user_id(), intval($token_id));
             wc_add_notice(__('This payment method was successfully set as your default.', 'woocommerce'));
         }
         wp_redirect(wc_get_account_endpoint_url('payment-methods'));
         exit;
     }
 }
 /**
  * Returns a list of all payment tokens associated with the current order
  *
  * @since 2.6
  * @return array An array of payment token objects
  */
 public function get_payment_tokens()
 {
     return WC_Payment_Tokens::get_order_tokens($this->get_id());
 }
 /**
  * Process the payment.
  *
  * @param int $order_id
  */
 public function process_payment($order_id)
 {
     $order = wc_get_order($order_id);
     // Payment/CC form is hosted on Simplify
     if ('hosted' === $this->mode) {
         return $this->process_hosted_payments($order);
     }
     // New CC info was entered
     if (isset($_POST['simplify_token'])) {
         $cart_token = wc_clean($_POST['simplify_token']);
         $customer_token = $this->get_users_token();
         $customer_token_value = !is_null($customer_token) ? $customer_token->get_token() : '';
         $this->process_customer($order, $customer_token, $cart_token);
         return $this->process_standard_payments($order, $cart_token, $customer_token_value);
     }
     // Possibly Create (or update) customer/save payment token, use an existing token, and then process the payment
     if (isset($_POST['wc-simplify_commerce-payment-token']) && 'new' !== $_POST['wc-simplify_commerce-payment-token']) {
         $token_id = wc_clean($_POST['wc-simplify_commerce-payment-token']);
         $token = WC_Payment_Tokens::get($token_id);
         if ($token->get_user_id() !== get_current_user_id()) {
             wc_add_notice(__('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'woocommerce'), 'error');
             return;
         }
         $this->process_customer($order, $token);
         return $this->process_standard_payments($order, '', $token->get_token());
     }
 }
 /**
  * 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;
 }
/**
 * 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;
}
 /**
  * Test setting a users default token.
  * @since 2.6.0
  */
 function test_wc_payment_tokens_set_users_default()
 {
     $token = WC_Helper_Payment_Token::create_cc_token($this->user_id);
     $token_id = $token->get_id();
     $token->save();
     $token2 = WC_Helper_Payment_Token::create_cc_token($this->user_id);
     $token_id_2 = $token2->get_id();
     $token2->save();
     $this->assertTrue($token->is_default());
     // first created is default
     $this->assertFalse($token2->is_default());
     WC_Payment_Tokens::set_users_default($this->user_id, $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($this->user_id, $token_id);
     $token->read($token_id);
     $token2->read($token_id_2);
     $this->assertTrue($token->is_default());
     $this->assertFalse($token2->is_default());
 }
 /**
  * Process the payment
  *
  * @param int  $order_id Reference.
  * @param bool $retry Should we retry on fail.
  * @param bool $force_customer Force user creation.
  *
  * @throws Exception If payment will not be accepted.
  *
  * @return array|void
  */
 public function process_payment($order_id, $retry = true, $force_customer = false)
 {
     try {
         $order = wc_get_order($order_id);
         $source = $this->get_source(get_current_user_id(), $force_customer);
         if (empty($source->source) && empty($source->customer)) {
             $error_msg = __('Please enter your card details to make a payment.', 'woocommerce-gateway-stripe');
             $error_msg .= ' ' . __('Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe');
             throw new Exception($error_msg);
         }
         // Store source to order meta.
         $this->save_source($order, $source);
         // Handle payment.
         if ($order->get_total() > 0) {
             if ($order->get_total() * 100 < 50) {
                 throw new Exception(__('Sorry, the minimum allowed order total is 0.50 to use this payment method.', 'woocommerce-gateway-stripe'));
             }
             WC_Stripe::log("Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}");
             // Make the request.
             $response = WC_Stripe_API::request($this->generate_payment_request($order, $source));
             if (is_wp_error($response)) {
                 // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
                 if ('customer' === $response->get_error_code() && $retry) {
                     delete_user_meta(get_current_user_id(), '_stripe_customer_id');
                     return $this->process_payment($order_id, false, $force_customer);
                     // Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
                 } elseif ('source' === $response->get_error_code() && $source->token_id) {
                     $token = WC_Payment_Tokens::get($source->token_id);
                     $token->delete();
                     throw new Exception(__('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe'));
                 }
                 throw new Exception($response->get_error_code() . ': ' . $response->get_error_message());
             }
             // Process valid response.
             $this->process_response($response, $order);
         } else {
             $order->payment_complete();
         }
         // Remove cart.
         WC()->cart->empty_cart();
         // Return thank you page redirect.
         return array('result' => 'success', 'redirect' => $this->get_return_url($order));
     } catch (Exception $e) {
         wc_add_notice($e->getMessage(), 'error');
         WC()->session->set('refresh_totals', true);
         WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage()));
         if ($order->has_status(array('pending', 'failed'))) {
             $this->send_failed_order_email($order_id);
         }
         return;
     }
 }
 /**
  * Test deleting a token.
  * @since 2.6.0
  */
 public function test_wc_payment_token_delete()
 {
     $token = WC_Helper_Payment_Token::create_stub_token(__FUNCTION__);
     $token_id = $token->get_id();
     $token->delete();
     $get_token = WC_Payment_Tokens::get($token_id);
     $this->assertNull($get_token);
 }
 /**
  * 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();
     }
 }
 /**
  * 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;
 }
 /**
  * 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;
 }
 /**
  * Sets a payment method as default and displays a message to the user
  *
  * @since  2.6
  * @param  int $id  Payment Token ID
  */
 public 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');
         woocommerce_account_payment_methods();
         return false;
     }
     if (get_current_user_id() !== $token->get_user_id()) {
         wc_add_notice(__('Invalid payment method', 'woocommerce'), 'error');
         woocommerce_account_payment_methods();
         return false;
     }
     if (false === wp_verify_nonce($_REQUEST['_wpnonce'], 'set-default-payment-method-' . $id)) {
         wc_add_notice(__('Invalid payment method', 'woocommerce'), 'error');
         woocommerce_account_payment_methods();
         return false;
     }
     WC_Payment_Tokens::set_users_default($token->get_user_id(), intval($id));
     wc_add_notice(__('This payment method was successfully set as your default.', 'woocommerce'));
     woocommerce_account_payment_methods();
 }
Example #14
0
 /**
  * Test setting a users default token.
  * @since 2.6.0
  */
 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());
 }
 /**
  * Set as default in Stripe
  */
 public function woocommerce_payment_token_set_default($token_id)
 {
     $token = WC_Payment_Tokens::get($token_id);
     if ('stripe' === $token->get_gateway_id()) {
         $stripe_customer = new WC_Stripe_Customer(get_current_user_id());
         $stripe_customer->set_default_card($token->get_token());
     }
 }