/**
  * 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;
 }
 /**
  * Process the captured payment when changing order status to completed
  *
  * @access      public
  * @param       int $order_id
  * @return      mixed
  */
 public function order_status_completed($order_id = null)
 {
     if (!$order_id) {
         $order_id = $_POST['order_id'];
     }
     if (get_post_meta($order_id, 'capture', true)) {
         $params = array();
         if (isset($_POST['amount'])) {
             $params['amount'] = round($_POST['amount']);
         }
         $transaction_id = get_post_meta($order_id, '_transaction_id', true);
         $charge = S4WC_API::capture_charge($transaction_id, $params);
         return $charge;
     }
 }
 /**
  * Gives front-end view of saved cards in the account page
  *
  * @access      public
  * @return      void
  */
 public function account_saved_cards()
 {
     global $s4wc;
     if ($s4wc->settings['saved_cards'] === 'yes') {
         // If user requested to delete a card, delete it
         if (isset($_POST['delete_card']) && wp_verify_nonce($_POST['_wpnonce'], 's4wc_delete_card')) {
             S4WC_API::delete_card(get_current_user_id(), intval($_POST['delete_card']));
         }
         $user_meta = get_user_meta(get_current_user_id(), $s4wc->settings['stripe_db_location'], true);
         $credit_cards = isset($user_meta['cards']) ? $user_meta['cards'] : false;
         $args = array('user_meta' => $user_meta, 'credit_cards' => $credit_cards);
         s4wc_get_template('saved-cards.php', $args);
     }
 }
 /**
  * Process the captured payment when changing order status to completed
  *
  * @access      public
  * @param       int $order_id
  * @return      mixed
  */
 public function order_status_completed($order_id = null)
 {
     if (!$order_id) {
         $order_id = $_POST['order_id'];
     }
     // `_s4wc_capture` added in 1.35, let `capture` last for a few more updates before removing
     if (get_post_meta($order_id, '_s4wc_capture', true) || get_post_meta($order_id, 'capture', true)) {
         $order = new WC_Order($order_id);
         $params = array('amount' => isset($_POST['amount']) ? $_POST['amount'] : $order->order_total * 100, 'expand[]' => 'balance_transaction');
         try {
             $charge = S4WC_API::capture_charge($order->transaction_id, $params);
             if ($charge) {
                 $order->add_order_note(sprintf(__('%s payment captured.', 'stripe-for-woocommerce'), get_class($this)));
                 // Save Stripe fee
                 if (isset($charge->balance_transaction) && isset($charge->balance_transaction->fee)) {
                     $stripe_fee = number_format($charge->balance_transaction->fee / 100, 2, '.', '');
                     update_post_meta($order_id, 'Stripe Fee', $stripe_fee);
                 }
             }
         } catch (Exception $e) {
             $order->add_order_note(sprintf(__('%s payment failed to capture. %s', 'stripe-for-woocommerce'), get_class($this), $this->get_error_message($e)));
         }
     }
 }
Example #5
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;
 }
 /**
  * Delete data from Stripe's servers by passing an API endpoint
  *
  * @access      public
  * @param       string $delete_location
  * @return      array
  */
 public static function delete_data($delete_location)
 {
     global $s4wc;
     $response = wp_remote_post(self::$api_endpoint . 'v1/' . $delete_location, array('method' => 'DELETE', 'headers' => array('Authorization' => 'Basic ' . base64_encode($s4wc->settings['secret_key'] . ':')), 'timeout' => 70, 'sslverify' => false, 'user-agent' => 'WooCommerce-Stripe'));
     return S4WC_API::parse_response($response);
 }
 /**
  * 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;
 }