Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 13, 2024
1 parent 576d024 commit c8ef0ae
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 191 deletions.
2 changes: 1 addition & 1 deletion config/bazar_stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

'success_url' => env('STRIPE_SUCCESS_URL', '/'),

'cancel_url' => env('STRIPE_CANCEL_URL', '/'),
'failure_url' => env('STRIPE_FAILURE_URL', '/'),

];
24 changes: 0 additions & 24 deletions src/Events/WebhookInvoked.php

This file was deleted.

39 changes: 0 additions & 39 deletions src/Http/Controllers/PaymentController.php

This file was deleted.

29 changes: 0 additions & 29 deletions src/Http/Controllers/WebhookController.php

This file was deleted.

29 changes: 0 additions & 29 deletions src/Listeners/HandleWebook.php

This file was deleted.

106 changes: 68 additions & 38 deletions src/StripeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Cone\Bazar\Stripe;

use Closure;
use Cone\Bazar\Gateway\Driver;
use Cone\Bazar\Gateway\Response;
use Cone\Bazar\Interfaces\LineItem;
Expand All @@ -12,7 +11,7 @@
use Illuminate\Support\Facades\URL;
use Stripe\Checkout\Session;
use Stripe\StripeClient;
use Throwable;
use Stripe\Webhook;

class StripeDriver extends Driver
{
Expand All @@ -26,10 +25,7 @@ class StripeDriver extends Driver
*/
public readonly StripeClient $client;

/**
* The payment redirect URL resolver callback.
*/
protected static ?Closure $redirectUrlAfterPayment = null;
protected ?Session $session = null;

/**
* Create a new driver instance.
Expand All @@ -42,34 +38,21 @@ public function __construct(array $config = [])
}

/**
* Set the redirect URL resolver after payment.
*/
public static function redirectUrlAfterPayment(Closure $callback): void
{
static::$redirectUrlAfterPayment = $callback;
}

/**
* Resolve the redirect URL after payment.
* {@inheritdoc}
*/
public function resolveRedirectUrlAfterPayment(Order $order, string $staus, ?Transaction $transaction = null): string
public function getTransactionUrl(Transaction $transaction): ?string
{
if (! is_null(static::$redirectUrlAfterPayment)) {
return call_user_func_array(static::$redirectUrlAfterPayment, [$order, $status, $transaction]);
}

return match ($status) {
'success' => $this->config['success_url'] ?? '/',
default => $this->config['cancel_url'] ?? '/',
};
return sprintf('https://dashboard.stripe.com/%spayments/%s', $this->config['test_mode'] ? 'test/' : '', $transaction->key);
}

/**
* {@inheritdoc}
*/
public function getTransactionUrl(Transaction $transaction): ?string
public function getCaptureUrl(Order $order): string
{
return sprintf('https://dashboard.stripe.com/%spayments/%s', $this->config['test_mode'] ? 'test/' : '', $transaction->key);
return URL::route('bazar.gateway.capture', [
'driver' => $this->name,
]).'&session_id={CHECKOUT_SESSION_ID}';
}

/**
Expand All @@ -90,32 +73,79 @@ protected function createSession(Order $order): Session
'quantity' => $item->getQuantity(),
];
})->toArray(),
// 'billing_address_collection' => 'required',
'mode' => 'payment',
'success_url' => $this->redirectUrl('success'),
'cancel_url' => $this->redirectUrl('cancelled'),
'success_url' => $this->getCaptureUrl($order),
'cancel_url' => $this->getFailureUrl($order),
]);
}

/**
* Get the redirect URL.
* {@inheritdoc}
*/
protected function redirectUrl(string $status): string
public function checkout(Request $request, Order $order): Order
{
return URL::route('bazar.stripe.payment', ['status' => $status]).'&session_id={CHECKOUT_SESSION_ID}';
$this->session = $this->createSession($order);

return $order;
}

/**
* {@inheritdoc}
*/
public function handleCheckout(Request $request): Response
{
$response = parent::handleCheckout($request);

return $response->url($this->session->url);
}

/**
* {@inheritdoc}
*/
public function checkout(Request $request, Order $order): Response
public function resolveOrderForCapture(Request $request): Order
{
try {
$url = $this->createSession($order)->url;
} catch (Throwable $exception) {
$url = $this->redirectUrl('failed');
return Order::query()->where('uuid', $this->session->client_reference_id)->firstOrFail();
}

/**
* {@inheritdoc}
*/
public function capture(Request $request, Order $order): Order
{
$this->pay($order, null, ['key' => $this->session->payment_intent]);

return $order;
}

/**
* {@inheritdoc}
*/
public function handleCapture(Request $request): Response
{
$this->session = $this->client->checkout->sessions->retrieve(
$request->input('session_id')
);

return parent::handleCapture($request);
}

/**
* {@inheritdoc}
*/
public function handleNotification(Request $request): Response
{
$event = Webhook::constructEvent(
$request->getContent(),
$request->server('HTTP_STRIPE_SIGNATURE'),
$this->config['secret']
);

if ($event->event->type === 'payment_intent.succeeded') {
$transaction = Transaction::query()->where('key', $event->data['object']['id'])->firstOrFail();

$transaction->markAsCompleted();
}

return parent::checkout($request, $order)->url($url);
return parent::handleNotification($request);
}
}
31 changes: 0 additions & 31 deletions src/StripeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace Cone\Bazar\Stripe;

use Cone\Bazar\Stripe\Events\WebhookInvoked;
use Cone\Bazar\Stripe\Http\Controllers\PaymentController;
use Cone\Bazar\Stripe\Http\Controllers\WebhookController;
use Cone\Bazar\Stripe\Listeners\HandleWebhook;
use Cone\Bazar\Support\Facades\Gateway;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -37,32 +33,5 @@ public function boot(): void
'bazar-stripe-config'
);
}

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

/**
* Register the package routes.
*/
protected function registerRoutes(): void
{
if (! $this->app->routesAreCached()) {
$this->app['router']
->get('/bazar/stripe/payment', PaymentController::class)
->name('bazar.stripe.payment');

$this->app['router']
->post('/bazar/stripe/webhook', WebhookController::class)
->name('bazar.stripe.webhook');
}
}

/**
* Register events.
*/
protected function registerEvents(): void
{
$this->app['events']->listen(WebhookInvoked::class, HandleWebhook::class);
}
}

0 comments on commit c8ef0ae

Please sign in to comment.