Skip to content

Commit

Permalink
Merge pull request #2976 from dparker1005/stripe-automatic-webhooks
Browse files Browse the repository at this point in the history
Setting up Stripe webhooks automatically
  • Loading branch information
dparker1005 authored May 8, 2024
2 parents a2a76d0 + dc7333a commit 82cfd7a
Showing 1 changed file with 36 additions and 41 deletions.
77 changes: 36 additions & 41 deletions classes/gateways/class.pmprogateway_stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ public static function init() {
'pmpro_payment_option_fields'
), 10, 2 );

// Show webhook setup banner on payment settings page.
add_action( 'update_option_pmpro_stripe_payment_flow', array( 'PMProGateway_stripe', 'update_option_pmpro_stripe_payment_flow' ), 10, 1 );
add_action( 'pmpro_payment_option_fields', array( 'PMProGateway_stripe', 'show_set_up_webhooks_popup' ) );

//old global RE showing billing address or not
global $pmpro_stripe_lite;
$pmpro_stripe_lite = apply_filters( "pmpro_stripe_lite", ! get_option( "pmpro_stripe_billingaddress" ) ); //default is opposite of the stripe_billingaddress setting
Expand Down Expand Up @@ -303,16 +299,12 @@ public static function pmpro_payment_option_fields( $values, $gateway ) {
$stripe->show_connect_payment_option_fields( true, $values, $gateway ); // Show live connect fields.
$stripe->show_connect_payment_option_fields( false, $values, $gateway ); // Show sandbox connect fields.

if ( self::using_legacy_keys() ) {
// Check if webhook is enabled or not.
$webhook = $stripe->does_webhook_exist();

// Check to see if events are missing.
if ( is_array( $webhook ) && isset( $webhook['enabled_events'] ) ) {
$events = $stripe->check_missing_webhook_events( $webhook['enabled_events'] );
if ( $events ) {
$stripe->update_webhook_events();
}
// If we have a webhook, make sure it has all the necessary events.
$webhook = $stripe->does_webhook_exist();
if ( is_array( $webhook ) && isset( $webhook['enabled_events'] ) ) {
$events = $stripe->check_missing_webhook_events( $webhook['enabled_events'] );
if ( $events ) {
$stripe->update_webhook_events();
}
}

Expand Down Expand Up @@ -369,12 +361,26 @@ public static function pmpro_payment_option_fields( $values, $gateway ) {
<input type="text" id="stripe_secretkey" name="stripe_secretkey" value="<?php echo esc_attr( $values['stripe_secretkey'] ) ?>" autocomplete="off" class="regular-text code pmpro-admin-secure-key" />
</td>
</tr>
<tr class="gateway pmpro_stripe_legacy_keys <?php if ( $stripe->show_legacy_keys_settings() ) { echo 'gateway_stripe'; } ?>" <?php if ( $gateway != "stripe" || ! $stripe->show_legacy_keys_settings() ) { ?>style="display: none;"<?php } ?>>
<tr class="pmpro_settings_divider gateway gateway_stripe_<?php echo esc_attr( $stripe->gateway_environment ); ?>" <?php if ( $gateway != "stripe" ) { ?>style="display: none;"<?php } ?>>
<td colspan="2">
<hr />
<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 } ?>>
<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 } ?>>
<th scope="row" valign="top">
<label><?php esc_html_e( 'Webhook', 'paid-memberships-pro' ); ?></label>
<label><?php esc_html_e( 'Webhook Status', 'paid-memberships-pro' ); ?></label>
</th>
<td>
<?php if ( ! empty( $webhook ) && is_array( $webhook ) && $stripe->show_legacy_keys_settings()) { ?>
<?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'] ) {
Expand All @@ -398,33 +404,26 @@ public static function pmpro_payment_option_fields( $values, $gateway ) {
</div>
<?php
}
} elseif ( $stripe->show_legacy_keys_settings() ) { ?>
} 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 recurring payments, manage failed payments, and synchronize cancellations.', 'paid-memberships-pro' );?></p>
</div>
<?php
}
?>
<p class="description"><?php esc_html_e( 'Webhook URL', 'paid-memberships-pro' ); ?>:
<code><?php echo esc_url( $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 } ?>>
<td colspan="2">
<hr />
<h2><?php esc_html_e( 'Webhook Status', 'paid-memberships-pro' ); ?></h2>
</td>
</tr>
<tr class="gateway gateway_stripe_<?php echo esc_attr( $stripe->gateway_environment ); ?>" <?php if ( $gateway != "stripe" ) { ?>style="display: none;"<?php } ?>>
<td colspan="2">
<th>
<label><?php esc_html_e( 'Webhook History', 'paid-memberships-pro' ); ?></label>
</th>
<td>
<?php
if ( ! empty( $stripe->get_secretkey() ) ) {
$required_webhook_events = self::webhook_events();
sort( $required_webhook_events );

$webhook_event_data = array();

$failed_webhooks = array();
$missing_webhooks = array();
$working_webhooks = array();
Expand Down Expand Up @@ -526,8 +525,6 @@ public static function pmpro_payment_option_fields( $values, $gateway ) {
<?php
}
?>
<p class="description"><?php esc_html_e( 'Webhook URL', 'paid-memberships-pro' ); ?>:
<code><?php echo esc_html( $stripe->get_site_webhook_url() ); ?></code></p>
</td>
</tr>
<tr class="pmpro_settings_divider gateway gateway_stripe" <?php if ( $gateway != "stripe" ) { ?>style="display: none;"<?php } ?>>
Expand Down Expand Up @@ -695,11 +692,7 @@ public static function pmpro_payment_option_fields( $values, $gateway ) {
* AJAX callback to create webhooks.
*/
public static function wp_ajax_pmpro_stripe_create_webhook( $silent = false ) {
$secretkey = sanitize_text_field( $_REQUEST['secretkey'] );

$stripe = new PMProGateway_stripe();
Stripe\Stripe::setApiKey( $secretkey );

$update_webhook_response = $stripe->update_webhook_events();

if ( empty( $update_webhook_response ) || is_wp_error( $update_webhook_response ) ) {
Expand Down Expand Up @@ -731,11 +724,7 @@ public static function wp_ajax_pmpro_stripe_create_webhook( $silent = false ) {
* AJAX callback to disable webhooks.
*/
public static function wp_ajax_pmpro_stripe_delete_webhook( $silent = false ) {
$secretkey = sanitize_text_field( $_REQUEST['secretkey'] );

$stripe = new PMProGateway_stripe();
Stripe\Stripe::setApiKey( $secretkey );

$webhook = $stripe->does_webhook_exist();

$r = array(
Expand Down Expand Up @@ -1196,8 +1185,9 @@ public static function stripe_connect_save_options() {
unset( $_GET['pmpro_stripe_access_token'] );
unset( $_GET['pmpro_stripe_publishable_key'] );

// Set a transient to show a banner to set up webhooks on the next page load.
set_transient( 'pmpro_stripe_connect_show_webhook_set_up_banner', true, 60 );
// Set up a webhook if needed.
$stripe = new PMProGateway_stripe();
$stripe->update_webhook_events();

wp_redirect( admin_url( sprintf( 'admin.php?%s', http_build_query( $_GET ) ) ) );
exit;
Expand Down Expand Up @@ -1280,10 +1270,12 @@ public static function stripe_connect_deauthorize() {
* If the checkout flow has changed to Stripe Checkout, remember to show a banner to set up webhooks.
*
* @since 2.12
* @deprecated TBD
*
* @param string $old_value The old value of the option.
*/
public static function update_option_pmpro_stripe_payment_flow( $old_value ) {
_deprecated_function( __FUNCTION__, 'TBD' );
global $pmpro_stripe_old_payment_flow;
$pmpro_stripe_old_payment_flow = empty( $old_value ) ? 'onsite' : $old_value;
}
Expand All @@ -1292,8 +1284,11 @@ public static function update_option_pmpro_stripe_payment_flow( $old_value ) {
* Show a modal to the user after connecting to Stripe or switching to Stripe Checkout.
*
* @since 2.12
* @deprecated TBD
*/
public static function show_set_up_webhooks_popup() {
_deprecated_function( __FUNCTION__, 'TBD' );

global $pmpro_stripe_old_payment_flow;

// Figure out if we need to show a popup.
Expand Down

0 comments on commit 82cfd7a

Please sign in to comment.