作者 郭文星
@@ -210,7 +210,8 @@ class Client extends Api @@ -210,7 +210,8 @@ class Client extends Api
210 //插入订单表 210 //插入订单表
211 $res=Db::name('verification_order')->insertGetId($data); 211 $res=Db::name('verification_order')->insertGetId($data);
212 if($res){ 212 if($res){
213 - $this->success('添加订单成功'); 213 + $res = (new \app\api\controller\v1\Wechat())->prepare_order($order_no);
  214 + $this->success('添加订单成功',$res);
214 }else{ 215 }else{
215 $this->error("添加订单失败!"); 216 $this->error("添加订单失败!");
216 } 217 }
@@ -9,7 +9,7 @@ use think\Config; @@ -9,7 +9,7 @@ use think\Config;
9 use think\Db; 9 use think\Db;
10 use think\Response; 10 use think\Response;
11 use think\Validate; 11 use think\Validate;
12 - 12 +use app\api\controller\Common;
13 /** 13 /**
14 * 首页公共 14 * 首页公共
15 */ 15 */
@@ -287,16 +287,16 @@ class Wechat extends Api @@ -287,16 +287,16 @@ class Wechat extends Api
287 */ 287 */
288 public function prepare_order($order_no = ''){ 288 public function prepare_order($order_no = ''){
289 $user_id = $this->auth->id; 289 $user_id = $this->auth->id;
290 - 290 +
291 if(empty($user_id)){ 291 if(empty($user_id)){
292 - return $this->error('用户信息错误'); 292 + return $this->error('预下单的用户信息错误');
293 } 293 }
294 $userInfo = Db::name('user')->where(['id'=>$user_id])->find(); 294 $userInfo = Db::name('user')->where(['id'=>$user_id])->find();
295 295
296 - $out_trade_no = $order_no ='168532907259455'; 296 + $out_trade_no = $order_no;
297 $app = WehcatModule::getInstance()->initWechatPay(); 297 $app = WehcatModule::getInstance()->initWechatPay();
298 $result = $app->order->unify([ 298 $result = $app->order->unify([
299 - 'body' => '腾讯充值中心-QQ会员充值', 299 + 'body' => '优惠券购买',
300 'out_trade_no' => $out_trade_no, 300 'out_trade_no' => $out_trade_no,
301 'total_fee' => 1, 301 'total_fee' => 1,
302 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型 302 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
@@ -306,10 +306,10 @@ class Wechat extends Api @@ -306,10 +306,10 @@ class Wechat extends Api
306 $prepay_id = $result['prepay_id']; 306 $prepay_id = $result['prepay_id'];
307 $jssdk = $app->jssdk; 307 $jssdk = $app->jssdk;
308 $config = $jssdk->sdkConfig($prepay_id); 308 $config = $jssdk->sdkConfig($prepay_id);
309 - return $this->success('下单成功',$config); 309 + return $config;
310 310
311 }else{ 311 }else{
312 - return $this->error('下单失败',$result); 312 + return $result;
313 } 313 }
314 314
315 } 315 }
@@ -322,15 +322,15 @@ class Wechat extends Api @@ -322,15 +322,15 @@ class Wechat extends Api
322 $app = WehcatModule::getInstance()->initWechatPay(); 322 $app = WehcatModule::getInstance()->initWechatPay();
323 $response = $app->handlePaidNotify(function($message, $fail){ 323 $response = $app->handlePaidNotify(function($message, $fail){
324 // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单 324 // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
325 - file_put_contents("1_cc.log",json_encode($message),FILE_APPEND);  
326 - $order = $message['out_trade_no']; 325 + Common::put_file_log('支付成功-微信支付回调信息',json_encode($message), 'pay_notify', __CLASS__ . __FUNCTION__ . __LINE__);
  326 + $out_trade_no = $message['out_trade_no'];
327 if ($message['return_code'] === 'SUCCESS' && $message['result_code'] === 'SUCCESS') { 327 if ($message['return_code'] === 'SUCCESS' && $message['result_code'] === 'SUCCESS') {
328 - 328 + $o_w["order_no"] = $out_trade_no;
  329 + $order_info = Db::name('verification_order')->where($o_w)->find();
  330 + $this->sales_distribution($order_info);
329 } else { 331 } else {
330 - return $fail('通信失败,请稍后再通知我'); 332 + Common::put_file_log('支付失败-微信支付回调信息',json_encode($message), 'pay_notify', __CLASS__ . __FUNCTION__ . __LINE__);
331 } 333 }
332 -  
333 -  
334 return true; // 返回处理完成 334 return true; // 返回处理完成
335 }); 335 });
336 $response->send(); // return $response; 336 $response->send(); // return $response;
@@ -368,7 +368,7 @@ class Wechat extends Api @@ -368,7 +368,7 @@ class Wechat extends Api
368 } 368 }
369 } 369 }
370 370
371 - //查询上级 371 + //支付后,根据用户角色 处理对应的事情
372 private function getParent($pid,$verification_store_id,$order_id){ 372 private function getParent($pid,$verification_store_id,$order_id){
373 //上级身份 373 //上级身份
374 $time = time(); 374 $time = time();
@@ -377,10 +377,16 @@ class Wechat extends Api @@ -377,10 +377,16 @@ class Wechat extends Api
377 $role = Db::name('verification_staff')->where($w1)->find(); 377 $role = Db::name('verification_staff')->where($w1)->find();
378 if(in_array($role['type'],['1','3'])){ 378 if(in_array($role['type'],['1','3'])){
379 //属于代理 企业付款到佣金 379 //属于代理 企业付款到佣金
380 - return ''; 380 + $this->grant_commission($pid,$order_id);
381 }else{ 381 }else{
382 //属于普通用户 发送代金券 382 //属于普通用户 发送代金券
383 - $w1['verification_store_id'] = $verification_store_id; 383 + $this->send_voucher($pid,$verification_store_id,$order_id,$time);
  384 + }
  385 + }
  386 +
  387 + //发放代金券
  388 + public function send_voucher($pid,$verification_store_id,$order_id,$time){
  389 + $w1['verification_store_id'] = $verification_store_id;
384 $w1['closetime'] = ['<',$time]; 390 $w1['closetime'] = ['<',$time];
385 $coupon = Db::name('verification_coupon')->where($w1)->select(); 391 $coupon = Db::name('verification_coupon')->where($w1)->select();
386 if(!empty($coupon)){ 392 if(!empty($coupon)){
@@ -389,16 +395,41 @@ class Wechat extends Api @@ -389,16 +395,41 @@ class Wechat extends Api
389 $save['verification_coupon_id'] = $v['id']; 395 $save['verification_coupon_id'] = $v['id'];
390 $save['verification_store_id'] = $v['verification_store_id']; 396 $save['verification_store_id'] = $v['verification_store_id'];
391 $save['user_id'] = $pid; 397 $save['user_id'] = $pid;
392 - $save['type'] = 0;  
393 - $save['closetime'] = $v['closetime'];  
394 - $save['receive_no'] = $time.uniqid();  
395 - $res = (new \app\api\controller\v1\Index())->build($save['receive_no']);  
396 - $save['qr_code'] = $res;//二维码  
397 - $save['createtime'] = $time;  
398 - $save['verification_order_id'] = $order_id; 398 + //用户是否已经领取该门店的券
  399 + $r = Db::name('verification_receive')->where($save)->find();
  400 + if(empty($r)){
  401 + $save['type'] = 0;
  402 + $save['closetime'] = $v['closetime'];
  403 + $save['receive_no'] = $time.uniqid();
  404 + $res = (new \app\api\controller\v1\Index())->build($save['receive_no']);
  405 + $save['qr_code'] = $res;//二维码
  406 + $save['createtime'] = $time;
  407 + $save['verification_order_id'] = $order_id;
  408 + Db::name('verification_receive')->insert($save);
  409 + }
399 } 410 }
400 } 411 }
  412 + }
  413 + // 发放佣金
  414 + public function grant_commission($pid=0,$order_id=0){
  415 + try{
  416 + $verification_order = Db::name('verification_order')->where(['id'=>$order_id])->find();
  417 + $app = WehcatModule::getInstance()->initWechatPay();
  418 + $result = $app->transfer->toBalance([
  419 + 'partner_trade_no' => '168532907259455', // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
  420 + 'openid' => 'o5OQw6PPEGaHqIjAir7gqeDV5MzQ',
  421 + 'check_name' => 'NO_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
  422 + 're_user_name' => '陈程', // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
  423 + 'amount' => 1, // 企业付款金额,单位为分
  424 + 'desc' => '理赔', // 企业付款操作说明信息。必填
  425 + ]);
  426 + if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
  427 + //记录佣金表
  428 + }else{
  429 + Common::put_file_log('失败-佣金发放',json_encode($result), 'grant_commission', __CLASS__ . __FUNCTION__ . __LINE__);
  430 + }
  431 + }catch(\Exception $e){
  432 + Common::put_file_log('异常-佣金发放',json_encode($result), 'grant_commission', __CLASS__ . __FUNCTION__ . __LINE__);
401 } 433 }
402 } 434 }
403 -  
404 } 435 }