/** * 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); }