Skip to content

Commit

Permalink
Merge pull request #3103 from dparker1005/stripe-webhook-admin-notice
Browse files Browse the repository at this point in the history
Add admin notice when Stripe webhooks are not set up
  • Loading branch information
dparker1005 authored Aug 2, 2024
2 parents 4e63393 + 4930739 commit 0ffcd92
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 30 deletions.
115 changes: 85 additions & 30 deletions classes/gateways/class.pmprogateway_stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ public static function init() {
add_action( 'admin_notices', array( 'PMProGateway_stripe', 'stripe_connect_show_errors' ) );
add_action( 'admin_notices', array( 'PMProGateway_stripe', 'stripe_connect_deauthorize' ) );

// Show warning if webhooks are not set up.
add_action( 'admin_notices', array( 'PMProGateway_stripe', 'show_stripe_webhook_setup_notice' ) );

add_filter( 'pmpro_process_refund_stripe', array( 'PMProGateway_stripe', 'process_refund' ), 10, 2 );
}

Expand Down Expand Up @@ -358,50 +361,51 @@ public static function pmpro_payment_option_fields( $values, $gateway ) {
<h2><?php esc_html_e( 'Webhook', 'paid-memberships-pro' ); ?></h2>
</td>
</tr>
<tr class="pmpro_settings_divider gateway gateway_stripe_<?php echo esc_attr( $stripe->gateway_environment ); ?>" <?php if ( $gateway != "stripe" ) { ?>style="display: none;"<?php } ?>>
<tr class="gateway gateway_stripe_<?php echo esc_attr( $stripe->gateway_environment ); ?>" <?php if ( $gateway != "stripe" ) { ?>style="display: none;"<?php } ?>>
<th scope="row" valign="top">
<label><?php esc_html_e( 'Webhook URL', 'paid-memberships-pro' ); ?></label>
</th>
<td>
<p><code><?php echo esc_html( $stripe->get_site_webhook_url() ); ?></code></p>
</td>
</tr>
<tr class="pmpro_settings_divider gateway gateway_stripe_<?php echo esc_attr( $stripe->gateway_environment ); ?>" <?php if ( $gateway != "stripe" ) { ?>style="display: none;"<?php } ?>>
<tr class="gateway gateway_stripe_<?php echo esc_attr( $stripe->gateway_environment ); ?>" <?php if ( $gateway != "stripe" ) { ?>style="display: none;"<?php } ?>>
<th scope="row" valign="top">
<label><?php esc_html_e( 'Webhook Status', 'paid-memberships-pro' ); ?></label>
</th>
<td>
<?php if ( ! empty( $webhook ) && is_array( $webhook ) ) { ?>
<button type="button" id="pmpro_stripe_create_webhook" class="button button-secondary" style="display: none;"><span class="dashicons dashicons-update-alt"></span> <?php esc_html_e( 'Create Webhook' ,'paid-memberships-pro' ); ?></button>
<?php
if ( ! empty( $webhook ) && is_array( $webhook ) ) {
?>
<button type="button" id="pmpro_stripe_create_webhook" class="button button-secondary" style="display: none;"><span class="dashicons dashicons-update-alt"></span> <?php esc_html_e( 'Create Webhook' ,'paid-memberships-pro' ); ?></button>
<?php
if ( 'disabled' === $webhook['status'] ) {
// Check webhook status.
?>
<div class="notice error inline">
<p id="pmpro_stripe_webhook_notice" class="pmpro_stripe_webhook_notice"><?php esc_html_e( 'A webhook is set up in Stripe, but it is disabled.', 'paid-memberships-pro' ); ?> <a id="pmpro_stripe_rebuild_webhook" href="#"><?php esc_html_e( 'Rebuild Webhook', 'paid-memberships-pro' ); ?></a></p>
</div>
<?php
} elseif ( $webhook['api_version'] < PMPRO_STRIPE_API_VERSION ) {
// Check webhook API version.
?>
<div class="notice error inline">
<p id="pmpro_stripe_webhook_notice" class="pmpro_stripe_webhook_notice"><?php esc_html_e( 'A webhook is set up in Stripe, but it is using an old API version.', 'paid-memberships-pro' ); ?> <a id="pmpro_stripe_rebuild_webhook" href="#"><?php esc_html_e( 'Rebuild Webhook', 'paid-memberships-pro' ); ?></a></p>
</div>
<?php
} else {
?>
<div class="notice notice-success inline">
<p id="pmpro_stripe_webhook_notice" class="pmpro_stripe_webhook_notice"><?php esc_html_e( 'Your webhook is enabled.', 'paid-memberships-pro' ); ?> <a id="pmpro_stripe_delete_webhook" href="#"><?php esc_html_e( 'Disable Webhook', 'paid-memberships-pro' ); ?></a></p>
</div>
<?php
}
} else { ?>
<button type="button" id="pmpro_stripe_create_webhook" class="button button-secondary"><span class="dashicons dashicons-update-alt"></span> <?php esc_html_e( 'Create Webhook' ,'paid-memberships-pro' ); ?></button>
if ( 'disabled' === $webhook['status'] ) {
// Check webhook status.
?>
<div class="notice error inline">
<p id="pmpro_stripe_webhook_notice" class="pmpro_stripe_webhook_notice"><?php esc_html_e('A webhook in Stripe is required to process recurring payments, manage failed payments, and synchronize cancellations.', 'paid-memberships-pro' );?></p>
<p id="pmpro_stripe_webhook_notice" class="pmpro_stripe_webhook_notice"><?php esc_html_e( 'A webhook is set up in Stripe, but it is disabled.', 'paid-memberships-pro' ); ?> <a id="pmpro_stripe_rebuild_webhook" href="#"><?php esc_html_e( 'Rebuild Webhook', 'paid-memberships-pro' ); ?></a></p>
</div>
<?php
} elseif ( $webhook['api_version'] < PMPRO_STRIPE_API_VERSION ) {
// Check webhook API version.
?>
<div class="notice error inline">
<p id="pmpro_stripe_webhook_notice" class="pmpro_stripe_webhook_notice"><?php esc_html_e( 'A webhook is set up in Stripe, but it is using an old API version.', 'paid-memberships-pro' ); ?> <a id="pmpro_stripe_rebuild_webhook" href="#"><?php esc_html_e( 'Rebuild Webhook', 'paid-memberships-pro' ); ?></a></p>
</div>
<?php
} else {
?>
<div class="notice notice-success inline">
<p id="pmpro_stripe_webhook_notice" class="pmpro_stripe_webhook_notice"><?php esc_html_e( 'Your webhook is enabled.', 'paid-memberships-pro' ); ?> <a id="pmpro_stripe_delete_webhook" href="#"><?php esc_html_e( 'Disable Webhook', 'paid-memberships-pro' ); ?></a></p>
</div>
<?php
}
} else { ?>
<button type="button" id="pmpro_stripe_create_webhook" class="button button-secondary"><span class="dashicons dashicons-update-alt"></span> <?php esc_html_e( 'Create Webhook' ,'paid-memberships-pro' ); ?></button>
<div class="notice error inline">
<p id="pmpro_stripe_webhook_notice" class="pmpro_stripe_webhook_notice"><?php esc_html_e('A webhook in Stripe is required to process payments, manage failed payments, and synchronize cancellations.', 'paid-memberships-pro' );?></p>
<?php
}
?>
</td>
</tr>
Expand Down Expand Up @@ -483,7 +487,7 @@ public static function pmpro_payment_option_fields( $values, $gateway ) {
}
}
if ( ! empty( $failed_webhooks ) ) {
echo '<div class="notice error inline"><p>'. esc_html__( 'Some webhooks recently sent by Stripe have not been received by your website. Please ensure that you have a webhook set up in Stripe for the Webhook URL shown below with all of the listed event types active. To test an event type again, please resend the most recent webhook event of that type from the Stripe webhook settings page or wait for it to be sent again in the future.', 'paid-memberships-pro' ) . '</p></div>';
echo '<div class="notice error inline"><p>'. esc_html__( 'Some webhooks recently sent by Stripe have not been received by your website. Please ensure that you have a webhook set up in Stripe for the Webhook URL shown above with all of the listed event types active. To test an event type again, please resend the most recent webhook event of that type from the Stripe webhook settings page or wait for it to be sent again in the future.', 'paid-memberships-pro' ) . '</p></div>';
} elseif ( ! empty( $missing_webhooks ) ) {
echo '<div class="notice inline"><p>'. esc_html__( 'Recent webhook attempts appear to have worked correctly, but there are some event types that have not been checked. Those event types will be checked as they are sent by Stripe. In the meantime, please ensure that you have a webhook set up in Stripe for the Webhook URL shown below with all of the listed event types active.', 'paid-memberships-pro' ) . '</p></div>';
} else {
Expand Down Expand Up @@ -707,7 +711,7 @@ public static function wp_ajax_pmpro_stripe_delete_webhook( $silent = false ) {
$r = array(
'success' => true,
'notice' => 'error',
'message' => __( 'A webhook in Stripe is required to process recurring payments, manage failed payments, and synchronize cancellations.', 'paid-memberships-pro' )
'message' => __( 'A webhook in Stripe is required to process payments, manage failed payments, and synchronize cancellations.', 'paid-memberships-pro' )
);
if ( ! empty( $webhook ) ) {
$delete_webhook_response = $stripe->delete_webhook( $webhook, $secretkey );
Expand Down Expand Up @@ -1248,6 +1252,57 @@ public static function update_option_pmpro_stripe_payment_flow( $old_value ) {
$pmpro_stripe_old_payment_flow = empty( $old_value ) ? 'onsite' : $old_value;
}

/**
* If Stripe is the current gateway but webhooks are not set up, show an admin notice.
*
* @since TBD
*/
public static function show_stripe_webhook_setup_notice() {
// If Stripe isn't the current gateway, we don't need to show the notice.
if ( 'stripe' !== pmpro_getOption( 'gateway' ) ) {
return;
}

// Only show on PMPro admin pages except for the payment settings page.
if ( empty( $_REQUEST['page'] ) || strpos( $_REQUEST['page'], 'pmpro' ) === false || 'pmpro-paymentsettings' === $_REQUEST['page'] ) {
return;
}

// Get webhook data from Stripe.
$stripe = new PMProGateway_stripe();
$webhook = $stripe->does_webhook_exist();

if ( empty( $webhook ) || ! is_array( $webhook ) ) {
// The webhook is not set up.
?>
<div class="notice notice-error" id="pmpro-stripe_webhook_setup-notice">
<p><strong><?php esc_html_e( 'Important Notice: Your Stripe Webhook Is Disabled or Not Configured', 'paid-memberships-pro' ); ?></strong></p>
<p>
<?php
esc_html_e( 'A valid Stripe webhook is required for your membership checkout to communicate with your payment gateway. We have detected that your Stripe webhook is either disabled or not configured for this website.', 'paid-memberships-pro' );
echo ' ';
echo '<a href="' . esc_url( admin_url( 'admin.php?page=pmpro-paymentsettings' ) ) . '">' . esc_html__( 'Set up webhooks now', 'paid-memberships-pro' ) . '</a>';
?>
</p>
</div>
<?php
} elseif ( 'disabled' === $webhook['status'] ) {
// The webhook is disabled.
?>
<div class="notice notice-error" id="pmpro-stripe_webhook_setup-notice">
<p><strong><?php esc_html_e( 'Important Notice: Stripe Webhooks Are Disabled', 'paid-memberships-pro' ); ?></strong></p>
<p>
<?php
esc_html_e( 'In order for Stripe to function properly, there must be a Stripe Webhook configured for this website.', 'paid-memberships-pro' );
echo ' ';
echo '<a href="' . esc_url( admin_url( 'admin.php?page=pmpro-paymentsettings' ) ) . '">' . esc_html__( 'Enable webhooks now', 'paid-memberships-pro' ) . '</a>';
?>
</p>
</div>
<?php
}
}

/**
* Show a modal to the user after connecting to Stripe or switching to Stripe Checkout.
*
Expand Down
6 changes: 6 additions & 0 deletions css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,12 @@ table.pmprommpu_levels tr.remove_level td {background: #F2DEDE; }
}
}

.pmpro_admin-pmpro-paymentsettings #pmpro_stripe_create_webhook {
align-items: center;
display: flex;
gap: 5px;
}

.pmpro_admin-pmpro-paymentsettings .pmpro-admin-secure-key {
-webkit-text-security: disc;
}
Expand Down

0 comments on commit 0ffcd92

Please sign in to comment.