Example #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.
     }
 }
 public function run()
 {
     /**
      * Truncate Product Tables before seed faker data
      */
     DB::table('subscription_plans')->truncate();
     Plan::create(array('name' => 'Monthly subscription', 'interval' => 'month', 'amount' => 5, 'currency' => 'USD', 'interval_count' => 1, 'trial_period_days' => 60));
 }
Example #3
0
 /**
  * Handles a Request to convert it to a Response.
  *
  * When $catch is true, the implementation must catch all exceptions
  * and do its best to convert them to a Response instance.
  *
  * @param Request $request A Request instance
  * @param int     $type The type of the request
  *                          (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  * @param bool    $catch Whether to catch exceptions or not
  *
  * @return Response A Response instance
  *
  * @throws \Exception When an Exception occurs during processing
  *
  * @api
  */
 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
 {
     $this->app->boot();
     if ($authorization = $request->headers->get('authorization')) {
         $token = $this->validateToken($authorization);
         // 1. Check owner_type
         if ($token['owner_type'] == 'user') {
             if ($driver = EloquentDriverRepository::findOrFail($token['owner_id'])) {
                 $subscription = $driver->subscription;
                 /**
                  * Need charge commission fee.
                  */
                 if ($subscription->needChargeCommission() && !$driver->paidCommission($subscription->current_period_end)) {
                     $this->payCommission($subscription, $driver);
                 }
                 /**
                  * Need charge subscription fee.
                  */
                 if ($subscription->needChargeSubscription()) {
                     if ($subscriptionPayment = $this->paypal->subscriptionPayment(EloquentPlanRepository::findOrFail($subscription->plan_id), $driver)) {
                         $subscription->renewSubscription(EloquentPlanRepository::findOrFail($subscription->plan_id), $driver);
                     } else {
                         $subscription->expired();
                         // Todo:: record errors and send email to info user account expired.
                     }
                 }
                 /**
                  *
                  * Canceled status.
                  *
                  * Don't charge subscription, change driver status to 0, and change driver subscription to past_due.
                  *
                  */
                 if ($subscription->status == 'canceled') {
                     if (Carbon::now() >= $subscription->current_period_end) {
                         $subscription->expired();
                         // Todo:: send email info user account expired.
                     }
                 }
             }
         }
     }
     $response = $this->app->handle($request, $type, $catch);
     return $response;
 }
Example #4
0
 /**
  * Driver renew the subscription status when the account expired.
  *
  * @param null $driver
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function renewSubscription($driver = null)
 {
     try {
         \DB::beginTransaction();
         if (is_null($driver)) {
             $driver = $this->getAuthenticatedDriver();
         }
         // Get driver subscription.
         $subscription = $driver->subscription;
         if ($subscription->status != 'past_due') {
             return $this->setStatusCode(406)->respondWithError('Account is still active.');
         }
         $plan = EloquentPlanRepository::findOrFail($subscription->plan_id);
         if ($subscriptionPayment = $this->paypal->subscriptionPayment($plan, $driver)) {
             $driver->paypal_metadata_id = \Input::get('metadata_id');
             $subscription->renewSubscription(EloquentPlanRepository::findOrFail($subscription->plan_id), $driver);
             \DB::commit();
             return $this->respondWithItem($subscription->driver);
         }
         return $this->setStatusCode(406)->respondWithError('Subscription payment failed.');
     } catch (\Exception $e) {
         return $this->errorInternalError($e->getMessage());
     }
 }
 public function transformSubscriptionPlan($id)
 {
     return EloquentPlanRepository::find($id);
 }