From 0d7d7b805da59d6e2e9a9a1ccec32b88d93eac77 Mon Sep 17 00:00:00 2001 From: Adam Borowski Date: Mon, 13 Jun 2022 15:28:47 +0200 Subject: [PATCH 1/2] feat(reader-revenue): prevent creating duplicate stripe webhooks --- .../readerRevenue/views/stripe-setup/index.js | 14 ++++++++++++-- .../reader-revenue/class-stripe-connection.php | 9 ++++++++- includes/wizards/class-reader-revenue-wizard.php | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/assets/wizards/readerRevenue/views/stripe-setup/index.js b/assets/wizards/readerRevenue/views/stripe-setup/index.js index 7282984ac9..69fc5045a5 100644 --- a/assets/wizards/readerRevenue/views/stripe-setup/index.js +++ b/assets/wizards/readerRevenue/views/stripe-setup/index.js @@ -115,6 +115,11 @@ const StripeSetup = () => { country_state_fields = [], } = Wizard.useWizardData( 'reader-revenue' ); + const hasWebhook = + data.webhook_url && + Array.isArray( data.webhooks ) && + data.webhooks.filter( webhook => webhook.url === data.webhook_url ).length > 0; + const [ isLoading, setIsLoading ] = useState( false ); const createWebhooks = () => { setIsLoading( true ); @@ -257,8 +262,13 @@ const StripeSetup = () => { ) } - diff --git a/includes/reader-revenue/class-stripe-connection.php b/includes/reader-revenue/class-stripe-connection.php index 04c114a55a..475260034c 100644 --- a/includes/reader-revenue/class-stripe-connection.php +++ b/includes/reader-revenue/class-stripe-connection.php @@ -420,6 +420,13 @@ public static function receive_webhook( $request ) { } } + /** + * Get URL of the webhook for this site. + */ + public static function get_webhook_url() { + return get_rest_url( null, NEWSPACK_API_NAMESPACE . '/stripe/webhook' ); + } + /** * Create Stripe webhooks. */ @@ -428,7 +435,7 @@ public static function create_webhooks() { try { $webhook = $stripe->webhookEndpoints->create( [ - 'url' => get_rest_url( null, NEWSPACK_API_NAMESPACE . '/stripe/webhook' ), + 'url' => self::get_webhook_url(), 'enabled_events' => [ 'charge.failed', 'charge.succeeded', diff --git a/includes/wizards/class-reader-revenue-wizard.php b/includes/wizards/class-reader-revenue-wizard.php index 927acc7e59..3f0bd44587 100644 --- a/includes/wizards/class-reader-revenue-wizard.php +++ b/includes/wizards/class-reader-revenue-wizard.php @@ -473,6 +473,7 @@ public function fetch_all_data() { $args['platform_data'] = wp_parse_args( $nrh_config, $args['platform_data'] ); } elseif ( Donations::is_platform_stripe() ) { $args['stripe_data']['webhooks'] = Stripe_Connection::list_webhooks(); + $args['stripe_data']['webhook_url'] = Stripe_Connection::get_webhook_url(); $args['stripe_data']['connection_error'] = Stripe_Connection::get_connection_error(); } return $args; From dc890be0083e277cfedbe60fcbc2c4879f3cb5e4 Mon Sep 17 00:00:00 2001 From: Adam Borowski Date: Tue, 21 Jun 2022 17:43:32 +0200 Subject: [PATCH 2/2] feat: add notice about webhooks having been created --- assets/wizards/readerRevenue/views/stripe-setup/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/assets/wizards/readerRevenue/views/stripe-setup/index.js b/assets/wizards/readerRevenue/views/stripe-setup/index.js index 69fc5045a5..31a5d9556e 100644 --- a/assets/wizards/readerRevenue/views/stripe-setup/index.js +++ b/assets/wizards/readerRevenue/views/stripe-setup/index.js @@ -262,6 +262,9 @@ const StripeSetup = () => { ) } + { hasWebhook && ( +

{ __( 'Webhooks have already been created.', 'newspack' ) }

+ ) }