WooCommerce 文档

title: "支付存储 (wc/store/payment)" post_status: publish comment_status: open taxonomy: category: - woocommerce post_tag: - Data Store - Reference - Block Development


支付存储 (wc/store/payment)

概述

支付数据存储用于存储支付方式数据和支付处理信息。当支付状态发生变化时,数据存储会反映这些变化。

⚠️ 目前,所有操作仅供内部使用,我们正在确定哪些操作对扩展程序与此数据存储进行交互是有用的。我们不鼓励扩展程序在此数据存储上分发操作。

以下示例显示了存储在支付数据存储中的数据。此示例显示了多个支付网关处于活动状态,并且保存了一个令牌的情况。

{
    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
}

用法

要使用此存储,您需要在引用它的任何模块中导入 paymentStore StoreDescriptor。 假设 @woocommerce/block-data 已注册为指向 wc.wcBlocksData 的外部,您可以通过以下方式导入键:

const { paymentStore } = window.wc.wcBlocksData;

选择器

getState

返回支付存储的当前状态。

🚨 避免使用此选择器,应使用专门的选择器。 此选择器仅用于在单元测试中模拟选择器。

返回值

示例

const store = select( paymentStore );
const currentState = store.getState();

isPaymentIdle

查询付款状态是否为 idle (待审)。

返回值

示例

const store = select( paymentStore );
const isPaymentIdle = store.isPaymentIdle();

isExpressPaymentStarted

查询是否已点击快捷付款方式。

返回值

示例

const store = select( paymentStore );
const isExpressPaymentStarted = store.isExpressPaymentStarted();

isPaymentProcessing

查询付款状态是否为 processing (处理中)。

返回值

示例

const store = select( paymentStore );
const isPaymentProcessing = store.isPaymentProcessing();

isPaymentReady

查询状态是否为 ready

返回值

示例

const store = select( paymentStore );
const isPaymentReady = store.isPaymentReady();

hasPaymentError

查询状态是否为 error

返回值

示例

const store = select( paymentStore );
const hasPaymentError = store.hasPaymentError();

isExpressPaymentMethodActive

返回是否启用了快速付款方式。当快速付款方式打开并接收用户输入时,此值为 true。对于 Google Pay,当模态框打开时为 true,但其他付款方式可能具有不同的用户界面。

返回值

示例

const store = select( paymentStore );
const isExpressPaymentMethodActive = store.isExpressPaymentMethodActive();

getActiveSavedToken

返回当前激活的已保存令牌。客户保存到账户中的付款方式与之关联的令牌。如果选择了其中一个,则此选择器返回当前激活的令牌。如果没有选择任何令牌,则返回一个空字符串。

返回值

示例

const store = select( paymentStore );
const activeSavedToken = store.getActiveSavedToken();

getActivePaymentMethod

返回当前激活的付款方式的 ID。

返回值

示例

const store = select( paymentStore );
const activePaymentMethod = store.getActivePaymentMethod();

getAvailablePaymentMethods

返回可用的付款方式。 这不包含快速付款方式。

返回值

示例

const store = select( paymentStore );
const availablePaymentMethods = store.getAvailablePaymentMethods();

availablePaymentMethods 将如下所示:

{
    "cheque": {
        name: "cheque",
    },
    "cod": {
        name: "cod",
    },
    "bacs": {
        name: "bacs",
    },
}

getAvailableExpressPaymentMethods

返回可用的快速付款方式。

返回值

示例

const store = select( paymentStore );
const availableExpressPaymentMethods =
    store.getAvailableExpressPaymentMethods();

availableExpressPaymentMethods 的结构如下:

{
    "payment_request": {
        name: "payment_request",
    },
    "other_express_method": {
        name: "other_express_method",
    },
}

getPaymentMethodData

返回当前的付款方式数据。 每次活动付款方式发生变化时,此数据都会改变,并且不会为每个付款方式持久化。 例如,如果客户选择了 PayPal,则付款方式数据将特定于该付款方式。 如果他们切换到 Stripe,则付款方式数据将被 Stripe 覆盖,之前的价值(当选择 PayPal 时)将不再可用。

返回值

示例

const store = select( paymentStore );
const paymentMethodData = store.getPaymentMethodData();

getSavedPaymentMethods

返回当前客户的所有已保存的付款方式。

返回值

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();

getActiveSavedPaymentMethods

返回当前客户的活动已保存的付款方式,即可以用于支付当前订单的付款方式。

返回值

object - 当前客户的活动已保存的付款方式,即可以用于支付此订单的付款方式。 这是一个对象,它将特定于每个付款方式。 例如,Stripe 的已保存令牌以如下方式返回:

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();

getIncompatiblePaymentMethods

返回与结账 Block 不兼容的付款方式列表。

返回值

示例

const store = select( paymentStore );
const incompatiblePaymentMethods = store.getIncompatiblePaymentMethods();

getShouldSavePaymentMethod

返回是否应将付款方式保存到客户的账户中。

返回值

示例

const store = select( paymentStore );
const shouldSavePaymentMethod = store.getShouldSavePaymentMethod();

paymentMethodsInitialized

返回是否已初始化付款方式。

返回值

示例

const store = select( paymentStore );
const paymentMethodsInitialized = store.paymentMethodsInitialized();

expressPaymentMethodsInitialized

返回是否已初始化快捷付款方式。

返回值

boolean: 如果已初始化快捷付款方式,则返回 true,否则返回 false

示例

const store = select( paymentStore );
const expressPaymentMethodsInitialized =
    store.expressPaymentMethodsInitialized();

getPaymentResult

返回上次付款尝试的结果。

返回值

{
    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();

(@deprecated) isPaymentPristine

查询 状态 是否为 pristine

⚠️ 此选择器已过时,将在未来的版本中移除。请使用 isPaymentIdle 代替。

返回值

示例

const store = select( paymentStore );
const isPaymentPristine = store.isPaymentPristine();

(@deprecated) isPaymentStarted

查询 状态 是否为 started

⚠️ 此选择器已过时,将在未来的版本中移除。请使用 isExpressPaymentStarted 代替。

返回值

示例

const store = select( paymentStore );
const isPaymentStarted = store.isPaymentStarted();

(@deprecated) isPaymentSuccess

查询 状态 是否为 success

⚠️ 此选择器已过时,将在未来的版本中移除。请使用 isPaymentReady 代替。

返回值

示例

const store = select( paymentStore );
const isPaymentSuccess = store.isPaymentSuccess();

(@deprecated) isPaymentFailed

查询付款的状态是否为failed

⚠️ 此选择器已过时,将在未来的发布版本中被移除使用hasPaymentError代替。

返回值

示例

const store = select( paymentStore );
const isPaymentFailed = store.isPaymentFailed();

(@deprecated) getCurrentStatus

返回一个包含布尔值的对象,表示付款状态

⚠️ 此选择器已过时,将在未来的发布版本中被移除使用上面的选择器。

返回值

示例

const store = select( paymentStore );
const currentStatus = store.getCurrentStatus();