/**
  * Processes purchase action.
  *
  * @since  1.0.0
  * @api
  *
  * @param MS_Model_Relationship $subscription The related membership relationship.
  */
 public function process_purchase($subscription)
 {
     $success = false;
     $note = '';
     $token = '';
     $external_id = '';
     $error = false;
     do_action('ms_gateway_stripe_process_purchase_before', $subscription, $this);
     $this->_api->set_gateway($this);
     $member = $subscription->get_member();
     $invoice = $subscription->get_current_invoice();
     if (!empty($_POST['stripeToken'])) {
         lib3()->array->strip_slashes($_POST, 'stripeToken');
         $token = $_POST['stripeToken'];
         $external_id = $token;
         try {
             $customer = $this->_api->get_stripe_customer($member, $token);
             if (0 == $invoice->total) {
                 // Free, just process.
                 $invoice->changed();
                 $success = true;
                 $note = __('No payment for free membership', 'membership2');
             } else {
                 // Send request to gateway.
                 $charge = $this->_api->charge($customer, $invoice->total, $invoice->currency, $invoice->name);
                 if (true == $charge->paid) {
                     $invoice->pay_it($this->id, $charge->id);
                     $note = __('Payment successful', 'membership2');
                     $note .= ' - Token: ' . $token;
                     $success = true;
                 } else {
                     $note = __('Stripe payment failed', 'membership2');
                 }
             }
         } catch (Exception $e) {
             $note = 'Stripe error: ' . $e->getMessage();
             MS_Model_Event::save_event(MS_Model_Event::TYPE_PAYMENT_FAILED, $subscription);
             MS_Helper_Debug::log($note);
             $error = $e;
         }
     } else {
         $note = 'Stripe gateway token not found.';
         MS_Helper_Debug::log($note);
     }
     do_action('ms_gateway_transaction_log', self::ID, 'process', $success, $subscription->id, $invoice->id, $invoice->total, $note, $external_id);
     if ($error) {
         throw $e;
     }
     return apply_filters('ms_gateway_stripe_process_purchase', $invoice, $this);
 }
 /**
  * Processes purchase action.
  *
  * @since  1.0.0
  * @param MS_Model_Relationship $subscription The related membership relationship.
  */
 public function process_purchase($subscription)
 {
     $success = false;
     $note = '';
     $error = false;
     do_action('ms_gateway_stripeplan_process_purchase_before', $subscription, $this);
     $this->_api->set_gateway($this);
     $member = $subscription->get_member();
     $invoice = $subscription->get_current_invoice();
     $token = '-';
     if (!empty($_POST['stripeToken'])) {
         lib2()->array->strip_slashes($_POST, 'stripeToken');
         $token = $_POST['stripeToken'];
         try {
             $customer = $this->_api->get_stripe_customer($member, $token);
             if (0 == $invoice->total) {
                 // Free, just process.
                 $invoice->changed();
                 $success = true;
                 $note = __('No payment for free membership', MS_TEXT_DOMAIN);
             } else {
                 // Get or create the subscription.
                 $stripe_sub = $this->_api->subscribe($customer, $invoice);
                 if ('active' == $stripe_sub->status) {
                     $invoice->pay_it($this->id, $stripe_sub->id);
                     $success = true;
                     $note = __('Payment successful', MS_TEXT_DOMAIN);
                     $note .= ' - Token: ' . $token;
                     $this->cancel_if_done($subscription, $stripe_sub);
                 } else {
                     $note = __('Stripe payment failed', MS_TEXT_DOMAIN);
                 }
             }
         } catch (Exception $e) {
             $note = 'Stripe error: ' . $e->getMessage();
             MS_Model_Event::save_event(MS_Model_Event::TYPE_PAYMENT_FAILED, $subscription);
             MS_Helper_Debug::log($note);
             $error = $e;
         }
     } else {
         $note = 'Stripe gateway token not found.';
         MS_Helper_Debug::log($note);
     }
     do_action('ms_gateway_transaction_log', self::ID, 'process', $success, $subscription->id, $invoice->id, $invoice->total, $note);
     if ($error) {
         throw $e;
     }
     return apply_filters('ms_gateway_stripeplan_process_purchase', $invoice, $this);
 }