|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sebdesign\VivaPayments\Events; |
|
4
|
|
|
|
|
5
|
|
|
use Sebdesign\VivaPayments\Enums\WebhookEventType; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @template TEventData of object |
|
9
|
|
|
*/ |
|
10
|
|
|
class WebhookEvent |
|
11
|
|
|
{ |
|
12
|
1 |
|
public function __construct( |
|
13
|
|
|
public readonly string $Url, |
|
14
|
|
|
/** @var TEventData */ |
|
15
|
|
|
public readonly object $EventData, |
|
16
|
|
|
public readonly string $Created, |
|
17
|
|
|
public readonly string $CorrelationId, |
|
18
|
|
|
public readonly WebhookEventType $EventTypeId, |
|
19
|
|
|
public readonly ?string $Delay, |
|
20
|
|
|
public readonly string $MessageId, |
|
21
|
|
|
public readonly string $RecipientId, |
|
22
|
|
|
public readonly int $MessageTypeId, |
|
23
|
|
|
) { |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @phpstan-param WebhookEventArray $attributes |
|
28
|
|
|
* @phpstan-return self<TEventData> |
|
29
|
|
|
*/ |
|
30
|
3 |
|
public static function create(array $attributes): self |
|
31
|
|
|
{ |
|
32
|
3 |
|
$eventType = WebhookEventType::from($attributes['EventTypeId']); |
|
33
|
|
|
|
|
34
|
3 |
|
$eventData = match ($eventType) { |
|
35
|
|
|
/** @phpstan-ignore-next-line */ |
|
36
|
3 |
|
WebhookEventType::TransactionPaymentCreated => TransactionPaymentCreated::create($attributes['EventData']), |
|
37
|
|
|
/** @phpstan-ignore-next-line */ |
|
38
|
3 |
|
WebhookEventType::TransactionFailed => TransactionFailed::create($attributes['EventData']), |
|
39
|
3 |
|
default => (object) $attributes['EventData'], |
|
40
|
|
|
}; |
|
41
|
|
|
|
|
42
|
|
|
/** @phpstan-ignore-next-line */ |
|
43
|
3 |
|
return new self(...[ |
|
44
|
|
|
...$attributes, |
|
45
|
|
|
'EventTypeId' => $eventType, |
|
46
|
|
|
'EventData' => $eventData, |
|
47
|
|
|
]); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|