-
Notifications
You must be signed in to change notification settings - Fork 3
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
DON-1112: Add user-friendly validation for amount errors on regular g… #1821
Merged
+277
−91
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bb8a8c7
DON-1112: Add user-friendly validation for amount errors on regular g…
bdsl 910ce74
DON-1112: Add validation for payment details step in regular giving form
bdsl 57957c1
DON-1112: Display errror message better on mandate form submission
bdsl e3aa4c1
DON-1112: Typo fix in comment
bdsl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -52,9 +52,9 @@ import {IdentityService} from '../../identity.service'; | |
import {ConversionTrackingService} from '../../conversionTracking.service'; | ||
import {PageMetaService} from '../../page-meta.service'; | ||
import {Person} from '../../person.model'; | ||
import {PostcodeService} from '../../postcode.service'; | ||
import {billingPostcodeRegExp, postcodeFormatHelpRegExp, postcodeRegExp, PostcodeService} from '../../postcode.service'; | ||
import {retryStrategy} from '../../observable-retry'; | ||
import {StripeService} from '../../stripe.service'; | ||
import {getStripeFriendlyError, StripeService} from '../../stripe.service'; | ||
import {getCurrencyMaxValidator} from '../../validators/currency-max'; | ||
import {getCurrencyMinValidator} from '../../validators/currency-min'; | ||
import {EMAIL_REGEXP} from '../../validators/patterns'; | ||
|
@@ -186,17 +186,6 @@ export class DonationStartFormComponent implements AfterContentChecked, AfterCon | |
|
||
tipPercentage = 15; | ||
tipValue: number | undefined; | ||
/** | ||
* Used just to take raw input and put together an all-caps, spaced UK postcode, assuming the | ||
* input was valid (even if differently formatted). Loosely based on https://stackoverflow.com/a/10701634/2803757 | ||
* with an additional tweak to allow (and trim) surrounding spaces. | ||
*/ | ||
private postcodeFormatHelpRegExp = new RegExp('^\\s*([A-Z]{1,2}\\d{1,2}[A-Z]?)\\s*(\\d[A-Z]{2})\\s*$'); | ||
// Based on the simplified pattern suggestions in https://stackoverflow.com/a/51885364/2803757 | ||
private postcodeRegExp = new RegExp('^([A-Z][A-HJ-Y]?\\d[A-Z\\d]? \\d[A-Z]{2}|GIR 0A{2})$'); | ||
|
||
// Intentionally looser to support most countries' formats. | ||
private billingPostcodeRegExp = new RegExp('^[0-9a-zA-Z -]{2,8}$'); | ||
|
||
private idCaptchaCode?: string; | ||
private stripeResponseErrorCode?: string; // stores error codes returned by Stripe after callout | ||
|
@@ -777,7 +766,7 @@ export class DonationStartFormComponent implements AfterContentChecked, AfterCon | |
this.paymentReadinessTracker.onStripeCardChange(state); | ||
|
||
if (state.error) { | ||
this.stripeError = this.getStripeFriendlyError(state.error, 'card_change'); | ||
this.stripeError = getStripeFriendlyError(state.error, 'card_change'); | ||
this.toast.showError(this.stripeError); | ||
this.stripeResponseErrorCode = state.error.code; | ||
} else { | ||
|
@@ -1439,70 +1428,17 @@ export class DonationStartFormComponent implements AfterContentChecked, AfterCon | |
|
||
private handleStripeError( | ||
error: StripeError | {message: string, code: string, decline_code?: string} | undefined, | ||
context: string, | ||
context: 'method_setup'| 'card_change'| 'confirm', | ||
) { | ||
this.submitting = false; | ||
this.stripeError = this.getStripeFriendlyError(error, context); | ||
this.stripeError = getStripeFriendlyError(error, context); | ||
this.toast.showError(this.stripeError); | ||
this.stripeResponseErrorCode = error?.code; | ||
|
||
this.jumpToStep('Payment details'); | ||
this.goToFirstVisibleError(); | ||
} | ||
|
||
/** | ||
* @param error | ||
* @param context 'method_setup', 'card_change' or 'confirm'. | ||
*/ | ||
private getStripeFriendlyError( | ||
error: StripeError | {message: string, code: string, decline_code?: string, description?: string} | undefined, | ||
context: string, | ||
): string { | ||
|
||
|
||
let prefix = ''; | ||
switch (context) { | ||
case 'method_setup': | ||
prefix = 'Payment setup failed: '; | ||
break; | ||
case 'card_change': | ||
prefix = 'Payment method update failed: '; | ||
break; | ||
case 'confirm': | ||
prefix = 'Payment processing failed: '; | ||
} | ||
|
||
if (! error || (! error.message && ! error.code)) { | ||
if (error && error.hasOwnProperty('description')) { | ||
// @ts-ignore - not sure why TS doesn't recognise that it must have a description because I just checked | ||
// with hasOwnProperty. | ||
return `${prefix}${error!.description}`; | ||
} | ||
return `${prefix}Sorry, we encountered an error. Please try again in a moment or contact us if this message persists.`; | ||
} | ||
|
||
let friendlyError = error.message; | ||
|
||
let customMessage = false; | ||
if (error.code === 'card_declined' && error.decline_code === 'generic_decline') { | ||
// Probably a custom Radar rule -> relatively likely to be an incorrect postcode. | ||
friendlyError = `The payment was declined. Please ensure details provided (including postcode) match your card. Contact your bank or [email protected] if the problem persists.`; | ||
customMessage = true; | ||
} | ||
|
||
if (error.code === 'card_declined' && error.decline_code === 'invalid_amount') { | ||
// We've seen e.g. HSBC in Nov '23 decline large donations with this code. | ||
friendlyError = 'The payment was declined. You might need to contact your bank before making a donation of this amount.'; | ||
customMessage = true; | ||
} | ||
|
||
if (customMessage && context === 'confirm') { | ||
prefix = ''; // Don't show extra context info in the most common `context`, when showing our already-long custom copy. | ||
} | ||
|
||
return `${prefix}${friendlyError}`; | ||
} | ||
|
||
private isBillingPostcodePossiblyInvalid() { | ||
return this.stripeResponseErrorCode === 'card_declined'; | ||
} | ||
|
@@ -2016,7 +1952,7 @@ export class DonationStartFormComponent implements AfterContentChecked, AfterCon | |
|
||
// Uppercase it in-place, then we can use patterns that assume upper case. | ||
homePostcode = homePostcode.toUpperCase(); | ||
var parts = homePostcode.match(this.postcodeFormatHelpRegExp); | ||
var parts = homePostcode.match(postcodeFormatHelpRegExp); | ||
if (parts === null) { | ||
// If the input doesn't even match the much looser pattern here, it's going to fail | ||
// the validator check in a moment and there's nothing we can/should do with it | ||
|
@@ -2113,7 +2049,7 @@ export class DonationStartFormComponent implements AfterContentChecked, AfterCon | |
|
||
return [ | ||
requiredNotBlankValidator, | ||
Validators.pattern(this.postcodeRegExp), | ||
Validators.pattern(postcodeRegExp), | ||
]; | ||
} | ||
|
||
|
@@ -2128,7 +2064,7 @@ export class DonationStartFormComponent implements AfterContentChecked, AfterCon | |
]); | ||
this.paymentGroup.controls.billingPostcode!.setValidators([ | ||
requiredNotBlankValidator, | ||
Validators.pattern(this.billingPostcodeRegExp), | ||
Validators.pattern(billingPostcodeRegExp), | ||
]); | ||
} | ||
|
||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function below moved to stripe.service.ts to allow sharing with new component. Not sure if that's the best place to move it to - not sure if its idiomatic or not to put extra stuff in the file alongside the service class in Angular.