public function member(StripeBilling $billing)
 {
     $customerId = Auth::user()->customer_id;
     $card = null;
     if (!empty($customerId)) {
         $card = $billing->retrieveCard($customerId);
     }
     $planId = Auth::user()->plan_id;
     $plan = MembershipPlan::findOrFail($planId);
     return view('dashboard.index', ['card' => $card, 'membershipPlan' => $plan]);
 }
 public function postChangePlan(Request $request, StripeBilling $billing)
 {
     $customerId = Auth::user()->customer_id;
     $planChanged = false;
     if (!empty($customerId)) {
         $newPlan = MembershipPlan::where('slug', $request->get('membership_plan'))->firstOrFail();
         $planChanged = $billing->updatePlan($customerId, $newPlan->slug);
         $user = Auth::user();
         $user->plan_id = $newPlan->id;
         $user->save();
     }
     if ($planChanged) {
         Flash::success('Your plan was successfully changed. You have been billed at the new rate.');
     } else {
         Flash::danger('We have no card on file for you.');
     }
     return redirect('dashboard');
 }
 public function postPlan($planSlug, Request $request, StripeBilling $billing)
 {
     $this->validate($request, ['first_name' => 'required', 'last_name' => 'required', 'password' => 'required', 'password_confirm' => 'required|same:password', 'email' => 'required|email|unique:users', 'phone' => 'required']);
     $plan = MembershipPlan::where('slug', $planSlug)->firstOrFail();
     $planName = ucwords(str_replace('-', ' ', $planSlug));
     if (!in_array($planSlug, ['annual-platinum', 'annual-gold', 'annual-silver', 'lifetime'])) {
         try {
             $charge = $billing->charge(array('amount' => $plan->amount, 'email' => $request->get('email'), 'name' => $request->get('first_name') . ' ' . $request->get('last_name') . ', ' . $planName, 'stripe-token' => $request->get('stripe-token'), 'plan' => $planSlug));
             $customer = true;
         } catch (\Exception $e) {
             Flash::danger($e->getMessage());
             return redirect()->refresh();
         }
     } else {
         try {
             $charge = $billing->oneTime(array('amount' => $plan->amount, 'name' => $request->get('first_name') . ' ' . $request->get('last_name') . ', ' . $planName, 'stripe-token' => $request->get('stripe-token')));
             $customer = false;
         } catch (\Exception $e) {
             Flash::danger($e->getMessage());
             return redirect()->refresh();
         }
     }
     if (!empty($charge['created'])) {
         $user = User::create(['plan_id' => $plan->id, 'first_name' => $request->get('first_name'), 'last_name' => $request->get('last_name'), 'email' => $request->get('email'), 'phone_number' => $request->get('phone'), 'street_address' => $request->get('address1') . ' ' . $request->get('address2'), 'city' => $request->get('city'), 'state' => $request->get('state_province'), 'zip' => $request->get('postal_code'), 'password' => Hash::make($request->get('password')), 'membership_expires' => $customer ? date('Y-m-d', strtotime('+1 year')) : null, 'customer_id' => $charge['id']]);
         if ($request->get('sms')) {
             Sms::create(['phone_number' => $request->get('phone')]);
         }
         if ($request->get('newsletter')) {
             try {
                 $mailchimp = new Mailchimp(env('MAILCHIMP_API_KEY'));
                 $mailchimp->lists->subscribe(env('MAILCHIMP_LIST_ID'), ['email' => $request->get('email')], null, 'html');
             } catch (Mailchimp_List_AlreadySubscribed $e) {
                 Flash::danger($e->getMessage());
                 return back();
             } catch (\Exception $e) {
                 Flash::danger($e->getMessage());
                 return back();
             }
         }
         Flash::success('Congratulations! You are now a Lambda Phoenix Center ' . $planName . ' member.');
         Auth::login($user);
         return redirect('dashboard/incentives/' . $planSlug);
     }
 }
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function edit($id)
 {
     $user = User::findOrFail($id);
     $plans = MembershipPlan::all();
     $planId = Auth::user()->plan_id;
     $plan = MembershipPlan::findOrFail($planId);
     return view('admin.membership.edit', array('user' => $user, 'plans' => $plans, 'membershipPlan' => $plan));
 }
 public function compose($view)
 {
     $planId = Auth::user()->plan_id;
     $plan = MembershipPlan::find($planId);
     $view->with('user', Auth::user())->with('membership', $plan);
 }