From 7d0ec404da7005e6869aeb3cc11495b88bbe1a89 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 14 Mar 2017 15:18:31 +0200 Subject: [PATCH] Working on gateway fees --- app/Http/Controllers/AccountController.php | 34 +--- .../Controllers/AccountGatewayController.php | 30 +++ app/Http/routes.php | 2 +- app/Models/AccountGatewaySettings.php | 12 ++ .../Datatables/AccountGatewayDatatable.php | 13 +- .../PaymentDrivers/BasePaymentDriver.php | 5 + .../Repositories/AccountGatewayRepository.php | 7 +- resources/lang/en/texts.php | 8 + resources/views/accounts/payments.blade.php | 186 ++++++++++++++---- 9 files changed, 221 insertions(+), 76 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index c3d65cf0a50a..733ab575ccba 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -9,7 +9,6 @@ use App\Http\Requests\SaveEmailSettings; use App\Http\Requests\UpdateAccountRequest; use App\Models\Account; use App\Models\AccountGateway; -use App\Models\AccountGatewaySettings; use App\Models\Affiliate; use App\Models\Document; use App\Models\Gateway; @@ -500,8 +499,8 @@ class AccountController extends BaseController 'showAdd' => $count < count(Gateway::$alternate) + 1, 'title' => trans('texts.online_payments'), 'tokenBillingOptions' => $tokenBillingOptions, - 'currency' => Utils::getFromCache(Session::get(SESSION_CURRENCY, DEFAULT_CURRENCY), - 'currencies'), + 'currency' => Utils::getFromCache(Session::get(SESSION_CURRENCY, DEFAULT_CURRENCY), 'currencies'), + 'taxRates' => TaxRate::scope()->whereIsInclusive(false)->get(['id', 'name', 'rate']), 'account' => $account, ]); } @@ -1198,35 +1197,6 @@ class AccountController extends BaseController return Redirect::to('settings/'.ACCOUNT_PAYMENTS); } - /** - * @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->save(); - - event(new UserSettingsChanged()); - - Session::flash('message', trans('texts.updated_settings')); - - return Redirect::to('settings/' . ACCOUNT_PAYMENTS); - } - /** * @return \Illuminate\Http\RedirectResponse */ diff --git a/app/Http/Controllers/AccountGatewayController.php b/app/Http/Controllers/AccountGatewayController.php index 35bdefe13284..177bdbe14ac4 100644 --- a/app/Http/Controllers/AccountGatewayController.php +++ b/app/Http/Controllers/AccountGatewayController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Account; +use App\Models\AccountGatewaySettings; use App\Models\AccountGateway; use App\Models\Gateway; use App\Services\AccountGatewayService; @@ -501,4 +502,33 @@ class AccountGatewayController extends BaseController return Redirect::to("gateways/{$accountGateway->public_id}/edit"); } + + /** + * @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); + } + } diff --git a/app/Http/routes.php b/app/Http/routes.php index a672a28ef374..669297016cb8 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -130,7 +130,7 @@ Route::group(['middleware' => 'auth:user'], function () { Route::get('settings/user_details', 'AccountController@showUserDetails'); Route::post('settings/user_details', 'AccountController@saveUserDetails'); - Route::post('settings/payment_gateway_limits', 'AccountController@savePaymentGatewayLimits'); + Route::post('settings/payment_gateway_limits', 'AccountGatewayController@savePaymentGatewayLimits'); Route::post('users/change_password', 'UserController@changePassword'); Route::resource('clients', 'ClientController'); diff --git a/app/Models/AccountGatewaySettings.php b/app/Models/AccountGatewaySettings.php index b0eb0e2920f1..b7402d82367e 100644 --- a/app/Models/AccountGatewaySettings.php +++ b/app/Models/AccountGatewaySettings.php @@ -12,6 +12,18 @@ class AccountGatewaySettings extends EntityModel */ protected $dates = ['updated_at']; + /** + * @var array + */ + protected $fillable = [ + 'fee_amount', + 'fee_percent', + 'fee_tax_name1', + 'fee_tax_rate1', + 'fee_tax_name2', + 'fee_tax_rate2', + ]; + /** * @var bool */ diff --git a/app/Ninja/Datatables/AccountGatewayDatatable.php b/app/Ninja/Datatables/AccountGatewayDatatable.php index d67644931c89..bf274eb40af4 100644 --- a/app/Ninja/Datatables/AccountGatewayDatatable.php +++ b/app/Ninja/Datatables/AccountGatewayDatatable.php @@ -103,6 +103,12 @@ class AccountGatewayDatatable extends EntityDatatable return $html; }, ], + [ + 'fees', + function ($model) { + return 'Fees description...'; + }, + ], ]; } @@ -160,7 +166,7 @@ class AccountGatewayDatatable extends EntityDatatable foreach (Cache::get('gatewayTypes') as $gatewayType) { $actions[] = [ - trans('texts.set_limits', ['gateway_type' => $gatewayType->name]), + trans('texts.set_limits_fees', ['gateway_type' => $gatewayType->name]), function () use ($gatewayType) { $accountGatewaySettings = AccountGatewaySettings::scope() ->where('account_gateway_settings.gateway_type_id', '=', $gatewayType->id) @@ -176,10 +182,7 @@ class AccountGatewayDatatable extends EntityDatatable return $gatewayType->id == GATEWAY_TYPE_CUSTOM; } else { $accountGateway = $this->getAccountGateway($model->id); - $paymentDriver = $accountGateway->paymentDriver(); - $gatewayTypes = $paymentDriver->gatewayTypes(); - - return in_array($gatewayType->id, $gatewayTypes); + return $accountGateway->paymentDriver()->supportsGatewayType($gatewayType->id); } }, ]; diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 4153aaa7322c..60ace77f6d54 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -888,6 +888,11 @@ class BasePaymentDriver return $links; } + public function supportsGatewayType($gatewayTypeId) + { + return in_array($gatewayTypeId, $this->gatewayTypes()); + } + protected function meetsGatewayTypeLimits($gatewayTypeId) { if (! $gatewayTypeId) { diff --git a/app/Ninja/Repositories/AccountGatewayRepository.php b/app/Ninja/Repositories/AccountGatewayRepository.php index f338ccb316cf..6a1b654ae000 100644 --- a/app/Ninja/Repositories/AccountGatewayRepository.php +++ b/app/Ninja/Repositories/AccountGatewayRepository.php @@ -18,6 +18,11 @@ class AccountGatewayRepository extends BaseRepository ->where('account_gateways.account_id', '=', $accountId) ->whereNull('account_gateways.deleted_at'); - return $query->select('account_gateways.id', 'account_gateways.public_id', 'gateways.name', 'account_gateways.deleted_at', 'account_gateways.gateway_id'); + return $query->select( + 'account_gateways.id', + 'account_gateways.public_id', + 'gateways.name', + 'account_gateways.deleted_at', + 'account_gateways.gateway_id'); } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index e37b8b35c816..f8fc19e3bc66 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2398,6 +2398,14 @@ $LANG = array( 'pro_plan_reports' => ':link to enable reports by joining the Pro Plan', 'mark_ready' => 'Mark Ready', + 'limits' => 'Limits', + 'fees' => 'Fees', + 'set_limits_fees' => 'Set :gateway_type Limits/Fees', + 'fee_amount' => 'Amount', + 'fee_percent' => 'Percent', + 'fees_tax_help' => 'Enable line item taxes to set fee tax rates.', + 'fees_sample' => 'For a :amount invoice the fees would be :total.', + ); return $LANG; diff --git a/resources/views/accounts/payments.blade.php b/resources/views/accounts/payments.blade.php index d0d7b419dde0..5d94bf49f56d 100644 --- a/resources/views/accounts/payments.blade.php +++ b/resources/views/accounts/payments.blade.php @@ -3,6 +3,7 @@ @section('content') @parent @include('accounts.nav', ['selected' => ACCOUNT_PAYMENTS]) + @include('money_script') {!! Former::open()->addClass('warn-on-exit') !!} {!! Former::populateField('token_billing_type_id', $account->token_billing_type_id) !!} @@ -43,14 +44,15 @@ {!! Datatable::table() ->addColumn( trans('texts.name'), - trans('texts.limit'), + trans('texts.limits'), + trans('texts.fees'), trans('texts.action')) ->setUrl(url('api/gateways/')) ->setOptions('sPaginationType', 'bootstrap') ->setOptions('bFilter', false) ->setOptions('bAutoWidth', false) - ->setOptions('aoColumns', [[ "sWidth"=> "50%" ], ["sWidth"=> "30%"], ["sWidth"=> "20%"]]) - ->setOptions('aoColumnDefs', [['bSortable'=>false, 'aTargets'=>[1]]]) + ->setOptions('aoColumns', [[ "sWidth"=> "30%" ], ["sWidth"=> "30%"], ["sWidth"=> "20%"], ["sWidth"=> "20%"]]) + ->setOptions('aoColumnDefs', [['bSortable'=>false, 'aTargets'=>[1, 2, 3]]]) ->render('datatable') !!} {!! Former::open( 'settings/payment_gateway_limits') !!} @@ -64,42 +66,107 @@ -
+
-
-
-
-
-

-
-
-
-
-
- {{ $currency->symbol }} - -
- -
-
-
-
-
+ +
+
+

+
+
+
+
+

+
+
+
+
+
+ {{ $currency->symbol }} + +
+ +
+
+
+
+
-
- {{ $currency->symbol }} - -
- -
-
-
- +
+ {{ $currency->symbol }} + +
+ +
+
+
+ +
+
+
+
+ + {!! Former::text('fee_amount') + ->onchange('updateFeeSample()') + ->type('number') + ->min('0') + ->step('any') !!} + + {!! Former::text('fee_percent') + ->onchange('updateFeeSample()') + ->type('number') + ->min('0') + ->step('any') + ->append('%') !!} + + @if ($account->invoice_item_taxes) + {!! Former::select('tax_rate1') + ->onchange('onTaxRateChange(1)') + ->addOption('', '') + ->label(trans('texts.tax_rate')) + ->fromQuery($taxRates, function($model) { return $model->name . ' ' . $model->rate . '%'; }, 'id') !!} + + {!! Former::text('fee_tax_name1') !!} + {!! Former::text('fee_tax_rate1') !!} + + @if ($account->enable_second_tax_rate) + {!! Former::select('tax_rate2') + ->onchange('onTaxRateChange(2)') + ->addOption('', '') + ->label(trans('texts.tax_rate')) + ->fromQuery($taxRates, function($model) { return $model->name . ' ' . $model->rate . '%'; }, 'id') !!} + @endif + + {!! Former::text('fee_tax_name2') !!} + {!! Former::text('fee_tax_rate2') !!} + + @endif + +
+ + @if (!$account->invoice_item_taxes && $account->invoice_taxes && count($taxRates)) +
{{ trans('texts.fees_tax_help') }}
+ @endif + +
+
+
+ +
@@ -117,8 +184,10 @@