前面一篇博客介紹了支付寶開放平台上如何創建應用,以及相應的 SDK 中提供的接口。
這篇博客介紹一下 WooCommerce 中如何添加新的支付方式。
創建一個簡單的支付網關
WooCommerce 中的支付網關是基於類的,可以通過插件的方式添加。在插件裡面,需要在插件加載後創建一個類,比如:
|
add_action( ‘plugins_loaded’, ‘init_your_gateway_class’ );
|
並且您的類需要繼承自 WooCommerce 網關基類,然後您才可以使用設置 API 以及其他一些 WooCommerce 提供的方法:
|
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()
在構造方法中,應該定義以下的變量:
- $this->id:自己的網關的唯一 ID,比如’your_gateway’
- $this->icon:如果您想在前端網關名稱的旁邊顯示一張圖片,輸入圖片的 URL
- $this->has_fields:布爾值,如果您想要支付域在結算頁面顯示的話,設置成 true
- $this->method_title:在 admin 頁面顯示的支付方法的標題
- $this->method_description:在 admin 頁面顯示的支付方法的描述
構造方法中應該定義並加載設置域:
|
$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 Settings API([2])。
自己網關的基本設置應該包含 enabled,title 和 description:
|
$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 重定向用戶到哪,這是通過返回的 array 實現的。
下面以 Cheque 網關的 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 redirect
return array(
‘result’ => ‘success’,
‘redirect’ => $this->get_return_url( $order )
);
}
|
這個方法實現了:
- 獲取並更新處理中的訂單
- 減少庫存並清空購物車
- 返回 success 和重定向 URL(這個例子中是感謝頁面)
Cheque 給訂單一個 On-Hold 狀態,因為支付不能被自動驗證。如果您在構建一個 direct 網關,那麼您應該在這裡完成訂單。在訂單被支付之後,應該使用 payment_complete 而不是 update_status:
|
$order->payment_complete();
|
這可以確保庫存確實減少並且狀態是正確的值。
如果支付失敗,您應該拋出一個錯誤並且返回 null:
|
wc_add_notice( __(‘Payment error:’, ‘woothemes’) . $error_message, ‘error’ );
return;
|
WooCommerce 會捕獲這個錯誤並在結算頁面中顯示。
更新訂單狀態
更新訂單狀態可以通過使用訂單類中的方法實現。更新到一個定製狀態的例子如下:
|
$order = new WC_Order( $order_id );
$order->update_status(‘on-hold’, __(‘Awaiting cheque payment’, ‘woothemes’));
|
上面的例子把訂單狀態更新為 On-Hold,並且添加一個注意消息通知顧客正在等待一個 Cheque。您也可以不再更新訂單狀態的時候添加註意消息:
|
$order->add_order_note( __(‘IPN payment completed’, ‘woothemes’) );
|
訂單狀態實踐建議
- 如果訂單完成但是 admin 需要人工驗證支付,使用 On-Hold
- 如果訂單失敗並且已經創建,設置為 Failed
- 如果訂單完成,讓 WooCommerce 處理狀態並且使用
$order->payment_complete()
. WooCommerce 會使用 Completed 或者 Processing 狀態並處理庫存。
參考
[1] Payment Gateway API
[2] Settings API
原文來自:victoriawy.com 感謝作者提供又一個支付寶解決方案。