Skip to content

Commit

Permalink
rework gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 13, 2024
1 parent a1d3dbe commit 8154dff
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 78 deletions.
8 changes: 4 additions & 4 deletions config/bazar.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
'default' => env('BAZAR_GATEWAY_DRIVER', 'transfer'),
'drivers' => [
'cash' => [
'success_url' => '/',
'failed_url' => '/',
'success_url' => '/?order={order}&status=success',
'failure_url' => '/?order={order}&status=failed',
],
'transfer' => [
'success_url' => '/',
'failed_url' => '/',
'success_url' => '/?order={order}&status=success',
'failure_url' => '/?order={order}&status=failed',
],
],
],
Expand Down
8 changes: 8 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Cone\Bazar\Http\Controllers\GatewayController;
use Illuminate\Support\Facades\Route;

// Gateway
Route::any('/gateway/{driver}/capture', [GatewayController::class, 'capture'])->name('gateway.capture');
Route::any('/gateway/{driver}/notification', [GatewayController::class, 'notification'])->name('gateway.notification');
21 changes: 19 additions & 2 deletions src/BazarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,27 @@ public function boot(): void
$this->registerPublishes();
}

if (! $this->app->routesAreCached()) {
$this->registerRoutes();
}

$this->registerEvents();
$this->registerViews();
}

/**
* Register routes.
*/
protected function registerRoutes(): void
{
$this->app['router']
->prefix('bazar')
->as('bazar.')
->group(function (): void {
$this->loadRoutesFrom(__DIR__.'/../routes/auth.php');
});
}

/**
* Register views.
*/
Expand Down Expand Up @@ -103,7 +120,7 @@ protected function registerCommands(): void
protected function registerEvents(): void
{
$this->app['events']->listen(Logout::class, Listeners\ClearCookies::class);
$this->app['events']->listen(Events\CheckoutProcessed::class, Listeners\PlaceOrder::class);
$this->app['events']->listen(Events\CheckoutProcessed::class, Listeners\RefreshInventory::class);
$this->app['events']->listen(Events\PaymentCaptured::class, Listeners\PlaceOrder::class);
$this->app['events']->listen(Events\PaymentCaptured::class, Listeners\RefreshInventory::class);
}
}
4 changes: 2 additions & 2 deletions src/Cart/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ public function isEmpty(): bool
*/
public function checkout(string $driver): Response
{
return App::call(function (Request $request) use ($driver): Response {
return Gateway::driver($driver)->checkout($request, $this->getModel()->toOrder());
return App::call(static function (Request $request) use ($driver): Response {
return Gateway::driver($driver)->handleCheckout($request);
});
}

Expand Down
30 changes: 30 additions & 0 deletions src/Events/CheckoutFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Cone\Bazar\Events;

use Cone\Bazar\Models\Order;
use Illuminate\Foundation\Events\Dispatchable;

class CheckoutFailed
{
use Dispatchable;

/**
* The order instance.
*/
public Order $order;

/**
* The gateway driver.
*/
public string $driver;

/**
* Create a new event instance.
*/
public function __construct(string $driver, Order $order)
{
$this->driver = $driver;
$this->order = $order;
}
}
8 changes: 7 additions & 1 deletion src/Events/CheckoutProcessed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ class CheckoutProcessed
*/
public Order $order;

/**
* The gateway driver.
*/
public string $driver;

/**
* Create a new event instance.
*/
public function __construct(Order $order)
public function __construct(string $driver, Order $order)
{
$this->driver = $driver;
$this->order = $order;
}
}
30 changes: 30 additions & 0 deletions src/Events/PaymentCaptureFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Cone\Bazar\Events;

use Cone\Bazar\Models\Order;
use Illuminate\Foundation\Events\Dispatchable;

class PaymentCaptureFailed
{
use Dispatchable;

/**
* The order instance.
*/
public Order $order;

/**
* The gateway driver.
*/
public string $driver;

/**
* Create a new event instance.
*/
public function __construct(string $driver, Order $order)
{
$this->driver = $driver;
$this->order = $order;
}
}
30 changes: 30 additions & 0 deletions src/Events/PaymentCaptured.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Cone\Bazar\Events;

use Cone\Bazar\Models\Order;
use Illuminate\Foundation\Events\Dispatchable;

class PaymentCaptured
{
use Dispatchable;

/**
* The order instance.
*/
public Order $order;

/**
* The gateway driver.
*/
public string $driver;

/**
* Create a new event instance.
*/
public function __construct(string $driver, Order $order)
{
$this->driver = $driver;
$this->order = $order;
}
}
30 changes: 2 additions & 28 deletions src/Gateway/CashDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Cone\Bazar\Gateway;

use Cone\Bazar\Events\CheckoutProcessed;
use Cone\Bazar\Models\Order;
use Cone\Bazar\Models\Transaction;
use Illuminate\Http\Request;

class CashDriver extends Driver
{
Expand All @@ -19,42 +17,18 @@ class CashDriver extends Driver
*/
public function pay(Order $order, ?float $amount = null, array $attributes = []): Transaction
{
$transaction = parent::pay($order, $amount, array_merge($attributes, [
return parent::pay($order, $amount, array_merge($attributes, [
'completed_at' => time(),
]));

$order->markAs(Order::PENDING);

return $transaction;
}

/**
* Process the refund.
*/
public function refund(Order $order, ?float $amount = null, array $attributes = []): Transaction
{
$transaction = parent::refund($order, $amount, array_merge($attributes, [
return parent::refund($order, $amount, array_merge($attributes, [
'completed_at' => time(),
]));

$order->markAs(Order::REFUNDED);

return $transaction;
}

/**
* Handle the checkout request.
*/
public function checkout(Request $request, Order $order): Response
{
$response = parent::checkout($request, $order);

$url = $order->status === Order::PENDING
? $this->config['success_url']
: $this->config['failed_url'];

CheckoutProcessed::dispatch($order);

return $response->url($url);
}
}
Loading

0 comments on commit 8154dff

Please sign in to comment.