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

112 lines
3.4 KiB
JavaScript
Raw Normal View History

/**
* Internal dependencies
*/
import { ACTION_TYPES } from './constants';
const {
ERROR,
FAILED,
SUCCESS,
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;
/**
* Used to dispatch a status update only for the given type.
*
* @param {string} type
*
* @return {Object} The action object.
*/
export const statusOnly = ( type ) => ( { type } );
/**
* Used to dispatch an error message along with setting current payment status
* to ERROR.
*
* @param {string} errorMessage Whatever error message accompanying the error
* condition.
*
* @return {Object} The action object.
*/
export const error = ( errorMessage ) => ( {
type: ERROR,
errorMessage,
} );
/**
* Used to dispatch a payment failed status update.
*
* @param {Object} action Incoming data for the
* action.
* @param {string} action.errorMessage Any message accompanying
* the failed payment.
* @param {Object} action.paymentMethodData Arbitrary extra
* information about the
* payment method in use
* (varies per payment
* method).
*
* @return {Object} An action object.
*/
export const failed = ( { errorMessage, paymentMethodData } ) => ( {
type: FAILED,
errorMessage,
paymentMethodData,
} );
/**
* Used to dispatch a payment success status update.
*
* @param {Object} action Incoming data for the
* action.
* @param {Object} action.paymentMethodData Arbitrary extra
* information about the
* payment method in use
* (varies per payment
* method).
*
* @return {Object} An action object.
*/
export const success = ( { paymentMethodData } ) => ( {
type: SUCCESS,
paymentMethodData,
} );
/**
* Used to dispatch an action for updating a registered payment method in the
* state.
*
* @param {Object} paymentMethods Payment methods to register.
* @return {Object} An action object.
*/
export const setRegisteredPaymentMethods = ( paymentMethods ) => ( {
type: SET_REGISTERED_PAYMENT_METHODS,
paymentMethods,
} );
/**
* Used to dispatch an action for updating a registered express payment
* method in the state.
*
* @param {Object} paymentMethods Payment methods to register.
* @return {Object} An action object.
*/
export const setRegisteredExpressPaymentMethods = ( paymentMethods ) => ( {
type: SET_REGISTERED_EXPRESS_PAYMENT_METHODS,
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
/**
* Set a flag indicating that the payment method info (e.g. a payment card)
* should be saved to user account after order completion.
*
* @param {boolean} shouldSavePaymentMethod
* @return {Object} An action object.
*/
export const setShouldSavePaymentMethod = ( shouldSavePaymentMethod ) => ( {
type: SET_SHOULD_SAVE_PAYMENT_METHOD,
shouldSavePaymentMethod,
} );