Attendize/app/Http/Controllers/ManageAccountController.php

212 lines
6.4 KiB
PHP
Raw Normal View History

2016-02-29 15:59:36 +00:00
<?php
namespace App\Http\Controllers;
use App\Models\Account;
use App\Models\AccountPaymentGateway;
2016-02-29 15:59:36 +00:00
use App\Models\Currency;
use App\Models\PaymentGateway;
2016-03-05 00:18:10 +00:00
use App\Models\Timezone;
2016-02-29 15:59:36 +00:00
use App\Models\User;
2016-03-05 00:18:10 +00:00
use Auth;
2016-09-06 20:39:27 +00:00
use Hash;
2016-03-05 00:18:10 +00:00
use HttpClient;
use Illuminate\Http\Request;
2016-03-05 00:18:10 +00:00
use Input;
2016-04-17 22:41:19 +00:00
use Mail;
2016-09-06 20:39:27 +00:00
use Validator;
2016-03-05 00:18:10 +00:00
class ManageAccountController extends MyBaseController
{
/**
* Show the account modal
*
* @param Request $request
* @return mixed
*/
public function showEditAccount(Request $request)
2016-03-05 00:18:10 +00:00
{
2016-02-29 15:59:36 +00:00
$data = [
2016-09-06 20:39:27 +00:00
'account' => Account::find(Auth::user()->account_id),
'timezones' => Timezone::lists('location', 'id'),
'currencies' => Currency::lists('title', 'id'),
'payment_gateways' => PaymentGateway::lists('provider_name', 'id'),
'account_payment_gateways' => AccountPaymentGateway::scope()->get()
2016-02-29 15:59:36 +00:00
];
return view('ManageAccount.Modals.EditAccount', $data);
2016-02-29 15:59:36 +00:00
}
2016-03-05 00:18:10 +00:00
public function showStripeReturn()
{
$error_message = 'There was an error connecting your Stripe account. Please try again.';
2016-02-29 15:59:36 +00:00
if (Input::get('error') || !Input::get('code')) {
\Session::flash('message', $error_message);
return redirect()->route('showEventsDashboard');
}
$request = [
2016-09-06 20:39:27 +00:00
'url' => 'https://connect.stripe.com/oauth/token',
2016-02-29 15:59:36 +00:00
'params' => [
'client_secret' => STRIPE_SECRET_KEY,
2016-09-06 20:39:27 +00:00
'code' => Input::get('code'),
'grant_type' => 'authorization_code',
2016-03-05 00:18:10 +00:00
],
2016-02-29 15:59:36 +00:00
];
$response = HttpClient::post($request);
$content = $response->json();
2016-03-05 00:18:10 +00:00
if (isset($content->error) || !isset($content->access_token)) {
2016-02-29 15:59:36 +00:00
\Session::flash('message', $error_message);
return redirect()->route('showEventsDashboard');
}
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
$account = Account::find(\Auth::user()->account_id);
2016-06-15 02:31:24 +00:00
2016-09-06 20:39:27 +00:00
$account->stripe_access_token = $content->access_token;
$account->stripe_refresh_token = $content->refresh_token;
2016-02-29 15:59:36 +00:00
$account->stripe_publishable_key = $content->stripe_publishable_key;
2016-09-06 20:39:27 +00:00
$account->stripe_data_raw = json_encode($content);
2016-06-15 02:31:24 +00:00
2016-02-29 15:59:36 +00:00
$account->save();
2016-03-05 00:18:10 +00:00
\Session::flash('message', 'You have successfully connected your Stripe account.');
2016-02-29 15:59:36 +00:00
return redirect()->route('showEventsDashboard');
}
/**
* Edit an account
*
* @return \Illuminate\Http\JsonResponse
*/
2016-03-05 00:18:10 +00:00
public function postEditAccount()
{
2016-02-29 15:59:36 +00:00
$account = Account::find(Auth::user()->account_id);
if (!$account->validate(Input::all())) {
2016-06-16 02:12:44 +00:00
return response()->json([
2016-09-06 20:39:27 +00:00
'status' => 'error',
2016-03-05 00:18:10 +00:00
'messages' => $account->errors(),
]);
2016-02-29 15:59:36 +00:00
}
2016-09-06 20:39:27 +00:00
$account->first_name = Input::get('first_name');
$account->last_name = Input::get('last_name');
$account->email = Input::get('email');
2016-02-29 15:59:36 +00:00
$account->timezone_id = Input::get('timezone_id');
$account->currency_id = Input::get('currency_id');
$account->save();
2016-06-16 02:12:44 +00:00
return response()->json([
2016-09-06 20:39:27 +00:00
'status' => 'success',
'id' => $account->id,
2016-03-05 00:18:10 +00:00
'message' => 'Account Successfully Updated',
]);
2016-02-29 15:59:36 +00:00
}
/**
* Save account payment information
*
* @param Request $request
* @return mixed
*/
public function postEditAccountPayment(Request $request)
2016-03-05 00:18:10 +00:00
{
2016-02-29 15:59:36 +00:00
$account = Account::find(Auth::user()->account_id);
$gateway_id = $request->get('payment_gateway_id');
switch ($gateway_id) {
case config('attendize.payment_gateway_stripe') : //Stripe
$config = $request->get('stripe');
break;
case config('attendize.payment_gateway_paypal') : //PayPal
$config = $request->get('paypal');
break;
case config('attendize.payment_gateway_coinbase') : //BitPay
$config = $request->get('coinbase');
break;
}
2016-02-29 15:59:36 +00:00
$account_payment_gateway = AccountPaymentGateway::firstOrNew(
[
'payment_gateway_id' => $gateway_id,
2016-09-06 20:39:27 +00:00
'account_id' => $account->id,
]);
$account_payment_gateway->config = $config;
$account_payment_gateway->account_id = $account->id;
$account_payment_gateway->payment_gateway_id = $gateway_id;
$account_payment_gateway->save();
2016-02-29 15:59:36 +00:00
$account->payment_gateway_id = $gateway_id;
2016-02-29 15:59:36 +00:00
$account->save();
return response()->json([
2016-09-06 20:39:27 +00:00
'status' => 'success',
'id' => $account_payment_gateway->id,
2016-03-05 00:18:10 +00:00
'message' => 'Payment Information Successfully Updated',
]);
2016-02-29 15:59:36 +00:00
}
/**
* Invite a user to the application
*
* @return \Illuminate\Http\JsonResponse
*/
2016-03-05 00:18:10 +00:00
public function postInviteUser()
{
$rules = [
'email' => ['required', 'email', 'unique:users,email,NULL,id,account_id,' . Auth::user()->account_id],
];
2016-03-05 00:18:10 +00:00
$messages = [
2016-06-15 02:31:24 +00:00
'email.email' => 'Please enter a valid E-mail address.',
'email.required' => 'E-mail address is required.',
2016-06-15 02:31:24 +00:00
'email.unique' => 'E-mail already in use for this account.',
];
2016-03-05 00:18:10 +00:00
$validation = Validator::make(Input::all(), $rules, $messages);
2016-03-05 00:18:10 +00:00
if ($validation->fails()) {
2016-06-16 02:12:44 +00:00
return response()->json([
2016-09-06 20:39:27 +00:00
'status' => 'error',
'messages' => $validation->messages()->toArray(),
]);
2016-03-05 00:18:10 +00:00
}
2016-02-29 15:59:36 +00:00
$temp_password = str_random(8);
2016-03-05 00:18:10 +00:00
$user = new User();
2016-06-15 02:31:24 +00:00
2016-09-06 20:39:27 +00:00
$user->email = Input::get('email');
$user->password = Hash::make($temp_password);
2016-02-29 15:59:36 +00:00
$user->account_id = Auth::user()->account_id;
2016-06-15 02:31:24 +00:00
2016-02-29 15:59:36 +00:00
$user->save();
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
$data = [
2016-09-06 20:39:27 +00:00
'user' => $user,
'temp_password' => $temp_password,
2016-09-06 20:39:27 +00:00
'inviter' => Auth::user(),
2016-02-29 15:59:36 +00:00
];
2016-03-05 00:18:10 +00:00
Mail::send('Emails.inviteUser', $data, function ($message) use ($data) {
2016-02-29 15:59:36 +00:00
$message->to($data['user']->email)
2016-09-06 20:39:27 +00:00
->subject($data['inviter']->first_name . ' ' . $data['inviter']->last_name . ' added you to an ' . config('attendize.app_name') . ' account.');
2016-02-29 15:59:36 +00:00
});
2016-03-05 00:18:10 +00:00
2016-06-16 02:12:44 +00:00
return response()->json([
2016-09-06 20:39:27 +00:00
'status' => 'success',
'message' => 'Success! <b>' . $user->email . '</b> has been sent further instructions.',
2016-02-29 15:59:36 +00:00
]);
}
}