/**
  * When a member cancels a subscription we need to notify Stripe to also
  * cancel the Stripe subscription.
  *
  * @since  1.0.0
  * @param MS_Model_Relationship $subscription The membership relationship.
  */
 public function cancel_membership($subscription)
 {
     parent::cancel_membership($subscription);
     $this->_api->set_gateway($this);
     $customer = $this->_api->find_customer($subscription->get_member());
     $membership = $subscription->get_membership();
     $stripe_sub = false;
     if ($customer) {
         $stripe_sub = $this->_api->get_subscription($customer, $membership);
     }
     if ($stripe_sub) {
         $stripe_sub->cancel(array('at_period_end' => true));
     }
 }
 /**
  * Request automatic payment to the gateway.
  *
  * @since  1.0.0
  * @api
  *
  * @param MS_Model_Relationship $subscription The related membership relationship.
  * @return bool True on success.
  */
 public function request_payment($subscription)
 {
     $was_paid = false;
     $note = '';
     $external_id = '';
     do_action('ms_gateway_stripe_request_payment_before', $subscription, $this);
     $this->_api->set_gateway($this);
     $member = $subscription->get_member();
     $invoice = $subscription->get_current_invoice();
     if (!$invoice->is_paid()) {
         try {
             $customer = $this->_api->find_customer($member);
             if (!empty($customer)) {
                 if (0 == $invoice->total) {
                     $invoice->changed();
                     $success = true;
                     $note = __('No payment for free membership', 'membership2');
                 } else {
                     $charge = $this->_api->charge($customer, $invoice->total, $invoice->currency, $invoice->name);
                     $external_id = $charge->id;
                     if (true == $charge->paid) {
                         $was_paid = true;
                         $invoice->pay_it($this->id, $external_id);
                         $note = __('Payment successful', 'membership2');
                     } else {
                         $note = __('Stripe payment failed', 'membership2');
                     }
                 }
             } else {
                 $note = "Stripe customer is empty for user {$member->username}";
                 MS_Helper_Debug::log($note);
             }
         } 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);
         }
     } else {
         // Invoice was already paid earlier.
         $was_paid = true;
         $note = __('Invoice already paid', 'membership2');
     }
     do_action('ms_gateway_transaction_log', self::ID, 'request', $was_paid, $subscription->id, $invoice->id, $invoice->total, $note, $external_id);
     do_action('ms_gateway_stripe_request_payment_after', $subscription, $was_paid, $this);
     return $was_paid;
 }