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

130 lines
2.6 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,
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
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;