PayController.php
9.2 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace app\pay\controller;
use cmf\controller\HomeBaseController;
use think\Db;
class PayController extends HomeBaseController
{
//提交订单
public function done()
{
$request = request();
$data = [];
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();
if (session('cart.id') != null && session('goods.id') == null) {
$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();
} elseif (session('cart.id') == null && session('goods.id') != null) {
$data = Db::name('zj_goods')
->field('price,id')
->where(['id' => session('goods.id')])
->select();
} else {
$this->success('error', '', false);
}
$whole = 0;
$orderGoods = [];
foreach ($data as $item) {
if (session('cart.id') == null && session('goods.id') != null) {
$item['num'] = 1;
}
$whole += $item['num'] * $item['price'];
}
if ($request->param('pay_type') == 2) {
$whole_num = $request->param('pay_num');
} elseif ($request->param('pay_type') == 1) {
$whole_num = 0;
} else {
$whole_num = $whole;
}
$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' => $whole_num,
'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) {
if (session('cart.id') == null && session('goods.id') != null) {
$v['num'] = 1;
}
$orderGoods[$k] = [
'oid' => $oid,
'gid' => $v['id'],
'num' => $v['num']
];
}
if (Db::name('zj_order_goods')->insertAll($orderGoods)) {
//微信支付
if ($order['pay_type'] == 1) {
Db::commit();
session('goods.id', null);
Db::name('zj_cart')->where(['id' => ['in', session('cart.id')]])->delete();
session('cart.id', null);
$info = [
'attach' => $oid,
'openid' => session('user.openid'),
'body' => '微信支付-天生红商城',
'total_fee' => $order['whole']
];
$this->wxPay($info);
} //积分支付
elseif ($order['pay_type'] == 3) {
//先判断剩余积分是否足够支付
$balance = Db::name('user')->where(['id' => session('user.id')])->value('balance');
if ($balance < $whole) {
$this->success('您的积分不足', '', false);
Db::rollback();
} else {
//先减积分
$balance -= $whole;
if (Db::name('user')->update(['id' => session('user.id'), 'balance' => $balance])) {
//支付后改变订单状态
$update = [
'id' => $oid,
'step' => 2,
];
if (Db::name('zj_order')->update($update)) {
Db::commit();
session('goods.id', null);
Db::name('zj_cart')->where(['id' => ['in', session('cart.id')]])->delete();
session('cart.id', null);
$this->success('积分支付成功', url('user/center/orderList'), true);
} else {
Db::rollback();
}
} else {
Db::rollback();
}
}
} //组合支付
elseif ($order['pay_type'] == 2) {
$pay_cash = $request->param('pay_cash'); //支付的金额
$pay_num = $request->param('pay_num'); //支付的积分
$balance = Db::name('user')->where(['id' => session('user.id')])->value('balance');
if ($pay_cash > $balance) {
$this->success('您的积分不足', '', false);
} else {
if($balance < $pay_num) {
$this->success('您的积分不足', '', false);
Db::rollback();
}else {
//先减余额
$balance -= $pay_num;
Db::name('user')->update(['id' => session('user.id'), 'balance' => (int)$balance]);
Db::commit();
session('goods.id', null);
Db::name('zj_cart')->where(['id' => ['in', session('cart.id')]])->delete();
session('cart.id', null);
$info = [
'attach' => $oid,
'openid' => session('user.openid'),
'body' => '微信支付-天生红商城',
'total_fee' => $pay_cash
];
$this->wxPay($info);
}
}
} else {
$this->success('请选择支付方式', '', false);
}
} else {
Db::rollback();
$this->success('您已下过该订单', '', false);
}
} else {
Db::rollback();
$this->success('未知错误', '', false);
}
}
}
//个人中心微信支付
public function payFromCenter()
{
$request = request();
if ($request->isAjax()) {
$oid = $request->param('oid');
$total_fee = Db::name('zj_order')->where(['id' => $oid])->value('whole');
if (Db::name('zj_order')->where(['id' => $oid])->value('pay_type') == 2) {
$total_fee -= Db::name('zj_order')->where(['id' => $oid])->value('whole_num');
}
$info = [
'attach' => $oid,
'openid' => session('user.openid'),
'body' => '微信支付-天生红商城',
'total_fee' => $total_fee
];
$this->wxPay($info);
}
}
private function wxPay($info)
{
require_once EXTEND_PATH . '/Payment.php';
$pay = new \Payment($info['attach'], $info['openid'], $info['body'], $info['total_fee'] * 100);
$this->success('ok', url('user/center/orderlist'), $pay->pay());
}
public function notify()
{
require_once EXTEND_PATH . '/Payment.php';
$pay = new \Payment();
$notify = $pay->handleNotify();
if (!empty($notify)) {
$oid = $notify['attach'];
if (Db::name('zj_order')->where(['id' => $oid])->value('step') == 2) {
echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
exit();
} else {
//改变订单状态
$update = [
'id' => $oid,
'step' => 2,
'out_trade_no' => $notify['out_trade_no']
];
if (Db::name('zj_order')->update($update)) {
echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
exit();
}
}
}
}
}