1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Http/Controllers/AccountGatewayController.php

549 lines
20 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-03-16 22:45:25 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
use App\Models\Account;
2017-03-14 14:18:31 +01:00
use App\Models\AccountGatewaySettings;
2017-01-30 20:40:43 +01:00
use App\Models\AccountGateway;
use App\Models\Gateway;
use App\Services\AccountGatewayService;
2015-03-26 07:24:02 +01:00
use Auth;
use Input;
use Redirect;
use Session;
2015-04-01 21:57:02 +02:00
use stdClass;
2015-04-02 15:12:12 +02:00
use URL;
2015-06-03 19:55:48 +02:00
use Utils;
2017-01-30 20:40:43 +01:00
use Validator;
use View;
2016-05-12 04:55:37 +02:00
use WePay;
2017-11-27 15:50:06 +01:00
use File;
2015-03-16 22:45:25 +01:00
class AccountGatewayController extends BaseController
{
2015-11-05 23:37:04 +01:00
protected $accountGatewayService;
public function __construct(AccountGatewayService $accountGatewayService)
{
2016-03-02 14:36:42 +01:00
//parent::__construct();
2015-11-05 23:37:04 +01:00
$this->accountGatewayService = $accountGatewayService;
}
2015-10-21 13:11:08 +02:00
public function index()
{
return Redirect::to('settings/' . ACCOUNT_PAYMENTS);
}
2015-03-16 22:45:25 +01:00
public function getDatatable()
{
2015-11-05 23:37:04 +01:00
return $this->accountGatewayService->getDatatable(Auth::user()->account_id);
2015-03-16 22:45:25 +01:00
}
2016-06-05 20:05:11 +02:00
public function show($publicId)
{
Session::reflash();
return Redirect::to("gateways/$publicId/edit");
}
2015-03-16 22:45:25 +01:00
public function edit($publicId)
{
$accountGateway = AccountGateway::scope($publicId)->firstOrFail();
2015-11-01 19:21:11 +01:00
$config = $accountGateway->getConfig();
2016-01-18 10:13:39 +01:00
2018-04-13 12:50:38 +02:00
if (! $accountGateway->isCustom()) {
2016-09-26 11:33:30 +02:00
foreach ($config as $field => $value) {
$config->$field = str_repeat('*', strlen($value));
}
2015-03-16 22:45:25 +01:00
}
$data = self::getViewModel($accountGateway);
$data['url'] = 'gateways/'.$publicId;
$data['method'] = 'PUT';
$data['title'] = trans('texts.edit_gateway') . ' - ' . $accountGateway->gateway->name;
2015-11-01 19:21:11 +01:00
$data['config'] = $config;
2015-05-31 14:37:29 +02:00
$data['hiddenFields'] = Gateway::$hiddenFields;
2015-04-16 19:12:56 +02:00
$data['selectGateways'] = Gateway::where('id', '=', $accountGateway->gateway_id)->get();
2015-03-16 22:45:25 +01:00
return View::make('accounts.account_gateway', $data);
}
public function update($publicId)
{
return $this->save($publicId);
}
public function store()
{
return $this->save();
}
/**
2017-01-30 20:40:43 +01:00
* Displays the form for account creation.
2015-03-16 22:45:25 +01:00
*/
2016-06-20 16:14:43 +02:00
public function create()
2015-03-16 22:45:25 +01:00
{
2017-01-30 17:05:31 +01:00
if (! \Request::secure() && ! Utils::isNinjaDev()) {
Session::now('warning', trans('texts.enable_https'));
2016-06-09 09:56:22 +02:00
}
$account = Auth::user()->account;
$accountGatewaysIds = $account->gatewayIds();
2017-10-11 12:02:41 +02:00
$wepay = Input::get('wepay');
2016-06-09 09:56:22 +02:00
2015-03-16 22:45:25 +01:00
$data = self::getViewModel();
$data['url'] = 'gateways';
$data['method'] = 'POST';
$data['title'] = trans('texts.add_gateway');
2017-10-11 12:02:41 +02:00
if ($wepay) {
return View::make('accounts.account_gateway_wepay', $data);
} else {
2016-06-22 20:42:09 +02:00
$availableGatewaysIds = $account->availableGatewaysIds();
2016-09-26 11:33:30 +02:00
$data['primaryGateways'] = Gateway::primary($availableGatewaysIds)->orderBy('sort_order')->get();
2016-06-22 20:42:09 +02:00
$data['secondaryGateways'] = Gateway::secondary($availableGatewaysIds)->orderBy('name')->get();
2016-06-09 09:56:22 +02:00
$data['hiddenFields'] = Gateway::$hiddenFields;
2017-10-11 12:02:41 +02:00
$data['accountGatewaysIds'] = $accountGatewaysIds;
2016-02-03 19:59:07 +01:00
2016-06-09 09:56:22 +02:00
return View::make('accounts.account_gateway', $data);
}
2015-03-16 22:45:25 +01:00
}
private function getViewModel($accountGateway = false)
{
$selectedCards = $accountGateway ? $accountGateway->accepted_credit_cards : 0;
2016-05-12 04:55:37 +02:00
$user = Auth::user();
2017-01-30 20:40:43 +01:00
$account = $user->account;
2015-03-16 22:45:25 +01:00
$creditCardsArray = unserialize(CREDIT_CARDS);
$creditCards = [];
foreach ($creditCardsArray as $card => $name) {
if ($selectedCards > 0 && ($selectedCards & $card) == $card) {
2017-11-19 21:34:34 +01:00
$creditCards['<div>' . $name['text'] . '</div>'] = ['value' => $card, 'data-imageUrl' => asset($name['card']), 'checked' => 'checked'];
2015-03-16 22:45:25 +01:00
} else {
2017-11-19 21:34:34 +01:00
$creditCards['<div>' . $name['text'] . '</div>'] = ['value' => $card, 'data-imageUrl' => asset($name['card'])];
2015-03-16 22:45:25 +01:00
}
}
$account->load('account_gateways');
$currentGateways = $account->account_gateways;
2015-04-15 18:35:41 +02:00
$gateways = Gateway::where('payment_library_id', '=', 1)->orderBy('name')->get();
2015-03-16 22:45:25 +01:00
if ($accountGateway) {
$accountGateway->fields = [];
}
2015-03-16 22:45:25 +01:00
foreach ($gateways as $gateway) {
2015-05-31 14:37:29 +02:00
$fields = $gateway->getFields();
2017-01-30 17:05:31 +01:00
if (! $gateway->isCustom()) {
2016-09-26 11:33:30 +02:00
asort($fields);
}
2016-05-14 23:23:20 +02:00
$gateway->fields = $gateway->id == GATEWAY_WEPAY ? [] : $fields;
2015-03-16 22:45:25 +01:00
if ($accountGateway && $accountGateway->gateway_id == $gateway->id) {
$accountGateway->fields = $gateway->fields;
}
}
return [
'account' => $account,
2016-05-12 04:55:37 +02:00
'user' => $user,
2015-03-16 22:45:25 +01:00
'accountGateway' => $accountGateway,
'config' => false,
'gateways' => $gateways,
'creditCardTypes' => $creditCards,
'countGateways' => $currentGateways->count(),
2015-03-16 22:45:25 +01:00
];
}
2015-11-05 23:37:04 +01:00
public function bulk()
{
$action = Input::get('bulk_action');
$ids = Input::get('bulk_public_id');
$count = $this->accountGatewayService->bulk($ids, $action);
2015-03-16 22:45:25 +01:00
2016-05-14 23:23:20 +02:00
Session::flash('message', trans("texts.{$action}d_account_gateway"));
2015-03-16 22:45:25 +01:00
2015-10-14 16:15:39 +02:00
return Redirect::to('settings/' . ACCOUNT_PAYMENTS);
2015-03-16 22:45:25 +01:00
}
/**
2017-01-30 20:40:43 +01:00
* Stores new account.
2017-01-30 20:54:09 +01:00
*
2017-01-30 20:49:42 +01:00
* @param mixed $accountGatewayPublicId
2015-03-16 22:45:25 +01:00
*/
public function save($accountGatewayPublicId = false)
{
2016-06-09 09:56:22 +02:00
$gatewayId = Input::get('primary_gateway_id') ?: Input::get('secondary_gateway_id');
2015-03-16 22:45:25 +01:00
$gateway = Gateway::findOrFail($gatewayId);
2016-06-09 09:56:22 +02:00
$rules = [];
2015-03-16 22:45:25 +01:00
$fields = $gateway->getFields();
2015-06-03 19:55:48 +02:00
$optional = array_merge(Gateway::$hiddenFields, Gateway::$optionalFields);
if ($gatewayId == GATEWAY_DWOLLA) {
2015-06-03 19:55:48 +02:00
$optional = array_merge($optional, ['key', 'secret']);
2018-03-07 16:23:25 +01:00
} elseif ($gatewayId == GATEWAY_PAYMILL) {
$rules['publishable_key'] = 'required';
} elseif ($gatewayId == GATEWAY_STRIPE) {
2016-02-18 18:00:21 +01:00
if (Utils::isNinjaDev()) {
// do nothing - we're unable to acceptance test with StripeJS
} else {
2016-02-18 18:00:21 +01:00
$rules['publishable_key'] = 'required';
$rules['enable_ach'] = 'boolean';
}
2015-06-03 19:55:48 +02:00
}
2015-03-16 22:45:25 +01:00
2016-05-14 23:23:20 +02:00
if ($gatewayId != GATEWAY_WEPAY) {
foreach ($fields as $field => $details) {
2017-01-30 20:40:43 +01:00
if (! in_array($field, $optional)) {
2016-05-14 23:23:20 +02:00
if (strtolower($gateway->name) == 'beanstream') {
if (in_array($field, ['merchant_id', 'passCode'])) {
$rules[$gateway->id . '_' . $field] = 'required';
}
} else {
$rules[$gateway->id . '_' . $field] = 'required';
2015-03-16 22:45:25 +01:00
}
}
}
}
$creditcards = Input::get('creditCardTypes');
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
2017-10-18 14:59:53 +02:00
$url = $accountGatewayPublicId ? "/gateways/{$accountGatewayPublicId}/edit" : 'gateways/create?other_providers=' . ($gatewayId == GATEWAY_WEPAY ? 'false' : 'true');
return Redirect::to($url)
2015-03-16 22:45:25 +01:00
->withErrors($validator)
->withInput();
} else {
$account = Account::with('account_gateways')->findOrFail(Auth::user()->account_id);
2015-04-16 19:12:56 +02:00
$oldConfig = null;
2015-03-16 22:45:25 +01:00
if ($accountGatewayPublicId) {
$accountGateway = AccountGateway::scope($accountGatewayPublicId)->firstOrFail();
2015-11-01 19:21:11 +01:00
$oldConfig = $accountGateway->getConfig();
2015-03-16 22:45:25 +01:00
} else {
2016-06-22 19:13:45 +02:00
// check they don't already have an active gateway for this provider
// TODO complete this
$accountGateway = AccountGateway::scope()
->whereGatewayId($gatewayId)
->first();
if ($accountGateway) {
Session::flash('error', trans('texts.gateway_exists'));
2017-01-30 20:40:43 +01:00
2016-06-22 19:13:45 +02:00
return Redirect::to("gateways/{$accountGateway->public_id}/edit");
}
2015-03-16 22:45:25 +01:00
$accountGateway = AccountGateway::createNew();
$accountGateway->gateway_id = $gatewayId;
2016-05-14 23:23:20 +02:00
2016-05-15 04:32:27 +02:00
if ($gatewayId == GATEWAY_WEPAY) {
2017-01-30 20:40:43 +01:00
if (! $this->setupWePay($accountGateway, $wepayResponse)) {
2016-05-15 04:32:27 +02:00
return $wepayResponse;
}
$oldConfig = $accountGateway->getConfig();
2016-05-14 23:23:20 +02:00
}
2015-03-16 22:45:25 +01:00
}
$config = new stdClass();
2016-05-14 23:23:20 +02:00
if ($gatewayId != GATEWAY_WEPAY) {
foreach ($fields as $field => $details) {
$value = trim(Input::get($gateway->id . '_' . $field));
// if the new value is masked use the original value
if ($oldConfig && $value && $value === str_repeat('*', strlen($value))) {
$value = $oldConfig->$field;
}
2017-07-17 09:52:32 +02:00
if (! $value && in_array($field, ['testMode', 'developerMode', 'sandbox'])) {
2016-05-14 23:23:20 +02:00
// do nothing
} else {
$config->$field = $value;
}
2015-03-16 22:45:25 +01:00
}
2017-01-30 17:05:31 +01:00
} elseif ($oldConfig) {
2016-05-14 23:23:20 +02:00
$config = clone $oldConfig;
2015-03-16 22:45:25 +01:00
}
2017-01-11 18:51:47 +01:00
$publishableKey = trim(Input::get('publishable_key'));
2015-11-29 21:13:50 +01:00
if ($publishableKey = str_replace('*', '', $publishableKey)) {
$config->publishableKey = $publishableKey;
} elseif ($oldConfig && property_exists($oldConfig, 'publishableKey')) {
$config->publishableKey = $oldConfig->publishableKey;
}
2017-01-11 18:51:47 +01:00
$plaidClientId = trim(Input::get('plaid_client_id'));
if (! $plaidClientId || $plaidClientId = str_replace('*', '', $plaidClientId)) {
$config->plaidClientId = $plaidClientId;
} elseif ($oldConfig && property_exists($oldConfig, 'plaidClientId')) {
$config->plaidClientId = $oldConfig->plaidClientId;
}
2017-01-11 18:51:47 +01:00
$plaidSecret = trim(Input::get('plaid_secret'));
if (! $plaidSecret || $plaidSecret = str_replace('*', '', $plaidSecret)) {
$config->plaidSecret = $plaidSecret;
} elseif ($oldConfig && property_exists($oldConfig, 'plaidSecret')) {
$config->plaidSecret = $oldConfig->plaidSecret;
}
2017-01-11 18:51:47 +01:00
$plaidPublicKey = trim(Input::get('plaid_public_key'));
if (! $plaidPublicKey || $plaidPublicKey = str_replace('*', '', $plaidPublicKey)) {
$config->plaidPublicKey = $plaidPublicKey;
} elseif ($oldConfig && property_exists($oldConfig, 'plaidPublicKey')) {
$config->plaidPublicKey = $oldConfig->plaidPublicKey;
}
2017-09-04 22:22:57 +02:00
if ($gatewayId == GATEWAY_STRIPE) {
$config->enableAlipay = boolval(Input::get('enable_alipay'));
2017-09-05 15:37:19 +02:00
$config->enableSofort = boolval(Input::get('enable_sofort'));
2017-10-19 15:49:15 +02:00
$config->enableSepa = boolval(Input::get('enable_sepa'));
$config->enableBitcoin = boolval(Input::get('enable_bitcoin'));
2017-11-27 15:50:06 +01:00
$config->enableApplePay = boolval(Input::get('enable_apple_pay'));
if ($config->enableApplePay && $uploadedFile = request()->file('apple_merchant_id')) {
$config->appleMerchantId = File::get($uploadedFile);
} elseif ($oldConfig && ! empty($oldConfig->appleMerchantId)) {
$config->appleMerchantId = $oldConfig->appleMerchantId;
}
2017-09-04 22:22:57 +02:00
}
2016-05-17 20:09:39 +02:00
if ($gatewayId == GATEWAY_STRIPE || $gatewayId == GATEWAY_WEPAY) {
$config->enableAch = boolval(Input::get('enable_ach'));
}
2016-05-07 04:33:03 +02:00
if ($gatewayId == GATEWAY_BRAINTREE) {
$config->enablePayPal = boolval(Input::get('enable_paypal'));
}
2015-03-16 22:45:25 +01:00
$cardCount = 0;
if ($creditcards) {
foreach ($creditcards as $card => $value) {
$cardCount += intval($value);
}
}
2015-04-16 19:12:56 +02:00
$accountGateway->accepted_credit_cards = $cardCount;
2015-07-12 21:43:45 +02:00
$accountGateway->show_address = Input::get('show_address') ? true : false;
2017-11-19 21:34:34 +01:00
$accountGateway->show_shipping_address = Input::get('show_shipping_address') ? true : false;
2015-07-12 21:43:45 +02:00
$accountGateway->update_address = Input::get('update_address') ? true : false;
2015-11-01 19:21:11 +01:00
$accountGateway->setConfig($config);
2015-04-16 19:12:56 +02:00
if ($accountGatewayPublicId) {
2015-03-16 22:45:25 +01:00
$accountGateway->save();
} else {
$account->account_gateways()->save($accountGateway);
}
if (isset($wepayResponse)) {
2016-05-14 23:23:20 +02:00
return $wepayResponse;
2015-03-16 22:45:25 +01:00
} else {
2016-10-05 16:02:36 +02:00
$this->testGateway($accountGateway);
2016-05-14 23:23:20 +02:00
if ($accountGatewayPublicId) {
$message = trans('texts.updated_gateway');
2016-10-05 16:02:36 +02:00
Session::flash('message', $message);
2017-01-30 20:40:43 +01:00
2016-10-05 16:02:36 +02:00
return Redirect::to("gateways/{$accountGateway->public_id}/edit");
2016-05-14 23:23:20 +02:00
} else {
$message = trans('texts.created_gateway');
2016-10-05 16:02:36 +02:00
Session::flash('message', $message);
2017-01-30 20:40:43 +01:00
return Redirect::to('/settings/online_payments');
2016-05-14 23:23:20 +02:00
}
}
2015-03-16 22:45:25 +01:00
}
}
private function testGateway($accountGateway)
{
$paymentDriver = $accountGateway->paymentDriver();
$result = $paymentDriver->isValid();
if ($result !== true) {
Session::flash('error', $result . ' - ' . trans('texts.gateway_config_error'));
}
}
2016-05-12 04:55:37 +02:00
protected function getWePayUpdateUri($accountGateway)
{
if ($accountGateway->gateway_id != GATEWAY_WEPAY) {
return null;
}
$wepay = Utils::setupWePay($accountGateway);
$update_uri_data = $wepay->request('account/get_update_uri', [
2017-01-30 20:40:43 +01:00
'account_id' => $accountGateway->getConfig()->accountId,
'mode' => 'iframe',
2016-05-12 04:55:37 +02:00
'redirect_uri' => URL::to('/gateways'),
]);
2016-05-12 04:55:37 +02:00
return $update_uri_data->uri;
}
2016-05-14 23:23:20 +02:00
protected function setupWePay($accountGateway, &$response)
2016-05-12 04:55:37 +02:00
{
$user = Auth::user();
$account = $user->account;
$rules = [
2016-05-12 04:55:37 +02:00
'company_name' => 'required',
'tos_agree' => 'required',
'first_name' => 'required',
'last_name' => 'required',
2017-03-08 10:29:13 +01:00
'email' => 'required|email',
2017-07-04 11:24:49 +02:00
'country' => 'required|in:US,CA,GB',
];
2016-05-17 17:32:17 +02:00
$validator = Validator::make(Input::all(), $rules);
2016-05-12 04:55:37 +02:00
if ($validator->fails()) {
return Redirect::to('gateways/create')
->withErrors($validator)
->withInput();
}
2017-03-08 10:29:13 +01:00
if (! $user->email) {
$user->email = trim(Input::get('email'));
$user->first_name = trim(Input::get('first_name'));
$user->last_name = trim(Input::get('last_name'));
$user->save();
}
2017-01-30 17:05:31 +01:00
try {
2016-05-12 04:55:37 +02:00
$wepay = Utils::setupWePay();
$userDetails = [
2016-05-12 04:55:37 +02:00
'client_id' => WEPAY_CLIENT_ID,
'client_secret' => WEPAY_CLIENT_SECRET,
'email' => Input::get('email'),
'first_name' => Input::get('first_name'),
'last_name' => Input::get('last_name'),
'original_ip' => \Request::getClientIp(true),
'original_device' => \Request::server('HTTP_USER_AGENT'),
'tos_acceptance_time' => time(),
2016-05-12 17:09:07 +02:00
'redirect_uri' => URL::to('gateways'),
2016-05-12 04:55:37 +02:00
'scope' => 'manage_accounts,collect_payments,view_user,preapprove_payments,send_money',
];
2016-05-17 17:32:17 +02:00
$wepayUser = $wepay->request('user/register/', $userDetails);
2016-05-12 04:55:37 +02:00
$accessToken = $wepayUser->access_token;
$accessTokenExpires = $wepayUser->expires_in ? (time() + $wepayUser->expires_in) : null;
$wepay = new WePay($accessToken);
$accountDetails = [
2017-01-30 20:40:43 +01:00
'name' => Input::get('company_name'),
'description' => trans('texts.wepay_account_description'),
2016-05-12 04:55:37 +02:00
'theme_object' => json_decode(WEPAY_THEME),
2016-05-18 15:51:20 +02:00
'callback_uri' => $accountGateway->getWebhookUrl(),
2017-01-30 20:40:43 +01:00
'rbits' => $account->present()->rBits,
2017-07-04 11:24:49 +02:00
'country' => Input::get('country'),
];
2016-05-17 17:32:17 +02:00
2017-07-04 11:24:49 +02:00
if (Input::get('country') == 'CA') {
$accountDetails['currencies'] = ['CAD'];
$accountDetails['country_options'] = ['debit_opt_in' => boolval(Input::get('debit_cards'))];
} elseif (Input::get('country') == 'GB') {
$accountDetails['currencies'] = ['GBP'];
2016-05-17 17:32:17 +02:00
}
$wepayAccount = $wepay->request('account/create/', $accountDetails);
2016-05-12 04:55:37 +02:00
try {
$wepay->request('user/send_confirmation/', []);
$confirmationRequired = true;
2017-01-30 17:05:31 +01:00
} catch (\WePayException $ex) {
2016-05-12 04:55:37 +02:00
if ($ex->getMessage() == 'This access_token is already approved.') {
2017-01-30 17:05:31 +01:00
$confirmationRequired = false;
2016-05-12 04:55:37 +02:00
} else {
throw $ex;
}
}
$accountGateway->gateway_id = GATEWAY_WEPAY;
$accountGateway->setConfig([
2016-05-12 04:55:37 +02:00
'userId' => $wepayUser->user_id,
'accessToken' => $accessToken,
'tokenType' => $wepayUser->token_type,
'tokenExpires' => $accessTokenExpires,
'accountId' => $wepayAccount->account_id,
2016-05-18 15:51:20 +02:00
'state' => $wepayAccount->state,
'testMode' => WEPAY_ENVIRONMENT == WEPAY_STAGE,
2017-07-04 11:24:49 +02:00
'country' => Input::get('country'),
]);
2016-05-12 04:55:37 +02:00
if ($confirmationRequired) {
Session::flash('message', trans('texts.created_wepay_confirmation_required'));
} else {
$updateUri = $wepay->request('/account/get_update_uri', [
2016-05-12 04:55:37 +02:00
'account_id' => $wepayAccount->account_id,
'redirect_uri' => URL::to('gateways'),
]);
2016-05-12 04:55:37 +02:00
2016-05-14 23:23:20 +02:00
$response = Redirect::to($updateUri->uri);
2017-01-30 20:40:43 +01:00
2016-05-14 23:23:20 +02:00
return true;
2016-05-12 04:55:37 +02:00
}
2016-05-14 23:23:20 +02:00
$response = Redirect::to("gateways/{$accountGateway->public_id}/edit");
2017-01-30 20:40:43 +01:00
2016-05-14 23:23:20 +02:00
return true;
2016-05-12 04:55:37 +02:00
} catch (\WePayException $e) {
Session::flash('error', $e->getMessage());
2016-05-14 23:23:20 +02:00
$response = Redirect::to('gateways/create')
2016-05-12 04:55:37 +02:00
->withInput();
2017-01-30 20:40:43 +01:00
2016-05-14 23:23:20 +02:00
return false;
2016-05-12 04:55:37 +02:00
}
}
public function resendConfirmation($publicId = false)
{
$accountGateway = AccountGateway::scope($publicId)->firstOrFail();
if ($accountGateway->gateway_id == GATEWAY_WEPAY) {
try {
$wepay = Utils::setupWePay($accountGateway);
$wepay->request('user/send_confirmation', []);
Session::flash('message', trans('texts.resent_confirmation_email'));
} catch (\WePayException $e) {
Session::flash('error', $e->getMessage());
}
}
return Redirect::to("gateways/{$accountGateway->public_id}/edit");
}
2017-03-14 14:18:31 +01:00
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function savePaymentGatewayLimits()
{
$gateway_type_id = intval(Input::get('gateway_type_id'));
$gateway_settings = AccountGatewaySettings::scope()->where('gateway_type_id', '=', $gateway_type_id)->first();
if (! $gateway_settings) {
$gateway_settings = AccountGatewaySettings::createNew();
$gateway_settings->gateway_type_id = $gateway_type_id;
}
$gateway_settings->min_limit = Input::get('limit_min_enable') ? intval(Input::get('limit_min')) : null;
$gateway_settings->max_limit = Input::get('limit_max_enable') ? intval(Input::get('limit_max')) : null;
if ($gateway_settings->max_limit !== null && $gateway_settings->min_limit > $gateway_settings->max_limit) {
$gateway_settings->max_limit = $gateway_settings->min_limit;
}
$gateway_settings->fill(Input::all());
$gateway_settings->save();
Session::flash('message', trans('texts.updated_settings'));
return Redirect::to('settings/' . ACCOUNT_PAYMENTS);
}
}