Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 7, 2024
1 parent 71330b3 commit 576d024
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class StripeWebhookHandler
{
public function handle(WebhookInvoked $event): void
{
// https://stripe.com/docs/api/events/types
$callback = match ($event->event->type) {
// https://stripe.com/docs/api/events/types
'payment_intent.payment_failed' => function (Event $event): void {
// mark transaction as failed
},
Expand All @@ -72,7 +72,7 @@ class StripeWebhookHandler
},
};

call_user_func_array($callback, [$evet->event]);
call_user_func_array($callback, [$event->event]);
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"php": "^8.1 || ^8.2",
"php": "^8.2 || ^8.3",
"conedevelopment/bazar": "@dev",
"stripe/stripe-php": "^13.1"
},
Expand Down
29 changes: 29 additions & 0 deletions src/Listeners/HandleWebook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Cone\Bazar\Stripe\Listeners;

use Cone\Bazar\Models\Transaction;
use Cone\Bazar\Stripe\Events\WebhookInvoked;
use Stripe\Event;

class HandleWebhook
{
/**
* Handle the event.
*/
public function handle(WebhookInvoked $event): void
{
$callback = match ($event->event->type) {
'payment_intent.succeeded' => function (Event $stripeEvent): void {
$transaction = Transaction::query()->where('key', $stripeEvent->data['object']['id'])->firstOrFail();

$transaction->markAsCompleted();
},
default => function (): void {
//
},
};

call_user_func_array($callback, [$event->event]);
}
}
25 changes: 7 additions & 18 deletions src/StripeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

class StripeDriver extends Driver
{
/**
* The driver name.
*/
protected string $name = 'stripe';

/**
* The Stripe client instance.
*/
Expand Down Expand Up @@ -47,7 +52,7 @@ public static function redirectUrlAfterPayment(Closure $callback): void
/**
* Resolve the redirect URL after payment.
*/
public function resolveRedirectUrlAfterPayment(Order $order, string $status, Transaction $transaction = null): string
public function resolveRedirectUrlAfterPayment(Order $order, string $staus, ?Transaction $transaction = null): string

Check warning on line 55 in src/StripeDriver.php

View workflow job for this annotation

GitHub Actions / 文A Typos check

"staus" should be "status".
{
if (! is_null(static::$redirectUrlAfterPayment)) {
return call_user_func_array(static::$redirectUrlAfterPayment, [$order, $status, $transaction]);
Expand All @@ -59,22 +64,6 @@ public function resolveRedirectUrlAfterPayment(Order $order, string $status, Tra
};
}

/**
* {@inheritdoc}
*/
public function pay(Order $order, float $amount = null, array $attributes = []): Transaction
{
return $order->pay($amount, 'stripe', $attributes);
}

/**
* {@inheritdoc}
*/
public function refund(Order $order, float $amount = null, array $attributes = []): Transaction
{
return $order->refund($amount, 'stripe', $attributes);
}

/**
* {@inheritdoc}
*/
Expand All @@ -101,7 +90,7 @@ protected function createSession(Order $order): Session
'quantity' => $item->getQuantity(),
];
})->toArray(),
'billing_address_collection' => 'required',
// 'billing_address_collection' => 'required',
'mode' => 'payment',
'success_url' => $this->redirectUrl('success'),
'cancel_url' => $this->redirectUrl('cancelled'),
Expand Down
19 changes: 19 additions & 0 deletions src/StripeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

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 @@ -36,6 +38,15 @@ public function boot(): void
);
}

$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)
Expand All @@ -46,4 +57,12 @@ public function boot(): void
->name('bazar.stripe.webhook');
}
}

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

0 comments on commit 576d024

Please sign in to comment.