WooCommerce 集成支付宝支付功能:添加新的支付网关
如何向 WooCommerce 添加新的支付方式。
创建一个简单的支付网关
WooCommerce 中的支付网关是基于类的,可以通过插件添加。在插件中,加载插件后,您需要创建一个类,例如:
add_action( 'plugins_loaded', 'init_your_gateway_class' );
并且您的类必须继承 WooCommerce 网关的基类,然后您可以使用 WooCommerce 提供的设置 API 和其他方法:
function init_your_gateway_class() {class WC_Gateway_Your_Gateway extends WC_Payment_Gateway {}}
除了您的类的定义之外,您还需要告知 WooCommerce 该类的存在。您可以通过添加过滤器来实现此目的:
function add_your_gateway_class( $methods ) {$methods[] = 'WC_Gateway_Your_Gateway'; return $methods;}add_filter( 'woocommerce_payment_gateways', 'add_your_gateway_class' );
要在类中实现的方法
大多数方法继承自 WC_Payment_Gateway 类,但某些方法需要在您自己的网关中实现。
__construct()
在construct方法中必须定义以下变量:
- $this->id:自己网关的唯一ID,如'your_gateway'
- $this->icon :如果您想在网关名称旁边显示图片,请输入图片的 URL
- $this->has_fields:boolean,如果您希望付款字段出现在结帐页面上,请设置为 true
- $ this->method_title:支付方式标题,显示在管理页面
- $this->method_description:显示在管理页面的支付方式描述
设置字段必须在构造方法中定义并加载:
$this->init_form_fields();
$this->init_settings();
调用init_settings()
之后,您可以获取设置并将其存储在变量中,例如:
$this->title = $this->get_option( 'title' );
最后您需要为您的设置添加保存钩子:
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
init_form_fields()
此方法用于设置$this->form_fields
,这是您在自己的网关设置页面上显示的选项。此方法要求您使用 WC 设置 API ([2])。
您自己的网关的基本设置应包括启用、标题和描述:
$this->form_fields = array('enabled' => array('title' => __( 'Enable/Disable', 'woocommerce' ),'type' => 'checkbox','label' => __( 'Enable Cheque Payment', 'woocommerce' ),'default' => 'yes'),'title' => array('title' => __( 'Title', 'woocommerce' ),'type' => 'text','description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),'default' => __( 'Cheque Payment', 'woocommerce' ),'desc_tip' => true,),'description' => array('title' => __( 'Customer Message', 'woocommerce' ),'type' => 'textarea','default' => '')
);
process_ payment( $order_id )
这是网关最重要的部分 - 处理付款和订单。此方法还告诉 WC 将用户重定向到哪里,这是通过返回的数组完成的。
以下以 Check 转换的 process_function 方法为例:
function process_payment( $order_id ) {global $woocommerce;$order = new WC_Order( $order_id );// Mark as on-hold (we're awaiting the cheque)$order->update_status('on-hold', __( 'Awaiting cheque payment', 'woocommerce' ));// Reduce stock levels$order->reduce_order_stock();// Remove cart$woocommerce->cart->empty_cart();// Return thankyou redirectreturn array('result' => 'success','redirect' => $this->get_return_url( $order ));
}
该方法执行:
- 获取并更新处理中的订单
- 减少库存并清理购物车
- 成功退货并重定向 URL(在本例中为带有感谢信息的页面)
支票会暂停订单,因为无法自动验证付款。如果您正在构建直接网关,则必须在此处完成订单。当订单支付时,应该使用 payment_complete 而不是 update_status:
$order->payment_complete();
这可以确保库存确实减少了,并且余额是正确的值。
如果付款失败,您必须提出错误并返回 null:
wc_add_notice( __('Payment error:', 'woothemes') . $error_message, 'error' );
return;
WooCommerce 将捕获此错误并将其显示在结帐页面上。
更新订单状态
更新订单状态可以使用 Orders 类中的方法来实现。自定义状态更新的示例如下:
$order = new WC_Order( $order_id );
$order->update_status('on-hold', __('Awaiting cheque payment', 'woothemes'));
上面的示例将订单状态更新为“待处理”,并添加警告消息以通知客户他们正在等待检查。不再更新订单状态时也可以添加备注:
$order->add_order_note( __('IPN payment completed', 'woothemes') );
订单状态实用建议
- 如果订单已完成但管理员需要手动验证付款,请使用待处理
- 如果订单失败且已创建,将其设置为 Failed
- 如果订单已完成,让 WooCommerce 处理状态并使用
$order-> payment_complete()
。 WooCommerce 将使用“已完成”或“正在处理”状态并处理库存。
参考
[1]支付网关API
[2]设置API
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。