Files
newznab-tmux/app/Jobs/ProcessBtcPayWebhook.php
T
2023-12-17 17:12:58 +01:00

40 lines
1.4 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\User;
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\Log;
use Spatie\WebhookClient\Jobs\ProcessWebhookJob;
class ProcessBtcPayWebhook extends ProcessWebhookJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*/
public function handle(): void
{
$payload = $this->webhookCall->payload;
// We have received a payment for an invoice and user should be upgraded to a paid plan based on order
if ($payload['type'] === 'InvoiceReceivedPayment') {
preg_match('/(?P<role>\w+(\+\+)?)[ ](?P<addYears>\d+)/i', $payload['metadata']['itemDesc'], $matches);
$user = User::query()->where('email', '=', $payload['metadata']['buyerEmail'])->first();
if ($user) {
User::updateUserRole($user->id, $matches['role']);
User::updateUserRoleChangeDate($user->id, null, $matches['addYears']);
Log::info('User upgraded to '.$matches['role'].' for BTCPay webhook: '.$payload['metadata']['buyerEmail']);
} else {
Log::error('User not found for BTCPay webhook: '.$payload['metadata']['buyerEmail']);
}
}
}
}