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

fix: Add country compatibility with BNPL Affirm at the form level #5

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/payment/PaymentPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class PaymentPage extends React.Component {
}

// If this is a redirect from Stripe Dynamic Payment Methods, show loading icon until getPaymentStatus is done.
if (isPaymentRedirect && (paymentStatus !== 'requires_payment_method' || paymentStatus !== 'canceled')) {
if (isPaymentRedirect && paymentStatus === null) {
return (
<PageLoading
srMessage={this.props.intl.formatMessage(messages['payment.loading.payment'])}
Expand Down
5 changes: 5 additions & 0 deletions src/payment/checkout/payment-form/PaymentForm.messages.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const messages = defineMessages({
defaultMessage: 'We apologize for the inconvenience but for the time being we require ASCII characters in the name field. We are working on addressing this and appreciate your patience.',
description: 'The form field feedback text for name format issue.',
},
'payment.form.errors.dynamic_payment_methods_not_compatible.country': {
id: 'payment.form.errors.dynamic_payment_methods_not_compatible.country',
defaultMessage: 'Country not available with selected payment method',
description: 'Notifies the user their billing country is not compatible with the Dynamic Payment Method selected.',
},
});

export default messages;
11 changes: 10 additions & 1 deletion src/payment/checkout/payment-form/StripePaymentForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import MonthlyBillingNotification from '../../../subscription/checkout/monthly-b
import { Secure3DModal } from '../../../subscription/secure-3d/secure-3d-modal/Secure3dModal';

import {
getRequiredFields, validateRequiredFields, validateAsciiNames,
getRequiredFields,
validateRequiredFields,
validateAsciiNames,
validateCountryPaymentMethodCompatibility,
} from './utils/form-validators';

import { getPerformanceProperties, markPerformanceIfAble } from '../../performanceEventing';
Expand Down Expand Up @@ -117,6 +120,7 @@ const StripePaymentForm = ({
const {
firstName,
lastName,
country,
} = values;

let stripeElementErrors = null;
Expand All @@ -134,6 +138,11 @@ const StripePaymentForm = ({
firstName,
lastName,
),
...validateCountryPaymentMethodCompatibility(
isDynamicPaymentMethodsEnabled,
stripeSelectedPaymentMethod,
country,
),
};

if (Object.keys(errors).length > 0 || stripeElementErrors) {
Expand Down
23 changes: 22 additions & 1 deletion src/payment/checkout/payment-form/StripePaymentForm.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jest.mock('@edx/frontend-platform/analytics', () => ({
jest.useFakeTimers('modern');

const validateRequiredFieldsMock = jest.spyOn(formValidators, 'validateRequiredFields');

const validateCountryPaymentMethodCompatibilityMock = jest.spyOn(formValidators, 'validateCountryPaymentMethodCompatibility');
const mockStore = configureMockStore();

configureI18n({
Expand Down Expand Up @@ -260,8 +260,10 @@ describe('<StripePaymentForm />', () => {
const testData = [
[
{ firstName: 'This field is required' },
{ country: 'Country not available with selected payment method' },
new SubmissionError({
firstName: 'This field is required',
country: 'Country not available with selected payment method',
}),
],
[
Expand All @@ -272,6 +274,7 @@ describe('<StripePaymentForm />', () => {

testData.forEach((testCaseData) => {
validateRequiredFieldsMock.mockReturnValueOnce(testCaseData[0]);
validateCountryPaymentMethodCompatibilityMock.mockReturnValueOnce(testCaseData[0]);
if (testCaseData[1]) {
expect(() => fireEvent.click(screen.getByText('Place Order')));
expect(submitStripePayment).not.toHaveBeenCalled();
Expand All @@ -294,4 +297,22 @@ describe('<StripePaymentForm />', () => {
expect(formValidators.validateRequiredFields(values)).toEqual(expectedErrors);
});
});

describe('validateCountryPaymentMethodCompatibility', () => {
it('returns errors if country is not compatible with Dynamic Payment Method (BNPL Affirm)', () => {
const values = {
country: 'BR',
};
const expectedErrors = {
country: 'payment.form.errors.dynamic_payment_methods_not_compatible.country',
};
const isDynamicPaymentMethodsEnabled = true;
const stripeSelectedPaymentMethod = 'affirm';
expect(formValidators.validateCountryPaymentMethodCompatibility(
isDynamicPaymentMethodsEnabled,
stripeSelectedPaymentMethod,
values.country,
)).toEqual(expectedErrors);
});
});
});
20 changes: 20 additions & 0 deletions src/payment/checkout/payment-form/utils/form-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@ export function validateRequiredFields(values) {
return errors;
}

export function validateCountryPaymentMethodCompatibility(
isDynamicPaymentMethodsEnabled,
stripeSelectedPaymentMethod,
selectedCountry,
) {
const errors = {};

// Only adding country validation on the form level for BNPL Affirm.
// For Klarna, there is validation on the Stripe API level,
// which is handled with error code 'dynamic-payment-methods-country-not-compatible'
if (isDynamicPaymentMethodsEnabled && stripeSelectedPaymentMethod === 'affirm') {
const countryListCompatibleAffirm = ['CA', 'US'];
if (!countryListCompatibleAffirm.includes(selectedCountry)) {
errors.country = 'payment.form.errors.dynamic_payment_methods_not_compatible.country';
}
}

return errors;
}

export function validateCardDetails(cardExpirationMonth, cardExpirationYear) {
const errors = {};

Expand Down
Loading