mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 13:44:42 +08:00
Cache webhook verification results
Looks like `permission_callback` can be called multiple times in some cases/newer WP, resulting in difficult to read logs and other issues, such as slower request handling or possible lack of idempotence (first returned true, but then request failed and returned false). So now caching it in a variable.
This commit is contained in:
parent
47ca09301d
commit
a028eafeef
1 changed files with 20 additions and 0 deletions
|
@ -83,6 +83,14 @@ class IncomingWebhookEndpoint {
|
|||
*/
|
||||
private $last_webhook_event_storage;
|
||||
|
||||
/**
|
||||
* Cached webhook verification results
|
||||
* to avoid repeating requests when permission_callback is called multiple times.
|
||||
*
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
private $verification_results = array();
|
||||
|
||||
/**
|
||||
* IncomingWebhookEndpoint constructor.
|
||||
*
|
||||
|
@ -160,7 +168,17 @@ class IncomingWebhookEndpoint {
|
|||
|
||||
try {
|
||||
$event = $this->event_from_request( $request );
|
||||
} catch ( RuntimeException $exception ) {
|
||||
$this->logger->error( 'Webhook parsing failed: ' . $exception->getMessage() );
|
||||
return false;
|
||||
}
|
||||
|
||||
$cache_key = $event->id();
|
||||
if ( isset( $this->verification_results[ $cache_key ] ) ) {
|
||||
return $this->verification_results[ $cache_key ];
|
||||
}
|
||||
|
||||
try {
|
||||
if ( $this->simulation->is_simulation_event( $event ) ) {
|
||||
return true;
|
||||
}
|
||||
|
@ -169,9 +187,11 @@ class IncomingWebhookEndpoint {
|
|||
if ( ! $result ) {
|
||||
$this->logger->error( 'Webhook verification failed.' );
|
||||
}
|
||||
$this->verification_results[ $cache_key ] = $result;
|
||||
return $result;
|
||||
} catch ( RuntimeException $exception ) {
|
||||
$this->logger->error( 'Webhook verification failed: ' . $exception->getMessage() );
|
||||
$this->verification_results[ $cache_key ] = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue