99 lines
2.4 KiB
PHP
99 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\Order;
|
|
|
|
use App\Enums\Order\PaymentModeEnum;
|
|
use App\Events\OrderIsRefunded;
|
|
use App\Models\Payment\Transaction;
|
|
use App\Models\Shop\Order;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class RefundOrder implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* Order
|
|
*
|
|
* @var \App\Models\Shop\Order
|
|
*/
|
|
private $order;
|
|
|
|
/**
|
|
* Transaction that was refund
|
|
*
|
|
* @var \App\Models\Payment\Transaction
|
|
*/
|
|
private $transaction;
|
|
|
|
/**
|
|
* Amount to refund
|
|
*
|
|
* @var integer
|
|
*/
|
|
private $amountToRefund;
|
|
|
|
/**
|
|
* Items that are refunded
|
|
*
|
|
* @var object
|
|
*/
|
|
private $items;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param \App\Models\Shop\Order $order
|
|
* @param \App\Models\Payment\Transaction $transaction
|
|
* @param integer $amountToRefund
|
|
* @param object $items
|
|
* @return void
|
|
*/
|
|
public function __construct(
|
|
Order $order,
|
|
Transaction $transaction,
|
|
int $amountToRefund,
|
|
object $items
|
|
) {
|
|
$this->order = $order;
|
|
$this->transaction = $transaction;
|
|
$this->amountToRefund = $amountToRefund;
|
|
$this->items = $items;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
* @throws \RuntimeException If the transaction payment mode given is inShop or if the transaction refund failed.
|
|
* @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
|
*/
|
|
public function handle()
|
|
{
|
|
// phpcs:enable
|
|
if ($this->transaction->payment_mode->equals(PaymentModeEnum::inShop())) {
|
|
throw new \RuntimeException('An inShop transaction cannot be refunded by the platform');
|
|
}
|
|
|
|
$refundTransaction = $this->transaction->getAdapter()->doRefundCustomer(
|
|
$this->transaction,
|
|
$this->amountToRefund,
|
|
$this->items
|
|
);
|
|
|
|
$refundTransaction->order()->associate($this->order);
|
|
$refundTransaction->saveOrFail();
|
|
|
|
OrderIsRefunded::dispatch(
|
|
$this->order->refresh(),
|
|
$refundTransaction->refresh()
|
|
);
|
|
}
|
|
}
|