From e163135f1f35785f111d556089489eb254d1fb68 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 1 Jun 2020 08:29:53 +1000 Subject: [PATCH] Fixes for refunding --- app/Repositories/BaseRepository.php | 4 +-- app/Services/Payment/RefundPayment.php | 41 +++++++++++++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index b79dad1a9401..6a1084bec687 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -296,8 +296,8 @@ class BaseRepository $model = $model->calc()->getInvoice(); - event(new InvoiceWasUpdated($model)); - + event(new InvoiceWasUpdated($model, $model->company)); + } if ($class->name == Credit::class) { diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index 18debdd5f499..cf4699d13bd7 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -5,15 +5,17 @@ namespace App\Services\Payment; use App\Exceptions\PaymentRefundFailed; use App\Factory\CreditFactory; use App\Factory\InvoiceItemFactory; +use App\Models\Activity; use App\Models\CompanyGateway; use App\Models\Credit; use App\Models\Payment; +use App\Repositories\ActivityRepository; class RefundPayment { public $payment; - public $refund_data + public $refund_data; private $credit_note; @@ -38,13 +40,13 @@ class RefundPayment { return $this->calculateTotalRefund() //sets amount for the refund (needed if we are refunding multiple invoices in one payment) - ->setStatus() - ->buildCreditNote() - ->buildCreditLineItems() - ->updateCreditables() - ->updatePaymentables() - ->createActivity() - ->processGatewayRefund(); + ->setStatus() //sets status of payment + ->buildCreditNote() //generate the credit note + ->buildCreditLineItems() //generate the credit note items + ->updateCreditables() //return the credits first + ->updatePaymentables() //update the paymentable items + ->createActivity() // create the refund activity + ->processGatewayRefund() //process the gateway refund if needed ->save(); } @@ -55,7 +57,7 @@ class RefundPayment $gateway = CompanyGateway::find($this->company_gateway_id); if ($gateway) { - $response = $gateway->driver($this->client)->refund($this, $this->total_refund); + $response = $gateway->driver($this->payment->client)->refund($this->payment, $this->total_refund); if (!$response) { throw new PaymentRefundFailed(); @@ -66,9 +68,28 @@ class RefundPayment return $this; } - private function createActivity() + private function createActivity(array $data, int $credit_id) { + $fields = new \stdClass; + $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; + + if (isset($this->refund_data['invoices'])) { + foreach ($this->refund_data['invoices'] as $invoice) { + $fields->invoice_id = $invoice->id; + + $activity_repo->save($fields, $this->payment); + } + } else { + $activity_repo->save($fields, $this->payment); + } + + return $this; } private function calculateTotalRefund()