PayController.php
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace app\pay\controller;
use cmf\controller\HomeBaseController;
use think\Db;
class PayController extends HomeBaseController
{
function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
if (empty(session('user.id'))) {
$this->error('登录失败');
}
}
//提交订单
public function done()
{
$request = request();
if ($request->isAjax()) {
$consignee = Db::name('zj_user_place')->field('name,province,city,county,mobile,place')
->where(['id' => $request->param('address_id'), 'uid' => session('user.id')])
->find();
$data = Db::name('zj_cart')->alias('c')
->join('zj_goods g', 'c.gid=g.id')
->field('c.num,g.price,g.id')
->where(['c.uid' => session('user.id'), 'c.id' => ['in', session('cart.id')]])
->select();
$whole = 0;
$orderGoods = [];
foreach ($data as $item) {
$whole += $item['num'] * $item['price'];
}
$order = [
'order_num' => date('YmdHis') . rand(100000, 999999),
'step' => 1,
'uid' => session('user.id'),
'site' => $consignee['province'] . $consignee['city'] . $consignee['county'] . $consignee['place'],
'name' => $consignee['name'],
'mobile' => $consignee['mobile'],
'remark' => $request->param('remark'),
'create_time' => time(),
'whole' => $whole,
'whole_num' => 0,
'pay_type' => $request->param('pay_type'),
'step' => 1
];
Db::startTrans();
if (Db::name('zj_order')->insert($order)) {
$oid = Db::name('zj_order')->getLastInsID();
foreach ($data as $k => $v) {
$orderGoods[$k] = [
'oid' => $oid,
'gid' => $v['id'],
'num' => $v['num']
];
}
if (Db::name('zj_order_goods')->insertAll($orderGoods)) {
Db::commit();
if ($order['pay_type'] == 1) {
$info = [
'attach' => $oid,
'openid' => session('openid'),
'body' => '微信支付-' . $order['name'],
'total_fee' => $order['whole']
];
$this->wxPay($info);
}
}
}
}
}
//微信支付
public function wxPay($info)
{
require_once EXTEND_PATH . '/Payment.php';
$pay = new \Payment($info['attach'], $info['openid'], $info['body'], $info['total_fee']);
$this->success('微信支付', '', $pay->pay());
}
public function notify() {
require_once EXTEND_PATH . '/Payment.php';
$pay = new \Payment();
$pay->handleNotify();
}
}