コード例 #1
0
 public function postCreditCard(Request $request, StripeBilling $billing)
 {
     $customerId = Auth::user()->customer_id;
     $cardUpdated = false;
     $chargeFailed = false;
     if (!empty($customerId)) {
         $cardUpdated = $billing->updateCreditCard($customerId, $request);
     } else {
         $planSlug = $request->get('membership_plan');
         $plan = MembershipPlan::where('slug', $planSlug)->firstOrFail();
         if (!in_array($planSlug, ['annual-platinum', 'annual-gold', 'annual-silver', 'lifetime'])) {
             try {
                 $charge = $billing->charge(array('amount' => $plan->amount, 'email' => Auth::user()->email, 'name' => Auth::user()->first_name . ' ' . Auth::user()->last_name . ', ' . $plan->membership_plan, 'stripe-token' => $request->get('stripe-token'), 'plan' => $planSlug));
                 $cardUpdated = true;
                 $chargeFailed = false;
             } catch (\Exception $e) {
                 $chargeFailed = true;
                 Flash::danger($e->getMessage());
             }
         } else {
             try {
                 $charge = $billing->oneTime(array('amount' => $plan->amount, 'name' => Auth::user()->first_name . ' ' . Auth::user()->last_name . ', ' . $plan->memebership_plan, 'stripe-token' => $request->get('stripe-token')));
                 $cardUpdated = true;
                 $chargeFailed = false;
             } catch (\Exception $e) {
                 $chargeFailed = true;
                 Flash::danger($e->getMessage());
             }
         }
         $user = Auth::user();
         $user->plan_id = $plan->id;
         $user->customer_id = $charge['id'];
         $user->save();
     }
     if ($cardUpdated) {
         Flash::success('Your card was successfully changed.');
     } elseif ($chargeFailed) {
         Flash::danger('There was a problem with your card. Please try again later.');
     } else {
         Flash::danger('We have no card on file for you.');
     }
     return redirect('dashboard');
 }