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

WIP fix(checkout): CHECKOUT-8264 Don't validate recaptcha if already validated #2697

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAction, ThunkAction } from '@bigcommerce/data-store';
import { concat, defer, from, of } from 'rxjs';
import { createAction, ReadableDataStore, ThunkAction } from '@bigcommerce/data-store';
import { concat, defer, from, Observable, of } from 'rxjs';
import { catchError, switchMap, take } from 'rxjs/operators';

import { InternalCheckoutSelectors } from '../checkout';
Expand Down Expand Up @@ -99,32 +99,53 @@ export default class SpamProtectionActionCreator {
}

execute(): ThunkAction<SpamProtectionAction, InternalCheckoutSelectors> {
return (store) =>
concat(
of(createAction(SpamProtectionActionType.ExecuteRequested)),
this.initialize()(store),
this._googleRecaptcha
.execute()
.pipe(take(1))
.pipe(
switchMap(async ({ error, token }) => {
if (error instanceof SpamProtectionChallengeNotCompletedError) {
throw error;
}
return (store) => {
const { config, checkout } = store.getState();
const {
checkoutSettings: { features },
} = config.getStoreConfigOrThrow();
const { shouldExecuteSpamCheck } = checkout.getCheckoutOrThrow();

const recaptchaExperiment = features['CHECKOUT-8264.recaptcha_error_fix'];

if (recaptchaExperiment) {
if (shouldExecuteSpamCheck) {
return this._executeRecaptcha(store);
}
} else {
return of(createAction(SpamProtectionActionType.ExecuteActionNotNeeded, undefined));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we change the CustomerActionCreator to check the shouldExecuteSpamCheck flag instead, similar to how the check is implemented in PaymentStrategyActionCreator:

shouldExecuteSpamCheck
? this._spamProtectionActionCreator.verifyCheckoutSpamProtection()(store)
: empty(),

This approach will reduce the risk of change as it won't impact the payment step, which already has a check in place as shown above.

Copy link
Contributor Author

@animesh1987 animesh1987 Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks DC. That seems good, that was my first thought as well 😄 Let me make that change.

}

if (error || !token) {
throw new SpamProtectionFailedError();
}
return this._executeRecaptcha(store);
};
}

return createAction(SpamProtectionActionType.ExecuteSucceeded, {
token,
});
}),
),
).pipe(
catchError((error) =>
throwErrorAction(SpamProtectionActionType.ExecuteFailed, error),
_executeRecaptcha(
store: ReadableDataStore<InternalCheckoutSelectors>,
): Observable<SpamProtectionAction> {
return concat(
of(createAction(SpamProtectionActionType.ExecuteRequested)),
this.initialize()(store),
this._googleRecaptcha
.execute()
.pipe(take(1))
.pipe(
switchMap(async ({ error, token }) => {
if (error instanceof SpamProtectionChallengeNotCompletedError) {
throw error;
}

if (error || !token) {
throw new SpamProtectionFailedError();
}

return createAction(SpamProtectionActionType.ExecuteSucceeded, {
token,
});
}),
),
);
).pipe(
catchError((error) => throwErrorAction(SpamProtectionActionType.ExecuteFailed, error)),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum SpamProtectionActionType {
ExecuteRequested = 'SPAM_PROTECTION_EXECUTE_REQUESTED',
ExecuteSucceeded = 'SPAM_PROTECTION_EXECUTE_SUCCEEDED',
ExecuteFailed = 'SPAM_PROTECTION_EXECUTE_FAILED',
ExecuteActionNotNeeded = 'EXECUTE_ACTION_NOT_NEEDED',
}

export type SpamProtectionAction =
Expand All @@ -23,7 +24,8 @@ export type SpamProtectionAction =
| ExecuteFailedAction
| CheckoutVerifyRequestedAction
| CheckoutVerifyFailedAction
| CheckoutVerifySucceededAction;
| CheckoutVerifySucceededAction
| ExecuteActionNotNeededAction;

export interface InitializeRequestedAction extends Action {
type: SpamProtectionActionType.InitializeRequested;
Expand Down Expand Up @@ -60,3 +62,7 @@ export interface CheckoutVerifyFailedAction extends Action<Error> {
export interface CheckoutVerifySucceededAction extends Action<Checkout> {
type: SpamProtectionActionType.VerifyCheckoutSucceeded;
}

export interface ExecuteActionNotNeededAction extends Action {
type: SpamProtectionActionType.ExecuteActionNotNeeded;
}