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-03-26 11:11:46 +00:00
|
|
|
SET_REGISTERED_PAYMENT_METHOD,
|
|
|
|
SET_REGISTERED_EXPRESS_PAYMENT_METHOD,
|
|
|
|
} = ACTION_TYPES;
|
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-03-26 13:31:09 +00:00
|
|
|
{ type, paymentMethodData, errorMessage, paymentMethod }
|
2020-03-10 13:39:21 +00:00
|
|
|
) => {
|
|
|
|
switch ( type ) {
|
|
|
|
case STARTED:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
currentStatus: STARTED,
|
|
|
|
};
|
|
|
|
case ERROR:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
currentStatus: ERROR,
|
|
|
|
errorMessage: errorMessage || state.errorMessage,
|
|
|
|
};
|
|
|
|
case FAILED:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
currentStatus: FAILED,
|
|
|
|
paymentMethodData: paymentMethodData || state.paymentMethodData,
|
|
|
|
errorMessage: errorMessage || state.errorMessage,
|
|
|
|
};
|
|
|
|
case SUCCESS:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
currentStatus: SUCCESS,
|
|
|
|
paymentMethodData: paymentMethodData || state.paymentMethodData,
|
|
|
|
};
|
|
|
|
case PROCESSING:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
currentStatus: PROCESSING,
|
2020-04-02 09:27:54 +00:00
|
|
|
errorMessage: '',
|
2020-03-10 13:39:21 +00:00
|
|
|
};
|
|
|
|
case COMPLETE:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
currentStatus: COMPLETE,
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
case SET_REGISTERED_PAYMENT_METHOD:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
paymentMethods: {
|
|
|
|
...state.paymentMethods,
|
|
|
|
[ paymentMethod.id ]: paymentMethod,
|
|
|
|
},
|
2020-03-10 13:39:21 +00:00
|
|
|
};
|
2020-03-26 11:11:46 +00:00
|
|
|
case SET_REGISTERED_EXPRESS_PAYMENT_METHOD:
|
2020-03-10 13:39:21 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2020-03-26 11:11:46 +00:00
|
|
|
expressPaymentMethods: {
|
|
|
|
...state.expressPaymentMethods,
|
|
|
|
[ paymentMethod.id ]: paymentMethod,
|
|
|
|
},
|
2020-03-10 13:39:21 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default reducer;
|