/** * updatePlans * -------------------------------------------------- * Updating the current braintree Plans. * @return The braintree plans. * @throws BraintreeNotConnected * -------------------------------------------------- */ public function updatePlans() { // Connecting to stripe, and making query. try { $braintreePlans = Braintree_Plan::all(); } catch (Exception $e) { // Something went wrong. return; } // Getting the plans. $plans = []; foreach ($braintreePlans as $plan) { $new_plan = new BraintreePlan(array('plan_id' => $plan->id, 'name' => $plan->name, 'billing_frequency' => $plan->billingFrequency, 'price' => $plan->price, 'currency' => $plan->currencyIsoCode, 'billing_day' => $plan->billingDayOfMonth)); $new_plan->user()->associate($this->user); array_push($plans, $new_plan); } // Delete old, save new. foreach (BraintreePlan::where('user_id', $this->user->id)->get() as $stripePlan) { BraintreeSubscription::where('plan_id', $stripePlan->id)->delete(); $stripePlan->delete(); } foreach ($plans as $plan) { $plan->save(); } return $plans; }
/** * 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(); } }