|
@@ -10,6 +10,8 @@ use Yansongda\Pay\Pay; |
|
@@ -10,6 +10,8 @@ use Yansongda\Pay\Pay; |
10
|
use app\admin\model\Account;
|
10
|
use app\admin\model\Account;
|
11
|
use app\admin\model\User;
|
11
|
use app\admin\model\User;
|
12
|
use app\admin\model\Message;
|
12
|
use app\admin\model\Message;
|
|
|
13
|
+use app\admin\model\Car;
|
|
|
14
|
+use app\admin\model\Product;
|
13
|
use think\Log;
|
15
|
use think\Log;
|
14
|
use fast\Http;
|
16
|
use fast\Http;
|
15
|
use think\Validate;
|
17
|
use think\Validate;
|
|
@@ -19,8 +21,8 @@ use Exception; |
|
@@ -19,8 +21,8 @@ use Exception; |
19
|
*/
|
21
|
*/
|
20
|
class Wxpay extends Api
|
22
|
class Wxpay extends Api
|
21
|
{
|
23
|
{
|
22
|
- protected $noNeedLogin = ['notify','notifyCharge'];
|
|
|
23
|
- protected $noNeedRight = ['notify','notifyCharge'];
|
24
|
+ protected $noNeedLogin = ['notify','notifyCharge','handle'];
|
|
|
25
|
+ protected $noNeedRight = ['notify','notifyCharge','handle'];
|
24
|
protected $user_id = '';//token存贮user_id
|
26
|
protected $user_id = '';//token存贮user_id
|
25
|
protected $order_status = [];//订单状态
|
27
|
protected $order_status = [];//订单状态
|
26
|
public function _initialize()
|
28
|
public function _initialize()
|
|
@@ -38,7 +40,6 @@ class Wxpay extends Api |
|
@@ -38,7 +40,6 @@ class Wxpay extends Api |
38
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
40
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
39
|
* @ApiParams (name="openid", type="string", required=true, description="小程序openid")
|
41
|
* @ApiParams (name="openid", type="string", required=true, description="小程序openid")
|
40
|
* @ApiParams (name="pay_order_sn", type="string", required=true, description="支付订单号")
|
42
|
* @ApiParams (name="pay_order_sn", type="string", required=true, description="支付订单号")
|
41
|
- * @ApiParams (name="share_uid", type="integer", required=false, description="分享用户id")
|
|
|
42
|
* @ApiReturn ({code: 0, msg: "无效的订单", time: "1554176100", data: null})
|
43
|
* @ApiReturn ({code: 0, msg: "无效的订单", time: "1554176100", data: null})
|
43
|
*/
|
44
|
*/
|
44
|
public function pay(){
|
45
|
public function pay(){
|
|
@@ -47,7 +48,6 @@ class Wxpay extends Api |
|
@@ -47,7 +48,6 @@ class Wxpay extends Api |
47
|
$pay = Pay::wechat(Service::getConfig('wechat'));
|
48
|
$pay = Pay::wechat(Service::getConfig('wechat'));
|
48
|
$openid = $this->request->post('openid');//小程序传递openid
|
49
|
$openid = $this->request->post('openid');//小程序传递openid
|
49
|
$pay_order_sn = $this->request->post('pay_order_sn');//支付订单号
|
50
|
$pay_order_sn = $this->request->post('pay_order_sn');//支付订单号
|
50
|
- $share_uid = $this->request->post('share_uid');//分享用户uid
|
|
|
51
|
if(empty($openid) && empty($pay_order_sn)){
|
51
|
if(empty($openid) && empty($pay_order_sn)){
|
52
|
$this->error('无效的参数');
|
52
|
$this->error('无效的参数');
|
53
|
}
|
53
|
}
|
|
@@ -78,12 +78,9 @@ class Wxpay extends Api |
|
@@ -78,12 +78,9 @@ class Wxpay extends Api |
78
|
// 'total_fee' => 1,
|
78
|
// 'total_fee' => 1,
|
79
|
'total_fee' => floatval($total_price)*100,//单位:分
|
79
|
'total_fee' => floatval($total_price)*100,//单位:分
|
80
|
'openid' => $openid,
|
80
|
'openid' => $openid,
|
|
|
81
|
+ 'notify_url' => url('api/Wxpay/notify','','',true),
|
|
|
82
|
+ 'return_url' => url('api/Wxpay/notify','','',true),
|
81
|
];
|
83
|
];
|
82
|
- if(!empty($share_uid)){
|
|
|
83
|
- $order['notify_url'] = url('api/Wxpay/notify/share_uid/'.$share_uid,'','',true);
|
|
|
84
|
- }else{
|
|
|
85
|
- $order['notify_url'] = url('api/Wxpay/notify','','',true);
|
|
|
86
|
- }
|
|
|
87
|
//跳转或输出
|
84
|
//跳转或输出
|
88
|
$this->success('成功',$pay->miniapp($order));
|
85
|
$this->success('成功',$pay->miniapp($order));
|
89
|
|
86
|
|
|
@@ -134,18 +131,13 @@ class Wxpay extends Api |
|
@@ -134,18 +131,13 @@ class Wxpay extends Api |
134
|
*/
|
131
|
*/
|
135
|
public function notify(){
|
132
|
public function notify(){
|
136
|
$pay = Pay::wechat(Service::getConfig('wechat'));
|
133
|
$pay = Pay::wechat(Service::getConfig('wechat'));
|
137
|
- $share_uid = $this->request->param('share_uid');
|
|
|
138
|
try {
|
134
|
try {
|
139
|
$data = $pay->verify();
|
135
|
$data = $pay->verify();
|
140
|
if($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS'){
|
136
|
if($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS'){
|
141
|
$porderModel = new Porder();
|
137
|
$porderModel = new Porder();
|
142
|
$porderModel->where(['pay_order_sn'=>$data['out_trade_no']])->update(['status'=>$this->order_status[1]]);
|
138
|
$porderModel->where(['pay_order_sn'=>$data['out_trade_no']])->update(['status'=>$this->order_status[1]]);
|
143
|
-
|
|
|
144
|
- //如果携带分享uid,则按照积分增加
|
|
|
145
|
- if(!empty($share_uid)){
|
|
|
146
|
- $person = new Person();
|
|
|
147
|
- $person->getScore($share_uid,'share_purchase');
|
|
|
148
|
- }
|
139
|
+ //减库存,删相应购物车记录
|
|
|
140
|
+ $this->handle($data['out_trade_no']);
|
149
|
}
|
141
|
}
|
150
|
} catch (Exception $e) {
|
142
|
} catch (Exception $e) {
|
151
|
echo "验签失败";
|
143
|
echo "验签失败";
|
|
@@ -227,4 +219,66 @@ class Wxpay extends Api |
|
@@ -227,4 +219,66 @@ class Wxpay extends Api |
227
|
return $pay->success()->send();
|
219
|
return $pay->success()->send();
|
228
|
}
|
220
|
}
|
229
|
|
221
|
|
|
|
222
|
+ /**
|
|
|
223
|
+ * @ApiTitle (分享购买增加积分)
|
|
|
224
|
+ * @ApiSummary (分享购买增加积分)
|
|
|
225
|
+ * @ApiMethod (GET)
|
|
|
226
|
+ * @ApiRoute (/api/wxpay/addScore)
|
|
|
227
|
+ * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
|
|
228
|
+ * @ApiParams (name="share_uid", type="integer", required=true, description="分享uid")
|
|
|
229
|
+ * @ApiReturn ({
|
|
|
230
|
+ "code": 1,
|
|
|
231
|
+ "msg": "成功",
|
|
|
232
|
+ "time": "1554184134",
|
|
|
233
|
+ "data": null
|
|
|
234
|
+ })
|
|
|
235
|
+ */
|
|
|
236
|
+ public function addScore(){
|
|
|
237
|
+ if($this->request->isGet()){
|
|
|
238
|
+ $share_uid = $this->request->get('share_uid');
|
|
|
239
|
+ //如果携带分享uid,则按照积分增加
|
|
|
240
|
+ $person = new Person();
|
|
|
241
|
+ $res = $person->getScore($share_uid,'share_purchase');
|
|
|
242
|
+ if($res){
|
|
|
243
|
+ $this->error('成功');
|
|
|
244
|
+ }else{
|
|
|
245
|
+ $this->error('失败');
|
|
|
246
|
+ }
|
|
|
247
|
+ }else{
|
|
|
248
|
+ $this->error('请求方式错误');
|
|
|
249
|
+ }
|
|
|
250
|
+ }
|
|
|
251
|
+
|
|
|
252
|
+
|
|
|
253
|
+ public function handle($pay_order_sn){
|
|
|
254
|
+ //查询订单信息的商品id,数量num
|
|
|
255
|
+ $data = Db::table('gc_porder')
|
|
|
256
|
+ ->where(['pay_order_sn'=>$pay_order_sn])
|
|
|
257
|
+ ->field('p_id,num')
|
|
|
258
|
+ ->select();
|
|
|
259
|
+ $pIds = array_column($data,'p_id');
|
|
|
260
|
+ //减库存
|
|
|
261
|
+ $product = Db::table('gc_product')
|
|
|
262
|
+ ->whereIn('id',$pIds)
|
|
|
263
|
+ ->field('id,stock')
|
|
|
264
|
+ ->select();
|
|
|
265
|
+ foreach($product as &$p_value){
|
|
|
266
|
+ foreach($data as $value){
|
|
|
267
|
+ if($p_value['id'] == $value['p_id']){
|
|
|
268
|
+ $p_value['stock'] = $p_value['stock'] - $value['num'];
|
|
|
269
|
+ }
|
|
|
270
|
+ }
|
|
|
271
|
+ }
|
|
|
272
|
+ //更新库存
|
|
|
273
|
+ $productModel = new Product();
|
|
|
274
|
+ $productModel->saveAll($product);
|
|
|
275
|
+
|
|
|
276
|
+ $userModel = new User();
|
|
|
277
|
+ $user = $userModel->where(['openid' => $data['openid']])->find();
|
|
|
278
|
+
|
|
|
279
|
+ //清理购物车
|
|
|
280
|
+ $carModel = new Car();
|
|
|
281
|
+ $carModel->whereIn('p_id',$pIds)->where('uid',$user['id'])->delete();
|
|
|
282
|
+ }
|
|
|
283
|
+
|
230
|
} |
284
|
} |