1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Services/Payment/RefundPayment.php

346 lines
11 KiB
PHP
Raw Normal View History

2020-05-29 00:21:47 +02:00
<?php
2020-07-01 03:06:40 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2020-07-01 03:06:40 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2020-07-01 03:06:40 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-07-01 03:06:40 +02:00
*/
2020-05-29 00:21:47 +02:00
namespace App\Services\Payment;
2020-05-29 08:24:41 +02:00
use App\Exceptions\PaymentRefundFailed;
2022-03-14 04:39:44 +01:00
use App\Jobs\Payment\EmailRefundPayment;
2020-06-01 00:29:53 +02:00
use App\Models\Activity;
2020-05-29 00:21:47 +02:00
use App\Models\Credit;
2020-06-01 05:16:06 +02:00
use App\Models\Invoice;
2020-05-29 00:21:47 +02:00
use App\Models\Payment;
2020-06-01 00:29:53 +02:00
use App\Repositories\ActivityRepository;
2020-07-17 11:47:17 +02:00
use App\Utils\Ninja;
2020-10-28 11:10:49 +01:00
use stdClass;
2020-05-29 00:21:47 +02:00
class RefundPayment
{
2023-09-27 06:12:03 +02:00
private float $total_refund = 0;
private float $credits_used = 0;
2020-05-29 08:24:41 +02:00
private $gateway_refund_status;
private $activity_repository;
2023-09-09 14:20:12 +02:00
private bool $refund_failed = false;
private string $refund_failed_message = '';
public function __construct(public Payment $payment, public array $refund_data)
2020-05-29 00:21:47 +02:00
{
2020-05-29 08:24:41 +02:00
$this->gateway_refund_status = false;
$this->activity_repository = new ActivityRepository();
2020-05-29 00:21:47 +02:00
}
public function run()
{
2023-09-09 14:20:12 +02:00
$this->payment = $this
->calculateTotalRefund() //sets amount for the refund (needed if we are refunding multiple invoices in one payment)
2023-09-27 06:12:03 +02:00
->updateCreditables() //return the credits first
2023-09-09 14:20:12 +02:00
->processGatewayRefund() //process the gateway refund if needed
2022-09-06 12:29:32 +02:00
->setStatus() //sets status of payment
->updatePaymentables() //update the paymentable items
->adjustInvoices()
2023-09-09 14:20:12 +02:00
->finalize()
2022-09-06 12:29:32 +02:00
->save();
2022-03-10 01:32:04 +01:00
if (array_key_exists('email_receipt', $this->refund_data) && $this->refund_data['email_receipt'] == 'true') {
$contact = $this->payment->client->contacts()->whereNotNull('email')->first();
EmailRefundPayment::dispatch($this->payment, $this->payment->company, $contact);
}
2022-03-14 02:35:05 +01:00
2022-10-13 23:24:28 +02:00
$notes = ctrans('texts.refunded') . " : {$this->total_refund} - " . ctrans('texts.gateway_refund') . " : ";
$notes .= $this->refund_data['gateway_refund'] !== false ? ctrans('texts.yes') : ctrans('texts.no');
$this->createActivity($notes);
2022-10-12 02:27:14 +02:00
2022-03-10 03:27:31 +01:00
return $this->payment;
2020-05-29 08:24:41 +02:00
}
2023-09-09 14:20:12 +02:00
private function finalize(): self
{
2023-10-26 04:57:44 +02:00
if($this->refund_failed) {
2023-09-09 14:20:12 +02:00
throw new PaymentRefundFailed($this->refund_failed_message);
2023-10-26 04:57:44 +02:00
}
2023-09-09 14:20:12 +02:00
return $this;
}
2020-06-27 02:05:31 +02:00
/**
* Process the refund through the gateway.
*
2023-09-12 02:27:30 +02:00
* $response
2023-09-09 14:20:12 +02:00
* [
* 'transaction_reference' => (string),
* 'transaction_response' => (string),
* 'success' => (bool),
* 'description' => (string),
* 'code' => (string),
* 'payment_id' => (int),
* 'amount' => (float),
* ];
2023-10-26 04:57:44 +02:00
*
* @return $this
2020-10-28 11:10:49 +01:00
* @throws PaymentRefundFailed
2020-06-27 02:05:31 +02:00
*/
2020-05-29 08:24:41 +02:00
private function processGatewayRefund()
{
2023-09-27 06:12:03 +02:00
$net_refund = ($this->total_refund - $this->credits_used);
if ($this->refund_data['gateway_refund'] !== false && $net_refund > 0) {
2020-06-27 02:05:31 +02:00
if ($this->payment->company_gateway) {
2023-09-27 06:12:03 +02:00
$response = $this->payment->company_gateway->driver($this->payment->client)->refund($this->payment, $net_refund);
2020-05-29 08:24:41 +02:00
2023-10-26 04:57:44 +02:00
if($response['amount'] ?? false) {
2023-09-27 06:12:03 +02:00
$net_refund = $response['amount'];
2023-10-26 04:57:44 +02:00
}
2023-09-09 14:20:12 +02:00
2023-10-26 04:57:44 +02:00
if($response['voided'] ?? false) {
//When a transaction is voided - all invoices attached to the payment need to be reversed, this
2023-09-09 14:20:12 +02:00
//block prevents the edge case where a partial refund was attempted.
2023-10-26 04:57:44 +02:00
$this->refund_data['invoices'] = $this->payment->invoices->map(function ($invoice) {
2023-09-09 14:20:12 +02:00
return [
'invoice_id' => $invoice->id,
'amount' => $invoice->pivot->amount,
];
})->toArray();
}
2023-09-27 06:12:03 +02:00
$this->payment->refunded += $net_refund;
2020-12-02 01:56:29 +01:00
if ($response['success'] == false) {
2020-12-02 01:56:29 +01:00
$this->payment->save();
2023-09-09 14:20:12 +02:00
$this->refund_failed = true;
$this->refund_failed_message = $response['description'] ?? '';
2020-05-29 08:24:41 +02:00
}
}
} else {
2023-09-27 06:12:03 +02:00
$this->payment->refunded += $net_refund;
}
2020-05-29 08:24:41 +02:00
$this->payment->setRefundMeta($this->refund_data);
2020-05-29 08:24:41 +02:00
return $this;
}
2020-06-27 02:05:31 +02:00
/**
* Create the payment activity.
*
2023-08-02 12:10:42 +02:00
* @param string $notes
* @return $this
2020-06-27 02:05:31 +02:00
*/
private function createActivity($notes)
2020-05-29 08:24:41 +02:00
{
2020-10-28 11:10:49 +01:00
$fields = new stdClass;
2020-06-01 00:29:53 +02:00
$activity_repo = new ActivityRepository();
$fields->payment_id = $this->payment->id;
$fields->user_id = $this->payment->user_id;
$fields->company_id = $this->payment->company_id;
$fields->activity_type_id = Activity::REFUNDED_PAYMENT;
// $fields->credit_id = $this->credit_note->id; // TODO
2022-10-13 23:24:28 +02:00
$fields->notes = $notes;
2020-06-01 00:29:53 +02:00
if (isset($this->refund_data['invoices'])) {
foreach ($this->refund_data['invoices'] as $invoice) {
2020-06-01 05:16:06 +02:00
$fields->invoice_id = $invoice['invoice_id'];
2021-05-06 23:12:07 +02:00
$activity_repo->save($fields, $this->payment, Ninja::eventVars(auth()->user() ? auth()->user()->id : null));
2020-06-01 00:29:53 +02:00
}
} else {
2021-05-06 23:12:07 +02:00
$activity_repo->save($fields, $this->payment, Ninja::eventVars(auth()->user() ? auth()->user()->id : null));
2020-06-01 00:29:53 +02:00
}
2020-05-29 08:24:41 +02:00
2020-06-01 00:29:53 +02:00
return $this;
2020-05-29 00:21:47 +02:00
}
2020-06-27 02:05:31 +02:00
/**
* Determine the amount of refund.
*
2020-06-27 02:05:31 +02:00
* @return $this
*/
2020-05-29 00:21:47 +02:00
private function calculateTotalRefund()
{
if (array_key_exists('invoices', $this->refund_data) && count($this->refund_data['invoices']) > 0) {
2020-06-01 05:16:06 +02:00
$this->total_refund = collect($this->refund_data['invoices'])->sum('amount');
} else {
2020-05-29 08:24:41 +02:00
$this->total_refund = $this->refund_data['amount'];
}
2020-05-29 00:21:47 +02:00
return $this;
}
2020-06-27 02:05:31 +02:00
/**
* Set the payment status.
2020-06-27 02:05:31 +02:00
*/
2020-05-29 00:21:47 +02:00
private function setStatus()
{
2021-01-11 11:51:54 +01:00
if ($this->total_refund == $this->payment->amount) {
2020-05-29 00:21:47 +02:00
$this->payment->status_id = Payment::STATUS_REFUNDED;
} else {
2020-05-29 00:21:47 +02:00
$this->payment->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
}
2020-05-29 08:24:41 +02:00
return $this;
2020-05-29 00:21:47 +02:00
}
2020-06-27 02:05:31 +02:00
/**
* Update the paymentable records.
*
2020-06-27 02:05:31 +02:00
* @return $this
*/
2020-05-29 08:24:41 +02:00
private function updatePaymentables()
2020-05-29 00:21:47 +02:00
{
if (isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) {
2020-05-29 08:24:41 +02:00
$this->payment->invoices->each(function ($paymentable_invoice) {
collect($this->refund_data['invoices'])->each(function ($refunded_invoice) use ($paymentable_invoice) {
if ($refunded_invoice['invoice_id'] == $paymentable_invoice->id) {
2020-05-29 08:24:41 +02:00
$paymentable_invoice->pivot->refunded += $refunded_invoice['amount'];
$paymentable_invoice->pivot->save();
}
});
});
2020-05-29 00:21:47 +02:00
}
2020-05-29 08:24:41 +02:00
return $this;
}
2020-05-29 00:21:47 +02:00
2020-06-27 02:05:31 +02:00
/**
* If credits have been bundled in this payment, we
* need to reverse these.
*
* @return $this
2020-06-27 02:05:31 +02:00
*/
2020-05-29 08:24:41 +02:00
private function updateCreditables()
{
2023-09-27 06:12:03 +02:00
2020-05-29 08:24:41 +02:00
if ($this->payment->credits()->exists()) {
2023-09-27 06:12:03 +02:00
$amount_to_refund = $this->total_refund;
2020-05-29 00:21:47 +02:00
//Adjust credits first!!!
2020-05-29 08:24:41 +02:00
foreach ($this->payment->credits as $paymentable_credit) {
2020-05-29 00:21:47 +02:00
$available_credit = $paymentable_credit->pivot->amount - $paymentable_credit->pivot->refunded;
2023-09-27 06:12:03 +02:00
if ($available_credit > $amount_to_refund) {
$paymentable_credit->pivot->refunded += $amount_to_refund;
2020-05-29 00:21:47 +02:00
$paymentable_credit->pivot->save();
$paymentable_credit->service()
->setStatus(Credit::STATUS_SENT)
2023-09-27 06:12:03 +02:00
->adjustBalance($amount_to_refund)
->updatePaidToDate($amount_to_refund * -1)
->save();
2020-05-29 00:21:47 +02:00
2023-09-27 06:12:03 +02:00
$this->credits_used += $amount_to_refund;
$amount_to_refund = 0;
2020-05-29 00:21:47 +02:00
} else {
$paymentable_credit->pivot->refunded += $available_credit;
$paymentable_credit->pivot->save();
$paymentable_credit->service()
->setStatus(Credit::STATUS_SENT)
2021-01-24 10:53:45 +01:00
->adjustBalance($available_credit)
->updatePaidToDate($available_credit * -1)
->save();
2020-05-29 00:21:47 +02:00
2023-09-27 06:12:03 +02:00
$this->credits_used += $available_credit;
$amount_to_refund -= $available_credit;
2020-05-29 00:21:47 +02:00
}
2023-09-27 06:12:03 +02:00
if ($amount_to_refund == 0) {
2020-05-29 00:21:47 +02:00
break;
}
}
}
2020-05-29 08:24:41 +02:00
return $this;
}
2020-06-27 02:05:31 +02:00
/**
* Reverse the payments made on invoices.
*
* @return $this
2020-06-27 02:05:31 +02:00
*/
2020-06-01 06:18:33 +02:00
private function adjustInvoices()
{
if (isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) {
2023-10-26 04:57:44 +02:00
foreach ($this->refund_data['invoices'] as $refunded_invoice) {
2021-08-08 03:02:41 +02:00
$invoice = Invoice::withTrashed()->find($refunded_invoice['invoice_id']);
2020-06-01 06:18:33 +02:00
if ($invoice->trashed()) {
2022-03-26 03:58:49 +01:00
$invoice->restore();
}
2022-03-26 12:21:35 +01:00
$invoice->service()
->updateBalance($refunded_invoice['amount'])
->updatePaidToDate($refunded_invoice['amount'] * -1)
->save();
2022-09-06 12:29:32 +02:00
$invoice->ledger()
->updateInvoiceBalance($refunded_invoice['amount'], "Refund of payment # {$this->payment->number}")
->save();
if ($invoice->amount == $invoice->balance) {
2020-06-01 06:18:33 +02:00
$invoice->service()->setStatus(Invoice::STATUS_SENT);
} else {
2020-06-01 06:18:33 +02:00
$invoice->service()->setStatus(Invoice::STATUS_PARTIAL);
}
//26-10-2022 - disable autobill to prevent future billings;
$invoice->auto_bill_enabled = false;
2021-10-14 08:54:38 +02:00
$invoice->saveQuietly();
2023-08-08 11:11:59 +02:00
//08-08-2023
2022-09-06 12:29:32 +02:00
$client = $invoice->client
->service()
2023-08-08 11:11:59 +02:00
->updateBalanceAndPaidToDate($refunded_invoice['amount'], -1 * $refunded_invoice['amount'])
2022-09-06 12:29:32 +02:00
->save();
if ($invoice->is_deleted) {
$invoice->delete();
}
2022-03-10 03:27:31 +01:00
}
} else {
2021-10-26 20:01:10 +02:00
//if we are refunding and no payments have been tagged, then we need to decrement the client->paid_to_date by the total refund amount.
2021-10-26 20:01:10 +02:00
$client = $this->payment->client->fresh();
2022-03-26 03:58:49 +01:00
if ($client->trashed()) {
2022-03-26 03:58:49 +01:00
$client->restore();
}
2022-03-26 12:21:35 +01:00
2021-10-26 20:01:10 +02:00
$client->service()->updatePaidToDate(-1 * $this->total_refund)->save();
}
2020-06-01 06:18:33 +02:00
return $this;
}
2020-06-27 02:05:31 +02:00
/**
* Saves the payment.
*
2020-06-27 02:05:31 +02:00
* @return Payment $payment
*/
2020-05-29 08:24:41 +02:00
private function save()
{
$this->payment->save();
return $this->payment;
}
2023-10-26 04:57:44 +02:00
}