Pay.php
10.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
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
<?php
namespace app\api\controller;
use app\admin\model\Order;
use app\common\controller\Api;
use EasyWeChat\Factory;
use think\Validate;
use function EasyWeChat\Kernel\Support\generate_sign;
/**
* 支付接口
*/
class Pay extends Api
{
protected $noNeedLogin = ['payOrderNotify','purchaseNotify'];
protected $noNeedRight = ['*'];
protected $uid = '';//token存贮uid
protected $order_status = [];//订单状态
public function _initialize()
{
parent::_initialize();
$this->uid = $this->auth->getUserId();
$this->order_status = config('verify.status');
}
/**
* @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="order_sn", type="inter", required=true, description="订单号")
*
*/
public function pay(){
if($this->request->isPost()){
$openid = $this->request->post('openid');
$order_sn = $this->request->post('order_sn');
$rule = config('verify.pay');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['openid'=>$openid,'order_sn'=>$order_sn])) {
$this->error($validate->getError());
}
$ffg_host = config('verify.ffg_host');
$status = $this->order_status;
//支付订单
$res = Common::findSoftWhereData('order',['uid'=>$this->uid,'order_sn'=>$order_sn,'status'=>$status[0]],'id,total_price');
if(!$res){
$this->error('无效的订单');
}
//暂时写支付成功
$status = $this->order_status;
$orderModel = new Order();
$orderModel->where(['uid'=>$this->uid,'order_sn'=>$order_sn,'status'=>$status[0]])->update(['status'=>$status[2]]);
$this->success('成功',['order_id'=>$res['id']]);
//创建支付对象
$config = config('verify.wx_pay');
$pay_order_sn = Common::genPayOrderSn('pay');//支付订单号
$app = Factory::payment($config);
$result = $app->order->unify([
'body' => '佛山市理想中网络科技有限公司',
'out_trade_no' => $pay_order_sn,//支付订单号
'total_fee' => $res['total_price']*100,//单位分
'notify_url' => $ffg_host.'/api/pay/payOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'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']);
//更新订单的支付订单号
$orderModel = new Order();
$res1 = $orderModel->where(['order_sn'=>$order_sn,'uid'=>$this->uid])->update(['pay_order_sn'=>$pay_order_sn]);
if($res1){
$this->success('成功',$params);
}else{
$this->error('失败');
}
}
$this->error($result['err_code_des']);
}else{
$this->error('请求方式错误');
}
}
/**
* 支付订单回调
* @ApiInternal
*/
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') {
//支付成功,更新订单状态
$status = $this->order_status;
$orderModel = new Order();
$orderModel->where(['pay_order_sn'=>$message['out_trade_no'],'status'=>$status[0]])->update(['status'=>$status[2]]);
}elseif($message['result_code'] === 'FAIL') {
//支付失败
}
} else {
return $fail('通信失败,请稍后再通知我');
}
return true; // 返回处理完成
});
$response->send();
}
/**
* @ApiTitle (立即购买)
* @ApiSummary (立即购买)
* @ApiMethod (POST)
* @ApiRoute (/api/pay/purchase)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="receive_name", type="string", required=true, description="收货人")
* @ApiParams (name="receive_mobile", type="string", required=true, description="联系方式")
* @ApiParams (name="receive_address", type="string", required=true, description="收货地址")
* @ApiParams (name="openid", type="string", required=true, description="小程序openid")
* @ApiParams (name="goods_id", type="string", required=true, description="商品id(多个以逗号隔开,如:1,3,4)")
* @ApiParams (name="price", type="string", required=true, description="商品价格(多个以逗号隔开,如:100,10,200)")
* @ApiParams (name="goods_number", type="string", required=true, description="商品数量(多个以逗号隔开,如:10,10,10)")
* @ApiParams (name="style", type="string", required=true, description="商品款式(多个以逗号隔开,如:‘款式1,款式2,款式3’)")
* @ApiParams (name="leave_message", type="string", required=false, description="留言")
* @ApiParams (name="coupon_id", type="inter", required=false, description="优惠券id")
*
* @ApiParams (name="total_goods_price", type="inter", required=true, description="商品总金额")
* @ApiParams (name="discount_price", type="inter", required=true, description="优惠金额")
* @ApiParams (name="total_expense_price", type="inter", required=true, description="运费总金额")
*/
public function purchase(){
if($this->request->isPost()){
$data = $this->request->post();
$rule = config('verify.purchase');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check($data)) {
$this->error($validate->getError());
}
//创建支付对象
$config = config('verify.wx_pay');
$app = Factory::payment($config);
$pay_order_sn = Common::genPayOrderSn('pay');//支付订单号
//写入订单
$res = Common::createOrder($data,$pay_order_sn,$this->uid);
if($res){
$this->success('成功',['order_id'=>$res['order_id']]);
}
$this->error('创建订单失败');
//暂时没有真正支付
$ffg_host = config('verify.ffg_host');
$result = $app->order->unify([
'body' => '佛山市理想中网络科技有限公司',
'out_trade_no' => $pay_order_sn,//支付订单号
'total_fee' => $data['security_price']*100,//单位分
'notify_url' => $ffg_host.'/api/pay/purchaseNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $data['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']);
//写入订单
$res = Common::createOrder($data,$pay_order_sn,$this->uid);
if($res){
$this->success('成功',$params);
}
$this->error('创建订单失败');
}
$this->error($result['err_code_des']);
}else{
$this->error('请求方式错误');
}
}
/**
* 立即购买回调
* @ApiInternal
*/
public function purchaseNotify(){
$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') {
//支付成功
$status = $this->order_status;
$orderModel = new Order();
$orderModel->where(['pay_order_sn'=>$message['out_trade_no'],'status'=>$status[0]])->update(['status'=>$status[2]]);
}elseif($message['result_code'] === 'FAIL') {
//支付失败,好像有问题
}
} else {
return $fail('通信失败,请稍后再通知我');
}
return true; // 返回处理完成
});
$response->send();
}
}