Created
April 1, 2024 15:03
-
-
Save davidbirkin/906c93ea7b22381f24290e6b1c6b4e54 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace App\Jobs\Webhooks; | |
use App\Models\License; | |
use Illuminate\Bus\Queueable; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
use Illuminate\Foundation\Bus\Dispatchable; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Queue\SerializesModels; | |
use Illuminate\Support\Facades\Http; | |
use Illuminate\Support\Str; | |
class SendLicenseStatusWebhook implements ShouldQueue | |
{ | |
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | |
public License $license; | |
public string $action; | |
/** | |
* Create a new job instance. | |
*/ | |
public function __construct( License $license, string $action ) | |
{ | |
$this->license = $license; | |
$this->action = $action; | |
} | |
/** | |
* Execute the job. | |
*/ | |
public function handle(): void | |
{ | |
$webhooks = $this->license->webhooks; | |
$webhooks = $webhooks->filter(function ( $webhook ) { | |
return Str::contains($webhook->actions, $this->action); | |
}); | |
foreach ($webhooks as $webhook) { | |
$payload = json_encode([ | |
'license' => $this->license->key, | |
'expires_at' => $this->license->expires_at, | |
'customer' => $this->license->customer, | |
], JSON_THROW_ON_ERROR); | |
$hash = hash_hmac('sha256', $payload, $webhook->secret); | |
Http::withHeaders([ | |
'Content-Type' => 'application/json', | |
'signature' => $hash | |
])->post($webhook->url, $payload); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment