/** * 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()); } }
/** * 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; } }
/** * Test deleting a token by ID. * @since 2.6.0 */ function test_wc_payment_tokens_delete() { $token = WC_Helper_Payment_Token::create_cc_token(); $token_id = $token->get_id(); WC_Payment_Tokens::delete($token_id); $get_token = WC_Payment_Tokens::get($token_id); $this->assertNull($get_token); }
/** * 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); }
/** * 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(); }
/** * 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()); } }