Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reader-revenue): prevent creating duplicate stripe webhooks #1710

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions assets/wizards/readerRevenue/views/stripe-setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -257,8 +262,13 @@ const StripeSetup = () => {
<Notice isInfo noticeText={ __( 'No webhooks defined.', 'newspack' ) } />
) }
<Card noBorder buttonsCard>
<Button isLink disabled={ isLoading } onClick={ createWebhooks } isSecondary>
{ __( 'Create Webhooks', 'newspack' ) }
<Button
isLink
disabled={ isLoading || hasWebhook }
onClick={ createWebhooks }
isSecondary
>
{ __( 'Create Webhook', 'newspack' ) }
</Button>
</Card>
</>
Expand Down
9 changes: 8 additions & 1 deletion includes/reader-revenue/class-stripe-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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',
Expand Down
1 change: 1 addition & 0 deletions includes/wizards/class-reader-revenue-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down