/** * returns period sum * * @param PaymentOption $paymentOption * * @return float */ public function getPeriodSum(PaymentOption $paymentOption) { return $paymentOption->price() * $paymentOption->quantity(); }
/** * saves a subscription to database * * @param \vvMalko\Subscriptions\Subscription\Subscription $subscription * @param \vvMalko\Subscriptions\Plans\Plan $plan * @param \vvMalko\Subscriptions\Plans\PaymentOption $paymentOption * @param \Carbon\Carbon $startDate * @param string $mode * * @return \vvMalko\Subscriptions\Subscription\Subscription */ private function saveSubscription(Subscription $subscription, Plan $plan, PaymentOption $paymentOption, Carbon $startDate = null, $mode) { if ($startDate === null) { $startDate = Carbon::now(); } $subscription->trial_ends_at = with(clone $startDate)->addDays($paymentOption->period()); $subscription->subscription_ends_at = with(clone $startDate)->addDays($paymentOption->days()); if ($subscription->save()) { $period = new Period(['start' => $startDate, 'end' => $subscription->subscription_ends_at, 'invoice_sum' => $plan->getPeriodSum($paymentOption), 'invoice_date' => Carbon::now(), 'state' => Period::STATE_UNPAID]); $subscription->periods()->save($period); $event = $mode === self::MODE_CREATE ? new SubscriptionWasCreated($subscription, $plan, $paymentOption) : new SubscriptionWasUpdated($subscription, $plan, $paymentOption); $subscription->raise($event); if ($plan->isFree()) { $period->markAsPaid(''); // assign period events to be raised via the subscription raising method $events = $period->releaseEvents(); foreach ($events as $event) { $subscription->raise($event); } } } return $subscription; }