Skip to content

Commit

Permalink
SK-119
Browse files Browse the repository at this point in the history
  • Loading branch information
bvlinsky authored and Mariusz Baranowski committed May 24, 2022
1 parent 5f6ce90 commit 3513601
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
19 changes: 19 additions & 0 deletions app/Events/OrderUpdatedPaid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Events;

use App\Models\Order;

class OrderUpdatedPaid extends OrderEvent
{
public function __construct(Order $order)
{
parent::__construct($order);
$this->order->refresh();
}

public function getOrder(): Order
{
return $this->order;
}
}
2 changes: 1 addition & 1 deletion app/Events/WebHookEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions app/Observers/PaymentObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
12 changes: 12 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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([
Expand All @@ -153,6 +157,8 @@ public function testPayuNotification($user): void
'id' => $payment->getKey(),
'paid' => true,
]);

Event::assertDispatched(OrderUpdatedPaid::class);
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -202,6 +210,8 @@ public function testOfflinePayment($user): void

$this->order->refresh();
$this->assertTrue($this->order->paid);

Event::assertDispatched(OrderUpdatedPaid::class);
}

/**
Expand Down

0 comments on commit 3513601

Please sign in to comment.