diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index 9ff34d666c78..d27a0eaf2027 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -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 diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index b665a29f2d67..f7a89f5d34b2 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -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 + } + + } } diff --git a/tests/Feature/CompanyGatewayTest.php b/tests/Feature/CompanyGatewayTest.php index 94747464121d..167433dda6e4 100644 --- a/tests/Feature/CompanyGatewayTest.php +++ b/tests/Feature/CompanyGatewayTest.php @@ -109,7 +109,7 @@ class CompanyGatewayTest extends TestCase return $passes; } - public function testFeesAreAppendedToInvoice() + public function testFeesAreAppendedToInvoice() //after refactor this may be redundant { $data = [];