> 插件接口類位置與文件名參考 設計接口與注冊接口,類名為接口標識
* 插件接口類的方法調用成功都應返回true
* 插件接口類的方法調用失敗應`throw new PaymentPluginException`
* 插件接口類應繼承payment_plugin類
* 插件接口類可直接調用csu_base插件的sql與csubase類
#### payment_plugin類
```
<?php
if (!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class payment_plugin {
public function __construct($order_id = '') {
//獲取訂單
$this->order = C::m('#payment#payment')->load_order($order_id, $order, true);
$this->order_id = $this->order['order_id'];
if ($this->order['is_refund']) {
//退款訂單獲取主訂單
$this->main_order_id = substr($this->order['subject'], 0, 20);
$this->main_order = C::m('#payment#payment')->load_order($this->main_order_id);
}
}
/**
* 退款時調用此方法可禁止單筆訂單多次退款
*/
public function disable_multi_refund() {
if ($this->main_order['amount'] != $this->order['amount']) {
throw new PaymentPluginException(lang('plugin/payment', 'order_refund_mustall'), 800);
}
}
}
?>
```
#### 插件接口類必須包含的方法
* __construct構造方法,構造方法會傳入order_id
* success支付成功方法
* refund退款方法,若不支持退款可直接`throw new PaymentPluginException('該插件不支持退款',$code)`,由于2.0支持單筆訂單多次退款,所以需要對訂單金額進行校驗。若不想支持多次退款,可在校驗訂單不一致后拋出異常
#### 插件接口類可選包含的方法
* cancel取消訂單方法,可在此做釋放訂單庫存等操作
* expire訂單超時取消方法,可在此做釋放訂單庫存等操作
#### 插件接口類demo
可參考payment插件下的payment_test.class.php
```
<?php
if (!defined('IN_DISCUZ')) {
exit('Access Denied');
}
if (!class_exists('payment_plugin')) {
include_once DISCUZ_ROOT . './source/plugin/payment/payment_plugin.class.php';
}
/**
* 由于插件接口類是被動被調用的,所以在此處可以直接使用csu_base插件的sql類與csubase類,以及本插件的payment類
* 插件接口必須包含success與refund方法,可包含cancel方法(訂單取消回調)與expire方法(訂單超時回調),可在此做釋放訂單之類操作
* 插件接口方法調用失敗請勿返回false,請throw new PaymentPluginException(string $msg,int $code,array $extra=[])
* 插件接口方法調用成功請返回true
*/
class payment_test extends payment_plugin {
public function __construct($order_id = '') {
parent::__construct($order_id);
}
public function success() {
//將false設置為true可演示調用success失敗
if (false) {
throw new PaymentPluginException('payment_success_fail_demo', 233333, ['url' => 'abc']);
/**
* 返回的error數組格式如下
* [
* 'msg'=>'payment_success_fail_demo',
* 'code'=>'233333',
* 'url'=>'abc'
* ]
**/
}
//支付后的回調
$content = [
$this->order['subject'],
lang('plugin/payment', 'order_id') . ':' . $this->order['order_id'],
lang('plugin/payment', 'amount') . ':' . payment::amount($this->order['amount']),
lang('plugin/payment', 'method_id') . ':' . payment::method_title($this->order['method_id']),
lang('plugin/payment', 'status') . ':' . payment::status_color($this->order['status']),
lang('plugin/payment', 'plugin_status') . ':' . lang('plugin/payment', 'plugin_status_' . $this->order['plugin_status']),
];
csubase::system_notice($this->order['uid'], lang('plugin/payment', 'pay_success'), implode('<br>', $content));
return true;
}
public function refund() {
//將false設置為true可演示調用refund失敗,此時傳入的order為主訂單的退款訂單,要獲取主訂單號可查看payment_plugin類,可通過調用disable_multi_refund禁止單筆訂單多次退款
//$this->disable_multi_refund();
if (false) {
throw new PaymentPluginException('method_refund_fail_demo', 233333, ['url' => 'abc']);
/**
* 返回的error數組格式如下
* [
* 'msg'=>'method_refund_fail_demo',
* 'code'=>'233333',
* 'url'=>'abc'
* ]
**/
//退款后的回調
}
$content = [
$this->order['subject'],
lang('plugin/payment', 'out_refund_id') . ':' . $this->order['order_id'],
lang('plugin/payment', 'refund_amount') . ':' . payment::amount($this->order['amount']),
lang('plugin/payment', 'status') . ':' . payment::status_color($this->order['status']),
lang('plugin/payment', 'plugin_status') . ':' . lang('plugin/payment', 'plugin_status_' . $this->order['plugin_status']),
];
csubase::system_notice($this->order['uid'], lang('plugin/payment', 'refund_success'), implode('<br>', $content));
return true;
}
public function cancel() {
//將false設置為true可演示調用success失敗
if (false) {
throw new PaymentPluginException('payment_cancel_fail_demo', 233333, ['url' => 'abc']);
/**
* 返回的error數組格式如下
* [
* 'msg'=>'payment_cancel_fail_demo',
* 'code'=>'233333',
* 'url'=>'abc'
* ]
**/
}
//取消訂單后的回調
$content = [
$this->order['subject'],
lang('plugin/payment', 'order_id') . ':' . $this->order['order_id'],
lang('plugin/payment', 'amount') . ':' . payment::amount($this->order['amount']),
lang('plugin/payment', 'method_id') . ':' . payment::method_title($this->order['method_id']),
lang('plugin/payment', 'status') . ':' . payment::status_color($this->order['status']),
lang('plugin/payment', 'plugin_status') . ':' . lang('plugin/payment', 'plugin_status_' . $this->order['plugin_status']),
];
csubase::system_notice($this->order['uid'], lang('plugin/payment', 'cancel_success'), implode('<br>', $content));
return true;
}
public function expire() {
//將false設置為true可演示調用success失敗
if (false) {
throw new PaymentPluginException('payment_expire_fail_demo', 233333, ['url' => 'abc']);
/**
* 返回的error數組格式如下
* [
* 'msg'=>'payment_expire_fail_demo',
* 'code'=>'233333',
* 'url'=>'abc'
* ]
**/
}
//取消訂單后的回調
$content = [
$this->order['subject'],
lang('plugin/payment', 'order_id') . ':' . $this->order['order_id'],
lang('plugin/payment', 'amount') . ':' . payment::amount($this->order['amount']),
lang('plugin/payment', 'method_id') . ':' . payment::method_title($this->order['method_id']),
lang('plugin/payment', 'status') . ':' . payment::status_color($this->order['status']),
lang('plugin/payment', 'plugin_status') . ':' . lang('plugin/payment', 'plugin_status_' . $this->order['plugin_status']),
];
csubase::system_notice($this->order['uid'], lang('plugin/payment', 'expire_success'), implode('<br>', $content));
return true;
}
}
?>
```
- 團隊說明
- 通用支付接口
- 支付接口申請方式
- 微信支付
- QQ錢包
- 積分支付
- 支付寶
- 開發模式與規范
- 插件接口
- 設計接口與注冊接口
- 創建訂單
- 訂單退款
- 插件接口類
- 支付接口
- 目錄結構
- 設計接口
- 編輯設置項
- 異常類
- 日志
- 數據字典
- 1.0
- 支付接口接入
- 插件接口接入
- 數據字典
- 支付接口表payment
- 插件接口表payment_api
- 訂單表payment_pay
- 支付流程
- 擔保中介交易系統
- 交易流程
- 交易參與者聯系資料
- 多平臺機器人
- 組件設置
- 日志
- 常見問題
- 【QQ機器人】CQHTTP
- 安裝流程
- 常見錯誤
- 開發文檔
- 開發模式
- 組件
- 內部組件
- 外部組件
- 機器人
- 插件調用機器人接口
- 威客任務
- 啟用版塊
- 擔保中介交易
- 珊瑚發帖文本內容安全云校驗
- 珊瑚圖片內容安全云校驗
- 實名認證支付寶
- 支付寶應用申請-公鑰
- 支付寶應用申請-公鑰證書