/**
  * Process the subscription payment and return the result
  *
  * @access      public
  * @param       int $amount
  * @return      array
  */
 public function process_subscription_payment($amount = 0)
 {
     global $s4wc;
     // Can't send to stripe without a value, assume it's good to go.
     if ($amount === 0) {
         return true;
     }
     // Get customer id
     $customer = get_user_meta($this->order->user_id, $s4wc->settings['stripe_db_location'], true);
     // Allow options to be set without modifying sensitive data like amount, currency, etc.
     $stripe_charge_data = apply_filters('s4wc_subscription_charge_data', array(), $this->order);
     // Set up basics for charging
     $stripe_charge_data['amount'] = $amount * 100;
     // amount in cents
     $stripe_charge_data['currency'] = strtolower(get_woocommerce_currency());
     $stripe_charge_data['customer'] = $customer['customer_id'];
     $stripe_charge_data['card'] = $customer['default_card'];
     $stripe_charge_data['description'] = $this->get_charge_description('subscription');
     $stripe_charge_data['expand[]'] = 'balance_transaction';
     $charge = S4WC_API::create_charge($stripe_charge_data);
     if (isset($charge->id)) {
         $this->order->add_order_note(sprintf(__('Subscription paid (%s)', 'stripe-for-woocommerce'), $charge->id));
         return $charge;
     }
     return false;
 }
Example #2
0
 /**
  * Set up the charge that will be sent to Stripe
  *
  * @access      private
  * @return      void
  */
 private function charge_set_up()
 {
     global $s4wc;
     $customer_info = get_user_meta($this->order->user_id, $s4wc->settings['stripe_db_location'], true);
     // Allow options to be set without modifying sensitive data like amount, currency, etc.
     $stripe_charge_data = apply_filters('s4wc_charge_data', array(), $this->form_data, $this->order);
     // Set up basics for charging
     $stripe_charge_data['amount'] = $this->form_data['amount'];
     // amount in cents
     $stripe_charge_data['currency'] = $this->form_data['currency'];
     $stripe_charge_data['capture'] = $this->settings['charge_type'] == 'capture' ? 'true' : 'false';
     $stripe_charge_data['expand[]'] = 'balance_transaction';
     // Make sure we only create customers if a user is logged in and wants to save their card
     if (is_user_logged_in() && $this->settings['saved_cards'] === 'yes' && ($this->form_data['save_card'] || $this->form_data['chosen_card'] !== 'new')) {
         // Add a customer or retrieve an existing one
         $customer = $this->get_customer();
         $stripe_charge_data['card'] = $customer['card'];
         $stripe_charge_data['customer'] = $customer['customer_id'];
         // Update default card
         if (count($customer_info['cards']) && $this->form_data['chosen_card'] !== 'new') {
             $default_card = $customer_info['cards'][intval($this->form_data['chosen_card'])]['id'];
             S4WC_DB::update_customer($this->order->user_id, array('default_card' => $default_card));
         }
     } else {
         // Set up one time charge
         $stripe_charge_data['card'] = $this->form_data['token'];
     }
     // Charge description
     $stripe_charge_data['description'] = $this->get_charge_description();
     // Create the charge on Stripe's servers - this will charge the user's card
     $charge = S4WC_API::create_charge($stripe_charge_data);
     $this->charge = $charge;
     $this->transaction_id = $charge->id;
 }
 /**
  * Send form data to Stripe
  * Handles sending the charge to an existing customer, a new customer (that's logged in), or a guest
  *
  * @access      protected
  * @return      bool
  */
 protected function send_to_stripe()
 {
     global $s4wc;
     // Get the credit card details submitted by the form
     $form_data = $this->get_form_data();
     // If there are errors on the form, don't bother sending to Stripe.
     if ($form_data['errors'] == 1) {
         return;
     }
     // Set up the charge for Stripe's servers
     try {
         // Allow options to be set without modifying sensitive data like amount, currency, etc.
         $stripe_charge_data = apply_filters('s4wc_charge_data', array(), $form_data, $this->order);
         // Set up basics for charging
         $stripe_charge_data['amount'] = $form_data['amount'];
         // amount in cents
         $stripe_charge_data['currency'] = $form_data['currency'];
         $stripe_charge_data['capture'] = $this->settings['charge_type'] == 'capture' ? 'true' : 'false';
         // Make sure we only create customers if a user is logged in
         if (is_user_logged_in() && $s4wc->settings['saved_cards'] === 'yes') {
             // Add a customer or retrieve an existing one
             $customer = $this->get_customer($form_data);
             $stripe_charge_data['card'] = $customer['card'];
             $stripe_charge_data['customer'] = $customer['customer_id'];
             // Update default card
             if (count($this->stripe_customer_info['cards']) && $form_data['chosen_card'] !== 'new') {
                 $default_card = $this->stripe_customer_info['cards'][(int) $form_data['chosen_card']]['id'];
                 S4WC_DB::update_customer($this->current_user_id, array('default_card' => $default_card));
             }
         } else {
             // Set up one time charge
             $stripe_charge_data['card'] = $form_data['token'];
         }
         // Set a default name, override with a product name if it exists for Stripe's dashboard
         $product_name = __('Purchases', 'stripe-for-woocommerce');
         $order_items = $this->order->get_items();
         // Grab first product name and use it
         foreach ($order_items as $key => $item) {
             $product_name = $item['name'];
             break;
         }
         // Charge description
         $charge_description = sprintf(__('Payment for %s (Order: %s)', 'stripe-for-woocommerce'), $product_name, $this->order->get_order_number());
         $stripe_charge_data['description'] = apply_filters('s4wc_charge_description', $charge_description, $form_data, $this->order);
         // Create the charge on Stripe's servers - this will charge the user's card
         $charge = S4WC_API::create_charge($stripe_charge_data);
         $this->transaction_id = $charge->id;
         // Save data for the "Capture"
         update_post_meta($this->order->id, '_transaction_id', $this->transaction_id);
         update_post_meta($this->order->id, 'capture', strcmp($this->settings['charge_type'], 'authorize') == 0);
         // Save data for cross-reference between Stripe Dashboard and WooCommerce
         update_post_meta($this->order->id, 'customer_id', $customer['customer_id']);
         return true;
     } catch (Exception $e) {
         // Stop page reload if we have errors to show
         unset(WC()->session->reload_checkout);
         $message = $this->get_stripe_error_message($e);
         wc_add_notice(__('Error:', 'stripe-for-woocommerce') . ' ' . $message, 'error');
         return false;
     }
 }
 /**
  * Process the subscription payment and return the result
  *
  * @access      public
  * @param       WC_Order $order
  * @param       int $amount
  * @return      array
  */
 public function process_subscription_payment($order, $amount = 0)
 {
     global $s4wc;
     // Can't send to stripe without a value, assume it's good to go.
     if ($amount === 0) {
         return true;
     }
     // Get customer id
     $customer = get_user_meta($order->user_id, $s4wc->settings['stripe_db_location'], true);
     // Set a default name, override with a product name if it exists for Stripe's dashboard
     $product_name = __('Subscription', 'stripe-for-woocommerce');
     $order_items = $order->get_items();
     // Grab first subscription name and use it
     foreach ($order_items as $key => $item) {
         if (isset($item['subscription_status'])) {
             $product_name = $item['name'];
             break;
         }
     }
     // Default charge description
     $charge_description = sprintf(__('Payment for %s (Order: %s)', 'stripe-for-woocommerce'), $product_name, $order->get_order_number());
     // Allow options to be set without modifying sensitive data like amount, currency, etc.
     $charge_data = apply_filters('s4wc_subscription_charge_data', array(), $order);
     // Set up basics for charging
     $charge_data['amount'] = $amount * 100;
     // amount in cents
     $charge_data['currency'] = strtolower(get_woocommerce_currency());
     $charge_data['customer'] = $customer['customer_id'];
     $charge_data['card'] = $customer['default_card'];
     $charge_data['description'] = apply_filters('s4wc_subscription_charge_description', $charge_description, $order);
     $charge = S4WC_API::create_charge($charge_data);
     if (isset($charge->id)) {
         $order->add_order_note(sprintf(__('Subscription paid (%s)', 'stripe-for-woocommerce'), $charge->id));
         return $charge;
     }
     return false;
 }