Пример #1
0
 /**
  * resolves all existing plans
  *
  * @param array $config
  */
 private function resolvePlans(array $config)
 {
     foreach ($config as $id => $planData) {
         $plan = Plan::createFromArray($id, $planData);
         $this->plans->put($id, $plan);
     }
 }
 /**
  * saves a subscription to database
  *
  * @param \Ipunkt\Subscriptions\Subscription\Subscription $subscription
  * @param \Ipunkt\Subscriptions\Plans\Plan $plan
  * @param \Ipunkt\Subscriptions\Plans\PaymentOption $paymentOption
  * @param \Carbon\Carbon $startDate
  * @param string $mode
  *
  * @return \Ipunkt\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;
 }