/**
  * Process the payment
  */
 function process_payment($order_id)
 {
     global $woocommerce;
     if (class_exists('WC_Subscriptions_Order') && WC_Subscriptions_Order::order_contains_subscription($order_id)) {
         $order = new WC_Order($order_id);
         $stripe_token = isset($_POST['stripe_token']) ? woocommerce_clean($_POST['stripe_token']) : '';
         // Use Stripe CURL API for payment
         try {
             $post_data = array();
             $customer_id = 0;
             // Check if paying via customer ID
             if (isset($_POST['stripe_customer_id']) && $_POST['stripe_customer_id'] !== 'new' && is_user_logged_in()) {
                 $customer_ids = get_user_meta(get_current_user_id(), '_stripe_customer_id', false);
                 if (isset($customer_ids[$_POST['stripe_customer_id']]['customer_id'])) {
                     $customer_id = $customer_ids[$_POST['stripe_customer_id']]['customer_id'];
                 } else {
                     throw new Exception(__('Invalid card.', 'wc_stripe'));
                 }
             } elseif (empty($stripe_token)) {
                 throw new Exception(__('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'wc_stripe'));
             }
             if (method_exists('WC_Subscriptions_Order', 'get_total_initial_payment')) {
                 $initial_payment = WC_Subscriptions_Order::get_total_initial_payment($order);
             } else {
                 $initial_payment = WC_Subscriptions_Order::get_sign_up_fee($order) + WC_Subscriptions_Order::get_price_per_period($order);
             }
             $customer_response = $this->add_customer_to_order($order, $customer_id, $stripe_token);
             if ($initial_payment > 0) {
                 $payment_response = $this->process_subscription_payment($order, $initial_payment);
             }
             if (is_wp_error($customer_response)) {
                 throw new Exception($customer_response->get_error_message());
             } else {
                 if (isset($payment_response) && is_wp_error($payment_response)) {
                     throw new Exception($payment_response->get_error_message());
                 } else {
                     // Payment complete
                     $order->payment_complete();
                     // Remove cart
                     $woocommerce->cart->empty_cart();
                     // Activate subscriptions
                     WC_Subscriptions_Manager::activate_subscriptions_for_order($order);
                     // Store token
                     if ($stripe_token) {
                         update_post_meta($order->id, '_stripe_token', $stripe_token);
                     }
                     // Return thank you page redirect
                     return array('result' => 'success', 'redirect' => $this->get_return_url($order));
                 }
             }
         } catch (Exception $e) {
             $woocommerce->add_error(__('Error:', 'wc_stripe') . ' "' . $e->getMessage() . '"');
             return;
         }
     } else {
         return parent::process_payment($order_id);
     }
 }
 /**
  * Delete a card
  */
 public function delete_card()
 {
     if (!isset($_POST['stripe_delete_card']) || !is_account_page()) {
         return;
     }
     if (!is_user_logged_in() || !($customer_id = get_user_meta(get_current_user_id(), '_stripe_customer_id', true)) || !is_string($customer_id) || !wp_verify_nonce($_POST['_wpnonce'], "stripe_del_card")) {
         wp_die(__('Unable to verify deletion, please try again', 'woocommerce-gateway-stripe'));
     }
     $stripe = new WC_Gateway_Stripe();
     $result = $stripe->stripe_request(array(), 'customers/' . $customer_id . '/cards/' . sanitize_text_field($_POST['stripe_delete_card']), 'DELETE');
     delete_transient('stripe_cards_' . $customer_id);
     if (is_wp_error($result)) {
         wc_add_notice(__('Unable to delete card.', 'woocommerce-gateway-stripe'), 'error');
     } else {
         wc_add_notice(__('Card deleted.', 'woocommerce-gateway-stripe'), 'success');
     }
     wp_safe_redirect(apply_filters('wc_stripe_manage_saved_cards_url', get_permalink(woocommerce_get_page_id('myaccount'))));
     exit;
 }
 /**
  * Cancel pre-auth on refund/cancellation
  *
  * @param  int $order_id
  */
 public function cancel_payment($order_id)
 {
     $order = new WC_Order($order_id);
     if ($order->payment_method == 'stripe') {
         $charge = get_post_meta($order_id, '_stripe_charge_id', true);
         if ($charge) {
             $stripe = new WC_Gateway_Stripe();
             $result = $stripe->stripe_request(array('amount' => $order->order_total * 100), 'charges/' . $charge . '/refund');
             if (is_wp_error($result)) {
                 $order->add_order_note(__('Unable to refund charge!', 'woocommerce-gateway-stripe') . ' ' . $result->get_error_message());
             } else {
                 $order->add_order_note(sprintf(__('Stripe charge refunded (Charge ID: %s)', 'woocommerce-gateway-stripe'), $result->id));
                 delete_post_meta($order->id, '_stripe_charge_captured');
                 delete_post_meta($order->id, '_stripe_charge_id');
             }
         }
     }
 }
 /**
  * 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);
     }
 }
 public function __construct()
 {
     parent::__construct();
 }
 /**
  * Process the payment
  *
  * @param  int $order_id
  * @return array
  */
 public function process_payment($order_id, $retry = true)
 {
     // Processing subscription
     if (class_exists('WC_Subscriptions_Order') && WC_Subscriptions_Order::order_contains_subscription($order_id)) {
         return $this->process_subscription($order_id, $retry);
         // Processing pre-order
     } elseif (class_exists('WC_Pre_Orders_Order') && WC_Pre_Orders_Order::order_contains_pre_order($order_id)) {
         return $this->process_pre_order($order_id, $retry);
         // Processing regular product
     } else {
         return parent::process_payment($order_id, $retry);
     }
 }
 /**
  * Process the payment
  */
 function process_payment($order_id)
 {
     global $woocommerce;
     if (class_exists('WC_Sponsorship_Order') && WC_Sponsorship_Order::order_contains_sponsorship($order_id)) {
         $order = new WC_Order($order_id);
         $stripe_token = isset($_POST['stripe_token']) ? woocommerce_clean($_POST['stripe_token']) : '';
         // Use Stripe CURL API for payment
         try {
             $post_data = array();
             $customer_id = 0;
             // Check if paying via customer ID
             if (isset($_POST['stripe_customer_id']) && $_POST['stripe_customer_id'] !== 'new' && is_user_logged_in()) {
                 $customer_ids = get_user_meta(get_current_user_id(), '_stripe_customer_id', false);
                 if (isset($customer_ids[$_POST['stripe_customer_id']]['customer_id'])) {
                     $customer_id = $customer_ids[$_POST['stripe_customer_id']]['customer_id'];
                 } else {
                     throw new Exception(__('Invalid card.', 'wc_stripe'));
                 }
             } elseif (empty($stripe_token)) {
                 throw new Exception(__('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'wc_stripe'));
             }
             $customer_response = $this->add_customer_to_order($order, $customer_id, $stripe_token);
             $customer_response = $this->add_customer_to_order($order, $stripe_token);
             if (is_wp_error($customer_response)) {
                 throw new Exception($customer_response->get_error_message());
             } else {
                 // Mark as on-hold (we're awaiting the cheque)
                 $order->update_status('on-hold', 'Awaiting the sponsorship project\'s goal to be met.');
                 // Empty awaiting payment session
                 if (defined($_SESSION) && array_key_exists('order_awaiting_payment', $_SESSION)) {
                     unset($_SESSION['order_awaiting_payment']);
                 }
                 // Remove cart
                 $woocommerce->cart->empty_cart();
                 // Store token
                 if ($stripe_token) {
                     update_post_meta($order->id, '_stripe_token', $stripe_token);
                 }
                 // Return thank you page redirect
                 return array('result' => 'success', 'redirect' => $this->get_return_url($order));
             }
         } catch (Exception $e) {
             $woocommerce->add_error(__('Error:', 'wc_stripe') . ' "' . $e->getMessage() . '"');
             return;
         }
     } else {
         return parent::process_payment($order_id);
     }
 }