2020-02-03 11:33:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* payment Ninja (https://paymentninja.com).
|
2020-02-03 11:33:07 +01:00
|
|
|
*
|
2023-08-20 06:36:22 +02:00
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
2020-02-03 11:33:07 +01:00
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. payment Ninja LLC (https://paymentninja.com)
|
2020-02-03 11:33:07 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-02-03 11:33:07 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Payment;
|
|
|
|
|
|
|
|
use App\Factory\PaymentFactory;
|
2020-02-20 22:05:01 +01:00
|
|
|
use App\Models\Invoice;
|
2020-02-03 11:33:07 +01:00
|
|
|
use App\Models\Payment;
|
2020-09-19 03:20:14 +02:00
|
|
|
use App\Models\PaymentHash;
|
2021-01-06 06:54:04 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-02-03 11:33:07 +01:00
|
|
|
|
|
|
|
class PaymentService
|
|
|
|
{
|
2021-01-06 06:54:04 +01:00
|
|
|
use MakesHash;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-30 03:27:19 +01:00
|
|
|
public function __construct(public Payment $payment)
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public function manualPayment($invoice): ?Payment
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
/* Create Payment */
|
|
|
|
$payment = PaymentFactory::create($invoice->company_id, $invoice->user_id);
|
|
|
|
|
|
|
|
$payment->amount = $invoice->balance;
|
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
|
|
|
$payment->client_id = $invoice->client_id;
|
|
|
|
$payment->transaction_reference = ctrans('texts.manual_entry');
|
2020-03-08 06:59:06 +01:00
|
|
|
$payment->currency_id = $invoice->client->getSetting('currency_id');
|
2020-02-03 11:33:07 +01:00
|
|
|
/* Create a payment relationship to the invoice entity */
|
2021-10-15 09:47:41 +02:00
|
|
|
$payment->saveQuietly();
|
2020-02-03 11:33:07 +01:00
|
|
|
|
|
|
|
$payment->invoices()->attach($invoice->id, [
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $payment->amount,
|
2020-02-03 11:33:07 +01:00
|
|
|
]);
|
|
|
|
|
2021-10-15 09:47:41 +02:00
|
|
|
event('eloquent.created: App\Models\Payment', $payment);
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
return $payment;
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
public function sendEmail($contact = null)
|
|
|
|
{
|
2020-05-09 00:35:49 +02:00
|
|
|
return (new SendEmail($this->payment, $contact))->run();
|
2020-02-20 22:05:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function reversePayment()
|
|
|
|
{
|
|
|
|
$invoices = $this->payment->invoices()->get();
|
|
|
|
$client = $this->payment->client;
|
|
|
|
|
|
|
|
$invoices->each(function ($invoice) {
|
|
|
|
if ($invoice->pivot->amount > 0) {
|
2021-01-24 07:44:14 +01:00
|
|
|
$invoice->service()
|
|
|
|
->updateBalance($invoice->pivot->amount)
|
2022-06-21 11:57:17 +02:00
|
|
|
->updatePaidToDate($invoice->pivot->amount * -1)
|
2021-01-24 07:44:14 +01:00
|
|
|
->setStatus(Invoice::STATUS_SENT)
|
|
|
|
->save();
|
2020-02-20 22:05:01 +01:00
|
|
|
}
|
|
|
|
});
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2020-10-10 12:57:28 +02:00
|
|
|
$this->payment
|
|
|
|
->ledger()
|
2024-04-28 05:52:44 +02:00
|
|
|
->updatePaymentBalance($this->payment->amount, "PaymentService");
|
2020-02-20 22:05:01 +01:00
|
|
|
|
|
|
|
$client->service()
|
|
|
|
->updateBalance($this->payment->amount)
|
2020-09-06 11:38:10 +02:00
|
|
|
->updatePaidToDate($this->payment->amount * -1)
|
2020-02-20 22:05:01 +01:00
|
|
|
->save();
|
2020-10-10 12:57:28 +02:00
|
|
|
|
|
|
|
return $this;
|
2020-02-20 22:05:01 +01:00
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public function refundPayment(array $data): ?Payment
|
2020-05-29 00:21:47 +02:00
|
|
|
{
|
|
|
|
return ((new RefundPayment($this->payment, $data)))->run();
|
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public function deletePayment($update_client_paid_to_date = true): ?Payment
|
2020-06-28 00:24:08 +02:00
|
|
|
{
|
2022-12-14 06:17:43 +01:00
|
|
|
return (new DeletePayment($this->payment, $update_client_paid_to_date))->run();
|
2020-06-28 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public function updateInvoicePayment(PaymentHash $payment_hash): ?Payment
|
2020-02-20 22:05:01 +01:00
|
|
|
{
|
2020-08-31 04:00:43 +02:00
|
|
|
return ((new UpdateInvoicePayment($this->payment, $payment_hash)))->run();
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
2020-08-12 00:17:32 +02:00
|
|
|
|
|
|
|
public function applyNumber()
|
|
|
|
{
|
|
|
|
$this->payment = (new ApplyNumber($this->payment))->run();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-01-06 06:54:04 +01:00
|
|
|
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) {
|
2021-11-07 07:12:51 +01:00
|
|
|
$invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($payable_invoice->invoice_id));
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-01-06 06:54:04 +01:00
|
|
|
$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
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $invoice->isPayable() || (int) $amount == 0) {
|
2021-01-06 06:54:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return $this;
|
2021-01-06 06:54:04 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 05:58:18 +01:00
|
|
|
public function applyCreditsToInvoice($invoice)
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
$amount = $invoice->amount;
|
2022-12-22 05:58:18 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$credits = $invoice->client
|
|
|
|
->service()
|
|
|
|
->getCredits();
|
2022-12-22 05:58:18 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach ($credits as $credit) {
|
|
|
|
//starting invoice balance
|
|
|
|
$invoice_balance = $invoice->balance;
|
2022-12-22 05:58:18 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
//credit payment applied
|
|
|
|
$credit->service()->applyPayment($invoice, $amount, $this->payment);
|
2022-12-22 05:58:18 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
//amount paid from invoice calculated
|
|
|
|
$remaining_balance = ($invoice_balance - $invoice->fresh()->balance);
|
2022-12-22 05:58:18 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
//reduce the amount to be paid on the invoice from the NEXT credit
|
|
|
|
$amount -= $remaining_balance;
|
2022-12-22 05:58:18 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
//break if the invoice is no longer PAYABLE OR there is no more amount to be applied
|
|
|
|
if (! $invoice->isPayable() || (int) $amount == 0) {
|
|
|
|
break;
|
2022-12-22 05:58:18 +01:00
|
|
|
}
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-12-22 05:58:18 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-13 04:30:45 +02:00
|
|
|
public function save()
|
|
|
|
{
|
2021-10-15 09:47:41 +02:00
|
|
|
$this->payment->saveQuietly();
|
2020-08-13 04:30:45 +02:00
|
|
|
|
|
|
|
return $this->payment->fresh();
|
|
|
|
}
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|