diff --git a/app/Events/OrderUpdatedPaid.php b/app/Events/OrderUpdatedPaid.php new file mode 100644 index 000000000..b2f70b821 --- /dev/null +++ b/app/Events/OrderUpdatedPaid.php @@ -0,0 +1,19 @@ +order->refresh(); + } + + public function getOrder(): Order + { + return $this->order; + } +} diff --git a/app/Events/WebHookEvent.php b/app/Events/WebHookEvent.php index d471e9734..0670205cf 100644 --- a/app/Events/WebHookEvent.php +++ b/app/Events/WebHookEvent.php @@ -24,7 +24,7 @@ abstract class WebHookEvent public function __construct() { $this->triggered_at = Carbon::now()->format('c'); - $this->issuer = Auth::user()->getAuthIdentifier() ? Auth::user() : null; + $this->issuer = Auth::user()?->getAuthIdentifier() ? Auth::user() : null; } public function getData(): array diff --git a/app/Observers/PaymentObserver.php b/app/Observers/PaymentObserver.php index 5aa4999c8..b98cd9e7d 100644 --- a/app/Observers/PaymentObserver.php +++ b/app/Observers/PaymentObserver.php @@ -2,25 +2,34 @@ namespace App\Observers; +use App\Events\OrderUpdatedPaid; use App\Models\Payment; class PaymentObserver { public function created(Payment $payment): void { - if ($payment->paid) { - $payment->order->update([ - 'paid' => $payment->order->isPaid(), - ]); - } + $this->process($payment); } public function updated(Payment $payment): void + { + $this->process($payment); + } + + private function process(Payment $payment): void { if ($payment->paid) { - $payment->order->update([ - 'paid' => $payment->order->isPaid(), - ]); + $isPaid = $payment->order->isPaid(); + + // update only if paid status changed + if ($payment->order->paid !== $isPaid) { + $payment->order->update([ + 'paid' => $isPaid, + ]); + + OrderUpdatedPaid::dispatch($payment->order); + } } } } diff --git a/docker-compose.yaml b/docker-compose.yaml index e8fa1469c..36679750c 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -23,6 +23,18 @@ services: depends_on: - mysql_service - elasticsearch + queue: + build: + context: ./docker + dockerfile: Dockerfile-dev + restart: unless-stopped + volumes: + - .:/usr/src/app + command: php artisan queue:work + depends_on: + - app + - mysql_service + - redis mysql_service: image: mariadb:10.5 restart: unless-stopped diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index 1accd0825..a78cb5964 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -2,12 +2,14 @@ namespace Tests\Feature; +use App\Events\OrderUpdatedPaid; use App\Models\Order; use App\Models\Payment; use App\Models\Product; use App\Models\ShippingMethod; use App\Models\Status; use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Http; use Tests\TestCase; @@ -127,6 +129,8 @@ public function testPayuNotificationUnauthorized(): void */ public function testPayuNotification($user): void { + Event::fake(OrderUpdatedPaid::class); + $this->$user->givePermissionTo('payments.edit'); $payment = Payment::factory()->make([ @@ -153,6 +157,8 @@ public function testPayuNotification($user): void 'id' => $payment->getKey(), 'paid' => true, ]); + + Event::assertDispatched(OrderUpdatedPaid::class); } /** @@ -172,6 +178,8 @@ public function testOfflinePaymentUnauthorized($user): void */ public function testOfflinePayment($user): void { + Event::fake(OrderUpdatedPaid::class); + $this->$user->givePermissionTo('payments.offline'); $code = $this->order->code; @@ -202,6 +210,8 @@ public function testOfflinePayment($user): void $this->order->refresh(); $this->assertTrue($this->order->paid); + + Event::assertDispatched(OrderUpdatedPaid::class); } /**