/**
  * 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);
     }
 }
 /**
  * For versions of WooCommerce prior to the existence of the woocommerce_available_gateways, 
  * hide available gateways with JavaScript.
  * 
  * @since 1.0
  */
 public static function gateway_scheduled_subscription_payment($user_id, $subscription_key)
 {
     $subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key);
     $order = new WC_Order($subscription['order_id']);
     $amount_to_charge = WC_Subscriptions_Order::get_price_per_period($order, $subscription['product_id']);
     $outstanding_payments = WC_Subscriptions_Order::get_outstanding_balance($order, $subscription['product_id']);
     if ($outstanding_payments > 0) {
         $amount_to_charge += $outstanding_payments;
     }
     do_action('scheduled_subscription_payment_' . $order->payment_method, $amount_to_charge, $order, $subscription['product_id']);
 }
 /**
  * Override the default PayPal standard args in WooCommerce for subscription purchases.
  *
  * @since 1.0
  */
 public static function paypal_standard_subscription_args($paypal_args)
 {
     extract(self::get_order_id_and_key($paypal_args));
     if (WC_Subscriptions_Order::order_contains_subscription($order_id)) {
         $order = new WC_Order($order_id);
         $order_items = $order->get_items();
         // Only one subscription allowed in the cart when PayPal Standard is active
         $product = $order->get_product_from_item($order_items[0]);
         // It's a subscription
         $paypal_args['cmd'] = '_xclick-subscriptions';
         if (count($order->get_items()) > 1) {
             foreach ($order->get_items() as $item) {
                 if ($item['qty'] > 1) {
                     $item_names[] = $item['qty'] . ' x ' . $item['name'];
                 } else {
                     if ($item['qty'] > 0) {
                         $item_names[] = $item['name'];
                     }
                 }
             }
             $paypal_args['item_name'] = sprintf(__('Order %s', WC_Subscriptions::$text_domain), $order->get_order_number());
         } else {
             $paypal_args['item_name'] = $product->get_title();
         }
         // Subscription unit of duration
         switch (strtolower(WC_Subscriptions_Order::get_subscription_period($order))) {
             case 'day':
                 $subscription_period = 'D';
                 break;
             case 'week':
                 $subscription_period = 'W';
                 break;
             case 'year':
                 $subscription_period = 'Y';
                 break;
             case 'month':
             default:
                 $subscription_period = 'M';
                 break;
         }
         $sign_up_fee = WC_Subscriptions_Order::get_sign_up_fee($order);
         $price_per_period = WC_Subscriptions_Order::get_price_per_period($order);
         $subscription_interval = WC_Subscriptions_Order::get_subscription_interval($order);
         $subscription_length = WC_Subscriptions_Order::get_subscription_length($order);
         $subscription_trial_length = WC_Subscriptions_Order::get_subscription_trial_length($order);
         if ($subscription_trial_length > 0) {
             // Specify a free trial period
             $paypal_args['a1'] = $sign_up_fee > 0 ? $sign_up_fee : 0;
             // Add the sign up fee to the free trial period
             // Sign Up interval
             $paypal_args['p1'] = $subscription_trial_length;
             // Sign Up unit of duration
             $paypal_args['t1'] = $subscription_period;
         } elseif ($sign_up_fee > 0) {
             // No trial period, so charge sign up fee and per period price for the first period
             if ($subscription_length == 1) {
                 $param_number = 3;
             } else {
                 $param_number = 1;
             }
             $paypal_args['a' . $param_number] = $price_per_period + $sign_up_fee;
             // Sign Up interval
             $paypal_args['p' . $param_number] = $subscription_interval;
             // Sign Up unit of duration
             $paypal_args['t' . $param_number] = $subscription_period;
         }
         // We have a recurring payment
         if (!isset($param_number) || $param_number == 1) {
             // Subscription price
             $paypal_args['a3'] = $price_per_period;
             // Subscription duration
             $paypal_args['p3'] = $subscription_interval;
             // Subscription period
             $paypal_args['t3'] = $subscription_period;
         }
         // Recurring payments
         if ($subscription_length == 1 || $sign_up_fee > 0 && $subscription_trial_length == 0 && $subscription_length == 2) {
             // Non-recurring payments
             $paypal_args['src'] = 0;
         } else {
             $paypal_args['src'] = 1;
             if ($subscription_length > 0) {
                 if ($sign_up_fee > 0 && $subscription_trial_length == 0) {
                     // An initial period is being used to charge a sign-up fee
                     $subscription_length--;
                 }
                 $paypal_args['srt'] = $subscription_length / $subscription_interval;
             }
         }
         // Force return URL so that order description & instructions display
         $paypal_args['rm'] = 2;
     }
     return $paypal_args;
 }
Exemplo n.º 4
0
 /**
  * notify handler
  * @since 2.2.0
  */
 function notify_handler()
 {
     global $woocommerce;
     $redirect = get_permalink(woocommerce_get_page_id('cart'));
     if (isset($_GET['stripeListener']) && $_GET['stripeListener'] == 'stripe') {
         if ($this->debug == 'yes') {
             $this->log->add('stripe', __('Post form: ', 'woocommerce') . print_r($_POST, true));
         }
         if ($woocommerce->verify_nonce('stripe_payment_submit')) {
             $order_id = $this->get_request('order');
             $order = new WC_Order($order_id);
             if ($order->order_key != $_REQUEST['key']) {
                 $woocommerce->add_error(__('Order key do not match!', 'woocommerce'));
                 wp_redirect($redirect);
                 //redirect page
                 exit;
             }
             $order_items = $order->get_items();
             $product = $order->get_product_from_item(array_pop($order_items));
             $this->product_type = $product->product_type;
             $params = $this->get_params($order);
             if ($this->debug == 'yes') {
                 $this->log->add('stripe', __('Post paramaters: ', 'woocommerce') . print_r($params, true));
             }
             $request = new stripe_request($this->get_config());
             $response = '';
             if ('subscription' == $product->product_type || 'subscription_variation' == $product->product_type) {
                 if ($this->debug == 'yes') {
                     $this->log->add('stripe', 'Starting subscription ... ');
                 }
                 $sign_up_fee = WC_Subscriptions_Order::get_sign_up_fee($order);
                 $price_per_period = WC_Subscriptions_Order::get_price_per_period($order);
                 $subscription_interval = WC_Subscriptions_Order::get_subscription_interval($order);
                 $subscription_length = WC_Subscriptions_Order::get_subscription_length($order);
                 $subscription_trial_length = WC_Subscriptions_Order::get_subscription_trial_length($order);
                 // Subscription unit of duration
                 switch (strtolower(WC_Subscriptions_Order::get_subscription_period($order))) {
                     case 'year':
                         $subscription_period = 'year';
                         break;
                     case 'month':
                     default:
                         $subscription_period = 'month';
                         break;
                 }
                 // add more param
                 $sparams = array();
                 $plan_name = get_post($product->id)->post_title;
                 //$plan_id = $product->id;
                 $plan_id = $order_id;
                 $response = $request->send($plan_id, 'retrieve');
                 if (!$response->success()) {
                     //create plan if not exists
                     if ($this->debug == 'yes') {
                         $this->log->add('stripe', sprintf(__('Create plan id: %s', 'woocommerce'), $plan_id));
                     }
                     $response = $request->send(array('amount' => number_format($price_per_period, 2, '.', '') * 100, 'interval' => $subscription_period, "currency" => $params['currency'], "id" => $plan_id, 'name' => $plan_name, 'trial_period_days' => $subscription_trial_length), 'plan');
                 }
                 if ($response->success()) {
                     if ($this->debug == 'yes') {
                         $this->log->add('stripe', print_r($response, true));
                     }
                     $response = $request->send(array("card" => $params['card'], "plan" => $plan_id, "email" => $order->billing_email), 'customer');
                     if ($this->debug == 'yes') {
                         $this->log->add('stripe', 'Customer create: ' . print_r($response->results, true));
                     }
                     if ($response->success() && $sign_up_fee > 0) {
                         $response = $request->send(array("customer" => $response->results->id, "amount" => number_format($sign_up_fee, 2, '.', '') * 100, "currency" => $params['currency'], "description" => __("Sign-up Fee", 'woocommerce')));
                         if ($this->debug == 'yes') {
                             $this->log->add('stripe', 'Sign-up fee response: ' . print_r($response->results, true));
                         }
                     }
                 } else {
                     //error
                     if ($this->debug == 'yes') {
                         $this->log->add('stripe', __('Error can not create plan', 'woocommerce'));
                     }
                     $woocommerce->add_error(__('Error can not create plan', 'woocommerce'));
                 }
             } else {
                 $response = $request->send($params);
             }
             //response result
             if ($response->success()) {
                 $order->add_order_note(__('Stripe payment completed', 'woocommerce') . ' (Transaction ID: ' . $response->get_transaction_id() . ')');
                 $order->payment_complete();
                 $woocommerce->cart->empty_cart();
                 $redirect = add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(woocommerce_get_page_id('thanks'))));
             } else {
                 if ($this->debug == 'yes') {
                     $this->log->add('stripe', 'Error: ' . $response->get_error(), true);
                 }
                 $woocommerce->add_error(__('Payment error', 'woocommerce') . ': ' . $response->get_error() . '');
             }
         }
         wp_redirect($redirect);
         //redirect page
         exit;
     }
 }