2023-10-26 02:37:13 +00:00
# Payment Store (`wc/store/payment`) <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
## Table of Contents <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
- [Overview ](#overview )
2023-10-26 02:37:13 +00:00
- [Usage ](#usage )
2022-10-12 16:04:31 +00:00
- [Selectors ](#selectors )
2023-10-26 02:37:13 +00:00
- [isPaymentIdle ](#ispaymentidle )
- [isExpressPaymentStarted ](#isexpresspaymentstarted )
- [isPaymentProcessing ](#ispaymentprocessing )
- [isPaymentReady ](#ispaymentready )
- [hasPaymentError ](#haspaymenterror )
2022-10-12 16:04:31 +00:00
- [isExpressPaymentMethodActive ](#isexpresspaymentmethodactive )
- [getActiveSavedToken ](#getactivesavedtoken )
- [getActivePaymentMethod ](#getactivepaymentmethod )
- [getAvailablePaymentMethods ](#getavailablepaymentmethods )
- [getAvailableExpressPaymentMethods ](#getavailableexpresspaymentmethods )
- [getPaymentMethodData ](#getpaymentmethoddata )
- [getSavedPaymentMethods ](#getsavedpaymentmethods )
- [getActiveSavedPaymentMethods ](#getactivesavedpaymentmethods )
- [getShouldSavePaymentMethod ](#getshouldsavepaymentmethod )
- [paymentMethodsInitialized ](#paymentmethodsinitialized )
- [expressPaymentMethodsInitialized ](#expresspaymentmethodsinitialized )
2023-10-26 02:37:13 +00:00
- [getPaymentResult ](#getpaymentresult )
2023-11-15 11:19:39 +00:00
- [(@deprecated) isPaymentPristine ](#deprecated-ispaymentpristine )
- [(@deprecated) isPaymentStarted ](#deprecated-ispaymentstarted )
- [(@deprecated) isPaymentSuccess ](#deprecated-ispaymentsuccess )
- [(@deprecated) isPaymentFailed ](#deprecated-ispaymentfailed )
- [(@deprecated) getCurrentStatus ](#deprecated-getcurrentstatus )
2022-10-12 16:04:31 +00:00
## Overview
2023-10-26 02:37:13 +00:00
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.
2022-10-12 16:04:31 +00:00
2023-11-15 11:19:39 +00:00
**⚠️ 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.
```js
{
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
}
```
2023-10-26 02:37:13 +00:00
## Usage
To utilize this store you will import the `PAYMENT_STORE_KEY` in any module referencing it. Assuming `@woocommerce/block-data` is registered as an external pointing to `wc.wcBlocksData` you can import the key via:
```js
2023-11-03 16:30:19 +00:00
const { PAYMENT_STORE_KEY } = window.wc.wcBlocksData;
2023-10-26 02:37:13 +00:00
```
2022-10-12 16:04:31 +00:00
## Selectors
2023-02-14 12:08:19 +00:00
### isPaymentIdle
2023-11-15 11:19:39 +00:00
Queries if the status is `idle` .
2023-02-14 12:08:19 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2023-02-14 12:08:19 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : True if the payment status is `idle` , false otherwise.
2023-02-14 12:08:19 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2023-02-14 12:08:19 +00:00
```js
const store = select( 'wc/store/payment' );
const isPaymentIdle = store.isPaymentIdle();
```
### isExpressPaymentStarted
Queries if an express payment method has been clicked.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2023-02-14 12:08:19 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : True if the button for an express payment method has been clicked, false otherwise.
2023-02-14 12:08:19 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2023-02-14 12:08:19 +00:00
```js
const store = select( 'wc/store/payment' );
2023-10-26 02:37:13 +00:00
const isExpressPaymentStarted = store.isExpressPaymentStarted();
2023-02-14 12:08:19 +00:00
```
2022-11-15 12:27:39 +00:00
### isPaymentProcessing
Queries if the status is `processing` .
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-11-15 12:27:39 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : True if the payment status is `processing` , false otherwise.
2022-11-15 12:27:39 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-11-15 12:27:39 +00:00
```js
const store = select( 'wc/store/payment' );
const isPaymentProcessing = store.isPaymentProcessing();
```
2023-02-14 12:08:19 +00:00
### isPaymentReady
2022-11-15 12:27:39 +00:00
2023-02-14 12:08:19 +00:00
Queries if the status is `ready` .
2022-11-15 12:27:39 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-11-15 12:27:39 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : True if the payment status is `ready` , false otherwise.
2022-11-15 12:27:39 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-11-15 12:27:39 +00:00
```js
const store = select( 'wc/store/payment' );
2023-02-14 12:08:19 +00:00
const isPaymentReady = store.isPaymentReady();
2022-11-15 12:27:39 +00:00
```
2023-02-14 12:08:19 +00:00
### hasPaymentError
2022-11-15 12:27:39 +00:00
2023-02-14 12:08:19 +00:00
Queries if the status is `error` .
2022-11-15 12:27:39 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-11-15 12:27:39 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : True if the payment status is `error` , false otherwise.
2022-11-15 12:27:39 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-11-15 12:27:39 +00:00
```js
const store = select( 'wc/store/payment' );
2023-02-14 12:08:19 +00:00
const hasPaymentError = store.hasPaymentError();
2022-11-15 12:27:39 +00:00
```
2022-10-12 16:04:31 +00:00
### isExpressPaymentMethodActive
2023-10-26 02:37:13 +00:00
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.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : Whether an express payment method is active.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
```js
const store = select( 'wc/store/payment' );
const isExpressPaymentMethodActive = store.isExpressPaymentMethodActive();
```
### getActiveSavedToken
2023-10-26 02:37:13 +00:00
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.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
- `string` : The active saved token ID, or empty string if a saved token is not selected.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
```js
const store = select( 'wc/store/payment' );
const activeSavedToken = store.getActiveSavedToken();
```
### getActivePaymentMethod
Returns the active payment method's ID.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
- `string` : The active payment method's ID.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
```js
const store = select( 'wc/store/payment' );
const activePaymentMethod = store.getActivePaymentMethod();
```
### getAvailablePaymentMethods
Returns the available payment methods. This does not include express payment methods.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
- `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.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
```js
const store = select( 'wc/store/payment' );
const availablePaymentMethods = store.getAvailablePaymentMethods();
```
`availablePaymentMethods` will look like this:
```js
{
"cheque": {
name: "cheque",
},
"cod": {
name: "cod",
},
"bacs": {
name: "bacs",
},
}
```
### getAvailableExpressPaymentMethods
Returns the available express payment methods.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
- `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.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
```js
const store = select( 'wc/store/payment' );
2022-11-10 16:15:31 +00:00
const availableExpressPaymentMethods =
store.getAvailableExpressPaymentMethods();
2022-10-12 16:04:31 +00:00
```
`availableExpressPaymentMethods` will look like this:
```js
{
"payment_request": {
name: "payment_request",
},
"other_express_method": {
name: "other_express_method",
},
}
```
### getPaymentMethodData
2023-10-26 02:37:13 +00:00
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.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
- `object` : The current payment method data. This is specific to each payment method so further details cannot be provided here.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
```js
const store = select( 'wc/store/payment' );
const paymentMethodData = store.getPaymentMethodData();
```
### getSavedPaymentMethods
Returns all saved payment methods for the current customer.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
- `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:
2022-10-12 16:04:31 +00:00
```js
savedPaymentMethods: {
2022-11-10 16:15:31 +00:00
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,
},
];
2022-10-12 16:04:31 +00:00
}
```
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
```js
const store = select( 'wc/store/payment' );
const savedPaymentMethods = store.getSavedPaymentMethods();
```
### getActiveSavedPaymentMethods
2023-10-26 02:37:13 +00:00
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.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
`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:
2022-10-12 16:04:31 +00:00
```js
activeSavedPaymentMethods: {
2022-11-10 16:15:31 +00:00
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,
},
];
2022-10-12 16:04:31 +00:00
}
```
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
```js
const store = select( 'wc/store/payment' );
const activeSavedPaymentMethods = store.getActiveSavedPaymentMethods();
```
2022-10-12 16:04:31 +00:00
### getShouldSavePaymentMethod
Returns whether the payment method should be saved to the customer's account.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : True if the payment method should be saved, false otherwise.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
```js
const store = select( 'wc/store/payment' );
const shouldSavePaymentMethod = store.getShouldSavePaymentMethod();
```
### paymentMethodsInitialized
Returns whether the payment methods have been initialized.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : True if the payment methods have been initialized, false otherwise.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
```js
const store = select( 'wc/store/payment' );
const paymentMethodsInitialized = store.paymentMethodsInitialized();
```
### expressPaymentMethodsInitialized
Returns whether the express payment methods have been initialized.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
`boolean` : True if the express payment methods have been initialized, false otherwise.
2022-10-12 16:04:31 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-10-12 16:04:31 +00:00
```js
const store = select( 'wc/store/payment' );
2022-11-10 16:15:31 +00:00
const expressPaymentMethodsInitialized =
store.expressPaymentMethodsInitialized();
2022-10-12 16:04:31 +00:00
```
2022-11-18 12:13:00 +00:00
### getPaymentResult
2023-11-15 11:19:39 +00:00
Returns the result of the last payment attempt.
2022-11-18 12:13:00 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-11-18 12:13:00 +00:00
2023-10-26 02:37:13 +00:00
- `object` : An object with the following properties:
2022-11-18 12:13:00 +00:00
```ts
{
message: string;
paymentStatus: 'success' | 'failure' | 'pending' | 'error' | 'not set';
paymentDetails: Record< string , string > | Record< string , never > ;
redirectUrl: string;
}
```
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
2022-11-18 12:13:00 +00:00
```js
const store = select( 'wc/store/payment' );
2023-10-26 02:37:13 +00:00
const paymentResult = store.getPaymentResult();
2022-11-18 12:13:00 +00:00
```
2023-11-15 11:19:39 +00:00
### (@deprecated) isPaymentPristine
Queries if the status is `pristine` .
> ⚠️ This selector is deprecated and will be removed in a future release. Please use `isPaymentIdle` instead.
#### _Returns_ <!-- omit in toc -->
- `boolean` : True if the payment status is `pristine` , false otherwise.
#### _Example_ <!-- omit in toc -->
```js
const store = select( 'wc/store/payment' );
const isPaymentPristine = store.isPaymentPristine();
```
### (@deprecated) isPaymentStarted
Queries if the status is `started` .
> ⚠️ This selector is deprecated and will be removed in a future release. Please use `isExpressPaymentStarted` instead.
#### _Returns_ <!-- omit in toc -->
- `boolean` : True if the payment status is `started` , false otherwise.
#### _Example_ <!-- omit in toc -->
```js
const store = select( 'wc/store/payment' );
const isPaymentStarted = store.isPaymentStarted();
```
### (@deprecated) isPaymentSuccess
Queries if the status is `success` .
> ⚠️ This selector is deprecated and will be removed in a future release. Please use `isPaymentReady` instead.
#### _Returns_ <!-- omit in toc -->
- `boolean` : True if the payment status is `success` , false otherwise.
#### _Example_ <!-- omit in toc -->
```js
const store = select( 'wc/store/payment' );
const isPaymentSuccess = store.isPaymentSuccess();
```
### (@deprecated) isPaymentFailed
Queries if the status is `failed` .
> ⚠️ This selector is deprecated and will be removed in a future release. Please use `hasPaymentError` instead.
#### _Returns_ <!-- omit in toc -->
- `boolean` : True if the payment status is `failed` , false otherwise.
#### _Example_ <!-- omit in toc -->
```js
const store = select( 'wc/store/payment' );
const isPaymentFailed = store.isPaymentFailed();
```
### (@deprecated) getCurrentStatus
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.
#### _Returns_ <!-- omit in toc -->
- `object` : The current payment status with the following keys:
- _isPristine_ `boolean` : True if the payment process has not started, does not have an error and has not finished. This is true initially.
- _isStarted_ `boolean` : True if the payment process has started.
- _isProcessing_ `boolean` : True if the payment is processing.
- _hasError_ `boolean` : True if the payment process has resulted in an error.
- _hasFailed_ `boolean` : True if the payment process has failed.
- _isSuccessful_ `boolean` : True if the payment process is successful.
- _isDoingExpressPayment_ `boolean` : True if an express payment method is active, false otherwise
#### _Example_ <!-- omit in toc -->
```js
const store = select( 'wc/store/payment' );
const currentStatus = store.getCurrentStatus();
```
2022-10-12 16:04:31 +00:00
<!-- FEEDBACK -->
---
[We're hiring! ](https://woocommerce.com/careers/ ) Come work with us!
2022-11-10 08:52:04 +00:00
🐞 Found a mistake, or have a suggestion? [Leave feedback about this document here. ](https://github.com/woocommerce/woocommerce-blocks/issues/new?assignees=&labels=type%3A+documentation&template=--doc-feedback.md&title=Feedback%20on%20./docs/third-party-developers/extensibility/data-store/payment.md )
2022-10-12 16:04:31 +00:00
<!-- /FEEDBACK -->