From 9d4c6a12ddeb59510e006194dd39cdfaf2f0f59a Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 19 Jul 2016 20:12:28 +0300 Subject: [PATCH] Use credits when autobilling invoices --- app/Services/PaymentService.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index 4c58f62f4624..b0c5353c2d27 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -57,6 +57,21 @@ class PaymentService extends BaseService return false; } + if ($credits = $client->credits->sum('balance')) { + $balance = $invoice->balance; + $amount = min($credits, $balance); + $data = [ + 'payment_type_id' => PAYMENT_TYPE_CREDIT, + 'invoice_id' => $invoice->id, + 'client_id' => $client->id, + 'amount' => $amount, + ]; + $payment = $this->paymentRepo->save($data); + if ($amount == $balance) { + return $payment; + } + } + $paymentDriver = $account->paymentDriver($invitation, GATEWAY_TYPE_TOKEN); $customer = $paymentDriver->customer();