<?php

namespace App\Jobs\StripeWebhooks;

use App\Http\Helpers\StripeClient;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;
use Stripe\Event;

class HandlePaymentSuccess implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /** @var \Spatie\WebhookClient\Models\WebhookCall */
    public $webhookCall;

    public function __construct(WebhookCall $webhookCall)
    {
        $this->webhookCall = $webhookCall;
    }

    public function handle()
    {
        logger('HandlePaymentSuccess', ['webhookCall' => $this->webhookCall]);
        $webhookCall = $this->webhookCall;
        $intent = Event::constructFrom($webhookCall->payload)->data?->object;
        StripeClient::confirmPayment($intent->id);
    }
}
