-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(payment): CHECKOUT-4852 Only show human verification message on m…
…ount if exceeded limit
- Loading branch information
Showing
8 changed files
with
189 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,108 @@ | ||
import React, { Component } from 'react'; | ||
import { CheckoutSelectors } from '@bigcommerce/checkout-sdk'; | ||
import { noop } from 'lodash'; | ||
import React, { Component, MouseEvent, ReactNode } from 'react'; | ||
|
||
import { withCheckout, CheckoutContextProps } from '../checkout'; | ||
import { TranslatedString } from '../locale'; | ||
import { LoadingOverlay } from '../ui/loading'; | ||
|
||
export interface SpamProtectionProps { | ||
didExceedSpamLimit?: boolean; | ||
onUnhandledError?(error: Error): void; | ||
} | ||
|
||
interface SpamProtectionState { | ||
shouldShowRetryButton: boolean; | ||
} | ||
|
||
interface WithCheckoutSpamProtectionProps { | ||
isExecutingSpamCheck: boolean; | ||
verify(): void; | ||
executeSpamCheck(): Promise<CheckoutSelectors>; | ||
} | ||
|
||
function mapToSpamProtectionProps( | ||
{ checkoutService, checkoutState }: CheckoutContextProps | ||
): WithCheckoutSpamProtectionProps { | ||
return { | ||
isExecutingSpamCheck: checkoutState.statuses.isExecutingSpamCheck(), | ||
verify: checkoutService.executeSpamCheck, | ||
executeSpamCheck: checkoutService.executeSpamCheck, | ||
}; | ||
} | ||
|
||
class SpamProtectionField extends Component<SpamProtectionProps & WithCheckoutSpamProtectionProps> { | ||
class SpamProtectionField extends Component< | ||
SpamProtectionProps & WithCheckoutSpamProtectionProps, | ||
SpamProtectionState | ||
> { | ||
state = { | ||
shouldShowRetryButton: false, | ||
}; | ||
|
||
async componentDidMount() { | ||
const { didExceedSpamLimit } = this.props; | ||
|
||
if (didExceedSpamLimit) { | ||
return; | ||
} | ||
|
||
this.verify(); | ||
} | ||
|
||
render() { | ||
const { | ||
isExecutingSpamCheck, | ||
verify, | ||
} = this.props; | ||
const { isExecutingSpamCheck } = this.props; | ||
|
||
return ( | ||
<div className="spamProtection-container"> | ||
<LoadingOverlay isLoading={ isExecutingSpamCheck }> | ||
<div className="spamProtection-panel optimizedCheckout-overlay"> | ||
<a | ||
className="spamProtection-panel-message optimizedCheckout-primaryContent" | ||
data-test="customer-continue-button" | ||
onClick={ verify } | ||
> | ||
<TranslatedString | ||
id="spam_protection.verify_action" | ||
/> | ||
</a> | ||
</div> | ||
{ this.renderContent() } | ||
</LoadingOverlay> | ||
</div> | ||
); | ||
} | ||
|
||
private renderContent(): ReactNode { | ||
const { didExceedSpamLimit } = this.props; | ||
const { shouldShowRetryButton } = this.state; | ||
|
||
if (!didExceedSpamLimit && !shouldShowRetryButton) { | ||
return; | ||
} | ||
|
||
return <div className="spamProtection-panel optimizedCheckout-overlay"> | ||
<a | ||
className="spamProtection-panel-message optimizedCheckout-primaryContent" | ||
data-test="spam-protection-verify-button" | ||
onClick={ this.handleRetry } | ||
> | ||
<TranslatedString | ||
id="spam_protection.verify_action" | ||
/> | ||
</a> | ||
</div>; | ||
} | ||
|
||
private async verify(): Promise<void> { | ||
const { | ||
executeSpamCheck, | ||
onUnhandledError = noop, | ||
} = this.props; | ||
|
||
try { | ||
await executeSpamCheck(); | ||
} catch (error) { | ||
this.setState({ shouldShowRetryButton: true }); | ||
|
||
// Notify the parent component if the user experiences a problem other than cancelling the reCaptcha challenge. | ||
if (error && error.type !== 'spam_protection_challenge_not_completed') { | ||
onUnhandledError(error); | ||
} | ||
} | ||
} | ||
|
||
private handleRetry: (event: MouseEvent) => void = event => { | ||
event.preventDefault(); | ||
|
||
this.verify(); | ||
}; | ||
} | ||
|
||
export default withCheckout(mapToSpamProtectionProps)(SpamProtectionField); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters