/** * 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 * @return array */ protected function get_customer() { global $s4wc; $output = array(); $customer_info = get_user_meta($this->order->user_id, $s4wc->settings['stripe_db_location'], true); if ($customer_info) { // If the user is already registered on the stripe servers, retreive their information $customer = S4WC_API::get_customer($customer_info['customer_id']); // If the user doesn't have cards or is adding a new one if ($this->form_data['chosen_card'] === 'new') { // Add new card on stripe servers and make default $card = S4WC_API::update_customer($customer_info['customer_id'] . '/cards', array('card' => $this->form_data['token'])); // Add new customer details to database S4WC_DB::update_customer($this->order->user_id, array('customer_id' => $customer->id, 'card' => array('id' => $card->id, 'brand' => $card->brand, 'last4' => $card->last4, 'exp_month' => $card->exp_month, 'exp_year' => $card->exp_year), 'default_card' => $card->id)); $output['card'] = $card->id; } else { $output['card'] = $customer_info['cards'][intval($this->form_data['chosen_card'])]['id']; } } else { $user = get_userdata($this->order->user_id); // Allow options to be set without modifying sensitive data like token, email, etc $customer_data = apply_filters('s4wc_customer_data', array(), $this->form_data, $this->order); // Set default customer description $customer_description = $user->user_login . ' (#' . $this->order->user_id . ' - ' . $user->user_email . ') ' . $this->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, $this->form_data, $this->order); $customer_data['email'] = $this->form_data['customer']['billing_email']; $customer_data['card'] = $this->form_data['token']; // Create the customer in the api with the above data $customer = S4WC_API::create_customer($this->order->user_id, $customer_data); $output['card'] = $customer->default_source; } // Set up charging data to include customer information $output['customer_id'] = $customer->id; // Save data for cross-reference between Stripe Dashboard and WooCommerce update_post_meta($this->order->id, 'Stripe Customer Id', $customer->id); return $output; }
/** * 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; }