1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00

Working on gateway fees

This commit is contained in:
David Bomba 2020-08-30 08:00:57 +10:00
parent 32e987a1b2
commit 1bc71fb5d8
3 changed files with 39 additions and 18 deletions

View File

@ -80,7 +80,7 @@ class PaymentController extends Controller
// This is tagged with a type_id of 3 which is for a pending gateway fee. // This is tagged with a type_id of 3 which is for a pending gateway fee.
//REFACTOR - In order to preserve state we should save the array of invoices and amounts and store it in db/cache and use a HASH //REFACTOR - In order to preserve state we should save the array of invoices and amounts and store it in db/cache and use a HASH
// to rehydrate these values in the payment response. // to rehydrate these values in the payment response.
// dd(request()->all()); // dd(request()->all());
$gateway = CompanyGateway::find(request()->input('company_gateway_id')); $gateway = CompanyGateway::find(request()->input('company_gateway_id'));
/*find invoices*/ /*find invoices*/
@ -112,14 +112,6 @@ class PaymentController extends Controller
return $payable_invoice['invoice_id'] == $inv->hashed_id; return $payable_invoice['invoice_id'] == $inv->hashed_id;
}); });
// if($invoice)
// $invoice->service()->addGatewayFee($gateway, $payable_invoice['amount'])->save();
/*Update the payable amount to include the fee*/
// $gateway_fee = $gateway->calcGatewayFee($payable_invoice['amount']);
// $payable_invoices[$key]['amount_with_fee'] = $payable_invoice['amount'] + $gateway_fee;
// $payable_invoices[$key]['fee'] = $gateway_fee;
$payable_invoices[$key]['due_date'] = $this->formatDate($invoice->due_date, $invoice->client->date_format()); $payable_invoices[$key]['due_date'] = $this->formatDate($invoice->due_date, $invoice->client->date_format());
$payable_invoices[$key]['invoice_number'] = $invoice->number; $payable_invoices[$key]['invoice_number'] = $invoice->number;
@ -174,15 +166,9 @@ class PaymentController extends Controller
public function response(PaymentResponseRequest $request) public function response(PaymentResponseRequest $request)
{ {
/*Payment Gateway*/
$gateway = CompanyGateway::find($request->input('company_gateway_id'))->firstOrFail(); $gateway = CompanyGateway::find($request->input('company_gateway_id'))->firstOrFail();
$payment_hash = $request->getPaymentHash();
$payment_invoices = $payment_hash->invoices();
$fee_total = $payment_hash->fee_total;
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payable_invoices, 'invoice_id')))->get();
$invoice_count = $invoices->count();
//REFACTOR - Entry point for the gateway response - we don't need to do anything at this point. //REFACTOR - Entry point for the gateway response - we don't need to do anything at this point.
// //
// - Inside each gateway driver, we should use have a generic code path (in BaseDriver.php)for successful/failed payment // - Inside each gateway driver, we should use have a generic code path (in BaseDriver.php)for successful/failed payment

View File

@ -14,6 +14,7 @@ namespace App\PaymentDrivers;
use App\Events\Invoice\InvoiceWasPaid; use App\Events\Invoice\InvoiceWasPaid;
use App\Factory\PaymentFactory; use App\Factory\PaymentFactory;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Models\Client; use App\Models\Client;
use App\Models\ClientGatewayToken; use App\Models\ClientGatewayToken;
use App\Models\CompanyGateway; use App\Models\CompanyGateway;
@ -158,4 +159,38 @@ class BaseDriver extends AbstractPaymentDriver
* @return Response The payment response * @return Response The payment response
*/ */
public function tokenBilling(ClientGatewayToken $cgt, float $amount, ?Invoice $invoice = null) {} public function tokenBilling(ClientGatewayToken $cgt, float $amount, ?Invoice $invoice = null) {}
/**
* 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($payable_invoices,'amount'));
/*Hydrate invoices*/
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payable_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

@ -109,7 +109,7 @@ class CompanyGatewayTest extends TestCase
return $passes; return $passes;
} }
public function testFeesAreAppendedToInvoice() public function testFeesAreAppendedToInvoice() //after refactor this may be redundant
{ {
$data = []; $data = [];