2020-03-10 13:39:21 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-03-26 11:11:46 +00:00
|
|
|
import { ACTION_TYPES, DEFAULT_PAYMENT_DATA } from './constants';
|
2020-03-10 13:39:21 +00:00
|
|
|
const {
|
|
|
|
STARTED,
|
|
|
|
ERROR,
|
|
|
|
FAILED,
|
|
|
|
SUCCESS,
|
|
|
|
PROCESSING,
|
|
|
|
PRISTINE,
|
|
|
|
COMPLETE,
|
2020-04-14 22:44:31 +00:00
|
|
|
SET_REGISTERED_PAYMENT_METHODS,
|
|
|
|
SET_REGISTERED_EXPRESS_PAYMENT_METHODS,
|
2020-05-12 15:12:28 +00:00
|
|
|
SET_SHOULD_SAVE_PAYMENT_METHOD,
|
2020-03-26 11:11:46 +00:00
|
|
|
} = ACTION_TYPES;
|
2020-03-10 13:39:21 +00:00
|
|
|
|
2020-06-07 20:47:16 +00:00
|
|
|
const hasSavedPaymentToken = ( paymentMethodData ) => {
|
|
|
|
return !! (
|
|
|
|
typeof paymentMethodData === 'object' && paymentMethodData.isSavedToken
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-10 13:39:21 +00:00
|
|
|
/**
|
|
|
|
* Reducer for payment data state
|
|
|
|
*
|
|
|
|
* @param {Object} state Current state.
|
|
|
|
* @param {Object} action Current action.
|
|
|
|
*/
|
|
|
|
const reducer = (
|
|
|
|
state = DEFAULT_PAYMENT_DATA,
|
2020-05-12 15:12:28 +00:00
|
|
|
{
|
|
|
|
type,
|
|
|
|
paymentMethodData,
|
|
|
|
shouldSavePaymentMethod,
|
|
|
|
errorMessage,
|
|
|
|
paymentMethods,
|
|
|
|
}
|
2020-03-10 13:39:21 +00:00
|
|
|
) => {
|
|
|
|
switch ( type ) {
|
|
|
|
case STARTED:
|
2020-04-08 15:46:34 +00:00
|
|
|
return state.currentStatus !== STARTED
|
|
|
|
? {
|
|
|
|
...state,
|
|
|
|
currentStatus: STARTED,
|
|
|
|
}
|
|
|
|
: state;
|
2020-03-10 13:39:21 +00:00
|
|
|
case ERROR:
|
2020-04-08 15:46:34 +00:00
|
|
|
return state.currentStatus !== ERROR
|
|
|
|
? {
|
|
|
|
...state,
|
|
|
|
currentStatus: ERROR,
|
|
|
|
errorMessage: errorMessage || state.errorMessage,
|
|
|
|
}
|
|
|
|
: state;
|
2020-03-10 13:39:21 +00:00
|
|
|
case FAILED:
|
2020-04-08 15:46:34 +00:00
|
|
|
return state.currentStatus !== FAILED
|
|
|
|
? {
|
|
|
|
...state,
|
|
|
|
currentStatus: FAILED,
|
|
|
|
paymentMethodData:
|
|
|
|
paymentMethodData || state.paymentMethodData,
|
|
|
|
errorMessage: errorMessage || state.errorMessage,
|
|
|
|
}
|
|
|
|
: state;
|
2020-03-10 13:39:21 +00:00
|
|
|
case SUCCESS:
|
2020-04-08 15:46:34 +00:00
|
|
|
return state.currentStatus !== SUCCESS
|
|
|
|
? {
|
|
|
|
...state,
|
|
|
|
currentStatus: SUCCESS,
|
|
|
|
paymentMethodData:
|
|
|
|
paymentMethodData || state.paymentMethodData,
|
2020-06-07 20:47:16 +00:00
|
|
|
hasSavedToken: hasSavedPaymentToken(
|
|
|
|
paymentMethodData
|
|
|
|
),
|
2020-04-08 15:46:34 +00:00
|
|
|
}
|
|
|
|
: state;
|
2020-03-10 13:39:21 +00:00
|
|
|
case PROCESSING:
|
2020-04-08 15:46:34 +00:00
|
|
|
return state.currentStatus !== PROCESSING
|
|
|
|
? {
|
|
|
|
...state,
|
|
|
|
currentStatus: PROCESSING,
|
|
|
|
errorMessage: '',
|
|
|
|
}
|
|
|
|
: state;
|
2020-03-10 13:39:21 +00:00
|
|
|
case COMPLETE:
|
2020-04-08 15:46:34 +00:00
|
|
|
return state.currentStatus !== COMPLETE
|
|
|
|
? {
|
|
|
|
...state,
|
|
|
|
currentStatus: COMPLETE,
|
|
|
|
}
|
|
|
|
: state;
|
2020-03-10 13:39:21 +00:00
|
|
|
|
|
|
|
case PRISTINE:
|
|
|
|
return {
|
|
|
|
...DEFAULT_PAYMENT_DATA,
|
|
|
|
currentStatus: PRISTINE,
|
2020-03-26 11:11:46 +00:00
|
|
|
// keep payment method registration state
|
|
|
|
paymentMethods: {
|
|
|
|
...state.paymentMethods,
|
|
|
|
},
|
|
|
|
expressPaymentMethods: {
|
|
|
|
...state.expressPaymentMethods,
|
|
|
|
},
|
2020-05-12 15:12:28 +00:00
|
|
|
shouldSavePaymentMethod: state.shouldSavePaymentMethod,
|
2020-03-26 11:11:46 +00:00
|
|
|
};
|
2020-04-14 22:44:31 +00:00
|
|
|
case SET_REGISTERED_PAYMENT_METHODS:
|
2020-03-26 11:11:46 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2020-07-13 22:52:13 +00:00
|
|
|
paymentMethods,
|
2020-03-10 13:39:21 +00:00
|
|
|
};
|
2020-04-14 22:44:31 +00:00
|
|
|
case SET_REGISTERED_EXPRESS_PAYMENT_METHODS:
|
2020-03-10 13:39:21 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2020-03-26 11:11:46 +00:00
|
|
|
expressPaymentMethods: {
|
|
|
|
...state.expressPaymentMethods,
|
2020-04-14 22:44:31 +00:00
|
|
|
...paymentMethods,
|
2020-03-26 11:11:46 +00:00
|
|
|
},
|
2020-03-10 13:39:21 +00:00
|
|
|
};
|
2020-05-12 15:12:28 +00:00
|
|
|
case SET_SHOULD_SAVE_PAYMENT_METHOD:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
shouldSavePaymentMethod,
|
|
|
|
};
|
2020-03-10 13:39:21 +00:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default reducer;
|