<?php
namespace App\Jobs;
use App\Application\Services\Blockchain\PaymentFinalizer;use App\Models\OperationsAlert;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\Cache;use Throwable;
final class FinalizePaymentJob implements ShouldQueue {use Dispatchable,InteractsWithQueue,Queueable,SerializesModels;public int $tries=6;public function __construct(public int $invoiceId){$this->onQueue('confirmations');}public function backoff():array{return \App\Support\RetryBackoff::exponential();}public function handle(PaymentFinalizer $f):void{$lock=Cache::lock("invoice-finalize:{$this->invoiceId}",180);if(!$lock->get())return;try{$f->finalize($this->invoiceId);}finally{$lock->release();}}public function failed(?Throwable $e):void{OperationsAlert::create(['severity'=>'critical','code'=>'PAYMENT_FINALIZATION_FAILED','subject_type'=>'invoice','subject_id'=>(string)$this->invoiceId,'message'=>$e?->getMessage()??'Finalization job exhausted.','context'=>[]]);}}
