1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Working on gateway fees

This commit is contained in:
David Bomba 2020-08-30 22:00:19 +10:00
parent a4d0e7759e
commit ac08a146a1
9 changed files with 73 additions and 26 deletions

View File

@ -136,12 +136,12 @@ class PaymentController extends Controller
$payment_method_id = request()->input('payment_method_id');
$invoice_totals = array_sum(array_column($payable_invoices,'amount'));
$fee_totals = $gateway->calcGatewayFee($invoice_totals);
$fee_totals = round($gateway->calcGatewayFee($invoice_totals), $invoices->first()->client->currency()->precision);
$payment_hash = new PaymentHash;
$payment_hash->hash = Str::random(128);
$payment_hash->data = $payable_invoices;
$payment_hash->fees = $fee_totals;
$payment_hash->fee_total = $fee_totals;
$payment_hash->save();
$totals = [
@ -156,6 +156,7 @@ class PaymentController extends Controller
'invoices' => $payable_invoices,
'token' => auth()->user()->client->gateway_token($gateway->id, $payment_method_id),
'payment_method_id' => $payment_method_id,
'amount_with_fee' => $invoice_totals + $fee_totals,
];
return $gateway

View File

@ -289,20 +289,22 @@ class CompanyGateway extends BaseModel
$pre_tax_fee = $fee;
if ($fees_and_limits->fee_tax_rate1) {
$fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate1 / 100;
info("fee after adding fee tax 1 = {$fee}");
}
//we shouldn't calculate the taxes - they'll be done when we re-process the invoice
// if ($fees_and_limits->fee_tax_rate1) {
// $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate1 / 100;
// info("fee after adding fee tax 1 = {$fee}");
// }
if ($fees_and_limits->fee_tax_rate2) {
$fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate2 / 100;
info("fee after adding fee tax 2 = {$fee}");
}
// if ($fees_and_limits->fee_tax_rate2) {
// $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate2 / 100;
// info("fee after adding fee tax 2 = {$fee}");
// }
if ($fees_and_limits->fee_tax_rate3) {
$fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate3 / 100;
info("fee after adding fee tax 3 = {$fee}");
}
// if ($fees_and_limits->fee_tax_rate3) {
// $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate3 / 100;
// info("fee after adding fee tax 3 = {$fee}");
// }
if($fees_and_limits->fee_cap > 0 && ($fee > $fees_and_limits->fee_cap))
$fee = $fees_and_limits->fee_cap;

View File

@ -16,12 +16,14 @@ use Illuminate\Database\Eloquent\Model;
class PaymentHash extends Model
{
protected $guarded = ['id'];
protected $casts = [
'data' => 'object'
];
public function invoices()
{
return $this->data->invoices;
return $this->data;
}
}

View File

@ -171,7 +171,7 @@ class BaseDriver extends AbstractPaymentDriver
{
/*Payment meta data*/
$payment_hash = $request->getPaymentHash();
/*Payment invoices*/
$payment_invoices = $payment_hash->invoices();
@ -179,10 +179,10 @@ class BaseDriver extends AbstractPaymentDriver
$fee_total = $payment_hash->fee_total;
/*Sum of invoice amounts*/
$invoice_totals = array_sum(array_column($payable_invoices,'amount'));
$invoice_totals = array_sum(array_column($payment_invoices,'amount'));
/*Hydrate invoices*/
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payable_invoices, 'invoice_id')))->get();
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id')))->get();
/*Append gateway fee to invoice line item of first invoice*/
if($fee_total != 0){

View File

@ -13,6 +13,7 @@
namespace App\PaymentDrivers;
use App\Factory\PaymentFactory;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\CompanyGateway;
@ -286,4 +287,39 @@ class BasePaymentDriver
return $payment;
}
/**
* When a successful payment is made, we need to append the gateway fee
* to an invoice
*
* @param PaymentResponseRequest $request The incoming payment request
* @return void Success/Failure
*/
public function appendGatewayFeeToInvoice(PaymentResponseRequest $request) :void
{
/*Payment meta data*/
$payment_hash = $request->getPaymentHash();
/*Payment invoices*/
$payment_invoices = $payment_hash->invoices();
/*Fee charged at gateway*/
$fee_total = $payment_hash->fee_total;
/*Sum of invoice amounts*/
$invoice_totals = array_sum(array_column($payment_invoices,'amount'));
/*Hydrate invoices*/
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id')))->get();
/*Append gateway fee to invoice line item of first invoice*/
if($fee_total != 0){
$invoices->first()->service()->addGatewayFee($this->company_gateway, $invoice_totals)->save();
//We need to update invoice balance / client balance at this point so
//that payment record sanity is preserved //todo
}
}
}

View File

@ -19,6 +19,7 @@ use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\StripePaymentDriver;
@ -92,7 +93,7 @@ class CreditCard
'amount' => $this->stripe->convertToStripeAmount($data['amount_with_fee'], $this->stripe->client->currency()->precision),
'currency' => $this->stripe->client->getCurrencyCode(),
'customer' => $this->stripe->findOrCreateCustomer(),
'description' => $data['invoices']->pluck('id'), //todo more meaningful description here:
'description' => collect($data['invoices'])->pluck('id'), //todo more meaningful description here:
];
if ($data['token']) {
@ -113,6 +114,8 @@ class CreditCard
{
$server_response = json_decode($request->input('gateway_response'));
$payment_hash = PaymentHash::whereRaw("BINARY `hash`= ?", [$request->input('payment_hash')])->firstOrFail();
$state = [
'payment_method' => $server_response->payment_method,
'payment_status' => $server_response->status,
@ -122,7 +125,7 @@ class CreditCard
'server_response' => $server_response,
];
$invoices = Invoice::whereIn('id', $this->stripe->transformKeys($state['hashed_ids']))
$invoices = Invoice::whereIn('id', $this->stripe->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))
->whereClientId($this->stripe->client->id)
->get();
@ -138,6 +141,10 @@ class CreditCard
$state['customer'] = $state['payment_intent']->customer;
if ($state['payment_status'] == 'succeeded') {
/* Add gateway fees if needed! */
$this->stripe->appendGatewayFeeToInvoice($request);
return $this->processSuccessfulPayment($state);
}

View File

@ -43,7 +43,7 @@ class AddGatewayFee extends AbstractService
public function run()
{
$gateway_fee = $this->company_gateway->calcGatewayFee($this->amount);
$gateway_fee = round($this->company_gateway->calcGatewayFee($this->amount), $this->invoice->client->currency()->precision);
$this->cleanPendingGatewayFees();
@ -71,7 +71,7 @@ class AddGatewayFee extends AbstractService
private function processGatewayFee($gateway_fee)
{
$invoice_item = new InvoiceItem;
$invoice_item->type_id = 4;
$invoice_item->type_id = '4';
$invoice_item->product_key = ctrans('texts.surcharge');
$invoice_item->notes = ctrans('texts.online_payment_surcharge');
$invoice_item->quantity = 1;

View File

@ -11,7 +11,7 @@
{!! Former::hidden('gateway_response')->id('gateway_response') !!}
{!! Former::hidden('store_card')->id('store_card') !!}
{!! Former::hidden('hashed_ids')->value($hashed_ids) !!}
{!! Former::hidden('payment_hash')->value($payment_hash) !!}
{!! Former::hidden('company_gateway_id')->value($payment_method_id) !!}
{!! Former::hidden('payment_method_id')->value($gateway->getCompanyGatewayId()) !!}
{!! Former::close() !!}

View File

@ -11,9 +11,8 @@
@csrf
<input type="hidden" name="gateway_response">
<input type="hidden" name="store_card">
@foreach($invoices as $invoice)
<input type="hidden" name="hashed_ids[]" value="{{ $invoice->hashed_id }}">
@endforeach
<input type="hidden" name="payment_hash" value="{{$payment_hash}}">
<input type="hidden" name="company_gateway_id" value="{{ $gateway->getCompanyGatewayId() }}">
<input type="hidden" name="payment_method_id" value="{{ $payment_method_id }}">
</form>