diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index 86b41ec31912..a0d6d494da13 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -108,6 +108,8 @@ class PaymentController extends Controller $settings = auth()->user()->client->getMergedSettings(); + //nlog($settings); + /* This loop checks for under / over payments and returns the user if a check fails */ foreach($payable_invoices as $payable_invoice) @@ -220,7 +222,7 @@ class PaymentController extends Controller $payment_hash = new PaymentHash; $payment_hash->hash = Str::random(128); - $payment_hash->data = ['invoices' => $payable_invoices->toArray()]; + $payment_hash->data = ['invoices' => $payable_invoices->toArray() , 'credits' => $credit_totals]; $payment_hash->fee_total = $fee_totals; $payment_hash->fee_invoice_id = $first_invoice->id; $payment_hash->save(); @@ -255,6 +257,7 @@ class PaymentController extends Controller public function response(PaymentResponseRequest $request) { + $gateway = CompanyGateway::findOrFail($request->input('company_gateway_id')); $payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->payment_hash])->first(); @@ -289,35 +292,7 @@ class PaymentController extends Controller $payment_hash->save(); } - /* Iterate through the invoices and apply credits to them */ - collect($payment_hash->invoices())->each(function ($payable_invoice) use ($payment, $payment_hash) { - $invoice = Invoice::find($this->decodePrimaryKey($payable_invoice->invoice_id)); - $amount = $payable_invoice->amount; - - $credits = $payment_hash->fee_invoice - ->client - ->service() - ->getCredits(); - - foreach ($credits as $credit) { - //starting invoice balance - $invoice_balance = $invoice->balance; - - //credit payment applied - $credit->service()->applyPayment($invoice, $amount, $payment); - - //amount paid from invoice calculated - $remaining_balance = ($invoice_balance - $invoice->fresh()->balance); - - //reduce the amount to be paid on the invoice from the NEXT credit - $amount -= $remaining_balance; - - //break if the invoice is no longer PAYABLE OR there is no more amount to be applied - if (!$invoice->isPayable() || (int)$amount == 0) { - break; - } - } - }); + $payment = $payment->service()->applyCredits($payment_hash)->save(); return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); } diff --git a/app/Models/PaymentHash.php b/app/Models/PaymentHash.php index 264884b429c3..2c1a9b0ed7bb 100644 --- a/app/Models/PaymentHash.php +++ b/app/Models/PaymentHash.php @@ -27,6 +27,11 @@ class PaymentHash extends Model return $this->data->invoices; } + public function credits_total() + { + return isset($this->data->credits) ? $this->data->credits : 0; + } + public function payment() { return $this->belongsTo(Payment::class)->withTrashed(); diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 983b3f5c7643..8366c4f5f627 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -213,6 +213,9 @@ class BaseDriver extends AbstractPaymentDriver $this->attachInvoices($payment, $this->payment_hash); + if($this->payment_hash->credits_total() > 0) + $payment = $payment->service()->applyCredits($this->payment_hash)->save(); + $payment->service()->updateInvoicePayment($this->payment_hash); event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); diff --git a/app/Services/Payment/PaymentService.php b/app/Services/Payment/PaymentService.php index 954a904b68aa..c0c4f0acbfdd 100644 --- a/app/Services/Payment/PaymentService.php +++ b/app/Services/Payment/PaymentService.php @@ -15,9 +15,12 @@ use App\Factory\PaymentFactory; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentHash; +use App\Utils\Traits\MakesHash; class PaymentService { + use MakesHash; + private $payment; public function __construct($payment) @@ -97,6 +100,43 @@ class PaymentService return $this; } + public function applyCredits($payment_hash) + { + /* Iterate through the invoices and apply credits to them */ + collect($payment_hash->invoices())->each(function ($payable_invoice) use ($payment_hash) { + + $invoice = Invoice::find($this->decodePrimaryKey($payable_invoice->invoice_id)); + + $amount = $payable_invoice->amount; + + $credits = $payment_hash->fee_invoice + ->client + ->service() + ->getCredits(); + + foreach ($credits as $credit) { + //starting invoice balance + $invoice_balance = $invoice->balance; + + //credit payment applied + $credit->service()->applyPayment($invoice, $amount, $this->payment); + + //amount paid from invoice calculated + $remaining_balance = ($invoice_balance - $invoice->fresh()->balance); + + //reduce the amount to be paid on the invoice from the NEXT credit + $amount -= $remaining_balance; + + //break if the invoice is no longer PAYABLE OR there is no more amount to be applied + if (!$invoice->isPayable() || (int)$amount == 0) { + break; + } + } + }); + + return $this; + } + public function save() { $this->payment->save();