public function viewMySubscription()
 {
     $user = Auth::user();
     $subscriber = DB::table('subscriptions')->join('users', 'users.id', '=', 'subscriptions.user_id')->join('stripe_plans', 'stripe_plans.plan_id', '=', 'subscriptions.stripe_plan')->select('subscriptions.name as plan_name', 'subscriptions.stripe_id', 'subscriptions.quantity', 'users.name as subscriber_name', 'users.email', 'users.stripe_id as cus_id', 'users.trial_ends_at as trial_date', 'stripe_plans.amount', 'stripe_plans.currency', 'stripe_plans.interval', 'stripe_plans.coupon_id', 'stripe_plans.trial_period_days as trial')->where('users.id', '=', Auth::user()->id)->first();
     if ($subscriber) {
         if ($subscriber->trial_date != null) {
             $date = Carbon::parse($subscriber->trial_date);
             $subscriber->trial_date = $date->toFormattedDateString();
         }
         if ($subscriber->coupon_id != null) {
             $coupon_info = StripeCoupon::where('coupon_id', '=', $subscriber->coupon_id)->first();
             $coupon = [];
             if ($coupon_info) {
                 $coupon['coupon_name'] = $coupon_info->coupon_name;
                 $redeem_by = Carbon::parse($coupon_info->redeem_by);
                 $coupon['redeem_by'] = $redeem_by->toFormattedDateString();
                 if ($coupon_info->percent_off != null) {
                     $coupon['discount'] = $coupon_info->percent_off . "%";
                     $coupon['discount_price'] = $subscriber->amount - $subscriber->amount * $coupon_info->percent_off / 100;
                 } else {
                     $coupon['discount'] = $coupon_info->amount_off / 100 . "/" . $coupon_info->currency;
                     $coupon['discount_price'] = $subscriber->amount - $coupon_info->amount_off / 100;
                 }
             }
             $subscriber->coupon = $coupon;
         }
     }
     return view('my-subscription', compact('subscriber', 'user'));
 }
Example #2
0
 public function redirectToVendorPlan()
 {
     if (Auth::guest()) {
         flash()->error('Your session is timed out. Please login and confirm again.');
         return Auth::guest('/login');
     }
     /*
      * View Plan for subscription
      * Using laravel cashier for plan retrieval
      * Method call from Route::get('/dashboard/vendor/plan', [ 'uses' => 'UserController@redirectToVendorPlan', 'as' => 'vendor.plan' ]);
      * */
     //$plan = StripePlan::where('active','=','1')->get();
     //$subscribed_plan =Subscription::where('user_id', Auth::user()->id)->get()->first()->name;
     $allplan = StripePlan::with('planFeature')->where('active', '=', '1')->get();
     //dd($allplan);
     $coupon_all = StripeCoupon::all();
     $plan_collect = [];
     foreach ($allplan as $plan) {
         if ($plan->interval_count > 1) {
             $plan->interval = "Every " . $plan->interval_count . " " . $plan->interval . "s";
         }
         $coupon_information = [];
         if ($plan->coupon_id != null) {
             foreach ($coupon_all as $coupon_single) {
                 if ($plan->coupon_id == $coupon_single->coupon_id) {
                     $coupon_information['coupon_name'] = $coupon_single->coupon_name;
                     $coupon_information['coupon_id'] = $coupon_single->coupon_id;
                     if ($coupon_single->percent_off != null) {
                         $coupon_information['discount'] = $coupon_single->percent_off . "%";
                         $coupon_information['discount_price'] = $plan->amount - $plan->amount * $coupon_single->percent_off / 100;
                     } else {
                         $coupon_information['discount'] = $coupon_single->amount_off / 100 . "/" . $coupon_single->currency;
                         $coupon_information['discount_price'] = $plan->amount - $coupon_single->amount_off / 100;
                     }
                     $coupon_information['currency'] = $coupon_single->currency;
                     $coupon_information['duration'] = $coupon_single->duration;
                     if ($coupon_single->duration == 'repeating') {
                         $coupon_information['duration'] = $coupon_single->duration_in_months . " Months";
                     }
                     $coupon_information['max_redemptions'] = $coupon_single->max_redemptions;
                     $coupon_redeem = Carbon::parse($coupon_single->redeem_by);
                     $coupon_information['redeem_by'] = $coupon_redeem->toFormattedDateString();
                 }
             }
         }
         $plan->coupon = $coupon_information;
         $plan_collect[] = $plan;
     }
     $allplan = collect($plan_collect);
     $user = Auth::user();
     $user_subscriptions = User::with('subscriptions')->whereId($user->id)->first();
     return view('plan', compact('allplan'))->with('user', $user_subscriptions)->with('authenticated', Auth::check());
 }