/** * Mark the given order as cancelled and set the order note * * @since 2.1.0 * @param WC_Order $order the order * @param string $message a message to display inside the "Payment Cancelled" order note * @param SV_WC_Payment_Gateway_API_Response optional $response the transaction response object */ protected function mark_order_as_cancelled($order, $message, $response = null) { $order_note = sprintf(_x('%s Transaction Cancelled (%s)', 'Cancelled order note', $this->text_domain), $this->get_method_title(), $message); // Mark order as cancelled if not already set if (!SV_WC_Plugin_Compatibility::order_has_status($order, 'cancelled')) { $order->update_status('cancelled', $order_note); } else { $order->add_order_note($order_note); } $this->add_debug_message($message, 'error'); }
/** * Mark the given order as failed and set the order note * * @since 2.0 * @param WC_Order $order the WC order object * @param string $message a message to display inside the 'Amazon Transaction Failed' order note */ protected function mark_order_as_failed(WC_Order $order, $message = '') { $order_note = sprintf(__('Amazon Transaction Failed (%s)', WC_Amazon_FPS::TEXT_DOMAIN), $message); // Mark order as failed if not already set, otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times if (!SV_WC_Plugin_Compatibility::order_has_status($order, 'failed')) { $order->update_status('failed', $order_note); } else { $order->add_order_note($order_note); } // add customer-facing error message SV_WC_Helper::wc_add_notice(__('An error occurred, please try again or try an alternate form of payment.', WC_Amazon_FPS::TEXT_DOMAIN), 'error'); // log the error message $this->log($message); }
/** * Process redirect back (non-IPN gateway) * * @since 2.1.0 */ public function process_redirect_back() { // log the redirect back request $this->log_transaction_response_request($_REQUEST); $response = null; try { // get the transaction response object for the current request $response = $this->get_transaction_response($_REQUEST); // get the associated order, or die trying $order = $response->get_order(); if (!$order || !$order->id) { $this->add_debug_message(sprintf("Order %s not found", $response->get_order_id()), 'error'); // if an order could not be determined, there's not a whole lot // we can do besides redirecting back to the home page return wp_redirect(get_home_url(null, '')); } // check for duplicate order processing if (!$order->needs_payment()) { $this->add_debug_message(sprintf("Order '%s' has already been processed", $order->get_order_number()), 'error'); $order_note = sprintf(__('%s duplicate transaction received', $this->text_domain), $this->get_method_title()); $order->add_order_note($order_note); // since the order has already been paid for, redirect to the 'thank you' page return wp_redirect($this->get_return_url($order)); } if ($this->process_transaction_response($order, $response)) { if (SV_WC_Plugin_Compatibility::order_has_status($order, 'on-hold')) { $order->reduce_order_stock(); // reduce stock for held orders, but don't complete payment } elseif (!SV_WC_Plugin_Compatibility::order_has_status($order, 'cancelled')) { $order->payment_complete(); // mark order as having received payment } // finally, redirect to the 'thank you' page return wp_redirect($this->get_return_url($order)); } else { // failed response, redirect back to pay page return wp_redirect($order->get_checkout_payment_url($this->use_form_post() && !$this->use_auto_form_post())); } } catch (SV_WC_Payment_Gateway_Exception $e) { // failure if (isset($order) && $order) { $this->mark_order_as_failed($order, $e->getMessage(), $response); return wp_redirect($order->get_checkout_payment_url($this->use_form_post() && !$this->use_auto_form_post())); } // otherwise, if no order is available, log the issue and redirect to home $this->add_debug_message('Redirect-back error: ' . $e->getMessage(), 'error'); return wp_redirect(get_home_url(null, '')); } }
/** * Mark the given order as failed, and set the order note * * @param WC_Order $order the order * @param string $order_note the order note to set */ private function order_failed($order, $order_note) { if (!SV_WC_Plugin_Compatibility::order_has_status($order, 'failed')) { $order->update_status('failed', $order_note); } else { // otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times $order->add_order_note($order_note); } }
/** * Handles payment processing * * @since 1.0.0 * @see WC_Payment_Gateway::process_payment() * @param int|string $order_id */ public function process_payment($order_id) { // give other actors an opportunity to intercept and implement the process_payment() call for this transaction if (true !== ($result = apply_filters('wc_payment_gateway_' . $this->get_id() . '_process_payment', true, $order_id, $this))) { return $result; } // add payment information to order $order = $this->get_order($order_id); try { // registered customer checkout (already logged in or creating account at checkout) if ($this->supports_tokenization() && 0 != SV_WC_Plugin_Compatibility::get_order_user_id($order) && $this->should_tokenize_payment_method() && (0 == $order->payment_total || $this->tokenize_before_sale())) { $order = $this->create_payment_token($order); } // payment failures are handled internally by do_transaction() // the order amount will be $0 if a WooCommerce Subscriptions free trial product is being processed // note that customer id & payment token are saved to order when create_payment_token() is called if (0 == $order->payment_total && !$this->transaction_forced() || $this->do_transaction($order)) { // add transaction data for zero-dollar "orders" if (0 == $order->payment_total) { $this->add_transaction_data($order); } if (SV_WC_Plugin_Compatibility::order_has_status($order, 'on-hold')) { $order->reduce_order_stock(); // reduce stock for held orders, but don't complete payment } else { $order->payment_complete(); // mark order as having received payment } WC()->cart->empty_cart(); return array('result' => 'success', 'redirect' => $this->get_return_url($order)); } } catch (SV_WC_Plugin_Exception $e) { $this->mark_order_as_failed($order, $e->getMessage()); } }