Webhook.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\PaymentGateways\Webhooks; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\Exception; |
| 6 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 7 | |
| 8 | /** |
| 9 | * @since 4.5.0 |
| 10 | */ |
| 11 | class Webhook |
| 12 | { |
| 13 | /** |
| 14 | * @since 4.5.0 |
| 15 | * |
| 16 | * @var string $webhookNotificationsListener |
| 17 | */ |
| 18 | private $webhookNotificationsListener = 'webhookNotificationsListener'; |
| 19 | |
| 20 | /** |
| 21 | * @since 4.5.0 |
| 22 | * |
| 23 | * @var PaymentGateway $paymentGateway |
| 24 | */ |
| 25 | private $paymentGateway; |
| 26 | |
| 27 | /** |
| 28 | * @since 4.5.0 |
| 29 | * |
| 30 | * @var WebhookEvents $webhookEvents |
| 31 | */ |
| 32 | public $events; |
| 33 | |
| 34 | public function __construct(PaymentGateway &$paymentGateway) |
| 35 | { |
| 36 | if ($paymentGateway->canListeningWebhookNotifications()) { |
| 37 | $paymentGateway->routeMethods[] = $this->webhookNotificationsListener; |
| 38 | } |
| 39 | |
| 40 | $this->paymentGateway = &$paymentGateway; |
| 41 | $this->events = new WebhookEvents($paymentGateway::id()); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @since 4.5.0 |
| 46 | * |
| 47 | * @throws Exception |
| 48 | */ |
| 49 | public function getNotificationUrl(array $args = []): string |
| 50 | { |
| 51 | if ( ! $this->paymentGateway->canListeningWebhookNotifications()) { |
| 52 | throw new Exception('Gateway does not support listening to webhook notifications.'); |
| 53 | } |
| 54 | |
| 55 | return $this->paymentGateway->generateGatewayRouteUrl($this->webhookNotificationsListener, $args); |
| 56 | } |
| 57 | } |
| 58 |