woocommerce/plugins/woocommerce-blocks/assets/js/base/context/cart-checkout/payment-methods/reducer.js

111 lines
2.3 KiB
JavaScript
Raw Normal View History

/**
* Internal dependencies
*/
import { ACTION_TYPES, DEFAULT_PAYMENT_DATA } from './constants';
const {
STARTED,
ERROR,
FAILED,
SUCCESS,
PROCESSING,
PRISTINE,
COMPLETE,
SET_REGISTERED_PAYMENT_METHOD,
SET_REGISTERED_EXPRESS_PAYMENT_METHOD,
} = ACTION_TYPES;
/**
* Reducer for payment data state
*
* @param {Object} state Current state.
* @param {Object} action Current action.
*/
const reducer = (
state = DEFAULT_PAYMENT_DATA,
{ type, paymentMethodData, errorMessage, paymentMethod }
) => {
switch ( type ) {
case STARTED:
return state.currentStatus !== STARTED
? {
...state,
currentStatus: STARTED,
}
: state;
case ERROR:
return state.currentStatus !== ERROR
? {
...state,
currentStatus: ERROR,
errorMessage: errorMessage || state.errorMessage,
}
: state;
case FAILED:
return state.currentStatus !== FAILED
? {
...state,
currentStatus: FAILED,
paymentMethodData:
paymentMethodData || state.paymentMethodData,
errorMessage: errorMessage || state.errorMessage,
}
: state;
case SUCCESS:
return state.currentStatus !== SUCCESS
? {
...state,
currentStatus: SUCCESS,
paymentMethodData:
paymentMethodData || state.paymentMethodData,
}
: state;
case PROCESSING:
return state.currentStatus !== PROCESSING
? {
...state,
currentStatus: PROCESSING,
errorMessage: '',
}
: state;
case COMPLETE:
return state.currentStatus !== COMPLETE
? {
...state,
currentStatus: COMPLETE,
}
: state;
case PRISTINE:
return {
...DEFAULT_PAYMENT_DATA,
currentStatus: PRISTINE,
// keep payment method registration state
paymentMethods: {
...state.paymentMethods,
},
expressPaymentMethods: {
...state.expressPaymentMethods,
},
};
case SET_REGISTERED_PAYMENT_METHOD:
return {
...state,
paymentMethods: {
...state.paymentMethods,
Refactor logic for handling active payment method with express payment methods via checkout (https://github.com/woocommerce/woocommerce-blocks/pull/2170) * remove logic server side for getting payment method from paymentdata * ensure stripe accounts for payment request type payment methods * make sure legacy payment method handling always runs last * add processedPaymentMethodId to payment method data context state * switch checkout processor to use new processedPaymentMethod id for submission * implement returning paymentMethodId from payment-request-express * include paymentMethodId in stripe cc success return value * include paymentMethodId in cheque success return value * add active payment method setting and handling via checkout express payment methods still need to implement: - onClick when their button is clicked - onClose when the express payment interface is closed (cancelled etc). * don’t expose setActivePaymentMethod on the payment method interface * remove/fix artifacts from earlier iterations of the pull * rename `id` property to `name` property for payment method registration * Revert "include paymentMethodId in cheque success return value" This reverts commit fe4ee8aced6d67bbd9033263ce61844349d18250. * Revert "include paymentMethodId in stripe cc success return value" This reverts commit 359a1f0089866110ec204182f8ffa14ab099c425. * Revert "implement returning paymentMethodId from payment-request-express" This reverts commit 117c68980b0876dee0acc78cec7754ccfe2a9bb1. * Revert "switch checkout processor to use new processedPaymentMethod id for submission" This reverts commit c38a05b63626dfc1336c7bb0e86417b798a803d6. * Revert "add processedPaymentMethodId to payment method data context state" This reverts commit 3d7923e7297f3c76efde536d26eaf68464ba9583. * improve isSuccess response check and variable name * implement paymentMethodId config option * doh php ain’t javascript * add missing dependency from rebase
2020-04-09 15:22:34 +00:00
[ paymentMethod.name ]: paymentMethod,
},
};
case SET_REGISTERED_EXPRESS_PAYMENT_METHOD:
return {
...state,
expressPaymentMethods: {
...state.expressPaymentMethods,
Refactor logic for handling active payment method with express payment methods via checkout (https://github.com/woocommerce/woocommerce-blocks/pull/2170) * remove logic server side for getting payment method from paymentdata * ensure stripe accounts for payment request type payment methods * make sure legacy payment method handling always runs last * add processedPaymentMethodId to payment method data context state * switch checkout processor to use new processedPaymentMethod id for submission * implement returning paymentMethodId from payment-request-express * include paymentMethodId in stripe cc success return value * include paymentMethodId in cheque success return value * add active payment method setting and handling via checkout express payment methods still need to implement: - onClick when their button is clicked - onClose when the express payment interface is closed (cancelled etc). * don’t expose setActivePaymentMethod on the payment method interface * remove/fix artifacts from earlier iterations of the pull * rename `id` property to `name` property for payment method registration * Revert "include paymentMethodId in cheque success return value" This reverts commit fe4ee8aced6d67bbd9033263ce61844349d18250. * Revert "include paymentMethodId in stripe cc success return value" This reverts commit 359a1f0089866110ec204182f8ffa14ab099c425. * Revert "implement returning paymentMethodId from payment-request-express" This reverts commit 117c68980b0876dee0acc78cec7754ccfe2a9bb1. * Revert "switch checkout processor to use new processedPaymentMethod id for submission" This reverts commit c38a05b63626dfc1336c7bb0e86417b798a803d6. * Revert "add processedPaymentMethodId to payment method data context state" This reverts commit 3d7923e7297f3c76efde536d26eaf68464ba9583. * improve isSuccess response check and variable name * implement paymentMethodId config option * doh php ain’t javascript * add missing dependency from rebase
2020-04-09 15:22:34 +00:00
[ paymentMethod.name ]: paymentMethod,
},
};
}
return state;
};
export default reducer;