Working on gateway fees

This commit is contained in:
Hillel Coren 2017-03-15 17:09:23 +02:00
parent 5fcf0c57a4
commit 5fd2c456da
10 changed files with 77 additions and 2 deletions

View File

@ -41,6 +41,10 @@ if (! defined('APP_NAME')) {
define('INVOICE_TYPE_STANDARD', 1); define('INVOICE_TYPE_STANDARD', 1);
define('INVOICE_TYPE_QUOTE', 2); 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_CONTACT', 'contact');
define('PERSON_USER', 'user'); define('PERSON_USER', 'user');
define('PERSON_VENDOR_CONTACT', 'vendorcontact'); define('PERSON_VENDOR_CONTACT', 'vendorcontact');

View File

@ -146,6 +146,7 @@ class ClientPortalController extends BaseController
'paymentTypes' => $paymentTypes, 'paymentTypes' => $paymentTypes,
'paymentURL' => $paymentURL, 'paymentURL' => $paymentURL,
'phantomjs' => Input::has('phantomjs'), 'phantomjs' => Input::has('phantomjs'),
'gatewayTypeId' => count($paymentTypes) == 1 ? $paymentTypes[0]['gatewayTypeId'] : false,
]; ];
if ($paymentDriver = $account->paymentDriver($invitation, GATEWAY_TYPE_CREDIT_CARD)) { if ($paymentDriver = $account->paymentDriver($invitation, GATEWAY_TYPE_CREDIT_CARD)) {

View File

@ -410,6 +410,22 @@ class Account extends Eloquent
return $user->getDisplayName(); 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 * @return string
*/ */

View File

@ -10,6 +10,7 @@ use App\Events\QuoteWasCreated;
use App\Events\QuoteWasUpdated; use App\Events\QuoteWasUpdated;
use App\Libraries\CurlUtils; use App\Libraries\CurlUtils;
use App\Models\Activity; use App\Models\Activity;
use App\Models\Traits\ChargesFees;
use DateTime; use DateTime;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Laracasts\Presenter\PresentableTrait; use Laracasts\Presenter\PresentableTrait;
@ -23,6 +24,7 @@ class Invoice extends EntityModel implements BalanceAffecting
{ {
use PresentableTrait; use PresentableTrait;
use OwnedByClientTrait; use OwnedByClientTrait;
use ChargesFees;
use SoftDeletes { use SoftDeletes {
SoftDeletes::trashed as parentTrashed; SoftDeletes::trashed as parentTrashed;
} }

View File

@ -72,4 +72,20 @@ class InvoiceItem extends EntityModel
{ {
return $this->belongsTo('App\Models\Account'); 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;
}
} }

View File

@ -132,6 +132,9 @@ class BasePaymentDriver
return redirect()->to('view/' . $this->invitation->invitation_key); 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 ($this->isGatewayType(GATEWAY_TYPE_TOKEN) || $gateway->is_offsite) {
if (Session::has('error')) { if (Session::has('error')) {
Session::reflash(); Session::reflash();
@ -846,6 +849,10 @@ class BasePaymentDriver
$label = trans('texts.payment_type_on_file', ['type' => $paymentMethod->payment_type->name]); $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[] = [ $links[] = [
'url' => $url, 'url' => $url,
'label' => $label, 'label' => $label,
@ -878,6 +885,10 @@ class BasePaymentDriver
$label = trans("texts.{$gatewayTypeAlias}"); $label = trans("texts.{$gatewayTypeAlias}");
} }
if ($fees = $this->invoice()->present()->gatewayFee($gatewayTypeId)) {
$label .= sprintf(' - %s %s', $fees, trans('texts.fee'));
}
$links[] = [ $links[] = [
'gatewayTypeId' => $gatewayTypeId, 'gatewayTypeId' => $gatewayTypeId,
'url' => $url, 'url' => $url,

View File

@ -252,4 +252,28 @@ class InvoicePresenter extends EntityPresenter
return $actions; 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;
}
} }

View File

@ -2400,6 +2400,7 @@ $LANG = array(
'limits' => 'Limits', 'limits' => 'Limits',
'fees' => 'Fees', 'fees' => 'Fees',
'fee' => 'Fee',
'set_limits_fees' => 'Set :gateway_type Limits/Fees', 'set_limits_fees' => 'Set :gateway_type Limits/Fees',
'fee_amount' => 'Amount', 'fee_amount' => 'Amount',
'fee_percent' => 'Percent', 'fee_percent' => 'Percent',

View File

@ -296,7 +296,7 @@
var feePercent = NINJA.parseFloat($('#fee_percent').val()) || 0; var feePercent = NINJA.parseFloat($('#fee_percent').val()) || 0;
var total = feeAmount + feePercent var total = feeAmount + feePercent
var subtotal = total; var subtotal = total;
console.log('feeAmount: %s', feeAmount);
var taxRate1 = $('#tax_rate1').val(); var taxRate1 = $('#tax_rate1').val();
if (taxRate1) { if (taxRate1) {
taxRate1 = NINJA.parseFloat(taxRatesMap[taxRate1].rate); taxRate1 = NINJA.parseFloat(taxRatesMap[taxRate1].rate);

View File

@ -123,7 +123,7 @@
@if (count($paymentTypes) > 1) @if (count($paymentTypes) > 1)
{!! DropdownButton::success(trans('texts.pay_now'))->withContents($paymentTypes)->large() !!} {!! DropdownButton::success(trans('texts.pay_now'))->withContents($paymentTypes)->large() !!}
@elseif (count($paymentTypes) == 1) @elseif (count($paymentTypes) == 1)
<a href='{!! $paymentURL !!}' class="btn btn-success btn-lg">{{ trans('texts.pay_now') }}</a> <a href='{!! $paymentURL !!}' class="btn btn-success btn-lg">{{ $invoice->present()->payNowLabel($gatewayTypeId) }}</a>
@endif @endif
@else @else
{!! Button::normal(trans('texts.download_pdf'))->withAttributes(['onclick' => 'onDownloadClick()'])->large() !!} {!! Button::normal(trans('texts.download_pdf'))->withAttributes(['onclick' => 'onDownloadClick()'])->large() !!}