示例#1
0
 /**
  * Delete card from stripe servers
  *
  * @access      public
  * @param       int $user_id
  * @param       int $position
  * @return      array
  */
 public static function delete_card($user_id, $position = 0)
 {
     global $s4wc;
     $user_meta = get_user_meta($user_id, $s4wc->settings['stripe_db_location'], true);
     S4WC_DB::delete_customer($user_id, array('card' => $user_meta['cards'][$position]['id']));
     return S4WC_API::delete_data('customers/' . $user_meta['customer_id'] . '/cards/' . $user_meta['cards'][$position]['id']);
 }
示例#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;
 }
 /**
  * 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;
     }
 }