docs/block-development/reference/data-store/payment.md
wc/store/payment)The payment data store is used to store payment method data and payment processing information. When the payment status changes, the data store will reflect this.
⚠️ Currently, all the actions are internal-only while we determine which ones will be useful for extensions to interact with. We do not encourage extensions to dispatch actions onto this data store yet.
An example of data held within the Payment Data Store is shown below. This example shows the state with several Payment Gateways active, and a saved token.
{
status: 'idle',
activePaymentMethod: 'stripe',
activeSavedToken: '1',
availablePaymentMethods: {
bacs: {
name: 'bacs'
},
cheque: {
name: 'cheque'
},
cod: {
name: 'cod'
},
stripe: {
name: 'stripe'
}
},
availableExpressPaymentMethods: {
payment_request: {
name: 'payment_request'
}
},
savedPaymentMethods: {
cc: [
{
method: {
gateway: 'stripe',
last4: '4242',
brand: 'Visa'
},
expires: '12/32',
is_default: true,
actions: {
'delete': {
url: 'https://store.local/checkout/delete-payment-method/1/?_wpnonce=123456',
name: 'Delete'
}
},
tokenId: 1
}
]
},
paymentMethodData: {
token: '1',
payment_method: 'stripe',
'wc-stripe-payment-token': '1',
isSavedToken: true
},
paymentResult: null,
paymentMethodsInitialized: true,
expressPaymentMethodsInitialized: true,
shouldSavePaymentMethod: false
}
To utilize this store you will import the paymentStore StoreDescriptor in any module referencing it. Assuming @woocommerce/block-data is registered as an external pointing to wc.wcBlocksData you can import the key via:
const { paymentStore } = window.wc.wcBlocksData;
Returns the current state of the payment store.
🚨 Instead of using this selector, the focused selectors should be used. This selector should only be used to mock selectors in our unit tests.
object: The current state of the payment store with the following properties:
string: The current status of the payment process. Possible values are: idle, started, processing, ready, error, success, failed.string: The ID of the active payment method.string: The ID of the active saved token.object: The available payment methods. This is currently just an object keyed by the payment method IDs. Each member contains a name entry with the payment method ID as its value.object: The available express payment methods. This is currently just an object keyed by the payment method IDs. Each member contains a name entry with the payment method ID as its value.object: The saved payment methods for the current customer. This is an object, it will be specific to each payment method. As an example, Stripe's saved tokens are returned like so:object: The current payment method data. This is specific to each payment method so further details cannot be provided here.object: An object with the following properties:
- message string: The message returned by the payment gateway.
- paymentStatus string: The status of the payment. Possible values are: success, failure, pending, error, not set.
- paymentDetails object: The payment details returned by the payment gateway.
- redirectUrl string: The URL to redirect to after checkout is complete.boolean: True if the payment methods have been initialized, false otherwise.boolean: True if the express payment methods have been initialized, false otherwise.boolean: True if the payment method should be saved, false otherwise.const store = select( paymentStore );
const currentState = store.getState();
Queries if the status is idle.
boolean: True if the payment status is idle, false otherwise.const store = select( paymentStore );
const isPaymentIdle = store.isPaymentIdle();
Queries if an express payment method has been clicked.
boolean: True if the button for an express payment method has been clicked, false otherwise.const store = select( paymentStore );
const isExpressPaymentStarted = store.isExpressPaymentStarted();
Queries if the status is processing.
boolean: True if the payment status is processing, false otherwise.const store = select( paymentStore );
const isPaymentProcessing = store.isPaymentProcessing();
Queries if the status is ready.
boolean: True if the payment status is ready, false otherwise.const store = select( paymentStore );
const isPaymentReady = store.isPaymentReady();
Queries if the status is error.
boolean: True if the payment status is error, false otherwise.const store = select( paymentStore );
const hasPaymentError = store.hasPaymentError();
Returns whether an express payment method is active, this will be true when the express payment method is open and taking user input. In the case of Google Pay it is when the modal is open but other payment methods may have different UIs.
boolean: Whether an express payment method is active.const store = select( paymentStore );
const isExpressPaymentMethodActive = store.isExpressPaymentMethodActive();
Returns the active saved token. Payment methods that customers have saved to their account have tokens associated with them. If one of these is selected then this selector returns the token that is currently active. If one is not selected this will return an empty string.
string: The active saved token ID, or empty string if a saved token is not selected.const store = select( paymentStore );
const activeSavedToken = store.getActiveSavedToken();
Returns the active payment method's ID.
string: The active payment method's ID.const store = select( paymentStore );
const activePaymentMethod = store.getActivePaymentMethod();
Returns the available payment methods. This does not include express payment methods.
object: The available payment methods. This is currently just an object keyed by the payment method IDs. Each member contains a name entry with the payment method ID as its value.const store = select( paymentStore );
const availablePaymentMethods = store.getAvailablePaymentMethods();
availablePaymentMethods will look like this:
{
"cheque": {
name: "cheque",
},
"cod": {
name: "cod",
},
"bacs": {
name: "bacs",
},
}
Returns the available express payment methods.
object: The available express payment methods. This is currently just an object keyed by the payment method IDs. Each member contains a name entry with the payment method ID as its value.const store = select( paymentStore );
const availableExpressPaymentMethods =
store.getAvailableExpressPaymentMethods();
availableExpressPaymentMethods will look like this:
{
"payment_request": {
name: "payment_request",
},
"other_express_method": {
name: "other_express_method",
},
}
Returns the current payment method data. This will change every time the active payment method changes and is not persisted for each payment method. For example, if the customer has PayPal selected, the payment method data will be specific to that payment method. If they switch to Stripe, the payment method data will be overwritten by Stripe, and the previous value (when PayPal was selected) will not be available anymore.
object: The current payment method data. This is specific to each payment method so further details cannot be provided here.const store = select( paymentStore );
const paymentMethodData = store.getPaymentMethodData();
Returns all saved payment methods for the current customer.
object: The saved payment methods for the current customer. This is an object, it will be specific to each payment method. As an example, Stripe's saved tokens are returned like so:savedPaymentMethods: {
cc: [
{
method: {
gateway: 'stripe',
last4: '4242',
brand: 'Visa',
},
expires: '04/24',
is_default: true,
actions: {
wcs_deletion_error: {
url: '#choose_default',
name: 'Delete',
},
},
tokenId: 2,
},
];
}
const store = select( paymentStore );
const savedPaymentMethods = store.getSavedPaymentMethods();
Returns the saved payment methods for the current customer that are active, i.e. the ones that can be used to pay for the current order.
object - The saved payment methods for the current customer that are active, i.e. the ones that can be used to pay for this order. This is an object, it will be specific to each payment method. As an example, Stripe's saved tokens are returned like so:
activeSavedPaymentMethods: {
cc: [
{
method: {
gateway: 'stripe',
last4: '4242',
brand: 'Visa',
},
expires: '04/24',
is_default: true,
actions: {
wcs_deletion_error: {
url: '#choose_default',
name: 'Delete',
},
},
tokenId: 2,
},
];
}
const store = select( paymentStore );
const activeSavedPaymentMethods = store.getActiveSavedPaymentMethods();
Returns the list of payment methods that are incompatible with Checkout block.
object: A list of incompatible payment methods with the following properties, or an empty object if no payment or express payment methods have been initialized:
string: The name of the payment method.const store = select( paymentStore );
const incompatiblePaymentMethods = store.getIncompatiblePaymentMethods();
Returns whether the payment method should be saved to the customer's account.
boolean: True if the payment method should be saved, false otherwise.const store = select( paymentStore );
const shouldSavePaymentMethod = store.getShouldSavePaymentMethod();
Returns whether the payment methods have been initialized.
boolean: True if the payment methods have been initialized, false otherwise.const store = select( paymentStore );
const paymentMethodsInitialized = store.paymentMethodsInitialized();
Returns whether the express payment methods have been initialized.
boolean: True if the express payment methods have been initialized, false otherwise.
const store = select( paymentStore );
const expressPaymentMethodsInitialized =
store.expressPaymentMethodsInitialized();
Returns the result of the last payment attempt.
object: An object with the following properties:{
message: string;
paymentStatus: 'success' | 'failure' | 'pending' | 'error' | 'not set';
paymentDetails: Record< string, string > | Record< string, never >;
redirectUrl: string;
}
const store = select( paymentStore );
const paymentResult = store.getPaymentResult();
Queries if the status is pristine.
⚠️ This selector is deprecated and will be removed in a future release. Please use
isPaymentIdleinstead.
boolean: True if the payment status is pristine, false otherwise.const store = select( paymentStore );
const isPaymentPristine = store.isPaymentPristine();
Queries if the status is started.
⚠️ This selector is deprecated and will be removed in a future release. Please use
isExpressPaymentStartedinstead.
boolean: True if the payment status is started, false otherwise.const store = select( paymentStore );
const isPaymentStarted = store.isPaymentStarted();
Queries if the status is success.
⚠️ This selector is deprecated and will be removed in a future release. Please use
isPaymentReadyinstead.
boolean: True if the payment status is success, false otherwise.const store = select( paymentStore );
const isPaymentSuccess = store.isPaymentSuccess();
Queries if the status is failed.
⚠️ This selector is deprecated and will be removed in a future release. Please use
hasPaymentErrorinstead.
boolean: True if the payment status is failed, false otherwise.const store = select( paymentStore );
const isPaymentFailed = store.isPaymentFailed();
Returns an object with booleans representing the payment status.
⚠️ This selector is deprecated and will be removed in a future release. Please use the selectors above.
object: The current payment status with the following keys:
boolean: True if the payment process has not started, does not have an error and has not finished. This is true initially.boolean: True if the payment process has started.boolean: True if the payment is processing.boolean: True if the payment process has resulted in an error.boolean: True if the payment process has failed.boolean: True if the payment process is successful.boolean: True if an express payment method is active, false otherwiseconst store = select( paymentStore );
const currentStatus = store.getCurrentStatus();