Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Feb 6, 2024
1 parent 56b7f57 commit 3c101b8
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/Listeners/HandlePaymentIntentSucceededEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cone\Bazar\Models\Transaction;
use Cone\Bazar\Stripe\Events\StripeWebhookInvoked;
use Cone\Bazar\Support\Facades\Gateway;
use Illuminate\Support\Facades\Date;
use Stripe\Event;
use Throwable;

Expand All @@ -16,23 +17,29 @@ class HandlePaymentIntentSucceededEvent
*/
public function handle(StripeWebhookInvoked $event): void
{
$order = Order::proxy()
->newQuery()
->where('uuid', $event->event->data['object']['metadata']['order'])
->firstOrFail();

switch ($event->event->type) {
case 'charge.refunded':
$this->handleRefund($event->event, $order);
break;
case 'payment_intent.succeeded':
$this->completeTransaction($event->event);
$this->handlePayment($event->event, $order);
break;
}
}

/**
* Handle payment.
* Handle the payment.
*/
public function completeTransaction(Event $event): void
public function handlePayment(Event $event, Order $order): void
{
try {
$transaction = Transaction::proxy()->newQuery()->where('key', $event->data['object']['id'])->firstOrFail();
} catch (Throwable $exception) {
$order = Order::proxy()->newQuery()->where('uuid', $event->data['object']['metadata']['order'])->firstOrFail();

$transaction = Gateway::driver('stripe')->pay(
$order,
$event->data['object']['amount'] / 100,
Expand All @@ -42,4 +49,20 @@ public function completeTransaction(Event $event): void

$transaction->markAsCompleted();
}

/**
* Handle the refund.
*/
public function handleRefund(Event $event, Order $order): void
{
foreach ($event->data['refunds']['data'] as $refund) {
if (is_null($order->transactions->firstWhere('key', $refund['id']))) {
Gateway::driver('stripe')->refund(
$order,
$refund['amount'] / 100,
['key' => $refund['id'], 'completed_at' => Date::now()]
);
}
}
}
}

0 comments on commit 3c101b8

Please sign in to comment.