mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 05:02:36 +01:00
Working on gateway fees
This commit is contained in:
parent
32e987a1b2
commit
1bc71fb5d8
@ -80,7 +80,7 @@ class PaymentController extends Controller
|
||||
// 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
|
||||
// to rehydrate these values in the payment response.
|
||||
// dd(request()->all());
|
||||
// dd(request()->all());
|
||||
|
||||
$gateway = CompanyGateway::find(request()->input('company_gateway_id'));
|
||||
/*find invoices*/
|
||||
@ -111,15 +111,7 @@ class PaymentController extends Controller
|
||||
$invoice = $invoices->first(function ($inv) use($payable_invoice) {
|
||||
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]['invoice_number'] = $invoice->number;
|
||||
|
||||
@ -174,15 +166,9 @@ class PaymentController extends Controller
|
||||
|
||||
public function response(PaymentResponseRequest $request)
|
||||
{
|
||||
/*Payment Gateway*/
|
||||
$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.
|
||||
//
|
||||
// - Inside each gateway driver, we should use have a generic code path (in BaseDriver.php)for successful/failed payment
|
||||
|
@ -14,6 +14,7 @@ namespace App\PaymentDrivers;
|
||||
|
||||
use App\Events\Invoice\InvoiceWasPaid;
|
||||
use App\Factory\PaymentFactory;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Models\Client;
|
||||
use App\Models\ClientGatewayToken;
|
||||
use App\Models\CompanyGateway;
|
||||
@ -158,4 +159,38 @@ class BaseDriver extends AbstractPaymentDriver
|
||||
* @return Response The payment response
|
||||
*/
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ class CompanyGatewayTest extends TestCase
|
||||
return $passes;
|
||||
}
|
||||
|
||||
public function testFeesAreAppendedToInvoice()
|
||||
public function testFeesAreAppendedToInvoice() //after refactor this may be redundant
|
||||
{
|
||||
|
||||
$data = [];
|
||||
|
Loading…
Reference in New Issue
Block a user