Exemplo n.º 1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     try {
         /**
          * This command will be run by crontab every night,
          * and check the all the drivers if the subscription ended.
          */
         $drivers = EloquentDriverRepository::where('status', '=', 1)->get();
         foreach ($drivers as $key => $driver) {
             $subscription = $driver->subscription;
             /**
              * Need charge commission fee.
              */
             if ($subscription->needChargeCommission() && !$driver->paidCommission($subscription->current_period_end)) {
                 $this->payCommission($subscription, $driver);
             }
             /**
              * Need change subscription.
              */
             if ($subscription->needChargeSubscription()) {
                 $plan = EloquentPlanRepository::findOrFail($subscription->plan_id);
                 if ($subscriptionPayment = $this->paypal->subscriptionPayment($plan, $driver)) {
                     $subscription->subscribe(EloquentPlanRepository::findOrFail($subscription->plan_id));
                     // Todo:: record all successful subscription payment.
                     return;
                 }
                 // Todo:: record all failed subscription payment
             }
         }
     } catch (\Exception $e) {
         print_r($e->getMessage());
         // Todo:: record the exception in subscription log.
     }
 }