123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- namespace app\api\controller;
- use app\admin\model\Goodorder;
- use app\admin\model\RecoveryBill;
- use app\admin\model\Recoveryorder;
- use app\admin\model\TablewareBill;
- use app\admin\model\TablewareOrder;
- use app\admin\model\transaction\Log;
- use app\admin\model\Userscore;
- use EasyWeChat\Factory;
- use app\common\controller\Api;
- use think\Db;
- class Notify extends Api
- {
- protected $noNeedLogin =['*'];
- protected $noNeedRight = ['*'];
- /**
- * @return void
- * @throws \EasyWeChat\Kernel\Exceptions\Exception
- * 商城购物回调
- */
- public function good_notify(){
- //必要配置
- $config = [
- // 必要配置
- 'app_id' => 'wxdc450b7c5d0a0f80',
- 'mch_id' => '1635096198',
- 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
- 'notify_url' => common_url().'/api/Notice/good_notify', // 你也可以在下单时单独设置来想覆盖它
- ];
- $app = Factory::payment($config);
- $response = $app->handlePaidNotify(function($message, $fail){
- file_put_contents('pay_log.log', json_encode($message));
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_model= new \app\admin\model\Pay();
- $order_model = new Goodorder();
- $user_model = new \app\admin\model\User();
- $transaction_log_model = new Log();
- $order = $pay_model->where('pay_no',$message['out_trade_no'])->where('status',1)->find();
- if (!$order) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户是否支付成功
- if ($message ['result_code'] === 'SUCCESS') {
- $username = $user_model->where('id',$order['user_id'])->value('username');
- $log_data = [
- 'user_id'=>$order['user_id'],
- 'username'=>$username,
- 'money'=>$order['price'],
- 'desc'=>'商城购物支付',
- 'create_time'=>time()
- ];
- $transaction_log_model->insert($log_data);
- $pay_model->save(['status'=>2,'pay_time'=>time()],['pay_no'=>$message['out_trade_no']]);
- $order_model->save(['status'=>2],['pay_no'=>$message['out_trade_no']]);
- // 用户支付失败
- }
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- return true; // 返回处理完成
- });
- }
- /**
- * @return void
- * @throws \EasyWeChat\Kernel\Exceptions\Exception
- * 月结回收支付回调
- */
- public function recovery_notify(){
- //必要配置
- $config = [
- // 必要配置
- 'app_id' => 'wxdc450b7c5d0a0f80',
- 'mch_id' => '1635096198',
- 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
- 'notify_url' => common_url().'/api/Notice/recovery_notify', // 你也可以在下单时单独设置来想覆盖它
- ];
- $app = Factory::payment($config);
- $response = $app->handlePaidNotify(function($message, $fail){
- file_put_contents('pay1_log.log', json_encode($message));
- $recovery_bill_model = new RecoveryBill();
- $user_model = new \app\admin\model\User();
- $transaction_log_model = new Log();
- file_put_contents('out_trade_no.log', $message['out_trade_no']);
- $order = $recovery_bill_model->where('order_no',$message['out_trade_no'])->where('status',1)->find();
- file_put_contents('order .log',json_encode($order));
- if (!$order) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户是否支付成功
- file_put_contents('ceshi1_log.log', 11);
- if ($message ['result_code'] === 'SUCCESS') {
- file_put_contents('ceshi2_log.log', $message['out_trade_no']);
- $use_score = $user_model->where('id',$order['c_user_id'])->field('score,username')->find();
- $date = getMonthRange(date('Y-m',$order['date']));
- file_put_contents('date.log', $date);
- $pre = config('site.score_recovery')/100;
- $log_data = [
- 'user_id'=>$order['c_user_id'],
- 'username'=>$use_score['username'],
- 'money'=>$order['amount_money'],
- 'desc'=>'回收月结支付',
- 'create_time'=>time()
- ];
- $score = intval($order['service_money']*$pre);
- file_put_contents('score.log', $score);
- $score_num = $use_score['score']+$score;
- file_put_contents('end.log', $score_num);
- $user_collont = new Userscore();
- $user_collont->insert([
- 'user_id'=>$order['c_user_id'],
- 'number'=>$score,
- 'type'=>1,
- 'desc'=>'回收月结支付获得',
- 'create_time'=>time(),
- ]);
- Db::name('user')->where('id' , $order['c_user_id'])->update(['score' => $score_num]);
- $transaction_log_model->insert($log_data);
- $recovery_bill_model->save(['status'=>2,'pay_time'=>time(),'pay_no'=>$message['transaction_id']],['order_no'=>$message['out_trade_no']]);
- Db::name('recovery_order')->whereBetween('create_time',[strtotime($date['sdate']),strtotime($date['edate'])])->where('c_user_id',$order['c_user_id'])->update(['status'=>3,'updatetime'=>time()]);
- // 用户支付失败
- }
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- return true; // 返回处理完成
- });
- }
- public function tableware_notify(){
- //必要配置
- $config = [
- // 必要配置
- 'app_id' => 'wxdc450b7c5d0a0f80',
- 'mch_id' => '1635096198',
- 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
- 'notify_url' => common_url().'/api/Notice/tableware_notify', // 你也可以在下单时单独设置来想覆盖它
- ];
- $app = Factory::payment($config);
- $response = $app->handlePaidNotify(function($message, $fail){
- file_put_contents('pay2_log.log', json_encode($message));
- $tableware_bill_model = new TablewareBill();
- $user_model = new \app\admin\model\User();
- $transaction_log_model = new Log();
- $order = $tableware_bill_model->where('order_no',$message['out_trade_no'])->where('status',1)->find();
- if (!$order) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户是否支付成功
- if ($message ['result_code'] === 'SUCCESS') {
- $use_score = $user_model->where('id',$order['c_user_id'])->field('score,username')->find();
- $date = getMonthRange(date('Y-m',$order['date']));
- $pre = config('site.score_tableware')/100;
- $log_data = [
- 'user_id'=>$order['c_user_id'],
- 'username'=>$use_score['username'],
- 'money'=>$order['amount_money'],
- 'desc'=>'餐具配送月结支付',
- 'create_time'=>time()
- ];
- $score = intval($order['amount_money']*$pre);
- $score_num = $use_score['score']+$score;
- $user_collont = new Userscore();
- $user_collont->insert([
- 'user_id'=>$order['c_user_id'],
- 'number'=>$score,
- 'type'=>1,
- 'desc'=>'餐具月结支付获得',
- 'create_time'=>time(),
- ]);
- Db::name('user')->where('id' , $order['c_user_id'])->update(['score' => $score_num]);
- $transaction_log_model->insert($log_data);
- $tableware_bill_model->save(['status'=>2,'pay_time'=>time(),'pay_no'=>$message['transaction_id'],'pay_remarks'=>'微信支付'],['order_no'=>$message['out_trade_no']]);
- Db::name('tableware_order')->whereBetween('create_time',[strtotime($date['sdate']),strtotime($date['edate'])])->where('c_user_id',$order['c_user_id'])->update(['status'=>5,'pay_time'=>time()]);
- // 用户支付失败
- }
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- return true; // 返回处理完成
- });
- }
- public function text()
- {
- $message['out_trade_no'] = input('out_trade_no');
- $recovery_bill_model = new RecoveryBill();
- $user_model = new \app\admin\model\User();
- $transaction_log_model = new Log();
- $order = $recovery_bill_model->where('order_no',$message['out_trade_no'])->where('status',1)->find();
- if (!$order) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- $use_score = $user_model->where('id',$order['c_user_id'])->field('score,username')->find();
- $date = getMonthRange(date('Y-m',$order['date']));
- $pre = config('site.score_recovery')/100;
- $log_data = [
- 'user_id'=>$order['c_user_id'],
- 'username'=>$use_score['username'],
- 'money'=>$order['amount_money'],
- 'desc'=>'回收月结支付',
- 'create_time'=>time()
- ];
- $score = intval(100*$pre);
- $score_num = $use_score['score']+$score;
- $user_collont = new Userscore();
- $user_collont->insert([
- 'user_id'=>$order['c_user_id'],
- 'number'=>$score,
- 'type'=>1,
- 'desc'=>'回收月结支付获得',
- 'create_time'=>time(),
- ]);
- Db::name('user')->where('id' , $order['c_user_id'])->update(['score' => $score_num]);
- $transaction_log_model->insert($log_data);
- $recovery_bill_model->save(['status'=>2,'pay_time'=>time(),'pay_no'=>1],['order_no'=>$message['out_trade_no']]);
- Db::name('recovery_order')->whereBetween('create_time',[strtotime($date['sdate']),strtotime($date['edate'])])->where('c_user_id',$order['c_user_id'])->update(['status'=>3,'updatetime'=>time()]);
- // 用户支付失败
- }
- /**
- * @return void
- * 我的厨余餐具订单
- */
- public function me_tab_order(){
- $page = input('page',1);
- $mon = input('date',date('Y-m'));
- $user = 40;
- $recovery_model = new Recoveryorder();
- $recovery_bill_model = new RecoveryBill();
- $date = getMonthRange($mon);
- $is_have = $recovery_bill_model->whereBetween('date',[strtotime($date['sdate']),strtotime($date['edate'])])->where('c_user_id',$user)->find();
- $list = $recovery_model->whereBetween('create_time',[strtotime($date['sdate']),strtotime($date['edate'])])->where('c_user_id',$user)->paginate('5','',['page'=>$page]);
- $sate = 1;
- $data['bill']=[];
- if($is_have){
- if($is_have['status']==1) {
- $sate = 2;
- }
- if($is_have['status']==2) {
- $sate = 3;
- }
- $data['bill']=$is_have;
- }
- $data['list']=$list;
- $data['sate']=$sate;
- $this->success('月份订单',$data);
- }
- }
|