mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 08:37:32 -05:00 
			
		
		
		
	Add Stripe Webhooks automagically
This commit is contained in:
		
							parent
							
								
									16f52e0f45
								
							
						
					
					
						commit
						61eb3380dd
					
				
							
								
								
									
										97
									
								
								app/PaymentDrivers/Stripe/Jobs/StripeWebhook.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								app/PaymentDrivers/Stripe/Jobs/StripeWebhook.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,97 @@
 | 
			
		||||
<?php
 | 
			
		||||
/**
 | 
			
		||||
 * Invoice Ninja (https://invoiceninja.com).
 | 
			
		||||
 *
 | 
			
		||||
 * @link https://github.com/invoiceninja/invoiceninja source repository
 | 
			
		||||
 *
 | 
			
		||||
 * @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
 | 
			
		||||
 *
 | 
			
		||||
 * @license https://www.elastic.co/licensing/elastic-license
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace App\PaymentDrivers\Stripe\Jobs;
 | 
			
		||||
 | 
			
		||||
use App\Jobs\Mail\PaymentFailedMailer;
 | 
			
		||||
use App\Jobs\Util\SystemLogger;
 | 
			
		||||
use App\Libraries\MultiDB;
 | 
			
		||||
use App\Models\Company;
 | 
			
		||||
use App\Models\CompanyGateway;
 | 
			
		||||
use App\Models\GatewayType;
 | 
			
		||||
use App\Models\Invoice;
 | 
			
		||||
use App\Models\Payment;
 | 
			
		||||
use App\Models\PaymentHash;
 | 
			
		||||
use App\Models\PaymentType;
 | 
			
		||||
use App\Models\SystemLog;
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
    public $tries = 1; 
 | 
			
		||||
 | 
			
		||||
    public $deleteWhenMissingModels = true;
 | 
			
		||||
 | 
			
		||||
    public int $company_gateway_id;
 | 
			
		||||
 | 
			
		||||
    public string $company_key;
 | 
			
		||||
 | 
			
		||||
    private array $events = [
 | 
			
		||||
        'source.chargeable',
 | 
			
		||||
        'charge.succeeded',
 | 
			
		||||
        'charge.failed',
 | 
			
		||||
        'payment_intent.succeeded',
 | 
			
		||||
        'payment_intent.payment_failed'
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
 | 
			
		||||
        $driver = $company_gateway->driver();
 | 
			
		||||
 | 
			
		||||
        $stripe = $driver->init();
 | 
			
		||||
 | 
			
		||||
        $endpoints = \Stripe\WebhookEndpoint::all([], $stripe->stripe_connect_auth);
 | 
			
		||||
 | 
			
		||||
        $webhook_url = $company_gateway->webhookUrl();
 | 
			
		||||
 | 
			
		||||
        foreach($endpoints['data'] as $endpoint)
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            if($endpoint->url === $webhook_url)
 | 
			
		||||
            {
 | 
			
		||||
                \Stripe\WebhookEndpoint::update($endpoint->id, $this->events, $stripe->stripe_connect_auth);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* Add new webhook */
 | 
			
		||||
        if(count($endpoints['data'] == 0)
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            \Stripe\WebhookEndpoint::create([
 | 
			
		||||
                'url' => $webhook_url,
 | 
			
		||||
                'enabled_events' => $this->events,
 | 
			
		||||
            ], $stripe->stripe_connect_auth)
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user