*/ protected $registeredHandlers = []; /** @var StripeClient */ protected $client; protected $hasHandledEvents = false; /** @var callable(V2\Core\EventNotification, StripeClient, UnhandledNotificationDetails): void */ protected $fallbackCallback; /** @var array everything we need to duplicate a client */ protected $clientConfig; /** @var null|callable(V2\Core\EventNotification, StripeClient): bool */ protected $preHandleCallback = null; /** * @param StripeClient $client The Stripe client to use for API interactions * @param callable(V2\Core\EventNotification, StripeClient, UnhandledNotificationDetails): void $fallbackCallback A callback that's invoked for unhandled events. It receives the notification as parsed, so it's a specific subclass for event types this SDK knows about and an Events\UnknownEventNotification otherwise; check UnhandledNotificationDetails::$isKnownEventType or use instanceof to narrow */ public function __construct($client, $fallbackCallback) { $this->client = $client; $this->fallbackCallback = $fallbackCallback; // Extract configuration from the client for creating new instances $this->clientConfig = [ 'api_key' => $client->getApiKey(), 'client_id' => $client->getClientId(), 'stripe_account' => $client->getStripeAccount(), 'stripe_version' => $client->getStripeVersion(), 'api_base' => $client->getApiBase(), 'connect_base' => $client->getConnectBase(), 'files_base' => $client->getFilesBase(), 'meter_events_base' => $client->getMeterEventsBase(), 'max_network_retries' => $client->getMaxNetworkRetries(), 'app_info' => $client->getAppInfo(), ]; } /** * Returns a sorted list of registered event types. * * @return string[] List of registered event type strings */ public function getRegisteredHandlers() { $eventTypes = array_keys($this->registeredHandlers); \sort($eventTypes); return $eventTypes; } /** * Dispatches a parsed event notification to the appropriate handler. * * @param V2\Core\EventNotification $notif The parsed event notification * * @return void */ protected function dispatch($notif) { $eventType = $notif->type; // Create a new client instance with the event's context instead of modifying the shared client $eventClient = $this->createClientWithContext($notif->context); if (null !== $this->preHandleCallback && !\call_user_func($this->preHandleCallback, $notif, $eventClient)) { return; } if (isset($this->registeredHandlers[$eventType])) { \call_user_func($this->registeredHandlers[$eventType], $notif, $eventClient); } else { \call_user_func($this->fallbackCallback, $notif, $eventClient, new UnhandledNotificationDetails(!$notif instanceof Events\UnknownEventNotification)); } } /** * Creates a new StripeClient instance with the specified stripe_context. * * @param null|string $context The stripe_context to use for the new client * * @return StripeClient A new StripeClient instance with the specified context */ protected function createClientWithContext($context) { $config = $this->clientConfig; $config['stripe_context'] = $context; return new StripeClient($config); } /** * Callbacks are expected to be registered on startup, so registering anything * after handling an event indicates a bug. * * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ private function assertHasntHandledYet() { if ($this->hasHandledEvents) { throw new Exception\BadMethodCallException('Cannot register new callbacks after an event has been handled. This is indicative of a bug.'); } } /** * Registers a handler for a specific event type. * * @param string $eventType The event type to register the handler for * @param callable $handler The handler function to call when the event is received * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ protected function register($eventType, $handler) { $this->assertHasntHandledYet(); if (isset($this->registeredHandlers[$eventType])) { throw new Exception\InvalidArgumentException("Callback for event type \"{$eventType}\" is already registered"); } $this->registeredHandlers[$eventType] = $handler; } /** * Registers a function that will be run before any event-specific callbacks. A useful place to * store event-agnostic logic, such as logging or checking for * [duplicate event deliveries](https://docs.stripe.com/webhooks#handle-duplicate-events). * * Returning `true` causes handling to continue as normal; returning `false` returns from * `.handle()` immediately, so neither the registered callback nor the fallback callback are called. * * @param callable(V2\Core\EventNotification, StripeClient): bool $handler Return false to stop handling before any callback runs * * @throws Exception\InvalidArgumentException if a pre-handle hook is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function preHandle($handler) { $this->assertHasntHandledYet(); if (null !== $this->preHandleCallback) { throw new Exception\InvalidArgumentException('A preHandle callback is already registered'); } $this->preHandleCallback = $handler; } // event-handler-methods: The beginning of the section generated from our OpenAPI spec /** * Registers a handler for the "v1.billing.meter.error_report_triggered" event. * * @param callable(Events\V1BillingMeterErrorReportTriggeredEventNotification, StripeClient): void $handler Handles v1.billing.meter.error_report_triggered events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV1BillingMeterErrorReportTriggered($handler) { $this->register('v1.billing.meter.error_report_triggered', $handler); } /** * Registers a handler for the "v1.billing.meter.no_meter_found" event. * * @param callable(Events\V1BillingMeterNoMeterFoundEventNotification, StripeClient): void $handler Handles v1.billing.meter.no_meter_found events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV1BillingMeterNoMeterFound($handler) { $this->register('v1.billing.meter.no_meter_found', $handler); } /** * Registers a handler for the "v2.commerce.product_catalog.imports.failed" event. * * @param callable(Events\V2CommerceProductCatalogImportsFailedEventNotification, StripeClient): void $handler Handles v2.commerce.product_catalog.imports.failed events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CommerceProductCatalogImportsFailed($handler) { $this->register('v2.commerce.product_catalog.imports.failed', $handler); } /** * Registers a handler for the "v2.commerce.product_catalog.imports.processing" event. * * @param callable(Events\V2CommerceProductCatalogImportsProcessingEventNotification, StripeClient): void $handler Handles v2.commerce.product_catalog.imports.processing events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CommerceProductCatalogImportsProcessing($handler) { $this->register('v2.commerce.product_catalog.imports.processing', $handler); } /** * Registers a handler for the "v2.commerce.product_catalog.imports.succeeded" event. * * @param callable(Events\V2CommerceProductCatalogImportsSucceededEventNotification, StripeClient): void $handler Handles v2.commerce.product_catalog.imports.succeeded events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CommerceProductCatalogImportsSucceeded($handler) { $this->register('v2.commerce.product_catalog.imports.succeeded', $handler); } /** * Registers a handler for the "v2.commerce.product_catalog.imports.succeeded_with_errors" event. * * @param callable(Events\V2CommerceProductCatalogImportsSucceededWithErrorsEventNotification, StripeClient): void $handler Handles v2.commerce.product_catalog.imports.succeeded_with_errors events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CommerceProductCatalogImportsSucceededWithErrors( $handler ) { $this->register( 'v2.commerce.product_catalog.imports.succeeded_with_errors', $handler ); } /** * Registers a handler for the "v2.core.account.closed" event. * * @param callable(Events\V2CoreAccountClosedEventNotification, StripeClient): void $handler Handles v2.core.account.closed events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountClosed($handler) { $this->register('v2.core.account.closed', $handler); } /** * Registers a handler for the "v2.core.account.created" event. * * @param callable(Events\V2CoreAccountCreatedEventNotification, StripeClient): void $handler Handles v2.core.account.created events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountCreated($handler) { $this->register('v2.core.account.created', $handler); } /** * Registers a handler for the "v2.core.account.updated" event. * * @param callable(Events\V2CoreAccountUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account.updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountUpdated($handler) { $this->register('v2.core.account.updated', $handler); } /** * Registers a handler for the "v2.core.account[configuration.customer].capability_status_updated" event. * * @param callable(Events\V2CoreAccountIncludingConfigurationCustomerCapabilityStatusUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account[configuration.customer].capability_status_updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountIncludingConfigurationCustomerCapabilityStatusUpdated( $handler ) { $this->register( 'v2.core.account[configuration.customer].capability_status_updated', $handler ); } /** * Registers a handler for the "v2.core.account[configuration.customer].updated" event. * * @param callable(Events\V2CoreAccountIncludingConfigurationCustomerUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account[configuration.customer].updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountIncludingConfigurationCustomerUpdated( $handler ) { $this->register( 'v2.core.account[configuration.customer].updated', $handler ); } /** * Registers a handler for the "v2.core.account[configuration.merchant].capability_status_updated" event. * * @param callable(Events\V2CoreAccountIncludingConfigurationMerchantCapabilityStatusUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account[configuration.merchant].capability_status_updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountIncludingConfigurationMerchantCapabilityStatusUpdated( $handler ) { $this->register( 'v2.core.account[configuration.merchant].capability_status_updated', $handler ); } /** * Registers a handler for the "v2.core.account[configuration.merchant].updated" event. * * @param callable(Events\V2CoreAccountIncludingConfigurationMerchantUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account[configuration.merchant].updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountIncludingConfigurationMerchantUpdated( $handler ) { $this->register( 'v2.core.account[configuration.merchant].updated', $handler ); } /** * Registers a handler for the "v2.core.account[configuration.recipient].capability_status_updated" event. * * @param callable(Events\V2CoreAccountIncludingConfigurationRecipientCapabilityStatusUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account[configuration.recipient].capability_status_updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountIncludingConfigurationRecipientCapabilityStatusUpdated( $handler ) { $this->register( 'v2.core.account[configuration.recipient].capability_status_updated', $handler ); } /** * Registers a handler for the "v2.core.account[configuration.recipient].updated" event. * * @param callable(Events\V2CoreAccountIncludingConfigurationRecipientUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account[configuration.recipient].updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountIncludingConfigurationRecipientUpdated( $handler ) { $this->register( 'v2.core.account[configuration.recipient].updated', $handler ); } /** * Registers a handler for the "v2.core.account[defaults].updated" event. * * @param callable(Events\V2CoreAccountIncludingDefaultsUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account[defaults].updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountIncludingDefaultsUpdated($handler) { $this->register('v2.core.account[defaults].updated', $handler); } /** * Registers a handler for the "v2.core.account[future_requirements].updated" event. * * @param callable(Events\V2CoreAccountIncludingFutureRequirementsUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account[future_requirements].updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountIncludingFutureRequirementsUpdated($handler) { $this->register('v2.core.account[future_requirements].updated', $handler); } /** * Registers a handler for the "v2.core.account[identity].updated" event. * * @param callable(Events\V2CoreAccountIncludingIdentityUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account[identity].updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountIncludingIdentityUpdated($handler) { $this->register('v2.core.account[identity].updated', $handler); } /** * Registers a handler for the "v2.core.account[requirements].updated" event. * * @param callable(Events\V2CoreAccountIncludingRequirementsUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account[requirements].updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountIncludingRequirementsUpdated($handler) { $this->register('v2.core.account[requirements].updated', $handler); } /** * Registers a handler for the "v2.core.account_link.returned" event. * * @param callable(Events\V2CoreAccountLinkReturnedEventNotification, StripeClient): void $handler Handles v2.core.account_link.returned events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountLinkReturned($handler) { $this->register('v2.core.account_link.returned', $handler); } /** * Registers a handler for the "v2.core.account_person.created" event. * * @param callable(Events\V2CoreAccountPersonCreatedEventNotification, StripeClient): void $handler Handles v2.core.account_person.created events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountPersonCreated($handler) { $this->register('v2.core.account_person.created', $handler); } /** * Registers a handler for the "v2.core.account_person.deleted" event. * * @param callable(Events\V2CoreAccountPersonDeletedEventNotification, StripeClient): void $handler Handles v2.core.account_person.deleted events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountPersonDeleted($handler) { $this->register('v2.core.account_person.deleted', $handler); } /** * Registers a handler for the "v2.core.account_person.updated" event. * * @param callable(Events\V2CoreAccountPersonUpdatedEventNotification, StripeClient): void $handler Handles v2.core.account_person.updated events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreAccountPersonUpdated($handler) { $this->register('v2.core.account_person.updated', $handler); } /** * Registers a handler for the "v2.core.event_destination.ping" event. * * @param callable(Events\V2CoreEventDestinationPingEventNotification, StripeClient): void $handler Handles v2.core.event_destination.ping events * * @throws Exception\InvalidArgumentException if this event type is already registered * @throws Exception\BadMethodCallException if the `.handle()` method has already been called on this handler. */ public function onV2CoreEventDestinationPing($handler) { $this->register('v2.core.event_destination.ping', $handler); } // event-handler-methods: The end of the section generated from our OpenAPI spec }