From 5fd2c456daab49606cb9510cc68a780fccaa6ee2 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 15 Mar 2017 17:09:23 +0200 Subject: [PATCH] Working on gateway fees --- app/Constants.php | 4 ++++ .../Controllers/ClientPortalController.php | 1 + app/Models/Account.php | 16 +++++++++++++ app/Models/Invoice.php | 2 ++ app/Models/InvoiceItem.php | 16 +++++++++++++ .../PaymentDrivers/BasePaymentDriver.php | 11 +++++++++ app/Ninja/Presenters/InvoicePresenter.php | 24 +++++++++++++++++++ resources/lang/en/texts.php | 1 + resources/views/accounts/payments.blade.php | 2 +- resources/views/invoices/view.blade.php | 2 +- 10 files changed, 77 insertions(+), 2 deletions(-) diff --git a/app/Constants.php b/app/Constants.php index 6b575a4b340b..5706f9088f5a 100644 --- a/app/Constants.php +++ b/app/Constants.php @@ -41,6 +41,10 @@ if (! defined('APP_NAME')) { define('INVOICE_TYPE_STANDARD', 1); define('INVOICE_TYPE_QUOTE', 2); + define('INVOICE_ITEM_TYPE_STANDARD', 1); + define('INVOICE_ITEM_TYPE_TASK', 2); + define('INVOICE_ITEM_TYPE_GATEWAY_FEE', 3); + define('PERSON_CONTACT', 'contact'); define('PERSON_USER', 'user'); define('PERSON_VENDOR_CONTACT', 'vendorcontact'); diff --git a/app/Http/Controllers/ClientPortalController.php b/app/Http/Controllers/ClientPortalController.php index 40a46176fc59..809b46fb8baf 100644 --- a/app/Http/Controllers/ClientPortalController.php +++ b/app/Http/Controllers/ClientPortalController.php @@ -146,6 +146,7 @@ class ClientPortalController extends BaseController 'paymentTypes' => $paymentTypes, 'paymentURL' => $paymentURL, 'phantomjs' => Input::has('phantomjs'), + 'gatewayTypeId' => count($paymentTypes) == 1 ? $paymentTypes[0]['gatewayTypeId'] : false, ]; if ($paymentDriver = $account->paymentDriver($invitation, GATEWAY_TYPE_CREDIT_CARD)) { diff --git a/app/Models/Account.php b/app/Models/Account.php index 431e9bd118b3..bf5084699afd 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -410,6 +410,22 @@ class Account extends Eloquent return $user->getDisplayName(); } + public function getGatewaySettings($gatewayTypeId) + { + if (! $this->relationLoaded('account_gateway_settings')) { + $this->load('account_gateway_settings'); + } + + foreach ($this->account_gateway_settings as $settings) { + if ($settings->gateway_type_id == $gatewayTypeId) { + return $settings; + } + } + + return false; + } + + /** * @return string */ diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index d2124dad42c8..76394cdb818d 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -10,6 +10,7 @@ use App\Events\QuoteWasCreated; use App\Events\QuoteWasUpdated; use App\Libraries\CurlUtils; use App\Models\Activity; +use App\Models\Traits\ChargesFees; use DateTime; use Illuminate\Database\Eloquent\SoftDeletes; use Laracasts\Presenter\PresentableTrait; @@ -23,6 +24,7 @@ class Invoice extends EntityModel implements BalanceAffecting { use PresentableTrait; use OwnedByClientTrait; + use ChargesFees; use SoftDeletes { SoftDeletes::trashed as parentTrashed; } diff --git a/app/Models/InvoiceItem.php b/app/Models/InvoiceItem.php index aecb9cc51f71..386716ee0e85 100644 --- a/app/Models/InvoiceItem.php +++ b/app/Models/InvoiceItem.php @@ -72,4 +72,20 @@ class InvoiceItem extends EntityModel { return $this->belongsTo('App\Models\Account'); } + + public function amount() + { + $amount = $this->cost * $this->qty; + $preTaxAmount = $amount; + + if ($this->tax_rate1) { + $amount += $preTaxAmount * $this->tax_rate1 / 100; + } + + if ($this->tax_rate2) { + $amount += $preTaxAmount * $this->tax_rate2 / 100; + } + + return $amount; + } } diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 60ace77f6d54..431c5ab53c58 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -132,6 +132,9 @@ class BasePaymentDriver return redirect()->to('view/' . $this->invitation->invitation_key); } + // apply gateway fees + $this->invoice()->setGatewayFee($this->gatewayType); + if ($this->isGatewayType(GATEWAY_TYPE_TOKEN) || $gateway->is_offsite) { if (Session::has('error')) { Session::reflash(); @@ -846,6 +849,10 @@ class BasePaymentDriver $label = trans('texts.payment_type_on_file', ['type' => $paymentMethod->payment_type->name]); } + if ($fees = $this->invoice()->present()->gatewayFee($paymentMethod->payment_type->gateway_type_id)) { + $label .= sprintf(' - %s %s', $fees, trans('texts.fee')); + } + $links[] = [ 'url' => $url, 'label' => $label, @@ -878,6 +885,10 @@ class BasePaymentDriver $label = trans("texts.{$gatewayTypeAlias}"); } + if ($fees = $this->invoice()->present()->gatewayFee($gatewayTypeId)) { + $label .= sprintf(' - %s %s', $fees, trans('texts.fee')); + } + $links[] = [ 'gatewayTypeId' => $gatewayTypeId, 'url' => $url, diff --git a/app/Ninja/Presenters/InvoicePresenter.php b/app/Ninja/Presenters/InvoicePresenter.php index 6ee92f1c2c8c..5c8f5f8fea25 100644 --- a/app/Ninja/Presenters/InvoicePresenter.php +++ b/app/Ninja/Presenters/InvoicePresenter.php @@ -252,4 +252,28 @@ class InvoicePresenter extends EntityPresenter return $actions; } + + public function gatewayFee($gatewayTypeId = false) + { + $invoice = $this->entity; + $account = $invoice->account; + $fees = $invoice->calcGatewayFee($gatewayTypeId); + + if (! $fees) { + return false; + } + + return $account->formatMoney($fees, $invoice->client); + } + + public function payNowLabel($gatewayTypeId = false) + { + $str = trans('texts.pay_now'); + + if ($fees = $this->gatewayFee($gatewayTypeId)) { + $str .= sprintf(' - %s %s', $fees, trans('texts.fee')); + } + + return $str; + } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 415803f51d68..05525d8fe327 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2400,6 +2400,7 @@ $LANG = array( 'limits' => 'Limits', 'fees' => 'Fees', + 'fee' => 'Fee', 'set_limits_fees' => 'Set :gateway_type Limits/Fees', 'fee_amount' => 'Amount', 'fee_percent' => 'Percent', diff --git a/resources/views/accounts/payments.blade.php b/resources/views/accounts/payments.blade.php index b253d0f80268..ab5aabce2955 100644 --- a/resources/views/accounts/payments.blade.php +++ b/resources/views/accounts/payments.blade.php @@ -296,7 +296,7 @@ var feePercent = NINJA.parseFloat($('#fee_percent').val()) || 0; var total = feeAmount + feePercent var subtotal = total; - console.log('feeAmount: %s', feeAmount); + var taxRate1 = $('#tax_rate1').val(); if (taxRate1) { taxRate1 = NINJA.parseFloat(taxRatesMap[taxRate1].rate); diff --git a/resources/views/invoices/view.blade.php b/resources/views/invoices/view.blade.php index 9f10dbabecdb..14a70192ab7a 100644 --- a/resources/views/invoices/view.blade.php +++ b/resources/views/invoices/view.blade.php @@ -123,7 +123,7 @@ @if (count($paymentTypes) > 1) {!! DropdownButton::success(trans('texts.pay_now'))->withContents($paymentTypes)->large() !!} @elseif (count($paymentTypes) == 1) - {{ trans('texts.pay_now') }} + {{ $invoice->present()->payNowLabel($gatewayTypeId) }} @endif @else {!! Button::normal(trans('texts.download_pdf'))->withAttributes(['onclick' => 'onDownloadClick()'])->large() !!}