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

Reset Invalid Payment Data #262

Merged
merged 1 commit into from
Dec 18, 2023
Merged
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
6 changes: 5 additions & 1 deletion src/pages/donation_form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ dataPersister.initialize( persistenceItems ).then( () => {
Promise.all( [
store.dispatch(
action( NS_PAYMENT, initializePayment ),
createInitialDonationPaymentValues( dataPersister, pageData.applicationVars.initialFormValues )
{
initialValues: createInitialDonationPaymentValues( dataPersister, pageData.applicationVars.initialFormValues ),
allowedIntervals: pageData.applicationVars.paymentIntervals,
allowedPaymentTypes: pageData.applicationVars.paymentTypes,
}
),
store.dispatch(
action( NS_ADDRESS, initializeAddress ),
Expand Down
7 changes: 7 additions & 0 deletions src/store/intervalValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function isValidInterval( interval: string, allowedIntervals: number[] ): boolean {
if ( !allowedIntervals.includes( Number( interval ) ) ) {
return false;
}

return true;
}
31 changes: 20 additions & 11 deletions src/store/payment/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ActionContext } from 'vuex';
import { InitialPaymentValues, IntervalData, TypeData } from '@src/view_models/Payment';
import { IntervalData, TypeData } from '@src/view_models/Payment';
import { DonationPayment } from '@src/store/payment/types';

import {
Expand All @@ -23,28 +23,37 @@ import {
} from '@src/store/payment/mutationTypes';
import { Validity } from '@src/view_models/Validity';
import { isValidAmount } from '@src/store/amountValidator';
import { PaymentInitialisationPayload } from '@src/view_models/PaymentInitialisationPayload';
import { isValidPaymentType } from '@src/store/paymentTypeValidator';
import { isValidInterval } from '@src/store/intervalValidator';

export const actions = {
[ discardInitialization ]( context: ActionContext<DonationPayment, any> ): void {
context.commit( SET_INITIALIZED, false );
},
[ initializePayment ]( context: ActionContext<DonationPayment, any>, initialValues: InitialPaymentValues ): Promise<boolean> {
let amountIsFilledAndValid = false, paymentIsFilled = false;
if ( initialValues.amount && initialValues.amount !== '0' ) {
amountIsFilledAndValid = isValidAmount( Number( initialValues.amount ) );
[ initializePayment ]( context: ActionContext<DonationPayment, any>, payload: PaymentInitialisationPayload ): Promise<boolean> {
const { initialValues, allowedPaymentTypes, allowedIntervals } = payload;

const amountIsValid = isValidAmount( Number( initialValues.amount ) );
const intervalIsValid = isValidInterval( initialValues.paymentIntervalInMonths, allowedIntervals );
const paymentTypeIsValid = isValidPaymentType( initialValues.type, initialValues.paymentIntervalInMonths, allowedPaymentTypes );

if ( amountIsValid ) {
context.commit( SET_AMOUNT, initialValues.amount );
context.commit( SET_AMOUNT_VALIDITY, amountIsFilledAndValid ? Validity.VALID : Validity.INVALID );
context.commit( SET_AMOUNT_VALIDITY, Validity.VALID );
}

if ( initialValues.type && initialValues.type !== '' ) {
context.commit( SET_INTERVAL, intervalIsValid ? initialValues.paymentIntervalInMonths : '0' );

if ( paymentTypeIsValid ) {
context.commit( SET_TYPE, initialValues.type );
context.commit( SET_TYPE_VALIDITY, Validity.VALID );
paymentIsFilled = true;
}
context.commit( SET_INTERVAL, initialValues.paymentIntervalInMonths );
context.commit( SET_INITIALIZED, amountIsFilledAndValid && paymentIsFilled );

return Promise.resolve( amountIsFilledAndValid && paymentIsFilled );
const paymentIsFilledAndValid = amountIsValid && paymentTypeIsValid;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const paymentIsFilledAndValid = amountIsValid && paymentTypeIsValid;
const paymentIsFilledAndValid = amountIsValid && paymentTypeIsValid && intervalIsValid;

I'd say to stop on the 1st page even when the interval could be "rescued" by setting it to one-time, but that is for the PM to decide

context.commit( SET_INITIALIZED, paymentIsFilledAndValid );

return Promise.resolve( paymentIsFilledAndValid );
},
[ markEmptyValuesAsInvalid ]( context: ActionContext<DonationPayment, any> ): void {
context.commit( MARK_EMPTY_FIELDS_INVALID );
Expand Down
14 changes: 14 additions & 0 deletions src/store/paymentTypeValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PaymentType } from '@src/view_models/PaymentType';

export function isValidPaymentType( paymentType: string, interval: string, allowedPaymentTypes: string[] ): boolean {
if ( !allowedPaymentTypes.includes( paymentType ) ) {
return false;
}

// Sofort payments can't be recurring so clear the type if it is initialised with an interval
if ( paymentType === PaymentType.SOFORT && ![ '', '0' ].includes( interval ) ) {
return false;
}

return true;
}
7 changes: 7 additions & 0 deletions src/view_models/PaymentInitialisationPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { InitialPaymentValues } from '@src/view_models/Payment';

export interface PaymentInitialisationPayload {
initialValues: InitialPaymentValues,
allowedIntervals: number[],
allowedPaymentTypes: string[]
}
7 changes: 7 additions & 0 deletions src/view_models/PaymentType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum PaymentType {
DIRECT_DEBIT = 'BEZ',
BANK_TRANSFER = 'UEB',
CREDIT_CARD = 'MCP',
PAYPAL = 'PPL',
SOFORT = 'SUB'
}
13 changes: 9 additions & 4 deletions tests/unit/components/pages/donation_form/AddressPage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { nextTick } from 'vue';
import AddressTypeBasic from '@src/components/pages/donation_form/AddressTypeBasic.vue';
import { Validity } from '@src/view_models/Validity';
import { Salutation } from '@src/view_models/Salutation';
import { PaymentInitialisationPayload } from '@src/view_models/PaymentInitialisationPayload';

const testCountry = {
countryCode: 'de',
Expand Down Expand Up @@ -72,10 +73,14 @@ describe( 'AddressPage.vue', () => {

const setPaymentType = ( store: Store<any>, paymentType: string ): Promise<any> => {
return store.dispatch( action( NS_PAYMENT, initializePayment ), {
amount: '100',
type: paymentType,
paymentIntervalInMonths: '0',
isCustomAmount: false,
allowedIntervals: [ 0 ],
allowedPaymentTypes: [ paymentType ],
initialValues: {
amount: '100',
type: paymentType,
paymentIntervalInMonths: '0',
isCustomAmount: false,
},
} );
};

Expand Down
20 changes: 13 additions & 7 deletions tests/unit/store/donation_store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { action } from '@src/store/util';
import { NS_ADDRESS, NS_PAYMENT } from '@src/store/namespaces';
import { initializeAddress } from '@src/store/address/actionTypes';
import { initializePayment } from '@src/store/payment/actionTypes';
import { PaymentInitialisationPayload } from '@src/view_models/PaymentInitialisationPayload';
import { PaymentType } from '@src/view_models/PaymentType';

describe( 'Donation Store', () => {

Expand All @@ -29,15 +31,19 @@ describe( 'Donation Store', () => {
const type = 'person';
const paymentIntervalInMonths = '1';
const isCustomAmount = false;

const initialData = {
amount,
type,
paymentIntervalInMonths,
isCustomAmount,
const payload: PaymentInitialisationPayload = {
allowedIntervals: [ 1 ],
allowedPaymentTypes: [ 'person' ],
initialValues: {
amount,
type,
paymentIntervalInMonths,
isCustomAmount,
},
};

const store = createStore();
await store.dispatch( action( NS_PAYMENT, initializePayment ), initialData );
await store.dispatch( action( NS_PAYMENT, initializePayment ), payload );

expect( store.state.payment.values.amount ).toBe( amount );
expect( store.state.payment.values.type ).toBe( type );
Expand Down
Loading
Loading