SuperAPI AI 工具集

woocommerce-payments

编程与开发

查看项目

更新于 10-16 03:17

技能介绍

用途:用于在 WooCommerce 中开发支付网关、结账集成与支付处理,覆盖经典结账和区块结账(WooCommerce 8.3 起默认)两种形态。

主要能力:通过 woocommerce_payment_gateways 过滤器注册网关类;继承 WC_Payment_Gateway 并实现 process_payment(),可选实现 process_refund();支持的产品能力包括 products、refunds、subscriptions、tokenization、pre-orders。核心方法涵盖 __construct()(设置 $this->id、$this->supports,调用 init_form_fields() 与 init_settings())、init_form_fields()(后台设置项)、process_payment( $order_id )(成功返回 ['result' => 'success', 'redirect' => $url],失败返回 ['result' => 'failure'])、process_refund( $order_id, $amount, $reason )(返回 true 或 WP_Error)、validate_fields()(校验结账字段,出错时调用 wc_add_notice())。

支付流程:客户点击下单后依次执行 validate_fields()、process_payment(),其中调用外部 API,再执行 $order->payment_complete()、清空购物车 WC()->cart->empty_cart(),并返回跳转到感谢页。令牌化可继承 WC_Payment_Gateway_CC 或 WC_Payment_Gateway_eCheck,并在 $this->supports 中加入 tokenization 并实现 add_payment_method()。

区块结账:需与经典网关分开注册。服务端继承 AbstractPaymentMethodType 并实现 initialize()、is_active()、get_payment_method_script_handles()、get_payment_method_data(),通过 woocommerce_blocks_payment_method_type_registration 钩子调用 $registry->register()。客户端使用 wc.wcBlocksRegistry 的 registerPaymentMethod 注册名称、标签、内容、canMakePayment、ariaLabel 与 supports.features。另需通过 FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', FILE, true ) 声明兼容性。

适用场景:为 WooCommerce 商店接入自定义或第三方支付渠道,支持退款、保存卡、订阅与预购等场景。

限制与常见错误:仅实现经典结账而不注册 AbstractPaymentMethodType 会导致区块结账不可用;未声明 cart_checkout_blocks 兼容性;缺少 validate_fields();未调用 $order->payment_complete()(影响订单状态与邮件);未清空购物车;硬编码货币或金额格式而非使用 $order->get_total() 与 WC 格式化函数。相关技能包括 woocommerce-setup、woocommerce-data、woocommerce-hooks。具体 API 端点、费用与价格信息暂未提供。