Advertisement
Guest User

Untitled

a guest
Apr 24th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.20 KB | None | 0 0
  1. <?php namespace App\Http\Controllers\Manager;
  2.  
  3. use App\Http\Requests;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Http\Request;
  6.  
  7. use Auth;
  8. use Validator;
  9. use Input;
  10. use Hash;
  11. use Config;
  12. use App\Bill;
  13. use App\Plan;
  14.  
  15. use Braintree_Configuration;
  16. use Braintree_ClientToken;
  17. use Braintree_Customer;
  18. use Braintree_Transaction;
  19. use Braintree_Subscription;
  20. use Braintree_PaymentMethod;
  21.  
  22. class AccountController extends Controller{
  23.  
  24.     public function __construct()
  25.     {
  26.         $this->middleware('auth');
  27.     }
  28.  
  29.     public function getProfile(){
  30.        
  31.         Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
  32.         Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
  33.         Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
  34.         Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
  35.        
  36.         $user = Auth::user();
  37.         $customer = $user->getBraintreeCustomer();
  38.         $clientToken = Braintree_ClientToken::generate(['customerId' => $customer->id]);
  39.        
  40.         return view('manager.account.profile', ['user' => $user, 'client_token' => $clientToken]);
  41.     }
  42.    
  43.     public function postProfile(){
  44.         $user = Auth::user();
  45.        
  46.         if($user->person_type == 'private')
  47.         {
  48.             $rules = [
  49.                 'name' => 'required'
  50.             ];
  51.         }
  52.        
  53.         elseif ($user->person_type == 'legal')
  54.         {
  55.             $rules = [
  56.                 'company_name' => 'required',
  57.                 'company_code' => 'required',
  58.                 'contact_person' => 'required',
  59.             ];
  60.         } else
  61.             $rules = [];
  62.        
  63.         $validator = Validator::make(Input::all(), $rules);
  64.        
  65.         if($validator->fails())
  66.         {
  67.             return redirect('manager/account/profile')->with('form', 'profile')->withErrors($validator)->withInput();
  68.         }
  69.  
  70.         if($user->person_type == 'private')
  71.         {
  72.             $user->name = Input::get('name');
  73.             $user->company_name = '';
  74.             $user->company_code = '';
  75.             $user->person_type = 'private';
  76.         }
  77.         elseif ($user->person_type == 'legal')
  78.         {
  79.             $user->name = Input::get('contact_person');
  80.             $user->company_name = Input::get('company_name');
  81.             $user->company_code = Input::get('company_code');
  82.             $user->person_type = 'legal';
  83.         }
  84.        
  85.         $user->country_id = Input::get('country');
  86.         $user->time_zone = Input::get('time_zone');
  87.         //$user->continue_subscription = Input::has('continue_subscription') ? 1 : 0;
  88.        
  89.         if(Input::hasFile('picture'))
  90.         {
  91.             if(Input::file('picture')->isValid())
  92.             {
  93.                 $destinationPath = 'img/user'; // upload path
  94.                 $extension = Input::file('picture')->getClientOriginalExtension(); // getting image extension
  95.                 $fileName = md5($user->id).'.'.$extension; // renaming image
  96.                 Input::file('picture')->move($destinationPath, $fileName); // uploading file to given path
  97.                 $img = \Image::make(asset($destinationPath . '/' . $fileName));
  98.                 $img->fit(100, 100);
  99.                 $img->save($destinationPath . '/' . $fileName);
  100.                 $user->picture = $fileName;
  101.             }
  102.         }
  103.  
  104.         $user->save();
  105.        
  106.         return redirect('manager/account/profile')->with('success.profile', __('Your profile was updated successfully.'));
  107.     }
  108.    
  109.     public function getBilling(){
  110.         $user = Auth::user();
  111.  
  112.         if (!$user->roleIs('assistant'))
  113.             return view('manager.account.billing', ['user' => $user]);
  114.         else
  115.             return redirect('manager/account/profile');
  116.     }
  117.    
  118.     public function getCart(){
  119.        
  120.     }
  121.    
  122.     public function postPassword()
  123.     {
  124.         $rules = [
  125.             'current_password' => 'required',
  126.             'new_password' => 'required|confirmed',
  127.         ];
  128.        
  129.         $validator = Validator::make(Input::all(), $rules);
  130.         $validator->after(function() use ($validator) {
  131.             if(!Hash::check(Input::get('current_password'), Auth::user()->password))
  132.             {
  133.                 $validator->errors()->add('current_password', 'Current password does not match.');
  134.             }
  135.         });
  136.        
  137.         if($validator->fails())
  138.         {
  139.             return redirect('manager/account/profile')->with('form', 'password')->withErrors($validator);
  140.         }
  141.        
  142.         Auth::user()->password = Hash::make(Input::get('new_password'));
  143.         Auth::user()->save();
  144.        
  145.         return redirect('manager/account/profile')->with('success.password', 'Your password has been successfully changed.');
  146.     }
  147.    
  148.     public function getPay($id)
  149.     {
  150.         $bill = Bill::where('user_id', Auth::id())->where('id', $id)->first();
  151.        
  152.         if(!$bill)
  153.         {
  154.             return redirect('manager/account/billing');
  155.         }
  156.        
  157.         Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
  158.         Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
  159.         Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
  160.         Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
  161.         //$result = Auth::user()->getBraintreeId();
  162.         $customer = Auth::user()->getBraintreeCustomer();
  163.        
  164.         /*if($result->success)
  165.         {
  166.             $customer_id = $result->customer->id;
  167.         */
  168.        
  169.         $client_token = Braintree_ClientToken::generate([
  170.             "customerId" => $customer->id
  171.         ]);
  172.  
  173.         return view('manager.account.pay', ['user' => Auth::user(), 'bill' => $bill, 'client_token' => $client_token]);
  174.     }
  175.    
  176.     public function postPay($id)
  177.     {      
  178.         $bill = Bill::where('user_id', Auth::id())->where('id', $id)->first();
  179.        
  180.         if(!$bill)
  181.         {
  182.             return redirect('manager/account/billing');
  183.         }
  184.        
  185.         Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
  186.         Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
  187.         Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
  188.         Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
  189.        
  190.         $result = Braintree_Transaction::sale([
  191.           'amount' => $bill->amount,
  192.           'paymentMethodNonce' => 'fake-valid-nonce', //Input::get('payment_method_nonce')
  193.           'orderId' => $bill->id
  194.         ]);
  195.        
  196.         if(!$result->success)
  197.         {
  198.             return redirect()->back();
  199.         }
  200.        
  201.         $bill->status = 'paid';
  202.         $bill->save();
  203.        
  204.         return redirect('manager/account/billing');
  205.     }
  206.    
  207.     public function getTest()
  208.     {
  209.         $service = ['title' => 'Subscription plan  till ', 'price' => 12.13];
  210.                         $external_bill = new \App\ExternalBill([
  211.                             'project_id' => 11,
  212.                             'pass' => '44d93aeaaadb5a291aceae08b8b16dbb',
  213.                             'title' => Auth::user()->name,
  214.                             'address' => Auth::user()->address ? Auth::user()->address : '-',
  215.                             'email' => Auth::user()->email,
  216.                             'status' => 1,
  217.                             'callback' => url('manager/webhook/bills'),
  218.                             'services' => [
  219.                                 $service
  220.                             ]
  221.                         ]);
  222.                         var_dump($external_bill);
  223.                         exit;
  224.         Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
  225.         Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
  226.         Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
  227.         Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
  228.        
  229.         $customer = Auth::user()->getBraintreeCustomer();
  230.        
  231.         if(!$customer)
  232.         {
  233.             throw new \Exception('Error');
  234.         }
  235.        
  236.         $data = [
  237.             'customerId' => $customer->id,
  238.             'paymentMethodNonce' => 'fake-valid-nonce'
  239.         ];
  240.        
  241.         if(isset($customer->addresses[0]))
  242.         {
  243.             $data['billingAddressId'] = 'xx';
  244.         }
  245.        
  246.         $result = Braintree_PaymentMethod::create($data);
  247.        
  248.        
  249.        
  250.         var_dump($result);
  251.         exit;
  252.         $result = Braintree_Subscription::create([
  253.           'paymentMethodToken' => $result->paymentMethod->token,
  254.           'planId' => 'basic'
  255.         ]);
  256.  
  257.         var_dump($result);
  258.     }
  259.  
  260.     public function getSubscription(){
  261.         if(Auth::user()->plan)
  262.         {
  263.             if(session('success'))
  264.             {
  265.                 return view('auth.payment');
  266.             } else {
  267.                 return redirect('manager/dashboard');
  268.             }
  269.         }
  270.        
  271.         Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
  272.         Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
  273.         Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
  274.         Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
  275.  
  276.         $customer = Auth::user()->getBraintreeCustomer();
  277.         $client_token = Braintree_ClientToken::generate([
  278.             "customerId" => $customer->id
  279.         ]);
  280.  
  281.         return view('auth.payment', ['client_token' => $client_token]);
  282.     }
  283.  
  284.         public function postSubscription()
  285.         {
  286.             if(!Input::has('plan'))
  287.             {
  288.                 return redirect()->back()->withInput()->with('subscription.errors', ['You must select subscription plan.']);
  289.             }
  290.            
  291.             Auth::user()->plan_id = Input::get('plan');
  292.                
  293.             if(Input::get('plan') == 1)
  294.             {
  295.                 Auth::user()->save();
  296.                 return redirect('manager');
  297.             }
  298.            
  299.             Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
  300.             Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
  301.             Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
  302.             Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
  303.  
  304.             $customer = Auth::user()->getBraintreeCustomer();
  305.  
  306.             if(!$customer)
  307.             {
  308.                 throw new \Exception('Error');
  309.             }
  310.  
  311.             $data = [
  312.                 'customerId' => $customer->id,
  313.                 'paymentMethodNonce' => Input::get('payment_method_nonce')
  314.             ];
  315.            
  316.             $result = Braintree_PaymentMethod::create($data);
  317.             $errors = [];
  318.            
  319.             if($result->success == false)
  320.             {
  321.                 foreach($result->errors->deepAll() as $error)
  322.                 {
  323.                     $errors[] = $error->message;
  324.                 }
  325.                
  326.                 return redirect()->back()->with('subscription.errors', $errors);
  327.             }
  328.            
  329.             $plan = Plan::find(Auth::user()->plan_id);
  330.             $plan_id = Input::get('period') == 12 ? $plan->braintree_id . '_YEAR' : $plan->braintree_id;
  331.            
  332.             $result = Braintree_Subscription::create([
  333.               'paymentMethodToken' => $result->paymentMethod->token, //$result->paymentMethod->token
  334.                'planId' => $plan_id
  335.             ]);
  336.            
  337.             if($result->success == false)
  338.             {
  339.                 foreach($result->errors->deepAll() as $error)
  340.                 {
  341.                     $errors[] = $error->message;
  342.                 }
  343.                 $errors[] = $result->message;
  344.                 return redirect()->back()->with('subscription.errors', $errors);
  345.             }
  346.            
  347.             $subscription = $result->subscription;
  348.             $user = Auth::user();
  349.             $user->braintree_subscription_id = $subscription->id;
  350.             $user->subscription_active_until = $subscription->nextBillingDate->format('Y-m-d H:i:s');            
  351.             $user->payment_period = Input::get('period');
  352.             $user->save();
  353.                
  354.             return redirect()->back()->with('success', true);
  355.         }
  356.        
  357.         /*public function getPlan()
  358.         {
  359.             $user = Auth::user();
  360.  
  361.             return view('manager.account.plan', ['user' => $user]);
  362.         }*/
  363.        
  364.         public function postChangePlan(Request $request)
  365.         {
  366.            
  367.             Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
  368.             Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
  369.             Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
  370.             Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
  371.            
  372.             $user = Auth::user();
  373.  
  374.             if($request->input('plan') == 2) {
  375.                 $plan_id = $user->payment_period == 12 ? 'BASIC_YEAR' : 'BASIC';
  376.                 $price = Plan::getPrice('BASIC', $user->payment_period == 12 ? true : false);
  377.             }
  378.             elseif($request->input('plan') == 3) {
  379.                 $plan_id = $user->payment_period == 12 ? 'PRO_YEAR' : 'PRO';
  380.                 $price = Plan::getPrice('PRO', $user->payment_period == 12 ? true : false);
  381.             } else {
  382.                 return redirect()->back();
  383.             }
  384.            
  385.             if($user->plan_id > $request->input('plan'))
  386.             {
  387.                 return redirect()->back();
  388.             }
  389.            
  390.             if(!$user->braintree_subscription_id)
  391.             {
  392.                 $result = Braintree_PaymentMethod::create([
  393.                     'customerId' => $user->getBraintreeCustomer()->id,
  394.                     'paymentMethodNonce' => $request->input('payment_method_nonce'),
  395.                 ]);
  396.                
  397.                 $plan_id = $plan_id;
  398.                
  399.                 $result = Braintree_Subscription::create([
  400.                   'paymentMethodToken' => $result->paymentMethod->token,
  401.                   'planId' => $plan_id,
  402.                 ]);
  403.                
  404.                 if($result->success == false){
  405.                     return redirect()->back()->with('plan.failed', $result->message);
  406.                 }
  407.                
  408.                 $user->plan_id = $request->input('plan');
  409.                 $user->braintree_subscription_id = $result->subscription->id;
  410.                 $user->subscription_active_until = $result->subscription->nextBillingDate->format('Y-m-d H:i:s');            
  411.                 $user->payment_period = 1;
  412.                 $user->save();
  413.                
  414.                 if($result->success)
  415.                 {
  416.                     return redirect()->back()->with('plan.changed', 'Your plan has been changed succesfully.');
  417.                 }
  418.                
  419.                 return redirect()->back()->with('plan.failed', 'Error.');
  420.             }
  421.  
  422.             try{
  423.                 $subscription = Braintree_Subscription::find(Auth::user()->braintree_subscription_id);
  424.                
  425.                 $result = Braintree_Subscription::update(Auth::user()->braintree_subscription_id, [
  426.                     'planId' => $plan_id,
  427.                     'price' => $price
  428.                 ]);
  429.                
  430.                 $user->plan_id = $request->input('plan');
  431.                 $user->save();
  432.                
  433.                 return redirect()->back()->with('plan.changed', 'Your plan has been changed succesfully.');
  434.             } catch(\Exception $e) {
  435.                 return redirect()->back()->with('plan.failed', $e->getMessage());
  436.             }
  437.         }
  438.    
  439. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement