Skip to content

Commit

Permalink
Use webhook status from Stripe in settings display
Browse files Browse the repository at this point in the history
Instead of determining webhook status ('Enabled' vs 'Disabled')
by using the presence or absence of secret keys, we check
the actual status using Stripe API.
  • Loading branch information
annemirasol committed Oct 30, 2024
1 parent 9cfc896 commit 2f9a0d3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
6 changes: 3 additions & 3 deletions client/components/webhook-description/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe( 'WebhookDescription', () => {
};
} );

render( <WebhookDescription isWebhookSecretEntered={ true } /> );
render( <WebhookDescription isWebhookEnabled={ true } /> );

expect(
screen.queryByTestId( 'webhook-information' )
Expand All @@ -44,7 +44,7 @@ describe( 'WebhookDescription', () => {
};
} );

render( <WebhookDescription isWebhookSecretEntered={ false } /> );
render( <WebhookDescription isWebhookEnabled={ false } /> );

expect(
screen.queryByTestId( 'webhook-information' )
Expand All @@ -64,7 +64,7 @@ describe( 'WebhookDescription', () => {
};
} );

render( <WebhookDescription isWebhookSecretEntered={ false } /> );
render( <WebhookDescription isWebhookEnabled={ false } /> );

expect(
screen.queryByTestId( 'webhook-information' )
Expand Down
9 changes: 4 additions & 5 deletions client/components/webhook-description/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const WebhookDescriptionInner = styled.div`
}
`;

export const WebhookDescription = ( { isWebhookSecretEntered } ) => {
export const WebhookDescription = ( { isWebhookEnabled } ) => {
const {
code,
message,
Expand All @@ -40,10 +40,9 @@ export const WebhookDescription = ( { isWebhookSecretEntered } ) => {
} = useWebhookStateMessage();
const isWarningMessage = code === 3 || code === 4;
const isSuccessMessage = code === 1;
const isSuccessMessageWithSecret =
isSuccessMessage && isWebhookSecretEntered;
const isSuccessMessageWithSecret = isSuccessMessage && isWebhookEnabled;
const webhookDescriptionClassesAr = [];
if ( isWebhookSecretEntered ) {
if ( isWebhookEnabled ) {
webhookDescriptionClassesAr.push( 'expanded' );
}
if ( isWarningMessage ) {
Expand All @@ -52,7 +51,7 @@ export const WebhookDescription = ( { isWebhookSecretEntered } ) => {

return (
<WebhookDescriptionWrapper>
{ ! isWebhookSecretEntered && <WebhookInformation /> }
{ ! isWebhookEnabled && <WebhookInformation /> }
<WebhookDescriptionInner
className={ webhookDescriptionClassesAr.join( ' ' ) }
>
Expand Down
20 changes: 4 additions & 16 deletions client/settings/account-details/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import styled from '@emotion/styled';
import SectionStatus from '../section-status';
import Tooltip from 'wcstripe/components/tooltip';
import { useAccount } from 'wcstripe/data/account';
import {
useAccountKeysTestWebhookSecret,
useAccountKeysWebhookSecret,
} from 'wcstripe/data/account-keys';
import { WebhookDescription } from 'wcstripe/components/webhook-description';

const AccountDetailsContainer = styled.div`
Expand Down Expand Up @@ -108,28 +104,20 @@ const PayoutsSection = () => {
};

const WebhooksSection = () => {
const [ testWebhookSecret ] = useAccountKeysTestWebhookSecret();
const [ webhookSecret ] = useAccountKeysWebhookSecret();
const { data } = useAccount();
const isTestModeEnabled = Boolean( data.testmode );

const isWebhookSecretEntered = Boolean(
isTestModeEnabled ? testWebhookSecret : webhookSecret
);
const isWebhookEnabled = Boolean( data.is_webhook_enabled );

return (
<>
<AccountSection>
<Label>{ __( 'Webhook', 'woocommerce-gateway-stripe' ) }</Label>
<SectionStatus isEnabled={ isWebhookSecretEntered }>
{ isWebhookSecretEntered
<SectionStatus isEnabled={ isWebhookEnabled }>
{ isWebhookEnabled
? __( 'Enabled', 'woocommerce-gateway-stripe' )
: __( 'Disabled', 'woocommerce-gateway-stripe' ) }
</SectionStatus>
</AccountSection>
<WebhookDescription
isWebhookSecretEntered={ isWebhookSecretEntered }
/>
<WebhookDescription isWebhookEnabled={ isWebhookEnabled } />
</>
);
};
Expand Down
1 change: 1 addition & 0 deletions includes/admin/class-wc-rest-stripe-account-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function get_account() {
'webhook_status_message' => WC_Stripe_Webhook_State::get_webhook_status_message(),
'webhook_url' => WC_Stripe_Helper::get_webhook_url(),
'configured_webhook_urls' => WC_Stripe_Webhook_State::get_configured_webhook_urls(),
'is_webhook_enabled' => WC_Stripe_Webhook_State::is_webhook_enabled(),
'oauth_connections' => [
'test' => $this->get_account_oauth_connection_data( 'test' ),
'live' => $this->get_account_oauth_connection_data( 'live' ),
Expand Down
26 changes: 26 additions & 0 deletions includes/class-wc-stripe-webhook-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,30 @@ public static function get_configured_webhook_urls() {
'test' => empty( $test_webhook['url'] ) ? null : rawurlencode( $test_webhook['url'] ),
];
}


/**
* Determine if the webhook is enabled by checking with Stripe.
*
* @return bool
*/
public static function is_webhook_enabled() {
error_log( 'is_webhook_enabled' );
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();

$key = self::get_testmode() ? 'test_webhook_data' : 'webhook_data';
if ( ! isset( $stripe_settings[ $key ] ) ) {
return false;
}

$webhook_id = $stripe_settings[ $key ]['id'];
$webhook_secret = $stripe_settings[ $key ]['secret'];
if ( empty( $webhook_id ) || empty( $webhook_secret ) ) {
return false;
}

WC_Stripe_API::set_secret_key( $webhook_secret );
$webhook = WC_Stripe_API::request( [], 'webhook_endpoints/' . $webhook_id, 'GET' );
return ! empty( $webhook->status ) && 'enabled' === $webhook->status;
}
};

0 comments on commit 2f9a0d3

Please sign in to comment.