PHP code example of spatie / laravel-stripe-webhooks
1. Go to this page and download the library: Download spatie/laravel-stripe-webhooks library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
spatie / laravel-stripe-webhooks example snippets
return [
/*
* Stripe will sign each webhook using a secret. You can find the used secret at the
* webhook configuration settings: https://dashboard.stripe.com/account/webhooks.
*/
'signing_secret' => env('STRIPE_WEBHOOK_SECRET'),
/*
* You can define a default job that should be run for all other Stripe event type
* without a job defined in next configuration.
* You may leave it empty to store the job in database but without processing it.
*/
'default_job' => '',
/*
* You can define the job that should be run when a certain webhook hits your application
* here. The key is the name of the Stripe event type with the `.` replaced by a `_`.
*
* You can find a list of Stripe webhook types here:
* https://stripe.com/docs/api#event_types.
*/
'jobs' => [
// 'source_chargeable' => \App\Jobs\StripeWebhooks\HandleChargeableSource::class,
// 'charge_failed' => \App\Jobs\StripeWebhooks\HandleFailedCharge::class,
],
/*
* The classname of the model to be used. The class should equal or extend
* Spatie\WebhookClient\Models\WebhookCall.
*/
'model' => \Spatie\WebhookClient\Models\WebhookCall::class,
/**
* This class determines if the webhook call should be stored and processed.
*/
'profile' => \Spatie\StripeWebhooks\StripeWebhookProfile::class,
/*
* Specify a connection and or a queue to process the webhooks
*/
'connection' => env('STRIPE_WEBHOOK_CONNECTION'),
'queue' => env('STRIPE_WEBHOOK_QUEUE'),
/*
* When disabled, the package will not verify if the signature is valid.
* This can be handy in local environments.
*/
'verify_signature' => env('STRIPE_SIGNATURE_VERIFY', true),
];
namespace App\Jobs\StripeWebhooks;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;
class HandleChargeableSource implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
/** @var \Spatie\WebhookClient\Models\WebhookCall */
public $webhookCall;
public function __construct(WebhookCall $webhookCall)
{
$this->webhookCall = $webhookCall;
}
public function handle()
{
// do your work here
// you can access the payload of the webhook call with `$this->webhookCall->payload`
}
}
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'stripe-webhooks::source.chargeable' => [
App\Listeners\ChargeSource::class,
],
];
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;
class ChargeSource implements ShouldQueue
{
public function handle(WebhookCall $webhookCall)
{
// do your work here
// you can access the payload of the webhook call with `$webhookCall->payload`
}
}
use Spatie\WebhookClient\Models\WebhookCall;
use Spatie\StripeWebhooks\ProcessStripeWebhookJob;
dispatch(new ProcessStripeWebhookJob(WebhookCall::find($id)));
use Spatie\StripeWebhooks\ProcessStripeWebhookJob;
class MyCustomStripeWebhookJob extends ProcessStripeWebhookJob
{
public function handle()
{
// do some custom stuff beforehand
parent::handle();
// do some custom stuff afterwards
}
}
use Illuminate\Http\Request;
use Spatie\WebhookClient\Models\WebhookCall;
use Spatie\WebhookClient\WebhookProfile\WebhookProfile;
class StripeWebhookProfile implements WebhookProfile
{
public function shouldProcess(Request $request): bool
{
return ! WebhookCall::where('payload->id', $request->get('id'))->exists();
}
}
// secret for when Stripe posts to webhook-url/account
'signing_secret_account' => 'whsec_abc',
// secret for when Stripe posts to webhook-url/connect
'signing_secret_connect' => 'whsec_123',
use Stripe\Event;
// ...
public function handle(WebhookCall $webhookCall)
{
/** @var \Stripe\StripeObject|null */
$stripeObject = Event::constructFrom($webhookCall->payload)->data?->object;
}