mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 14:57:26 +08:00
Delete all webhooks before adding
This commit is contained in:
parent
3d3fe37185
commit
4c59337247
4 changed files with 16 additions and 20 deletions
|
@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Onboarding;
|
|||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use WooCommerce\PayPalCommerce\Webhooks\WebhookRegistrar;
|
||||
|
||||
/**
|
||||
* Exposes and handles REST routes related to onboarding.
|
||||
|
@ -249,7 +250,7 @@ class OnboardingRESTController {
|
|||
}
|
||||
|
||||
$webhook_registrar = $this->container->get( 'webhook.registrar' );
|
||||
$webhook_registrar->unregister();
|
||||
assert( $webhook_registrar instanceof WebhookRegistrar );
|
||||
$webhook_registrar->register();
|
||||
|
||||
return array();
|
||||
|
|
|
@ -62,8 +62,6 @@ class ResubscribeEndpoint {
|
|||
// Validate nonce.
|
||||
$this->request_data->read_request( $this->nonce() );
|
||||
|
||||
$this->registrar->unregister();
|
||||
|
||||
if ( ! $this->registrar->register() ) {
|
||||
wp_send_json_error( 'Webhook subscription failed.', 500 );
|
||||
return false;
|
||||
|
|
|
@ -152,7 +152,6 @@ class WebhookModule implements ModuleInterface {
|
|||
add_action(
|
||||
'init',
|
||||
function () use ( $registrar ) {
|
||||
$registrar->unregister();
|
||||
$registrar->register();
|
||||
}
|
||||
);
|
||||
|
|
|
@ -88,6 +88,8 @@ class WebhookRegistrar {
|
|||
* @return bool
|
||||
*/
|
||||
public function register(): bool {
|
||||
$this->unregister();
|
||||
|
||||
$webhook = $this->webhook_factory->for_url_and_events(
|
||||
$this->rest_endpoint->url(),
|
||||
$this->rest_endpoint->handled_event_types()
|
||||
|
@ -113,27 +115,23 @@ class WebhookRegistrar {
|
|||
|
||||
/**
|
||||
* Unregister webhooks with PayPal.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function unregister(): bool {
|
||||
$data = (array) get_option( self::KEY, array() );
|
||||
if ( ! $data ) {
|
||||
return false;
|
||||
}
|
||||
public function unregister(): void {
|
||||
try {
|
||||
$webhook = $this->webhook_factory->from_array( $data );
|
||||
$success = $this->endpoint->delete( $webhook );
|
||||
$webhooks = $this->endpoint->list();
|
||||
foreach ( $webhooks as $webhook ) {
|
||||
try {
|
||||
$this->endpoint->delete( $webhook );
|
||||
} catch ( RuntimeException $deletion_error ) {
|
||||
$this->logger->error( "Failed to delete webhook {$webhook->id()}: {$deletion_error->getMessage()}" );
|
||||
}
|
||||
}
|
||||
} catch ( RuntimeException $error ) {
|
||||
$this->logger->error( 'Failed to delete webhooks: ' . $error->getMessage() );
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $success ) {
|
||||
delete_option( self::KEY );
|
||||
$this->last_webhook_event_storage->clear();
|
||||
$this->logger->info( 'Webhooks deleted.' );
|
||||
}
|
||||
return $success;
|
||||
delete_option( self::KEY );
|
||||
$this->last_webhook_event_storage->clear();
|
||||
$this->logger->info( 'Webhooks deleted.' );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue