<?php

namespace App\Http\Controllers\WapAlipay\Pay;

use Illuminate\Routing\Controller;
use App\Libs\Pay\WechatPay;
use Illuminate\Http\Request;
use App\Modules\Subscribe\Services\YearOrderService;
use App\Modules\Subscribe\Services\BookOrderService;
use App\Modules\Subscribe\Services\OrderService;
use App\Modules\Product\Services\ProductService;
use App\Modules\Book\Services\BookConfigService;
use App\Modules\User\Services\UserService;
use App\Modules\Channel\Services\ChannelService;
use Log;
use DB;
use Cookie;
use Redis;
use Hashids;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Support\XML;
use App\Modules\OfficialAccount\Services\ForceSubscribeService;
use App\Jobs\SendTexts;
use App\Jobs\SendStatisticsList;
use App\Libs\SMS;
use App\Modules\Activity\Services\ActivityService;
use App\Modules\SendOrder\Services\SendOrderService;
use GuzzleHttp\Client;

class OrdersController extends Controller
{
    /**
     * @apiDefine pay 支付
     */

    /**
     * 支付唤起入口
     * 功能:授权、请求支付参数、并跳转微信支付
     * TODO 暂时不创建订单
     * tail -f /var/www/ydy_wap_backend/storage/logs/laravel-2017-12-01.log
     * zwap/goToPay?distribution_channel_id=3&price=1&uid=3
     * @apiVersion 1.0.0
     * @apiDescription 支付
     * @api {get} http://pay.aizhuishu.com/goToPay 支付
     * @apiGroup pay
     * @apiName wxindex
     * @apiParam {Int}  product_id   product_id
     * @apiParam {Int}  uid   uid
     * @apiParam {Int}  distribution_channel_id   distribution_channel_id
     * @apiParam {String}  pay_redirect_url   pay_redirect_url
     * @apiSuccess {int}         code 状态码
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {
     *       }
     */
    function wxindex(Request $request){
        $product_id = $request->has('product_id')?$request->get('product_id'):'';
        $uid = $request->has('uid')?$request->get('uid'):'';
        $distribution_channel_id = $request->has('distribution_channel_id')?$request->get('distribution_channel_id'):'';
        $pay_redirect_url = $request->has('pay_redirect_url')?$request->get('pay_redirect_url'):'';
        $send_order_id = $request->has('send_order_id')?$request->get('send_order_id'):0;
        $activity_id = $request->has('activity_id')?$request->get('activity_id'):0;
        $n = $request->has('n')?$request->get('n'):0;
        if($send_order_id && strlen($send_order_id)>50 ){
            try{
                $send_order_id = decrypt($send_order_id);
            }catch (\Exception $e){
                $send_order_id = 0;
            }
        }

        if($send_order_id && strlen($send_order_id)>50){
            $send_order_id = 0;
        }

        //活动判断
        if($activity_id){
            $limit = $request->get('limit',0);
            if($limit && $limit == 1){
                $order = OrderService::userIsParticipateActivity($uid,$activity_id);
                if($order){
                    $activity = ActivityService::getById($activity_id);
                    if($activity){
                        $url = env('PROTOCOL','https').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST','leyuee').'.com'.$activity->activity_page;
                        return redirect()->to($url);
                    }
                    return '';
                }
            }
        }

        if(empty($product_id) || empty($uid) || empty($distribution_channel_id) || empty($pay_redirect_url)){
            return response()->error('WAP_PARAM_ERROR');
        }
        $openid = $request->has('openid')?$request->get('openid'):'';
        $bid = $request->has('bid')?$request->get('bid'):0;
        $hash_bid = $bid;
        $fromtype = $request->has('fromtype')?$request->get('fromtype'):$request->get('from');

        if($fromtype && strlen($fromtype)>50 ){
            try{
                $fromtype = decrypt($fromtype);
            }catch (\Exception $e){
                $fromtype = 'main';
            }
        }
        if($fromtype && strlen($fromtype)>50){
            $fromtype = 'main';
        }
        if(empty($product_id) || empty($uid) || empty($distribution_channel_id) || empty($pay_redirect_url)){
            return response()->error('WAP_PARAM_ERROR');
        }


        //根据分校id获取支付配置id
        Log::info($request->all());
        $channel = ChannelService::getById($distribution_channel_id);
        if(!$channel || !$channel->pay_merchant_id){
            return response()->error('WAP_PARAM_ERROR');
        }
        //获取支付类型
        $pay_merchant = DB::table('pay_merchants')->select('appid','source','config_info')->where('id',$channel->pay_merchant_id)->where('is_enabled',1)->first();
        if(!$pay_merchant || !$pay_merchant->appid || !$pay_merchant->source) return response()->error('WAP_PARAM_ERROR');
        //重定向 获取用户信息的次数
        $n++;
        if($n >= 5){
            //重定向次数过多,则授权公众号出问题
            $this->payAlert($channel->pay_merchant_id,'','',$n);
            $back_url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com' . '/pay';
            return redirect()->to($back_url);
        }
        if(empty($openid)){
            $trade_no = date("YmdHis").hexdec(uniqid());
            $params = compact('uid','product_id','distribution_channel_id','send_order_id','bid','trade_no','pay_redirect_url','fromtype','activity_id','n');
            $redirect_url  = env('CREATE_PAY_URL').'?'.http_build_query($params);
            //$redirect_url  = env('CREATE_PAY_URL').'?uid='.$uid.'&product_id='.$product_id.'&distribution_channel_id='
               // .$distribution_channel_id.'&send_order_id='.$send_order_id.'&bid='.$bid.'&pay_redirect_url='.urlencode($pay_redirect_url)

            $auth_url = env('AUTH_URL').'?gzh_app_id='.$pay_merchant->appid.'&redirect_url='.urlencode($redirect_url);
            Log::info('redirect_auth_url:'.$auth_url);
            header("Location:".$auth_url);
            exit();
        }
        $trade_no = $request->input('trade_no');
        $order_info = OrderService::getByTradeNo($trade_no);
        if($order_info) return response()->error('WAP_SYS_ERROR');

        $cid = $request->has('cid')?$request->get('cid'):'';
        $product_info = ProductService::getProductSingle($product_id);
        $price = $product_info->price*100;
        if($uid < 32){
            $price = 1;
        }
        if(!$send_order_id){
            try{
                $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
            }catch (\Exception $e){

            }
        }

        if(in_array($uid,explode(',',env('TEST_UID')))){
            $price = 1;
        }
        if($bid){
            try{
                $bid = Hashids::decode($bid)[0];
            }catch (\Exception $e){
                $bid = 0;
            }

        }

        try{
            $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:'.$distribution_channel_id;
            Redis::hset($key,$uid,time());
            $date = date('Y-m-d');
            Redis::hincrby('order_stat:'.$distribution_channel_id,'order_num_'.$date,1);
            if($send_order_id){
                Redis::hincrby('order_promotion_stat:'.$send_order_id,'order_num_'.$date,1);
                Redis::hincrby('order_promotion_stat:'.$send_order_id,'total',1);
            }
        }catch (\Exception  $e){

        }

        
        $data = [];

        $data['price'] = $price;

        $data['create_ip'] = $request->getClientIp();

        $data['openid'] = $openid;//

        $data['body'] = 'novel read';
        $official_name = $this->getSubscribeOfficialName($uid);
        if($official_name){
            $data['body'] = '搜索公众号'.$official_name.',请继续阅读';
        }
        $data['detail'] = 'novel read';
        $data['trade_no'] = $trade_no;
        $data['remark'] = json_encode(
            [
                'uid'=>$uid,
                'dcd'=>$distribution_channel_id,
                'bid'=>$bid,
                'cp'=>$request->getClientIp(),
                'pms'=>$pay_merchant->source,
                'pmi'=>$channel->pay_merchant_id,
                'pd'=>$product_id,
                'soi'=>$send_order_id,
            ]
        );
        if($product_info->type == 'YEAR_ORDER'){
            $order_type = 'YEAR';
        }elseif ($product_info->type == 'BOOK_ORDER'){
            $order_type = 'BOOK';
        }elseif ($product_info->type == 'TICKET_RECHARGE'){
            $order_type = 'RECHARGE';
        }else{
            $order_type = '';
        }
        /*
        $this->createOrderTotal([
            'distribution_channel_id'=>$distribution_channel_id,
            'uid'=>$uid,
            'product_id'=>$product_id,
            'price'=>$price/100,
            'pay_type'=>1,
            'trade_no'=>$data['trade_no'],
            'pay_merchant_source'=>$pay_merchant->source,
            'pay_merchant_id'=>$channel->pay_merchant_id,
            'create_ip'=>$request->getClientIp(),
            'send_order_id'=>$send_order_id,
            'order_type'=>$order_type,
            'from_bid'=>$bid
        ]);*/
        $send_order_name = '';
        if($send_order_id){
            $send_order_info= SendOrderService::getById($send_order_id);
            if($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)){
                $send_order_name = $send_order_info->name;
            }
        }
        $this->createUnPayOrder([
            'distribution_channel_id'=>$distribution_channel_id,
            'uid'=>$uid,
            'product_id'=>$product_id,
            'price'=>$price/100,
            'pay_type'=>1,
            'trade_no'=>$data['trade_no'],
            'pay_merchant_source'=>$pay_merchant->source,
            'pay_merchant_id'=>$channel->pay_merchant_id,
            'create_ip'=>$request->getClientIp(),
            'send_order_id'=>$send_order_id,
            'send_order_name'=>$send_order_name,
            'order_type'=>$order_type,
            'from_bid'=>$bid,
            'from_type'=>$fromtype,
            'activity_id'=>$activity_id
        ]);
        Log::info($data);

        $config = [];
        if($pay_merchant->config_info){
            $config = json_decode($pay_merchant->config_info,true);
        }
        $wechatPay = WechatPay::instance($pay_merchant->source,$config);

        if(!$wechatPay) return response()->error('WAP_PARAM_ERROR');

        $pay_info = $wechatPay->send($data);
        if(!$pay_info){
            $pay_info = $wechatPay->send($data);
        }

        if(!isset($pay_info['appId']) || !isset($pay_info['package'])){
            //支付异常
            $this->payAlert($channel->pay_merchant_id,$trade_no,$pay_info);
        }
        $pay_info['pay_redirect_url']  = urldecode($pay_redirect_url);
        if($request->has('cid')){
            $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'].'&cid='.$request->input('cid');
        }
        if($hash_bid){
            $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'].'&bid='.$hash_bid;
        }
        $pay_url_info = parse_url($pay_redirect_url);
        $pay_info['pay_wait_url']  = $pay_url_info['scheme'].'://'.$pay_url_info['host'].'/pay/wait?order='.$data['trade_no'].'&redirect='.urlencode($pay_info['pay_redirect_url']);

        //$h5_scheme = env('H5_SCHEME','https');
        //$jsSdkSign = $this->jsSdkSign($pay_info,str_replace('http',$h5_scheme,url()->current()));
        Log::info('$pay_info is');
        $jsSdkSign =0;
        Log::info($pay_info);
        Log::info('jsSdkSign---- :'.$jsSdkSign);
        $pay_order = $trade_no;
        return view('pay.order.index',compact('pay_info','referer','jsSdkSign','pay_order'));
    }

    public function reportError(Request $request){
        $data = $request->post('data');
        DB::table('pay_page_error')->insert([
            'msg'=>$data,
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s')
        ]);
        return response()->success();
    }
    private function jsSdkSign($pay_info,$url){
        if(!isset($pay_info['appId']) || !isset($pay_info['package'])){
            return 0;
        }

        $appid = $pay_info['appId'];

        $jsapi_ticket = Redis::hget($appid,'jsapi_ticket');
        //如果能获取到jsapi_ticket 则直接签名
        if($jsapi_ticket){
            $sign_arr = [
                'noncestr'=>$pay_info['nonceStr'],
                'jsapi_ticket'=>$jsapi_ticket,
                'timestamp'=>$pay_info['timeStamp'],
                'url'=>$url
            ];
            return sha1($this->arr_to_url($sign_arr));
        }
        //获取不到jsapi_ticket
        $app_secret_info = DB::table('official_setting')->select('secret')->first();
        if($app_secret_info && !empty($app_secret_info->secret)){
            $client = new Client(['timeout'  => 5]);
            try{
                //获取access_token
                $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$app_secret_info->secret;
                $res = $client->request('get',$url)->getBody()->getContents();
                if(!$res){
                    return 0;
                }
                $res = json_decode($res,1);
                if(!isset($res['access_token']) || empty($res['access_token'])){
                    return 0;
                }
                Redis::hset($appid,'access_token',$res['access_token']);
                //获取jsapi_ticket
                $res = null;
                $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$res['access_token'].'&type=jsapi';
                $res = $client->request('get',$url)->getBody()->getContents();
                if(!$res){
                    return 0;
                }
                $res = json_decode($res,1);
                if(!isset($res['ticket']) || empty($res['ticket'])){
                    return 0;
                }
                Redis::hset($appid,'jsapi_ticket',$res['ticket']);
                Redis::EXPIRE($appid,7000);
                $sign_arr = [
                    'noncestr'=>$pay_info['nonceStr'],
                    'jsapi_ticket'=>$res['ticket'],
                    'timestamp'=>$pay_info['timeStamp'],
                    'url'=>$url
                ];

                return sha1($this->arr_to_url($sign_arr));
            }catch (\Exception $e){
                return 0;
            }
        }

        return 0;
    }
    public function waitPage(Request $request){

        $order = $request->input('order');
        //$order = "201712081711401585932843356442";
        $order = (string)$order;
        $url = urldecode($request->input('redirect'));
        return view('pay.order.wait',compact('order','url'));
        //return view('pay.order.wait');
    }

    /**
     * 官方微信回调
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    function wcback_official(Request $request){
        $xml = XML::parse(strval($request->getContent()));
        Log::info('xml is');
        Log::info($xml);
        if(!$xml || !is_array($xml)) return 'fail';
        $remarks = json_decode($xml['attach'],true);
        $pay_merchant_id = $remarks['pmi'];

        $pay_merchant = DB::table('pay_merchants')->select('appid','source','config_info')->where('id',$pay_merchant_id)->where('is_enabled',1)->first();

        $application = WechatPay::instance('OFFICIALPAY',json_decode($pay_merchant->config_info,true));
        //$app = new Application($options);
        $app = $application->app;
        $response = $app->payment->handleNotify(function($notify, $successful){
            if(!$successful) return 'fail';
            $trade_no = $notify->out_trade_no;
            $order = OrderService::getByTradeNo($trade_no);
            if(isset($order->status) && $order->status=='PAID'){
                Log::info('has_pay:'.$trade_no);
                return true;
            }
            DB::beginTransaction();
            try{
                $transaction_id = $notify->transaction_id;
                $remarks = json_decode($notify->attach,true);
                $uid = $remarks['uid'];
                $distribution_channel_id = $remarks['dcd'];
                $product_id = $remarks['pd'];
                $product = ProductService::getProductSingle($product_id);
                $pay_merchant_source = $remarks['pms'];
                $pay_merchant_id = $remarks['pmi'];
                $send_order_id = $remarks['soi'];
                $create_ip = $remarks['cp'];
                $price =$product->price;
                $bid = $remarks['bid'];
                //$this->updateOrderTotal($trade_no,$transaction_id);
                // 更新其他定制Order表
                if($product->type == 'YEAR_ORDER'){
                    Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
                    $order_type = 'YEAR';
                    $this->yearOrder($uid,$distribution_channel_id,$price,$send_order_id);
                    $order->order_type = $order_type;
                    $order->status = 'PAID';
                    $order->pay_end_at = date('Y-m-d H:i:s');
                    $order->transaction_id = $transaction_id;
                    $order->save();
                    //$price = $price/100;
                    /*
                    $this->createOrder(
                        compact(
                            'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
                            'pay_merchant_id','create_ip','bid','transaction_id'
                        )
                    );*/
                }elseif($product->type == 'BOOK_ORDER'){
                    Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
                    $order_type = 'BOOK';
                    $this->bookOrder($product_id,$uid,$send_order_id,$price,$distribution_channel_id);
                    $order->order_type = $order_type;
                    $order->status = 'PAID';
                    $order->pay_end_at = date('Y-m-d H:i:s');
                    $order->transaction_id = $transaction_id;
                    $order->save();
                    //$price = $price/100;
                    /*
                    $this->createOrder(
                        compact(
                            'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
                            'pay_merchant_id','create_ip','bid','transaction_id'
                        )
                    );*/
                }elseif($product->type == 'TICKET_RECHARGE'){
                    Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
                    $order_type = 'RECHARGE';
                    $this->userCharge($product,$uid);
                    $order->order_type = $order_type;
                    $order->status = 'PAID';
                    $order->pay_end_at = date('Y-m-d H:i:s');
                    $order->transaction_id = $transaction_id;
                    $order->save();
                    //$price = $price/100;
                    /*
                    $this->createOrder(
                        compact(
                            'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
                            'pay_merchant_id','create_ip','bid','transaction_id'
                        ));*/
                }else{
                    DB::rollback();
                    return 'Order not exist.';
                }
                $this->successPayPushMsg($uid,$product);
                $this->orderStatistical($order);
                $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:'.$distribution_channel_id;
                Redis::hdel($key,$uid);
                DB::commit();
                return true;
            }catch (\Exception $e){
                DB::rollback();
                return 'fail';
            }
        });
        return $response;
    }

    /**
     * 通联支付回调
     * @param Request $request
     */
    function wcback_allinpay(Request $request)
    {
    	Log::info('wcback_allinpay_request');
    	try{
    		$data = $request->all();
    		Log::info($request->all());
            if(!$data['trxreserved']) return 'fail';

            $pay_merchant_id = json_decode($data['trxreserved'],true)['pmi'];

            $pay_merchant = DB::table('pay_merchants')->select('appid','source','config_info')->where('id',$pay_merchant_id)->where('is_enabled',1)->first();
    		unset($data['_url']);
    		if(!$pay_merchant->config_info) return 'fail';

    		$wechatPay = WechatPay::instance('ALLINPAY',json_decode($pay_merchant->config_info,true));
    		if($wechatPay->NotifyValidSign($data,$wechatPay->appkey)){
    			Log::info('allinpay_notify_sign_success');
    			/**
    			 只有0000表示交易成功或下单成功,其他为失败
    			 0000:交易成功
    			 3045,3088:交易超时
    			 3008:余额不足
    			 3999:交易失败
    			 2008:交易处理中
    			 3050:交易已撤销
    			 */
    			if($data['trxstatus'] == '0000')
    			{
    				// 修改表比较多,开启事务
    				DB::beginTransaction();
    				$trade_no = $data['cusorderid'];
    				$transaction_id = $data['chnltrxid'];
    				//$openid = $data['acct'];
    				$remarks = $data['trxreserved'];
//     				$remarks = '{"openid":"oEteU1VNvYozhXuu8TXhByPBtSl4","distribution_channel_id":2,"product_id":1,"uid":1,"price":"1","trade_no":"201711301937151585490779316114","create_ip":"::1","servicer":"AllinPay"}';
    				$remarks = json_decode($remarks,true);
    				Log::info('$remarks');
    				Log::info($remarks);

    				 
    				// 更新Order
    				$order = OrderService::getByTradeNo($trade_no);
    				// 防止重复推送
    				if(isset($order->status) && $order->status=='PAID'){
    					Log::info('has_pay:'.$trade_no);
    					echo 'success';
    					exit();
    				}
                    $uid = $remarks['uid'];
    				$distribution_channel_id = $remarks['dcd'];
                    $product_id = $remarks['pd'];
                    $product = ProductService::getProductSingle($product_id);
                    $pay_merchant_source = $remarks['pms'];
                    $pay_merchant_id = $remarks['pmi'];
                    $send_order_id = $remarks['soi'];
                    $create_ip = $remarks['cp'];
                    $price =$product->price;
                    $bid = $remarks['bid'];
    				Log::info('save_order_end');

                    //$this->updateOrderTotal($trade_no,$transaction_id);
    				Log::info($product);

    				Log::info('product_type:'.$product->type);

    				// 更新其他定制Order表
    				if($product->type == 'YEAR_ORDER'){
    				    Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
                        $order_type = 'YEAR';
                        $this->yearOrder($uid,$distribution_channel_id,$price,$send_order_id);
                        $order->order_type = $order_type;
                        $order->status = 'PAID';
                        $order->pay_end_at = date('Y-m-d H:i:s');
                        $order->transaction_id = $transaction_id;
                        $order->save();
                        //$price = $price/100;
                        /*
                        $this->createOrder(
                            compact(
                                'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
                                'pay_merchant_id','create_ip','bid','transaction_id'
                            )
                        );*/
    				}elseif($product->type == 'BOOK_ORDER'){
                        Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
                        $order_type = 'BOOK';
                        $this->bookOrder($product_id,$uid,$send_order_id,$price,$distribution_channel_id);
                        $order->order_type = $order_type;
                        $order->status = 'PAID';
                        $order->pay_end_at = date('Y-m-d H:i:s');
                        $order->transaction_id = $transaction_id;
                        $order->save();
                        //$price = $price/100;
                        /*
                        $this->createOrder(
                            compact(
                                'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
                                'pay_merchant_id','create_ip','bid','transaction_id'
                            )
                        );*/
    				}elseif($product->type == 'TICKET_RECHARGE'){
                        Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
                        $order_type = 'RECHARGE';
                        $this->userCharge($product,$uid);
                        $order->order_type = $order_type;
                        $order->status = 'PAID';
                        $order->pay_end_at = date('Y-m-d H:i:s');
                        $order->transaction_id = $transaction_id;
                        $order->save();
                        //$price = $price/100;
                        /*
                        $this->createOrder(
                            compact(
                                'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
                                'pay_merchant_id','create_ip','bid','transaction_id'
                            ));*/
    				}
                    $this->successPayPushMsg($uid,$product);
                    $this->orderStatistical($order);
    				DB::commit();
                    //redis 删除未支付的uid
                    try{
                        $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:'.$distribution_channel_id;
                        Redis::hdel($key,$uid);
                        //Redis::srem($key,$uid);
                    }catch (\Exception $e){
                        Log::info('redis remote error-----------------------');
                        Log::info($e);
                    }

    			}
    		}else{
    			Log::info('allinpay_notify_sign_fail');
    		}
    	}
    	catch(\Exception $e){
    		var_dump($e->getMessage());
    		DB::rollBack();
    		Log::info('receive_allinpay_ept:'.$e->getMessage());
    	}
    	Log::info('pay_callback_end');

    	echo "success";
    }

    public function wcback_lianlianpay(Request $request){
        $data = $request->getContent();
        Log::info('wcback_lianlianpay call back enter');
        Log::info($request->getContent());
        $data = json_decode($data,true);
        if($data['result_pay'] == 'SUCCESS'){
            $trade_no = $data['no_order'];
            $order = OrderService::getByTradeNo($trade_no);
            if(!$order) return response()->json(['ret_code'=>'-1']); 
            $pay_merchant_id = $order->pay_merchant_id;

            $pay_merchant = DB::table('pay_merchants')->select('appid','source','config_info')->where('id',$pay_merchant_id)->where('is_enabled',1)->first();
            unset($data['_url']);
            if(!$pay_merchant->config_info) response()->json(['ret_code'=>'-1']);

            $wechatPay = WechatPay::instance('LIANLIANPAY',json_decode($pay_merchant->config_info,true));
            $uid = $order->uid;
            $distribution_channel_id = $order->distribution_channel_id;
            $price = $order->price;
            $send_order_id = $order->send_order_id;
            $product_id = $order->product_id;

            if(!$wechatPay->rsaCheck($data)){
                Log::info('sign check error');
                //return response()->json(['ret_code'=>'-1']);
            }

            // 防止重复推送
            if(isset($order->status) && $order->status=='PAID'){
                Log::info('has_pay:'.$trade_no);
                return response()->json(['ret_code'=>'0000','ret_msg'=>'交易成功']); 
            }
            DB::beginTransaction();
            try {
                $product = ProductService::getProductSingle($order->product_id);
                $transaction_id = $data['oid_paybill'];
                // 更新其他定制Order表
                if($product->type == 'YEAR_ORDER'){
                    Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
                    $order_type = 'YEAR';
                    $this->yearOrder($uid,$distribution_channel_id,$price,$send_order_id);
                    $order->order_type = $order_type;
                    $order->status = 'PAID';
                    $order->pay_end_at = date('Y-m-d H:i:s');
                    $order->transaction_id = $transaction_id;
                    $order->save();
                }elseif($product->type == 'BOOK_ORDER'){
                    Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
                    $order_type = 'BOOK';
                    $this->bookOrder($product_id,$uid,$send_order_id,$price,$distribution_channel_id);
                    $order->order_type = $order_type;
                    $order->status = 'PAID';
                    $order->pay_end_at = date('Y-m-d H:i:s');
                    $order->transaction_id = $transaction_id;
                    $order->save();
                }elseif($product->type == 'TICKET_RECHARGE'){
                    Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
                    $order_type = 'RECHARGE';
                    $this->userCharge($product,$uid);
                    $order->order_type = $order_type;
                    $order->status = 'PAID';
                    $order->pay_end_at = date('Y-m-d H:i:s');
                    $order->transaction_id = $transaction_id;
                    $order->save();
                }
                $this->successPayPushMsg($uid,$product);
                $this->orderStatistical($order);
                DB::commit();
                //redis 删除未支付的uid
                $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id'.$distribution_channel_id;
                Redis::hdel($key,$uid);
                return response()->json(['ret_code'=>'0000','ret_msg'=>'交易成功']); 
            } catch (\Exception $e) {
                DB::rollBack();
                Log::info('lianlian:'.$e->getMessage());
            }
        }
        return response()->json(['ret_code'=>'-1']); 
    }

    /**
     * 微众支付回调
     * @param Request $request
     */
    function wcback_palmpay(Request $request)
    {
        Log::info('wcback_palmpay_request');
        try{
            $data = $request->except('_url');


            Log::info($request->all());

            //订单
            $trade_no = $data['outTradeNo'];
            $order = OrderService::getByTradeNo($trade_no);
            if(!$order) die('failed');

            // 防止重复推送
            if(isset($order->status) && $order->status=='PAID'){
                Log::info('has_pay:'.$trade_no);
                echo 'success';
                exit();
            }

            $pay_merchant = DB::table('pay_merchants')->select('appid','source','config_info')->where('id',$order->pay_merchant_id)->where('is_enabled',1)->first();

            //校验签名
            $sign = _sign($data,json_decode($pay_merchant->config_info,1)['appKey']);
            if($sign != $data['sign']) die('failed');

            $transaction_id = $data['chorderid'];

            if($data['status'] == 'success'){
                Log::info('wcback_palmpay_notify_sign_success');

                if($data['status'] == 'success')
                {
                    // 修改表比较多,开启事务
                    DB::beginTransaction();

                    // 更新Order
                    $order = OrderService::getByTradeNo($trade_no);

                    $uid = $order->uid;
                    $distribution_channel_id = $order->distribution_channel_id;
                    $product_id = $order->product_id;
                    $product = ProductService::getProductSingle($product_id);
                    $send_order_id = $order->send_order_id;
                    $price =$product->price;
                    Log::info('save_order_end');

                    Log::info($product);

                    Log::info('product_type:'.$product->type);

                    // 更新其他定制Order表
                    if($product->type == 'YEAR_ORDER'){
                        Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
                        $order_type = 'YEAR';
                        $this->yearOrder($uid,$distribution_channel_id,$price,$send_order_id);
                        $order->order_type = $order_type;
                        $order->status = 'PAID';
                        $order->pay_end_at = date('Y-m-d H:i:s');
                        $order->transaction_id = $transaction_id;
                        $order->save();
                    }elseif($product->type == 'BOOK_ORDER'){
                        Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
                        $order_type = 'BOOK';
                        $this->bookOrder($product_id,$uid,$send_order_id,$price,$distribution_channel_id);
                        $order->order_type = $order_type;
                        $order->status = 'PAID';
                        $order->pay_end_at = date('Y-m-d H:i:s');
                        $order->transaction_id = $transaction_id;
                        $order->save();
                    }elseif($product->type == 'TICKET_RECHARGE'){
                        Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
                        $order_type = 'RECHARGE';
                        $this->userCharge($product,$uid);
                        $order->order_type = $order_type;
                        $order->status = 'PAID';
                        $order->pay_end_at = date('Y-m-d H:i:s');
                        $order->transaction_id = $transaction_id;
                        $order->save();
                    }
                    $this->successPayPushMsg($uid,$product);
                    $this->orderStatistical($order);
                    DB::commit();
                    //redis 删除未支付的uid
                    try{
                        $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:'.$distribution_channel_id;
                        Redis::hdel($key,$uid);
                    }catch (\Exception $e){
                        Log::info('redis remote error-----------------------');
                        Log::info($e);
                    }

                }
            }else{
                Log::info('wcback_palmpay_notify_sign_fail');
            }
        }
        catch(\Exception $e){
            var_dump($e->getMessage());
            DB::rollBack();
            Log::info('receive_wcback_palmpay_ept:'.$e->getMessage());
        }
        Log::info('pay_callback_end');

        echo "success";
    }

    /**
     * 公众号签名@华灯初上
     * @param $params
     * @return string
     */
    protected function getSign($params,$key)
    {
        $url = $this->arr_to_url($params, false);
        $url = $url . '&key=' . $key;

        $sign = md5($url);
        return $sign;
    }

    /**
     * 公众号签名@华灯初上
     * @param $array
     * @param bool $has_sign
     * @return string
     */
    protected function arr_to_url($array, $has_sign = false)
    {
        ksort($array);
        reset($array);
        $arg = "";
        while (list ($name, $val) = each($array)) {
            if ($name == 'sign' && !$has_sign) continue;
            if (strpos($name, "_") === 0)
                continue;
            if (is_array($val))
                $val = join(',', $val);
            if ($val === "")
                continue;
            $arg .= $name . "=" . $val . "&";
        }
        $arg = substr($arg, 0, count($arg) - 2);

        return $arg;
    }

    /**
     * 单本充值会掉
     * @param $product_id
     * @param $uid
     * @param $send_order_id
     * @param $fee
     */
    protected function bookOrder($product_id,$uid,$send_order_id,$fee,$distribution_channel_id){
        $book_conf = BookConfigService::getBookByProduct($product_id);
        $insert_data['bid'] = isset($book_conf->bid)?$book_conf->bid:'';
        $insert_data['book_name'] = isset($book_conf->book_name)?$book_conf->book_name:'';
        $insert_data['uid'] = $uid;
        $insert_data['distribution_channel_id'] = $distribution_channel_id;
        $insert_data['fee'] = $fee;
        $insert_data['send_order_id'] = $send_order_id;
        $insert_data['charge_balance'] = 0;
        $insert_data['reward_balance'] = 0;
        Log::info('start_save_book_order');
        Log::info($insert_data);
        return BookOrderService::save_book_order($insert_data);
    }

    /**
     * 包年
     * @param $uid
     * @param $distribution_channel_id
     * @param $fee
     * @param $send_order_id
     * @return mixed
     */
    protected function yearOrder($uid,$distribution_channel_id,$fee,$send_order_id){
        Log::info('start_save_year_order');
        $insert_data['uid'] = $uid;
        $insert_data['distribution_channel_id'] = $distribution_channel_id;
        $insert_data['fee'] = $fee;
        $insert_data['send_order_id'] = $send_order_id;
        Log::info($insert_data);
        return YearOrderService::save_year_order($insert_data);
    }

    /**
     * 用户充值
     * @param $product
     * @param $uid\
     */
    protected function userCharge($product,$uid){
        $total = $product->price*100+$product->given;
        UserService::addBalance($uid,$total, $product->price*100,$product->given);
        Log::info('update_user_balance_end:'.$uid.' balance_add:'.$total);
    }

    /**
     * 添加订单
     * @param array $data
     * @return mixed
     */
    protected function createOrder(array $data){
        $insert_data = array();
        $insert_data['uid'] = $data['uid'];
        $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
        $insert_data['product_id'] = $data['product_id'];
        $insert_data['price'] = $data['price'];
        $insert_data['status'] = 'PAID';
        $insert_data['pay_type'] = 1;
        $insert_data['trade_no'] = $data['trade_no'];
        $insert_data['send_order_id'] = $data['send_order_id'];
        $insert_data['order_type'] = $data['order_type'];
        $insert_data['pay_merchant_source'] = $data['pay_merchant_source'];
        $insert_data['pay_merchant_id'] = $data['pay_merchant_id'];
        $insert_data['transaction_id'] = $data['transaction_id'];
        $insert_data['pay_end_at'] = date('Y-m-d H:i:s');
        $insert_data['create_ip'] =$data['create_ip'];
        $insert_data['from_bid'] =$data['bid'];
        Log::info('createOrder');
        Log::info($insert_data);
        return OrderService::save_order($insert_data);
    }

    /**
     * 添加位置付订单
     * @param $data
     * @return mixed
     */
    protected function createUnPayOrder($data){
        $data['status'] = 'UNPAID';
        $data['transaction_id'] = '';
        $data['pay_end_at'] = '0000-00-00 00:00:00';
        return OrderService::save_order($data);
    }

    /**
     * order total
     * @param $data
     *
     */
    protected function createOrderTotal($data){
        $data['status'] = 'UNPAID';
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['updated_at'] = date('Y-m-d H:i:s');
        DB::table('orders_total')->insert($data);
    }

    protected function  updateOrderTotal($trade_no,$transaction_id){
        $data['status'] = 'PAID';
        $data['transaction_id'] = $transaction_id;
        $data['pay_end_at'] = date('Y-m-d H:i:s');
        //DB::table('orders_total')->where('trade_no',$trade_no)->update($data);
    }

    protected function successPayPushMsg($uid,$product_info){
        try {
            $force_sub_info = $this->getSubscribe($uid);
            $data = UserService::getById($uid);
            if(!in_array($data->distribution_channel_id,[5,123])){
                //return false;
            }
            if($force_sub_info){
            $content_format = "您好,你已经成功充值\r\n\r\n会员:%s\r\n会员ID:%s\r\n充值金额:%s\r\n充值状态:充值成功\r\n如有疑问,请点击用户中心-联系客服\r\n\r\n><a href='%s'>点击继续阅读上次阅读</a>";
            if($product_info->type == 'YEAR_ORDER'){
                $money_text = $product_info->price.'元(尊贵的年费VIP会员)';
            }elseif ($product_info->type == 'TICKET_RECHARGE'){
                if($product_info->given){
                    $money_text = $product_info->price.'元('.($product_info->price*100).'书币+赠送'.$product_info->given.'书币)';
                }else{
                    $money_text = $product_info->price.'元('.($product_info->price*100).'书币)';
                }
            }else {
                $money_text = '未知';
            }
            $delay = 0;
            $url = env('PROTOCOL').'://site'.encodeDistributionChannelId($data->distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/continue';;
            $content = sprintf($content_format,$data->nickname?$data->nickname:'匿名',$uid,$money_text,$url);
            $res['openid'] = $force_sub_info->openid;
            $res['appid'] = $force_sub_info->appid;
            $res['content'] = $content;
            $res['type'] = 'one_task';
            $res['send_time'] = date("Y-m-d H:i:s");
            $res['task_id'] = md5('pay_success_push');
            $send_data=array(
                'send_time'=>date("Y-m-d H:i:s"),
                'data' => $res
            );
            dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
        }
        } catch (\Exception $e) {
            
        }

        return true;
    }

    private function orderStatistical($order){
        try{
            $send_data=array(
                'type'=>'order_info',
                'data' => $order->toArray()
            );
            $send = new SendStatisticsList($send_data);
            $job = ($send)->onConnection('rabbitmq')->delay(0)->onQueue('send_statistics_list');
            dispatch($job);
        }catch (\Exception $e){
            Log::info($e);
        }

    }
    private function getSubscribe($uid){
        return ForceSubscribeService::forceSubscribeUsersByUid(['uid'=>$uid]);
    }

    public function getSubscribeOfficialName($uid){
        $subscribe = $this->getSubscribe($uid);
        if($subscribe && isset($subscribe->appid)){
            $official = DB::table('official_accounts')->where('appid',$subscribe->appid)->select('nickname')->first();
            if($official && isset($official->nickname)){
                return $official->nickname;
            }
        }
        return '';
    }

    private function payAlert($pay_merchant_id,$trade_no='',$pay_info='',$n=0){
        $change_pay_id = 9;
        if($pay_merchant_id == 9){
            $change_pay_id = 12;
        }
        try{
            $time = (int)date('H');
            $change = false;
            if($time <= 8 || $time>=23){
                if($pay_info){
                    DB::table('distribution_channels')->where('pay_merchant_id',$pay_merchant_id)->update(['pay_merchant_id'=>$change_pay_id]);
                    $change = true;
                }
            }
            $phone_arr = ['15868100210','18072713392','15088790066','13858057394','18668029091','18668420256'];
            //$phone_arr = ['18668029091'];
            if($n){
                $content = '支付通道:'.$pay_merchant_id.',获取授权信息失败';
            }else{
                $content = '支付通道:'.$pay_merchant_id.',订单号为'.$trade_no.'支付异常,异常信息:'.json_encode($pay_info);
            }
            if($change){
                $content .= ',已经切换到支付通道:'.$change_pay_id;
            }
            foreach ($phone_arr as $phone){
                SMS::send($phone,$content);
            }
            file_put_contents(date('Y-m-d').'.txt',json_encode($pay_info),FILE_APPEND);
        }catch (\Exception $e){

        }
    }
}