예제 #1
0
 public function doPayPlan($planId)
 {
     if (Input::has('payment_method_nonce')) {
         // get the detials of the plan
         $plans = Braintree_Plan::all();
         $user = Auth::user();
         // find the correct plan to show
         // no way currently to get only one plan
         foreach ($plans as $plan) {
             if ($plan->id == 'fruit_analytics_plan_' . $planId) {
                 $planName = $plan->name;
             }
         }
         // lets see, if the user already has a subscripton
         if ($user->subscriptionId) {
             try {
                 $result = Braintree_Subscription::cancel($user->subscriptionId);
             } catch (Exception $e) {
                 return Redirect::route('auth.plan')->with('error', "Couldn't process subscription, try again later.");
             }
         }
         // create the new subscription
         $result = Braintree_Subscription::create(array('planId' => 'fruit_analytics_plan_' . $planId, 'paymentMethodNonce' => Input::get('payment_method_nonce')));
         if ($result->success) {
             // update user plan to subscrition
             $user->plan = $planId;
             $user->subscriptionId = $result->subscription->id;
             $user->save();
             IntercomHelper::subscribed($user, $planId);
             return Redirect::route('auth.dashboard')->with('success', 'Subscribed to ' . $planName);
         } else {
             return Redirect::route('auth.plan')->with('error', "Couldn't process subscription, try again later.");
         }
     }
 }