2022-05-30 00:19:16 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-05-30 00:19:16 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Stripe\Jobs;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\CompanyGateway;
|
|
|
|
use App\PaymentDrivers\Stripe\Utilities;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
class StripeWebhook implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Utilities;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public $tries = 1;
|
2022-05-30 00:19:16 +02:00
|
|
|
|
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
|
|
|
|
public int $company_gateway_id;
|
|
|
|
|
|
|
|
public string $company_key;
|
|
|
|
|
2022-05-30 12:05:34 +02:00
|
|
|
private bool $url_found = false;
|
|
|
|
|
2022-05-30 00:19:16 +02:00
|
|
|
private array $events = [
|
|
|
|
'source.chargeable',
|
|
|
|
'charge.succeeded',
|
|
|
|
'charge.failed',
|
|
|
|
'payment_intent.succeeded',
|
2022-06-21 11:57:17 +02:00
|
|
|
'payment_intent.payment_failed',
|
2022-12-17 12:53:54 +01:00
|
|
|
'mandate.updated',
|
|
|
|
'checkout.session.completed',
|
|
|
|
'payment_method.automatically_updated'
|
2022-05-30 00:19:16 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct(string $company_key, int $company_gateway_id)
|
|
|
|
{
|
|
|
|
$this->company_key = $company_key;
|
|
|
|
$this->company_gateway_id = $company_gateway_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
MultiDB::findAndSetDbByCompanyKey($this->company_key);
|
|
|
|
|
|
|
|
$company = Company::where('company_key', $this->company_key)->first();
|
|
|
|
|
|
|
|
$company_gateway = CompanyGateway::find($this->company_gateway_id);
|
|
|
|
|
2022-05-30 12:05:34 +02:00
|
|
|
$stripe = $company_gateway->driver()->init();
|
2022-05-30 00:19:16 +02:00
|
|
|
|
|
|
|
$endpoints = \Stripe\WebhookEndpoint::all([], $stripe->stripe_connect_auth);
|
|
|
|
|
|
|
|
$webhook_url = $company_gateway->webhookUrl();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach ($endpoints['data'] as $endpoint) {
|
|
|
|
if ($endpoint->url === $webhook_url) {
|
2022-05-30 12:05:34 +02:00
|
|
|
\Stripe\WebhookEndpoint::update($endpoint->id, ['enabled_events' => $this->events], $stripe->stripe_connect_auth);
|
|
|
|
|
|
|
|
$this->url_found = true;
|
2022-05-30 00:19:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new webhook */
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $this->url_found) {
|
2022-05-30 00:19:16 +02:00
|
|
|
\Stripe\WebhookEndpoint::create([
|
|
|
|
'url' => $webhook_url,
|
|
|
|
'enabled_events' => $this->events,
|
2022-05-30 12:05:34 +02:00
|
|
|
], $stripe->stripe_connect_auth);
|
2022-05-30 00:19:16 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|