/**
  * Create customer on stripe servers
  *
  * @access      public
  * @param       array $customer_data
  * @return      array
  */
 public static function create_customer($customer_data)
 {
     // Create a customer on Stripe servers
     $customer = S4WC_API::post_data($customer_data, 'customers');
     $active_card = $customer->cards->data[array_search($customer->default_card, $customer->cards->data)];
     // Save users customer information for later use
     $customerArray = array('customer_id' => $customer->id, 'card' => array('id' => $active_card->id, 'brand' => $active_card->type, 'last4' => $active_card->last4, 'exp_year' => $active_card->exp_year, 'exp_month' => $active_card->exp_month), 'default_card' => $active_card->id);
     S4WC_DB::update_customer(get_current_user_id(), $customerArray);
     return $customer;
 }
Beispiel #2
0
 /**
  * Create customer on stripe servers
  *
  * @access      public
  * @param       int $user_id
  * @param       array $customer_data
  * @return      array
  */
 public static function create_customer($user_id, $customer_data)
 {
     // Create a customer on Stripe servers
     $customer = S4WC_API::post_data($customer_data, 'customers');
     $key = S4WC_API::find_card_index($customer->sources->data, 'id', $customer->default_source);
     if ($key > -1) {
         $active_card = $customer->sources->data[$key];
         // Save users customer information for later use
         $customerArray = array('customer_id' => $customer->id, 'card' => array('id' => $active_card->id, 'brand' => $active_card->type, 'last4' => $active_card->last4, 'exp_year' => $active_card->exp_year, 'exp_month' => $active_card->exp_month), 'default_card' => $active_card->id);
         S4WC_DB::update_customer($user_id, $customerArray);
         return $customer;
     } else {
         return false;
     }
 }
Beispiel #3
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;
 }
 /**
  * Create a customer if the current user isn't already one
  * Retrieve a customer if one already exists
  * Add a card to a customer if necessary
  *
  * @access      protected
  * @param       array $form_data
  * @return      array
  */
 protected function get_customer($form_data)
 {
     $output = array();
     if (!$this->stripe_customer_info) {
         // Allow options to be set without modifying sensitive data like token, email, etc
         $customer_data = apply_filters('s4wc_customer_data', array(), $form_data, $this->order);
         // Set default customer description
         $customer_description = $this->current_user->user_login . ' (#' . $this->current_user_id . ' - ' . $this->current_user->user_email . ') ' . $form_data['customer']['name'];
         // username (user_id - user_email) Full Name
         // Set up basics for customer
         $customer_data['description'] = apply_filters('s4wc_customer_description', $customer_description, $form_data, $this->order);
         $customer_data['email'] = $form_data['customer']['billing_email'];
         $customer_data['card'] = $form_data['token'];
         // Create the customer in the api with the above data
         $customer = S4WC_API::create_customer($customer_data);
         $output['card'] = $customer->default_card;
     } else {
         // If the user is already registered on the stripe servers, retreive their information
         $customer = S4WC_API::get_customer($this->stripe_customer_info['customer_id']);
         // If the user doesn't have cards or is adding a new one
         if (!count($this->stripe_customer_info['cards']) || $form_data['chosen_card'] == 'new') {
             // Add new card on stripe servers and make default
             $card = S4WC_API::update_customer($this->stripe_customer_info['customer_id'] . '/cards', array('card' => $form_data['token']));
             // Add new customer details to database
             $customerArray = array('customer_id' => $customer->id, 'card' => array('id' => $card->id, 'brand' => $card->type, 'last4' => $card->last4, 'exp_year' => $card->exp_year, 'exp_month' => $card->exp_month), 'default_card' => $card->id);
             S4WC_DB::update_customer($this->current_user_id, $customerArray);
             $output['card'] = $card->id;
         } else {
             $output['card'] = $this->stripe_customer_info['cards'][(int) $form_data['chosen_card']]['id'];
         }
     }
     // Set up charging data to include customer information
     $output['customer_id'] = $customer->id;
     return $output;
 }
 /**
  * Set up the charge that will be sent to Stripe
  *
  * @access      private
  * @return      void
  */
 private function charge_set_up()
 {
     global $s4wc;
     // Add a customer or retrieve an existing one
     $customer = $this->get_customer();
     $customer_info = get_user_meta($this->order->user_id, $s4wc->settings['stripe_db_location'], true);
     // Update default card
     if ($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));
     }
     $initial_payment = WC_Subscriptions_Order::get_total_initial_payment($this->order);
     $charge = $this->process_subscription_payment($initial_payment);
     $this->charge = $charge;
     $this->transaction_id = $charge->id;
 }
 /**
  * Send subscription 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 subscription_to_stripe()
 {
     // 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 {
         // Add a customer or retrieve an existing one
         $description = $this->current_user->user_login . ' (#' . $this->current_user_id . ' - ' . $this->current_user->user_email . ') ' . $form_data['customer']['name'];
         // username (user_id - user_email) Full Name
         $customer = $this->get_customer($description, $form_data);
         // Update default card
         if ($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));
         }
         $initial_payment = WC_Subscriptions_Order::get_total_initial_payment($this->order);
         $charge = $this->process_subscription_payment($this->order, $initial_payment);
         $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(__('Subscription Error:', 'stripe-for-woocommerce') . ' ' . $message, 'error');
         return false;
     }
 }