/** * updateSubscriptions * -------------------------------------------------- * Updating the StripeSubscriptions. * @returns The stripe plans. * @throws StripeNotConnected * -------------------------------------------------- */ public function updateSubscriptions() { // Connecting to stripe. // Deleting all subscription to avoid constraints. $this->updatePlans(); $subscriptions = array(); foreach ($this->getCustomers() as $customer) { $decodedData = json_decode($this->loadJSON(\Stripe\Customer::retrieve($customer['id'])->subscriptions->all()), TRUE); foreach ($decodedData['data'] as $subscription) { $new_subscription = new StripeSubscription(array('subscription_id' => $subscription['id'], 'start' => $subscription['start'], 'status' => $subscription['status'], 'customer' => $subscription['customer'], 'ended_at' => $subscription['ended_at'], 'canceled_at' => $subscription['canceled_at'], 'quantity' => $subscription['quantity'], 'discount' => $subscription['discount'], 'trial_start' => $subscription['trial_start'], 'trial_end' => $subscription['trial_start'], 'discount' => $subscription['discount'])); $plan = StripePlan::where('plan_id', $subscription['plan']['id'])->first(); if ($plan === null) { // Stripe 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; }
/** * handleSubscriptionDeletion * -------------------------------------------------- * Handling subscription deletion. * On deletion we'll have to create a subscription. * @param eventThe specific stripe event. * -------------------------------------------------- */ private function handleSubscriptionDeletion($event) { $subscriptionData = $event['data']['object']; $subscription = new StripeSubscription(array('subscription_id' => $subscriptionData['id'], 'start' => $subscriptionData['start'], 'status' => $subscriptionData['status'], 'customer' => $subscriptionData['customer'], 'ended_at' => $subscriptionData['ended_at'], 'canceled_at' => $subscriptionData['canceled_at'], 'quantity' => $subscriptionData['quantity'], 'discount' => $subscriptionData['discount'], 'trial_start' => $subscriptionData['trial_start'], 'trial_end' => $subscriptionData['trial_start'], 'discount' => $subscriptionData['discount'])); // Creating the plan if necessary. $plan = StripePlan::where('plan_id', $subscriptionData['plan']['id'])->first(); if (is_null($plan)) { return; } $subscription->plan()->associate($plan); $subscription->push(); }