Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php namespace App\Http\Controllers\Manager;
- use App\Http\Requests;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use Auth;
- use Validator;
- use Input;
- use Hash;
- use Config;
- use App\Bill;
- use App\Plan;
- use Braintree_Configuration;
- use Braintree_ClientToken;
- use Braintree_Customer;
- use Braintree_Transaction;
- use Braintree_Subscription;
- use Braintree_PaymentMethod;
- class AccountController extends Controller{
- public function __construct()
- {
- $this->middleware('auth');
- }
- public function getProfile(){
- Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
- Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
- Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
- Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
- $user = Auth::user();
- $customer = $user->getBraintreeCustomer();
- $clientToken = Braintree_ClientToken::generate(['customerId' => $customer->id]);
- return view('manager.account.profile', ['user' => $user, 'client_token' => $clientToken]);
- }
- public function postProfile(){
- $user = Auth::user();
- if($user->person_type == 'private')
- {
- $rules = [
- 'name' => 'required'
- ];
- }
- elseif ($user->person_type == 'legal')
- {
- $rules = [
- 'company_name' => 'required',
- 'company_code' => 'required',
- 'contact_person' => 'required',
- ];
- } else
- $rules = [];
- $validator = Validator::make(Input::all(), $rules);
- if($validator->fails())
- {
- return redirect('manager/account/profile')->with('form', 'profile')->withErrors($validator)->withInput();
- }
- if($user->person_type == 'private')
- {
- $user->name = Input::get('name');
- $user->company_name = '';
- $user->company_code = '';
- $user->person_type = 'private';
- }
- elseif ($user->person_type == 'legal')
- {
- $user->name = Input::get('contact_person');
- $user->company_name = Input::get('company_name');
- $user->company_code = Input::get('company_code');
- $user->person_type = 'legal';
- }
- $user->country_id = Input::get('country');
- $user->time_zone = Input::get('time_zone');
- //$user->continue_subscription = Input::has('continue_subscription') ? 1 : 0;
- if(Input::hasFile('picture'))
- {
- if(Input::file('picture')->isValid())
- {
- $destinationPath = 'img/user'; // upload path
- $extension = Input::file('picture')->getClientOriginalExtension(); // getting image extension
- $fileName = md5($user->id).'.'.$extension; // renaming image
- Input::file('picture')->move($destinationPath, $fileName); // uploading file to given path
- $img = \Image::make(asset($destinationPath . '/' . $fileName));
- $img->fit(100, 100);
- $img->save($destinationPath . '/' . $fileName);
- $user->picture = $fileName;
- }
- }
- $user->save();
- return redirect('manager/account/profile')->with('success.profile', __('Your profile was updated successfully.'));
- }
- public function getBilling(){
- $user = Auth::user();
- if (!$user->roleIs('assistant'))
- return view('manager.account.billing', ['user' => $user]);
- else
- return redirect('manager/account/profile');
- }
- public function getCart(){
- }
- public function postPassword()
- {
- $rules = [
- 'current_password' => 'required',
- 'new_password' => 'required|confirmed',
- ];
- $validator = Validator::make(Input::all(), $rules);
- $validator->after(function() use ($validator) {
- if(!Hash::check(Input::get('current_password'), Auth::user()->password))
- {
- $validator->errors()->add('current_password', 'Current password does not match.');
- }
- });
- if($validator->fails())
- {
- return redirect('manager/account/profile')->with('form', 'password')->withErrors($validator);
- }
- Auth::user()->password = Hash::make(Input::get('new_password'));
- Auth::user()->save();
- return redirect('manager/account/profile')->with('success.password', 'Your password has been successfully changed.');
- }
- public function getPay($id)
- {
- $bill = Bill::where('user_id', Auth::id())->where('id', $id)->first();
- if(!$bill)
- {
- return redirect('manager/account/billing');
- }
- Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
- Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
- Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
- Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
- //$result = Auth::user()->getBraintreeId();
- $customer = Auth::user()->getBraintreeCustomer();
- /*if($result->success)
- {
- $customer_id = $result->customer->id;
- */
- $client_token = Braintree_ClientToken::generate([
- "customerId" => $customer->id
- ]);
- return view('manager.account.pay', ['user' => Auth::user(), 'bill' => $bill, 'client_token' => $client_token]);
- }
- public function postPay($id)
- {
- $bill = Bill::where('user_id', Auth::id())->where('id', $id)->first();
- if(!$bill)
- {
- return redirect('manager/account/billing');
- }
- Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
- Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
- Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
- Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
- $result = Braintree_Transaction::sale([
- 'amount' => $bill->amount,
- 'paymentMethodNonce' => 'fake-valid-nonce', //Input::get('payment_method_nonce')
- 'orderId' => $bill->id
- ]);
- if(!$result->success)
- {
- return redirect()->back();
- }
- $bill->status = 'paid';
- $bill->save();
- return redirect('manager/account/billing');
- }
- public function getTest()
- {
- $service = ['title' => 'Subscription plan till ', 'price' => 12.13];
- $external_bill = new \App\ExternalBill([
- 'project_id' => 11,
- 'pass' => '44d93aeaaadb5a291aceae08b8b16dbb',
- 'title' => Auth::user()->name,
- 'address' => Auth::user()->address ? Auth::user()->address : '-',
- 'email' => Auth::user()->email,
- 'status' => 1,
- 'callback' => url('manager/webhook/bills'),
- 'services' => [
- $service
- ]
- ]);
- var_dump($external_bill);
- exit;
- Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
- Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
- Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
- Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
- $customer = Auth::user()->getBraintreeCustomer();
- if(!$customer)
- {
- throw new \Exception('Error');
- }
- $data = [
- 'customerId' => $customer->id,
- 'paymentMethodNonce' => 'fake-valid-nonce'
- ];
- if(isset($customer->addresses[0]))
- {
- $data['billingAddressId'] = 'xx';
- }
- $result = Braintree_PaymentMethod::create($data);
- var_dump($result);
- exit;
- $result = Braintree_Subscription::create([
- 'paymentMethodToken' => $result->paymentMethod->token,
- 'planId' => 'basic'
- ]);
- var_dump($result);
- }
- public function getSubscription(){
- if(Auth::user()->plan)
- {
- if(session('success'))
- {
- return view('auth.payment');
- } else {
- return redirect('manager/dashboard');
- }
- }
- Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
- Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
- Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
- Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
- $customer = Auth::user()->getBraintreeCustomer();
- $client_token = Braintree_ClientToken::generate([
- "customerId" => $customer->id
- ]);
- return view('auth.payment', ['client_token' => $client_token]);
- }
- public function postSubscription()
- {
- if(!Input::has('plan'))
- {
- return redirect()->back()->withInput()->with('subscription.errors', ['You must select subscription plan.']);
- }
- Auth::user()->plan_id = Input::get('plan');
- if(Input::get('plan') == 1)
- {
- Auth::user()->save();
- return redirect('manager');
- }
- Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
- Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
- Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
- Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
- $customer = Auth::user()->getBraintreeCustomer();
- if(!$customer)
- {
- throw new \Exception('Error');
- }
- $data = [
- 'customerId' => $customer->id,
- 'paymentMethodNonce' => Input::get('payment_method_nonce')
- ];
- $result = Braintree_PaymentMethod::create($data);
- $errors = [];
- if($result->success == false)
- {
- foreach($result->errors->deepAll() as $error)
- {
- $errors[] = $error->message;
- }
- return redirect()->back()->with('subscription.errors', $errors);
- }
- $plan = Plan::find(Auth::user()->plan_id);
- $plan_id = Input::get('period') == 12 ? $plan->braintree_id . '_YEAR' : $plan->braintree_id;
- $result = Braintree_Subscription::create([
- 'paymentMethodToken' => $result->paymentMethod->token, //$result->paymentMethod->token
- 'planId' => $plan_id
- ]);
- if($result->success == false)
- {
- foreach($result->errors->deepAll() as $error)
- {
- $errors[] = $error->message;
- }
- $errors[] = $result->message;
- return redirect()->back()->with('subscription.errors', $errors);
- }
- $subscription = $result->subscription;
- $user = Auth::user();
- $user->braintree_subscription_id = $subscription->id;
- $user->subscription_active_until = $subscription->nextBillingDate->format('Y-m-d H:i:s');
- $user->payment_period = Input::get('period');
- $user->save();
- return redirect()->back()->with('success', true);
- }
- /*public function getPlan()
- {
- $user = Auth::user();
- return view('manager.account.plan', ['user' => $user]);
- }*/
- public function postChangePlan(Request $request)
- {
- Braintree_Configuration::environment(Config::get('payments.braintree.environment'));
- Braintree_Configuration::merchantId(Config::get('payments.braintree.merchant_id'));
- Braintree_Configuration::publicKey(Config::get('payments.braintree.public_key'));
- Braintree_Configuration::privateKey(Config::get('payments.braintree.private_key'));
- $user = Auth::user();
- if($request->input('plan') == 2) {
- $plan_id = $user->payment_period == 12 ? 'BASIC_YEAR' : 'BASIC';
- $price = Plan::getPrice('BASIC', $user->payment_period == 12 ? true : false);
- }
- elseif($request->input('plan') == 3) {
- $plan_id = $user->payment_period == 12 ? 'PRO_YEAR' : 'PRO';
- $price = Plan::getPrice('PRO', $user->payment_period == 12 ? true : false);
- } else {
- return redirect()->back();
- }
- if($user->plan_id > $request->input('plan'))
- {
- return redirect()->back();
- }
- if(!$user->braintree_subscription_id)
- {
- $result = Braintree_PaymentMethod::create([
- 'customerId' => $user->getBraintreeCustomer()->id,
- 'paymentMethodNonce' => $request->input('payment_method_nonce'),
- ]);
- $plan_id = $plan_id;
- $result = Braintree_Subscription::create([
- 'paymentMethodToken' => $result->paymentMethod->token,
- 'planId' => $plan_id,
- ]);
- if($result->success == false){
- return redirect()->back()->with('plan.failed', $result->message);
- }
- $user->plan_id = $request->input('plan');
- $user->braintree_subscription_id = $result->subscription->id;
- $user->subscription_active_until = $result->subscription->nextBillingDate->format('Y-m-d H:i:s');
- $user->payment_period = 1;
- $user->save();
- if($result->success)
- {
- return redirect()->back()->with('plan.changed', 'Your plan has been changed succesfully.');
- }
- return redirect()->back()->with('plan.failed', 'Error.');
- }
- try{
- $subscription = Braintree_Subscription::find(Auth::user()->braintree_subscription_id);
- $result = Braintree_Subscription::update(Auth::user()->braintree_subscription_id, [
- 'planId' => $plan_id,
- 'price' => $price
- ]);
- $user->plan_id = $request->input('plan');
- $user->save();
- return redirect()->back()->with('plan.changed', 'Your plan has been changed succesfully.');
- } catch(\Exception $e) {
- return redirect()->back()->with('plan.failed', $e->getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement