/**
  * Process the pre-order.
  *
  * @param WC_Order $order
  *
  * @return array
  */
 protected function process_pre_order($order_id)
 {
     if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) {
         try {
             $order = new WC_Order($order_id);
             // Reduce stock levels
             $order->reduce_order_stock();
             // Remove cart
             $this->api->empty_card();
             // Is pre ordered!
             WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
             // Return thank you page redirect
             return array('result' => 'success', 'redirect' => $this->get_return_url($order));
         } catch (Exception $e) {
             $this->api->add_error('<strong>' . esc_attr($this->title) . '</strong>: ' . $e->getMessage());
             return array('result' => 'fail', 'redirect' => '');
         }
     } else {
         return parent::process_payment($order_id);
     }
 }
 /**
  * Process the pre-order
  *
  * @param  int $order_id
  * @return array
  */
 public function process_pre_order($order_id)
 {
     if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) {
         $order = new WC_Order($order_id);
         // Order limit
         if ($order->order_total * 100 < 50) {
             $error_msg = __('Sorry, the minimum allowed order total is 0.50 to use this payment method.', 'woocommerce-payment-gateway-boilerplate');
         }
         if ($this->sandbox == 'yes') {
             $error_msg .= ' ' . __('Developers: Please make sure that you\'re including jQuery and there are no JavaScript errors on the page.', 'woocommerce-payment-gateway-boilerplate');
         }
         // Reduce stock levels
         $order->reduce_order_stock();
         // Remove cart
         WC()->cart->empty_cart();
         // Is pre ordered!
         WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
         // Return thank you page redirect
         return array('result' => 'success', 'redirect' => $this->get_return_url($order));
         return array('result' => 'fail', 'redirect' => '');
     } else {
         return parent::process_payment($order_id);
     }
 }
 /**
  * Process the pre-order.
  *
  * @param WC_Order $order
  * @param string   $cart_token
  * @uses  Simplify_ApiException
  * @uses  Simplify_BadRequestException
  * @return array
  */
 protected function process_pre_order($order, $cart_token = '')
 {
     if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order->id)) {
         try {
             if ($order->order_total * 100 < 50) {
                 $error_msg = __('Sorry, the minimum allowed order total is 0.50 to use this payment method.', 'woocommerce');
                 throw new Simplify_ApiException($error_msg);
             }
             if (empty($cart_token)) {
                 $error_msg = __('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'woocommerce');
                 if ('yes' == $this->sandbox) {
                     $error_msg .= ' ' . __('Developers: Please make sure that you\'re including jQuery and there are no JavaScript errors on the page.', 'woocommerce');
                 }
                 throw new Simplify_ApiException($error_msg);
             }
             // Create customer
             $customer = Simplify_Customer::createCustomer(array('token' => $cart_token, 'email' => $order->billing_email, 'name' => trim($order->get_formatted_billing_full_name()), 'reference' => $order->id));
             if (is_object($customer) && '' != $customer->id) {
                 $customer_id = wc_clean($customer->id);
                 // Store the customer ID in the order
                 update_post_meta($order->id, '_simplify_customer_id', $customer_id);
             } else {
                 $error_msg = __('Error creating user in Simplify Commerce.', 'woocommerce');
                 throw new Simplify_ApiException($error_msg);
             }
             // Reduce stock levels
             $order->reduce_order_stock();
             // Remove cart
             WC()->cart->empty_cart();
             // Is pre ordered!
             WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
             // Return thank you page redirect
             return array('result' => 'success', 'redirect' => $this->get_return_url($order));
         } catch (Simplify_ApiException $e) {
             if ($e instanceof Simplify_BadRequestException && $e->hasFieldErrors() && $e->getFieldErrors()) {
                 foreach ($e->getFieldErrors() as $error) {
                     wc_add_notice($error->getFieldName() . ': "' . $error->getMessage() . '" (' . $error->getErrorCode() . ')', 'error');
                 }
             } else {
                 wc_add_notice($e->getMessage(), 'error');
             }
             return array('result' => 'fail', 'redirect' => '');
         }
     } else {
         return parent::process_standard_payments($order, $cart_token);
     }
 }
 /**
  * Process the pre-order.
  *
  * @param WC_Order $order
  *
  * @return array
  */
 protected function process_pre_order($order_id)
 {
     if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) {
         try {
             $order = new WC_Order($order_id);
             if (!isset($_POST['iugu_token'])) {
                 if ('yes' == $this->debug) {
                     $this->log->add($this->id, 'Error doing the pre-order for order ' . $order->get_order_number() . ': Missing the "iugu_token".');
                 }
                 $error_msg = __('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'iugu-woocommerce');
                 throw new Exception($error_msg);
             }
             // Create customer payment method.
             $payment_method_id = $this->api->create_customer_payment_method($order, $_POST['iugu_token']);
             if (!$payment_method_id) {
                 if ('yes' == $this->debug) {
                     $this->log->add($this->id, 'Invalid customer method ID for order ' . $order->get_order_number());
                 }
                 $error_msg = __('An error occurred while trying to save your data. Please contact us for get help.', 'iugu-woocommerce');
                 throw new Exception($error_msg);
             }
             // Save the payment method ID in order data.
             update_post_meta($order->id, '_iugu_customer_payment_method_id', $payment_method_id);
             // Reduce stock levels
             $order->reduce_order_stock();
             // Remove cart
             $this->api->empty_card();
             // Is pre ordered!
             WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
             // Return thank you page redirect
             return array('result' => 'success', 'redirect' => $this->get_return_url($order));
         } catch (Exception $e) {
             $this->api->add_error('<strong>' . esc_attr($this->title) . '</strong>: ' . $e->getMessage());
             return array('result' => 'fail', 'redirect' => '');
         }
     } else {
         return parent::process_payment($order_id);
     }
 }
 /**
  * Handle the pre-order initial payment/tokenization, or defer back to the normal payment
  * processing flow
  *
  * @since 4.1.0
  * @see SV_WC_Payment_Gateway::process_payment()
  * @param boolean $result the result of this pre-order payment process
  * @param int $order_id the order identifier
  * @return true|array true to process this payment as a regular transaction, otherwise
  *         return an array containing keys 'result' and 'redirect'
  */
 public function process_payment($result, $order_id)
 {
     if (WC_Pre_Orders_Order::order_contains_pre_order($order_id) && WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) {
         $order = $this->get_gateway()->get_order($order_id);
         try {
             // using an existing tokenized payment method
             if (isset($order->payment->token) && $order->payment->token) {
                 // save the tokenized card info for completing the pre-order in the future
                 $this->get_gateway()->add_transaction_data($order);
             } else {
                 // otherwise tokenize the payment method
                 $order = $this->get_gateway()->create_payment_token($order);
             }
             // mark order as pre-ordered / reduce order stock
             WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
             // empty cart
             WC()->cart->empty_cart();
             // redirect to thank you page
             return array('result' => 'success', 'redirect' => $this->get_gateway()->get_return_url($order));
         } catch (SV_WC_Payment_Gateway_Exception $e) {
             $this->get_gateway()->mark_order_as_failed($order, sprintf(__('Pre-Order Tokenization attempt failed (%s)', 'woocommerce-plugin-framework'), $this->get_gateway()->get_method_title(), $e->getMessage()));
         }
     }
     // processing regular product
     return $result;
 }
 /**
  * Process the transaction after receiving the token from Amazon
  *
  * @since 2.0
  * @param WC_Order $order the WC Order object
  */
 protected function process_transaction(WC_Order $order)
 {
     /* processing subscription */
     if (wc_amazon_fps()->is_subscriptions_active() && WC_Subscriptions_Order::order_contains_subscription($order)) {
         // set the initial payment total
         $order->amazon_order_total = WC_Subscriptions_Order::get_total_initial_payment($order);
         // if there is a free trial, mark the order as paid, otherwise process it
         if (0 == $order->amazon_order_total) {
             $this->mark_order_as_processing($order);
         } else {
             parent::process_transaction($order);
         }
         /* processing pre-order */
     } elseif (wc_amazon_fps()->is_pre_orders_active() && WC_Pre_Orders_Order::order_contains_pre_order($order) && WC_Pre_Orders_Order::order_requires_payment_tokenization($order)) {
         // mark order as pre-ordered / reduce order stock
         WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
         /* processing regular product (or a pre-order charged upfront) */
     } else {
         parent::process_transaction($order);
     }
 }
 /**
  * Process the pre-order
  * @param int $order_id
  * @return array
  */
 public function process_pre_order($order_id, $retry, $force_customer)
 {
     if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) {
         try {
             $order = wc_get_order($order_id);
             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'));
             }
             $source = $this->get_source(get_current_user_id(), true);
             // We need a source on file to continue.
             if (empty($source->customer) || empty($source->source)) {
                 throw new Exception(__('Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe'));
             }
             // Store source to order meta
             $this->save_source($order, $source);
             // Reduce stock levels
             $order->reduce_order_stock();
             // Remove cart
             WC()->cart->empty_cart();
             // Is pre ordered!
             WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
             // Return thank you page redirect
             return array('result' => 'success', 'redirect' => $this->get_return_url($order));
         } catch (Exception $e) {
             wc_add_notice($e->getMessage(), 'error');
             return;
         }
     } else {
         return parent::process_payment($order_id, $retry, $force_customer);
     }
 }
 /**
  * Process the pre-order
  *
  * @param int $order_id
  * @return array
  */
 protected function process_pre_order($order_id)
 {
     if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) {
         $order = wc_get_order($order_id);
         if (isset($_POST['payeezy-token']) && !empty($_POST['payeezy-token'])) {
             $post_id = wc_clean($_POST['payeezy-token']);
             $post = get_post($post_id);
             $card_meta = get_post_meta($post->ID, '_payeezy_card', true);
         } else {
             $card = '';
             $payeezy = new WC_Payeezy_API();
             $response = $payeezy->authorize($this, $order, 0, $card);
             if (isset($response->transaction_status) && 'approved' == $response->transaction_status) {
                 $card_meta = array('token' => $response->token->token_data->value, 'expiry' => $response->card->exp_date, 'cardtype' => $response->card->type);
             } else {
                 $error_msg = __('Payment was declined - please try another card.', 'woocommerce-payeezy');
                 throw new Exception($error_msg);
             }
         }
         // Store the ID in the order
         update_post_meta($order->id, '_payeezy_token', $card_meta['token']);
         update_post_meta($order->id, '_payeezy_expiry', $card_meta['expiry']);
         update_post_meta($order->id, '_payeezy_cardtype', $card_meta['cardtype']);
         // Reduce stock levels
         $order->reduce_order_stock();
         // Remove cart
         WC()->cart->empty_cart();
         // Is pre ordered!
         WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
         // Return thank you page redirect
         return array('result' => 'success', 'redirect' => $this->get_return_url($order));
     } else {
         return parent::process_payment($order_id);
     }
 }
 /**
  * Process initial payment for a pre-order
  *
  * @since 2.0
  * @param \WC_Order $order the order object
  * @throws WC_Gateway_Braintree_Exception
  * @return array
  */
 private function process_pre_order_payment($order)
 {
     // do pre-authorization
     if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order->id)) {
         // for an existing customer using a saved credit card, there's no way in braintree to simply
         // perform a $1 auth/void, so assume the saved card is valid already. If there is an issue
         // with the saved card, the pre-order payment will fail upon release anyway
         // exceptions are thrown if either the create_customer() or create_credit_card() method fails
         // add the braintree customer ID to the order, or create a new braintree customer and add/verify the new card added if needed
         if (!empty($order->braintree_order['customerId'])) {
             update_post_meta($order->id, '_wc_braintree_customer_id', $order->braintree_order['customerId']);
         } else {
             $order = $this->create_customer($order);
         }
         // add the braintree credit card token to the order, or create a new credit card for the customer and verify it if needed
         if (!empty($order->braintree_order['paymentMethodToken'])) {
             update_post_meta($order->id, '_wc_braintree_cc_token', $order->braintree_order['paymentMethodToken']);
         } else {
             $order = $this->create_credit_card($order);
         }
         // mark order as pre-ordered / reduce order stock
         WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
         // empty cart
         SV_WC_Plugin_Compatibility::WC()->cart->empty_cart();
         // redirect to thank you page
         return array('result' => 'success', 'redirect' => $this->get_return_url($order));
     } else {
         // charged upfront (or paying for a newly-released pre-order with the gateway), process just like regular product
         return parent::process_payment($order->id);
     }
 }
 /**
  * Process the pre-order
  *
  * @param int $order_id
  * @return array
  */
 public function process_pre_order($order_id, $retry = true)
 {
     if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) {
         $order = new WC_Order($order_id);
         $stripe_token = isset($_POST['stripe_token']) ? wc_clean($_POST['stripe_token']) : '';
         $card_id = isset($_POST['stripe_card_id']) ? wc_clean($_POST['stripe_card_id']) : '';
         $customer_id = is_user_logged_in() ? get_user_meta(get_current_user_id(), '_stripe_customer_id', true) : 0;
         if (!$customer_id || !is_string($customer_id)) {
             $customer_id = 0;
         }
         try {
             $post_data = array();
             // Check amount
             if ($order->order_total * 100 < 50) {
                 throw new Exception(__('Sorry, the minimum allowed order total is 0.50 to use this payment method.', 'woocommerce-gateway-stripe'));
             }
             // Pay using a saved card!
             if ($card_id !== 'new' && $card_id && $customer_id) {
                 $post_data['customer'] = $customer_id;
                 $post_data['card'] = $card_id;
             } elseif (empty($stripe_token)) {
                 $error_msg = __('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'woocommerce-gateway-stripe');
                 if ($this->testmode) {
                     $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);
             }
             // Save token
             if (!$customer_id) {
                 $customer_id = $this->add_customer($order, $stripe_token);
                 if (is_wp_error($customer_id)) {
                     throw new Exception($customer_id->get_error_message());
                 }
                 unset($post_data['card']);
                 $post_data['customer'] = $customer_id;
             } elseif (!$card_id || $card_id === 'new') {
                 $card_id = $this->add_card($customer_id, $stripe_token);
                 if (is_wp_error($card_id)) {
                     // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id and retry.
                     if ('customer' === $card_id->get_error_code() && $retry) {
                         delete_user_meta(get_current_user_id(), '_stripe_customer_id');
                         return $this->process_pre_order($order_id, false);
                         // false to prevent retry again (endless loop)
                     }
                     throw new Exception($card_id->get_error_message());
                 }
                 $post_data['card'] = $card_id;
                 $post_data['customer'] = $customer_id;
             }
             // Store the ID in the order
             update_post_meta($order->id, '_stripe_customer_id', $customer_id);
             // Store the ID in the order
             update_post_meta($order->id, '_stripe_card_id', $card_id);
             // Reduce stock levels
             $order->reduce_order_stock();
             // Remove cart
             WC()->cart->empty_cart();
             // Is pre ordered!
             WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
             // Return thank you page redirect
             return array('result' => 'success', 'redirect' => $this->get_return_url($order));
         } catch (Exception $e) {
             WC()->add_error($e->getMessage());
             return;
         }
     } else {
         return parent::process_payment($order_id);
     }
 }
 /**
  * Handle the pre-order initial payment/tokenization, or defer back to the normal payment
  * processing flow
  *
  * @since 1.0
  * @see SV_WC_Payment_Gateway::process_payment()
  * @param boolean $result the result of this pre-order payment process
  * @param int $order_id the order identifier
  * @return true|array true to process this payment as a regular transaction, otherwise
  *         return an array containing keys 'result' and 'redirect'
  * @throws SV_WC_Payment_Gateway_Feature_Unsupported_Exception if pre-orders are not supported by this gateway or its current configuration
  */
 public function process_pre_order_payment($result, $order_id)
 {
     if (!$this->supports_pre_orders()) {
         throw new SV_WC_Payment_Gateway_Feature_Unsupported_Exception('Pre-Orders not supported by gateway');
     }
     if (WC_Pre_Orders_Order::order_contains_pre_order($order_id) && WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) {
         $order = $this->get_order($order_id);
         try {
             // using an existing tokenized payment method
             if (isset($order->payment->token) && $order->payment->token) {
                 // save the tokenized card info for completing the pre-order in the future
                 $this->add_transaction_data($order);
             } else {
                 // otherwise tokenize the payment method
                 $order = $this->create_payment_token($order);
             }
             // mark order as pre-ordered / reduce order stock
             WC_Pre_Orders_Order::mark_order_as_pre_ordered($order);
             // empty cart
             SV_WC_Plugin_Compatibility::WC()->cart->empty_cart();
             // redirect to thank you page
             return array('result' => 'success', 'redirect' => $this->get_return_url($order));
         } catch (Exception $e) {
             $this->mark_order_as_failed($order, sprintf(_x('Pre-Order Tokenization attempt failed (%s)', 'Supports direct payment method pre-orders', $this->text_domain), $this->get_method_title(), $e->getMessage()));
         }
     }
     // processing regular product
     return $result;
 }