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

133 lines
2.7 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_METHODS,
SET_REGISTERED_EXPRESS_PAYMENT_METHODS,
Allow shopper to save Stripe payment method in user account for subsequent purchases (https://github.com/woocommerce/woocommerce-blocks/pull/2453) * always default "save my card for next time" checkbox to unchecked: This is based on the previous checkout behaviour. I.e. the shopper has to actively opt-in to save their card. * Implement "save payment method for next purchase" in checkout: - send "save card" option using existing post key - wc-stripe-new-payment-method - comment out inappropriate use of "save" when using a saved card (tbc) * don't hard code the payment gateway name in 'save payment method' key * refactor "save payment info" checkbox so payment methods can opt-in: - Add options.allowSavePaymentToken to payment method registration / config. - Opt-in in Stripe CC, it allows saved cards. - Remove render of "save my card" checkbox from Stripe CC UI component. - Render "save my card" checkbox automatically in payment method tab (based on allowSavePaymentToken option). + todo/follow up comments * rejig "save my payment method" behaviour so it's generic: - Any payment method that supports "save" can opt-in: - options.allowSavePaymentToken = true/false - handle `wc-XXX-new-payment-method` key server side to persist - Add support in payment context/state reducer for storing checkbox state, expose value and action via context - Convert state flag to appropriate API key/value in payment processor - Remove previous stripe-specific implementation + bonus add comment to payment context about preserving state in PRISTINE action * rename payment method "allow save" option, more consistent with UI * remove last vestiges of gateway-specific "save card" impl: - No need to pass CheckboxControl to payment methods; checkbox is now handled automatically by checkout. - Remove shouldSavePayment prop passing through various layers of stripe payment processing code. (Now handled in context/processor.) * change new option property name and shape. Also adds validation. * update type-defs * use more reliable `activePaymentMethod` for saved payment method Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-05-12 15:12:28 +00:00
SET_SHOULD_SAVE_PAYMENT_METHOD,
} = ACTION_TYPES;
const hasSavedPaymentToken = ( paymentMethodData ) => {
return !! (
typeof paymentMethodData === 'object' && paymentMethodData.isSavedToken
);
};
/**
* Reducer for payment data state
*
* @param {Object} state Current state.
* @param {Object} action Current action.
*/
const reducer = (
state = DEFAULT_PAYMENT_DATA,
Allow shopper to save Stripe payment method in user account for subsequent purchases (https://github.com/woocommerce/woocommerce-blocks/pull/2453) * always default "save my card for next time" checkbox to unchecked: This is based on the previous checkout behaviour. I.e. the shopper has to actively opt-in to save their card. * Implement "save payment method for next purchase" in checkout: - send "save card" option using existing post key - wc-stripe-new-payment-method - comment out inappropriate use of "save" when using a saved card (tbc) * don't hard code the payment gateway name in 'save payment method' key * refactor "save payment info" checkbox so payment methods can opt-in: - Add options.allowSavePaymentToken to payment method registration / config. - Opt-in in Stripe CC, it allows saved cards. - Remove render of "save my card" checkbox from Stripe CC UI component. - Render "save my card" checkbox automatically in payment method tab (based on allowSavePaymentToken option). + todo/follow up comments * rejig "save my payment method" behaviour so it's generic: - Any payment method that supports "save" can opt-in: - options.allowSavePaymentToken = true/false - handle `wc-XXX-new-payment-method` key server side to persist - Add support in payment context/state reducer for storing checkbox state, expose value and action via context - Convert state flag to appropriate API key/value in payment processor - Remove previous stripe-specific implementation + bonus add comment to payment context about preserving state in PRISTINE action * rename payment method "allow save" option, more consistent with UI * remove last vestiges of gateway-specific "save card" impl: - No need to pass CheckboxControl to payment methods; checkbox is now handled automatically by checkout. - Remove shouldSavePayment prop passing through various layers of stripe payment processing code. (Now handled in context/processor.) * change new option property name and shape. Also adds validation. * update type-defs * use more reliable `activePaymentMethod` for saved payment method Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-05-12 15:12:28 +00:00
{
type,
paymentMethodData,
shouldSavePaymentMethod,
errorMessage,
paymentMethods,
}
) => {
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,
hasSavedToken: hasSavedPaymentToken(
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,
},
Allow shopper to save Stripe payment method in user account for subsequent purchases (https://github.com/woocommerce/woocommerce-blocks/pull/2453) * always default "save my card for next time" checkbox to unchecked: This is based on the previous checkout behaviour. I.e. the shopper has to actively opt-in to save their card. * Implement "save payment method for next purchase" in checkout: - send "save card" option using existing post key - wc-stripe-new-payment-method - comment out inappropriate use of "save" when using a saved card (tbc) * don't hard code the payment gateway name in 'save payment method' key * refactor "save payment info" checkbox so payment methods can opt-in: - Add options.allowSavePaymentToken to payment method registration / config. - Opt-in in Stripe CC, it allows saved cards. - Remove render of "save my card" checkbox from Stripe CC UI component. - Render "save my card" checkbox automatically in payment method tab (based on allowSavePaymentToken option). + todo/follow up comments * rejig "save my payment method" behaviour so it's generic: - Any payment method that supports "save" can opt-in: - options.allowSavePaymentToken = true/false - handle `wc-XXX-new-payment-method` key server side to persist - Add support in payment context/state reducer for storing checkbox state, expose value and action via context - Convert state flag to appropriate API key/value in payment processor - Remove previous stripe-specific implementation + bonus add comment to payment context about preserving state in PRISTINE action * rename payment method "allow save" option, more consistent with UI * remove last vestiges of gateway-specific "save card" impl: - No need to pass CheckboxControl to payment methods; checkbox is now handled automatically by checkout. - Remove shouldSavePayment prop passing through various layers of stripe payment processing code. (Now handled in context/processor.) * change new option property name and shape. Also adds validation. * update type-defs * use more reliable `activePaymentMethod` for saved payment method Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-05-12 15:12:28 +00:00
shouldSavePaymentMethod: state.shouldSavePaymentMethod,
};
case SET_REGISTERED_PAYMENT_METHODS:
return {
...state,
paymentMethods: {
...state.paymentMethods,
...paymentMethods,
},
};
case SET_REGISTERED_EXPRESS_PAYMENT_METHODS:
return {
...state,
expressPaymentMethods: {
...state.expressPaymentMethods,
...paymentMethods,
},
};
Allow shopper to save Stripe payment method in user account for subsequent purchases (https://github.com/woocommerce/woocommerce-blocks/pull/2453) * always default "save my card for next time" checkbox to unchecked: This is based on the previous checkout behaviour. I.e. the shopper has to actively opt-in to save their card. * Implement "save payment method for next purchase" in checkout: - send "save card" option using existing post key - wc-stripe-new-payment-method - comment out inappropriate use of "save" when using a saved card (tbc) * don't hard code the payment gateway name in 'save payment method' key * refactor "save payment info" checkbox so payment methods can opt-in: - Add options.allowSavePaymentToken to payment method registration / config. - Opt-in in Stripe CC, it allows saved cards. - Remove render of "save my card" checkbox from Stripe CC UI component. - Render "save my card" checkbox automatically in payment method tab (based on allowSavePaymentToken option). + todo/follow up comments * rejig "save my payment method" behaviour so it's generic: - Any payment method that supports "save" can opt-in: - options.allowSavePaymentToken = true/false - handle `wc-XXX-new-payment-method` key server side to persist - Add support in payment context/state reducer for storing checkbox state, expose value and action via context - Convert state flag to appropriate API key/value in payment processor - Remove previous stripe-specific implementation + bonus add comment to payment context about preserving state in PRISTINE action * rename payment method "allow save" option, more consistent with UI * remove last vestiges of gateway-specific "save card" impl: - No need to pass CheckboxControl to payment methods; checkbox is now handled automatically by checkout. - Remove shouldSavePayment prop passing through various layers of stripe payment processing code. (Now handled in context/processor.) * change new option property name and shape. Also adds validation. * update type-defs * use more reliable `activePaymentMethod` for saved payment method Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-05-12 15:12:28 +00:00
case SET_SHOULD_SAVE_PAYMENT_METHOD:
return {
...state,
shouldSavePaymentMethod,
};
}
return state;
};
export default reducer;