thinkphp5.0支付寶在線支付下單整個流程,包括創建訂單、支付成功回調更新訂單狀態、最終跳轉到商戶訂單詳情頁
查看演示
下載資源:
17
次 下載資源
下載積分:
998
積分
支付寶在線支付控制器代碼 public function alipay() {//發起支付寶支付
$order_no = date("YmdHis") . rand(100000, 999999);
if (request()->isPost()) { //支付表單提交,并喚起支付寶在線支付
//調用 application\index\model\Pay.php
$Pay = new Pay;
$result = $Pay->alipay([
'notify_url' => request()->domain() . url('index/index/alipay_notify'),
'return_url' => request()->domain() . url('index/index/alipay_return')."?order_no=".$order_no."&",
'out_trade_no' => input('post.orderid/s', '', 'trim,strip_tags'),
'subject' => input('post.subject/s', '', 'trim,strip_tags'),
'total_fee' => input('post.total_fee/f'), //訂單金額,單位為元
'body' => input('post.body/s', '', 'trim,strip_tags'),
]);
if (!$result['code']) {
return $this->error($result['msg']);
}
return $result['msg'];
}
//創建訂單
db('order_sucaihuo')->insert(array(
'order_no' => $order_no,
'order_money' => 0.1, //訂單金額
'state' => 0, //支付狀態 0 未支付, 1已支付
'uid' => 1, //用戶uid
'addtime' => time(), //下單時間
'update_time' => 0 //支付時間
));
$this->view->orderid = $order_no;
return $this->fetch();
}
//支付寶客戶端會每隔一段時間請求一次
public function alipay_notify() {//異步訂單通知
$Pay = new Pay;
$result = $Pay->notify_alipay();
if ($result == 'success') {
$pay_info = $_REQUEST;
$order_no = $pay_info['out_trade_no'];
$order_info = db('order_sucaihuo')->where('order_no', $order_no)->find();
//若是未付款則更新
if ($order_info['state'] == 0) {
$data['trade_no'] = $pay_info['trade_no'];
$data['state'] = 1;
$data['update_time'] = time();
db('order_sucaihuo')->where("order_no", $order_no)->update($data);
}
}
//測試支付回調,linux記得開啟777寫入權限
file_put_contents("notify.txt", $result);
file_put_contents("request.txt", json_encode($_REQUEST));
}
壓縮包有訂單表 CREATE TABLE IF NOT EXISTS `order_sucaihuo` (
`id` int(11) unsigned NOT NULL,
`uid` int(11) NOT NULL,
`order_no` varchar(30) NOT NULL,
`trade_no` varchar(150) DEFAULT NULL COMMENT '交易號',
`order_money` decimal(10,2) DEFAULT '0.00',
`state` int(2) NOT NULL DEFAULT '0',
`addtime` int(10) NOT NULL,
`update_time` int(10) DEFAULT '0'
) ENGINE=MyISAM AUTO_INCREMENT=6718 DEFAULT CHARSET=utf8;