/** * @return mixed */ public function getSubscriptions() { return $this->hasMany(SubscriptionModel::className(), ['userId' => 'id'])->orderBy(['createdAt' => SORT_DESC]); }
/** * Create a new Stripe subscription. * * @param string|null $token * @param array $options * * @return SubscriptionModel * * @throws Exception */ public function create($token = null, array $options = []) { $customer = $this->getStripeCustomer($token, $options); $subscription = $customer->subscriptions->create($this->buildPayload()); if ($this->skipTrial) { $trialEndsAt = null; } else { $trialEndsAt = $this->trialDays ? Carbon::now()->addDays($this->trialDays) : null; } $subscriptionModel = new SubscriptionModel(['userId' => $this->user->id, 'name' => $this->name, 'stripeId' => $subscription->id, 'stripePlan' => $this->plan, 'quantity' => $this->quantity, 'trialEndAt' => $trialEndsAt, 'endAt' => null]); if ($subscriptionModel->save()) { return $subscriptionModel; } else { throw new Exception('Subscription was not saved.'); } }