1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
<?php
defined( 'ABSPATH' ) || exit();
function wc_stripe_update_option_3_3_13( $settings, $key, $value ) {
if ( method_exists( $settings, 'update_option' ) ) {
$settings->update_option( $key, $value );
} else {
$settings->settings[ $key ] = $value;
update_option( $settings->get_option_key(), $settings->settings );
}
}
wc_stripe_update_option_3_3_13( stripe_wc()->advanced_settings, 'email_enabled', get_option( 'woocommerce_stripe_email_receipt', 'no' ) );
delete_option( 'woocommerce_stripe_email_receipt' );
$settings = stripe_wc()->api_settings;
$client = WC_Stripe_Gateway::load();
$url = get_rest_url( null, '/wc-stripe/v1/webhook' );
foreach ( array( 'live', 'test' ) as $mode ) {
$webhook_id = $settings->get_webhook_id( $mode );
if ( ! $webhook_id ) {
$webhooks = $client->mode( $mode )->webhookEndpoints->all( array( 'limit' => 50 ) );
if ( ! is_wp_error( $webhooks ) ) {
foreach ( $webhooks->data as $webhook ) {
if ( $webhook->url === $url ) {
wc_stripe_update_option_3_3_13( $settings, "webhook_id_{$mode}", $webhook->id );
$webhook_id = $webhook->id;
wc_stripe_log_info( sprintf( 'Webhook %1$s saved in settings. Mode: %2$s', $webhook_id, $mode ) );
break;
}
}
} else {
wc_stripe_log_error( sprintf( 'Error fetching webhooks during version 3.1.3 update. Reason: %s', $webhooks->get_error_message() ) );
}
}
if ( $webhook_id ) {
$webhook = $client->mode( $mode )->webhookEndpoints->retrieve( $webhook_id );
if ( ! is_wp_error( $webhook ) ) {
if ( ! in_array( '*', $webhook->enabled_events ) ) {
$webhook = $client->mode( $mode )->webhookEndpoints->update( $webhook_id, array(
'url' => $url,
'enabled_events' => array_values( array_unique( array_merge( array(
'charge.failed',
'charge.succeeded',
'source.chargeable',
'payment_intent.succeeded',
'charge.refunded',
'charge.dispute.created',
'charge.dispute.closed',
'review.opened',
'review.closed'
), $webhook->enabled_events ) ) )
) );
if ( is_wp_error( $webhook ) ) {
wc_stripe_log_error( sprintf( 'Error updating Stripe webhook. Mode: %1$s. Reason: %2$s', $mode, $webhook->get_error_message() ) );
}
}
}
}
}