/**
  * updateSubscriptions
  * --------------------------------------------------
  * Updating the BraintreeSubscriptions.
  * @return The stripe plans.
  * @throws BraintreeNotConnected
  * --------------------------------------------------
  */
 public function updateSubscriptions()
 {
     // Updating plans to be up to date.
     $this->updatePlans();
     $subscriptions = array();
     // Clollecting subscriptions.
     try {
         $braintreeSubscriptions = Braintree_Subscription::search(array(Braintree_SubscriptionSearch::status()->in(array(Braintree_Subscription::ACTIVE))));
     } catch (Exception $e) {
         // Something went wrong.
         return;
     }
     foreach ($braintreeSubscriptions as $subscription) {
         $new_subscription = new BraintreeSubscription(array('start' => $subscription->firstBillingDate, 'status' => $subscription->status));
         $plan = BraintreePlan::where('plan_id', $subscription->planId)->first();
         if ($plan === null) {
             // Braintree integrity error, link to a non-existing plan.
             return array();
         }
         $new_subscription->plan()->associate($plan);
         array_push($subscriptions, $new_subscription);
     }
     // Save new.
     foreach ($subscriptions as $subscription) {
         $subscription->save();
     }
     return $subscriptions;
 }
 /**
  * disconnect
  * --------------------------------------------------
  * Disconnecting the user from braintree.
  * @throws BraintreeNotConnected
  * --------------------------------------------------
  */
 public function disconnect()
 {
     /* Check valid connection */
     if (!$this->user->isBraintreeConnected()) {
         throw new BraintreeNotConnected();
     }
     $this->user->connections()->where('service', 'braintree')->delete();
     /* Deleting all widgets, plans, subscribtions */
     foreach ($this->user->widgets() as $widget) {
         if ($widget->descriptor->category == 'braintree') {
             /* Saving data while it is accessible. */
             $dataID = 0;
             if (!is_null($widget->data)) {
                 $dataID = $widget->data->id;
             }
             $widget->delete();
             /* Deleting data if it was present. */
             if ($dataID > 0) {
                 Data::find($dataID)->delete();
             }
         }
     }
     /* Deleting all plans. */
     foreach ($this->user->braintreePlans as $braintreePlan) {
         BraintreeSubscription::where('plan_id', $braintreePlan->id)->delete();
         $braintreePlan->delete();
     }
 }