Pay.php
10.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace app\api\controller;
use app\admin\model\PayMember;
use app\admin\model\ToBalance;
use app\common\controller\Api;
use EasyWeChat\Factory;
use function GuzzleHttp\Promise\inspect;
use think\Db;
use app\admin\model\User;
use think\Log;
use fast\Http;
use think\Validate;
use Exception;
use function EasyWeChat\Kernel\Support\generate_sign;
/**
* 支付接口
*/
class Pay extends Api
{
protected $noNeedLogin = ['payMemberNotify','paySecurityNotify','payOrderNotify'];
protected $noNeedRight = ['*'];
protected $uid = '';//token存贮uid
protected $order_status = [];//订单状态
public function _initialize()
{
parent::_initialize();
$this->uid = $this->auth->getUserId();
}
/**
* @ApiTitle (充值/支付订单)
* @ApiSummary (充值支付)
* @ApiMethod (POST)
* @ApiRoute (/api/pay/pay)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="openid", type="string", required=true, description="小程序openid")
* @ApiParams (name="num", type="inter", required=false, description="支付订单号")
*
*/
public function pay(){
if($this->request->isPost()){
$openid = $this->request->post('openid');
$order_sn = $this->request->post('num');
if(empty($openid)){
$this->error(['code'=>2,'msg'=>'openid不能为空']);
}
if($order_sn){
//支付订单
$res = Db::name('order')->where(['num'=>$order_sn,'uid'=>$this->uid])->find();
if($res){
if($res['status'] == 2){
$this->error('该订单已支付过了');
}else{
//创建支付对象
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$result = $app->order->unify([
'body' => '山东航拍',
'out_trade_no' => $order_sn,//支付订单号
'total_fee' => $money*100,//单位分
'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $openid,
]);
if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
$params = [
'appId' => $config['app_id'],
'timeStamp' => time(),
'nonceStr' => $result['nonce_str'], // 统一下单返回的随机字符串
'package' => 'prepay_id='.$result['prepay_id'], // 统一下单Id
'signType' => 'MD5', // 签名方法
];
// 注意这里用的是商户平台的Key进行二次签名
$params['paySign'] = generate_sign($params, $config['key']);
$this->success('成功',$params);
}
$this->error($result['err_code_des']);
}
}else{
$this->error('无效的订单');
}
}
}else{
$this->error('请求方式错误');
}
}
//购买会员回调通知(无需调用)
public function payMemberNotify(){
$config = config('verify.wx_pay');
$app = Factory::payment($config);
Log::info('会员');
$response = $app->handlePaidNotify(function($message, $fail){
//return_code 表示通信状态
Log::info($message);
if ($message['return_code'] === 'SUCCESS') {
if ($message['result_code'] === 'SUCCESS') {
//支付成功
//更新成为会员
$userModel = new User();
$userModel->where('openid',$message['openid'])->update(['member'=>1]);
//增加记录
$payMemberModel = new PayMember();
$payMemberModel->create(['openid'=>$message['openid'],'pay_order'=>$message['out_trade_no']]);
}elseif($message['result_code'] === 'FAIL') {
//支付失败
}
} else {
return $fail('通信失败,请稍后再通知我');
}
return true; // 返回处理完成
});
$response->send();
}
//支付订单(无需调用)
public function payOrderNotify(){
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$response = $app->handlePaidNotify(function($message, $fail){
//return_code 表示通信状态
if ($message['return_code'] === 'SUCCESS') {
if ($message['result_code'] === 'SUCCESS') {
//支付成功,更新订单状态
$orderModel = new \app\admin\model\Order();
$orderModel->where(['order_sn'=>$message['out_trade_no']])->update(['status'=>1]);
//创建订房记录
$this->auth->createHouseRecord($message['out_trade_no']);
}elseif($message['result_code'] === 'FAIL') {
//支付失败
}
} else {
return $fail('通信失败,请稍后再通知我');
}
return true; // 返回处理完成
});
$response->send();
}
/**
* @ApiTitle (更新订单号(支付订单失败后调用))
* @ApiSummary (更新订单号(支付订单失败后调用))
* @ApiMethod (POST)
* @ApiRoute (/api/pay/updateOrder)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_sn", type="inter", required=false, description="支付订单号")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571812329",
"data": null
})
*/
public function updateOrder(){
if($this->request->isPost()){
$order_sn = $this->request->post('order_sn');
$rule = config('verify.order_sn');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['order_sn'=>$order_sn])) {
$this->error($validate->getError());
}
$orderModel = new \app\admin\model\Order();
$pay_order = $this->auth->genPayOrderSn('pay');//支付订单号
$res = $orderModel->where(['order_sn'=>$order_sn,'uid'=>$this->uid])->update(['order_sn'=>$pay_order]);
if($res){
$this->success('成功');
}else{
$this->error('失败');
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (提现到微信零钱)
* @ApiSummary (提现到微信零钱)
* @ApiMethod (POST)
* @ApiRoute (/api/pay/payBalance)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="openid", type="string", required=true, description="小程序openid")
* @ApiParams (name="price", type="inter", required=true, description="提现金额")
*/
public function payBalance(){
if($this->request->isPost()){
$price = $this->request->post('price');
$openid = $this->request->post('openid');
$rule = config('verify.to_balance');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['price'=>$price,'openid'=>$openid])) {
$this->error($validate->getError());
}
if($price < 0.3){
$this->error('提现金额不能低于0.3元');
}
//每天提现次数不超过三次
$count = Db::name('to_balance')->where('uid',$this->uid)->whereTime('createtime','today')->count();
if($count >= 3){
$this->error('每天提现次数不能超过3次');
}
//查询该用户钱包金额
$userModel = new User();
$user = $userModel->where(['id'=>$this->uid])->field('id,money,nickname')->find();
if($price > $user['money']){
$this->error('钱包金额不足');
}
//创建支付对象
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$balance_order = $this->auth->genPayOrderSn('balance');//提现单号
$result = $app->transfer->toBalance([
'partner_trade_no' => $balance_order, // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
'openid' => $openid,
'check_name' => 'NO_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
're_user_name' => '', // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
'amount' => $price*100, // 企业付款金额,单位为分
'desc' => '提现到零钱', // 企业付款操作说明信息。必填
]);
if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
//创建提现记录
$balanceRecordModel = new ToBalance();
$data['uid'] = $this->uid;
$data['openid'] = $openid;
$data['price'] = $price;
$data['to_order'] = $result['partner_trade_no'];
$balanceRecordModel->create($data);
//更新余额表
$userModel = new User();
$userModel->where(['id'=>$this->uid])->setDec('money',$price);
$this->success('成功');
}
$this->error($result['err_code_des']);
}else{
$this->error('请求方式错误');
}
}
}