Fixes for refunding

This commit is contained in:
David Bomba 2020-06-01 08:29:53 +10:00
parent 0803ffda11
commit e163135f1f
2 changed files with 33 additions and 12 deletions

View File

@ -296,8 +296,8 @@ class BaseRepository
$model = $model->calc()->getInvoice(); $model = $model->calc()->getInvoice();
event(new InvoiceWasUpdated($model)); event(new InvoiceWasUpdated($model, $model->company));
} }
if ($class->name == Credit::class) { if ($class->name == Credit::class) {

View File

@ -5,15 +5,17 @@ namespace App\Services\Payment;
use App\Exceptions\PaymentRefundFailed; use App\Exceptions\PaymentRefundFailed;
use App\Factory\CreditFactory; use App\Factory\CreditFactory;
use App\Factory\InvoiceItemFactory; use App\Factory\InvoiceItemFactory;
use App\Models\Activity;
use App\Models\CompanyGateway; use App\Models\CompanyGateway;
use App\Models\Credit; use App\Models\Credit;
use App\Models\Payment; use App\Models\Payment;
use App\Repositories\ActivityRepository;
class RefundPayment class RefundPayment
{ {
public $payment; public $payment;
public $refund_data public $refund_data;
private $credit_note; 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) return $this->calculateTotalRefund() //sets amount for the refund (needed if we are refunding multiple invoices in one payment)
->setStatus() ->setStatus() //sets status of payment
->buildCreditNote() ->buildCreditNote() //generate the credit note
->buildCreditLineItems() ->buildCreditLineItems() //generate the credit note items
->updateCreditables() ->updateCreditables() //return the credits first
->updatePaymentables() ->updatePaymentables() //update the paymentable items
->createActivity() ->createActivity() // create the refund activity
->processGatewayRefund(); ->processGatewayRefund() //process the gateway refund if needed
->save(); ->save();
} }
@ -55,7 +57,7 @@ class RefundPayment
$gateway = CompanyGateway::find($this->company_gateway_id); $gateway = CompanyGateway::find($this->company_gateway_id);
if ($gateway) { 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) { if (!$response) {
throw new PaymentRefundFailed(); throw new PaymentRefundFailed();
@ -66,9 +68,28 @@ class RefundPayment
return $this; 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() private function calculateTotalRefund()