-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from dystcz/feature/stripe-webhooks
Handle webhooks on queue
- Loading branch information
Showing
19 changed files
with
1,115 additions
and
548 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
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' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandleOtherEvent::class, | ||
|
||
/* | ||
* 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' => [ | ||
'payment_intent_created' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentCreated::class, | ||
'payment_intent_succeeded' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentSucceeded::class, | ||
'payment_intent_payment_failed' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentFailed::class, | ||
'payment_intent_cancelled' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentCancelled::class, | ||
// 'payment_intent_processing' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentProcessing::class, | ||
// 'payment_intent_requires_action' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentRequiresAction::class, | ||
// 'source_chargeable' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandleChargeableSource::class, | ||
// 'charge_failed' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandleChargeFailed::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), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('webhook_calls', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
|
||
$table->string('name'); | ||
$table->string('url'); | ||
$table->json('headers')->nullable(); | ||
$table->json('payload')->nullable(); | ||
$table->text('exception')->nullable(); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Dystcz\LunarApiStripeAdapter\Jobs\Webhooks; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Spatie\WebhookClient\Models\WebhookCall; | ||
|
||
class HandleChargeFailed implements ShouldQueue | ||
{ | ||
use InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public WebhookCall $webhookCall; | ||
|
||
public function __construct(WebhookCall $webhookCall) | ||
{ | ||
$this->webhookCall = $webhookCall; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
// do your work here | ||
|
||
// you can access the payload of the webhook call with `$this->webhookCall->payload` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Dystcz\LunarApiStripeAdapter\Jobs\Webhooks; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Spatie\WebhookClient\Models\WebhookCall; | ||
|
||
class HandleChargeableSource implements ShouldQueue | ||
{ | ||
use InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public WebhookCall $webhookCall; | ||
|
||
public function __construct(WebhookCall $webhookCall) | ||
{ | ||
$this->webhookCall = $webhookCall; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
// do your work here | ||
|
||
// you can access the payload of the webhook call with `$this->webhookCall->payload` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Dystcz\LunarApiStripeAdapter\Jobs\Webhooks; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Spatie\WebhookClient\Models\WebhookCall; | ||
use Stripe\Event; | ||
use Throwable; | ||
|
||
class HandleOtherEvent implements ShouldQueue | ||
{ | ||
use InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public WebhookCall $webhookCall; | ||
|
||
public function __construct(WebhookCall $webhookCall) | ||
{ | ||
$this->webhookCall = $webhookCall; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
try { | ||
$event = Event::constructFrom($this->webhookCall->payload); | ||
} catch (Throwable $e) { | ||
$this->fail($e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Dystcz\LunarApiStripeAdapter\Jobs\Webhooks; | ||
|
||
use Dystcz\LunarApi\Domain\Orders\Actions\FindOrderByIntent; | ||
use Dystcz\LunarApi\Domain\Orders\Events\OrderPaymentCanceled; | ||
use Dystcz\LunarApi\Domain\Payments\Data\PaymentIntent; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\App; | ||
use Lunar\Stripe\Facades\StripeFacade; | ||
use Spatie\WebhookClient\Models\WebhookCall; | ||
use Stripe\Event; | ||
use Throwable; | ||
|
||
class HandlePaymentIntentCancelled implements ShouldQueue | ||
{ | ||
use InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public WebhookCall $webhookCall; | ||
|
||
public function __construct(WebhookCall $webhookCall) | ||
{ | ||
$this->webhookCall = $webhookCall; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
try { | ||
$event = Event::constructFrom($this->webhookCall->payload); | ||
$paymentIntent = new PaymentIntent(intent: $event->data->object); | ||
} catch (Throwable $e) { | ||
$this->fail($e); | ||
} | ||
|
||
try { | ||
$order = App::make(FindOrderByIntent::class)($paymentIntent); | ||
} catch (Throwable $e) { | ||
$this->fail($e); | ||
} | ||
|
||
$paymentAdapter = StripeFacade::getFacadeRoot(); | ||
|
||
OrderPaymentCanceled::dispatch($order, $paymentAdapter, $paymentIntent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Dystcz\LunarApiStripeAdapter\Jobs\Webhooks; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Spatie\WebhookClient\Models\WebhookCall; | ||
use Stripe\Event; | ||
use Throwable; | ||
|
||
class HandlePaymentIntentCreated implements ShouldQueue | ||
{ | ||
use InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public WebhookCall $webhookCall; | ||
|
||
public function __construct(WebhookCall $webhookCall) | ||
{ | ||
$this->webhookCall = $webhookCall; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
try { | ||
$event = Event::constructFrom($this->webhookCall->payload); | ||
} catch (Throwable $e) { | ||
$this->fail($e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Dystcz\LunarApiStripeAdapter\Jobs\Webhooks; | ||
|
||
use Dystcz\LunarApi\Domain\Orders\Actions\FindOrderByIntent; | ||
use Dystcz\LunarApi\Domain\Orders\Events\OrderPaymentFailed; | ||
use Dystcz\LunarApi\Domain\Payments\Data\PaymentIntent; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\App; | ||
use Lunar\Stripe\Facades\StripeFacade; | ||
use Spatie\WebhookClient\Models\WebhookCall; | ||
use Stripe\Event; | ||
use Throwable; | ||
|
||
class HandlePaymentIntentFailed implements ShouldQueue | ||
{ | ||
use InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public WebhookCall $webhookCall; | ||
|
||
public function __construct(WebhookCall $webhookCall) | ||
{ | ||
$this->webhookCall = $webhookCall; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
try { | ||
$event = Event::constructFrom($this->webhookCall->payload); | ||
$paymentIntent = new PaymentIntent(intent: $event->data->object); | ||
} catch (Throwable $e) { | ||
$this->fail($e); | ||
} | ||
|
||
try { | ||
$order = App::make(FindOrderByIntent::class)($paymentIntent); | ||
} catch (Throwable $e) { | ||
$this->fail($e); | ||
} | ||
|
||
$paymentAdapter = StripeFacade::getFacadeRoot(); | ||
|
||
OrderPaymentFailed::dispatch($order, $paymentAdapter, $paymentIntent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Dystcz\LunarApiStripeAdapter\Jobs\Webhooks; | ||
|
||
use Dystcz\LunarApi\Domain\Payments\Data\PaymentIntent; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Spatie\WebhookClient\Models\WebhookCall; | ||
use Stripe\Event; | ||
use Throwable; | ||
|
||
class HandlePaymentIntentProcessing implements ShouldQueue | ||
{ | ||
use InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public WebhookCall $webhookCall; | ||
|
||
public function __construct(WebhookCall $webhookCall) | ||
{ | ||
$this->webhookCall = $webhookCall; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
try { | ||
$event = Event::constructFrom($this->webhookCall->payload); | ||
$paymentIntent = new PaymentIntent(intent: $event->data->object); | ||
} catch (Throwable $e) { | ||
$this->fail($e); | ||
} | ||
} | ||
} |
Oops, something went wrong.