<?php
namespace app\home\service;


use app\model\PaymentListModel;
use app\model\RechargeApplyModel;
use app\model\StockMarketModel;
use app\model\UserWithdrawalModel;
use app\model\WalletListModel;
use app\utility\RsaUtility;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use think\facade\Db;
use think\facade\Log;


/**
 * 处理团队佣金业务
 */
class HTPayService extends BaseHomeService
{

    public $mch_id;
    public $sign_key;
    public function __construct()

    {
        $key=env('WALLET.NAME');
        $config=$this->getPayConfig($key);
        $this->mch_id=$config['mch_id'];
        $this->sign_key=$config['sign_key'];
    }
    public function qrNotify($data)
    {
        Log::info('收到合泰异步回调:'.json_encode($data));
        $sign=$this->generateQueryString($data,$this->sign_key);
        if($sign==strtoupper($data['sign'])){
            if($data['tradeResult']==1){
                $order_info=RechargeApplyModel::getOrderByNo([
                    'order_no'=>$data['mchOrderNo']
                ]);
                if($order_info && $data['amount']==$order_info['total_amount']){
                    if($order_info['status']==0){
                        return (new PayService())->dealPayNotify($order_info);
                    }
                }else{
                    Log::info('合泰支付订单不存在:'.json_encode($data));
                }
            }else{
                Log::info('合泰支付订单支付失败:'.json_encode($data));
            }
        }else{
            Log::info('合泰签名校验失败:'.json_encode($data));
        }
    }
    //代付通知
    public function arNotify($data){
        Log::info('收到合泰代付异步回调:'.json_encode($data));
        $sign=$this->generateQueryString($data,$this->sign_key);
        if($sign==strtoupper($data['sign'])){
            if($data['tradeResult']==1){
                $order_info=UserWithdrawalModel::getUserDrawalInfo([
                    'order_no'=>$data['mchOrderNo']
                ]);
                if($order_info && $order_info['status']==3){
                    UserWithdrawalModel::where([
                        'id'=>$order_info['id']
                    ])->update([
                        'status'=>4,
                        'deal_time'=>date('Y-m-d H:i:s')
                    ]);
                    Log::info('合泰代付支付成功:'.json_encode($data));
                }else{
                    Log::info('合泰代付订单不存在:'.json_encode($data));
                }
            }else{
                UserWithdrawalModel::where([
                    'order_no'=>$data['mchOrderNo']
                ])->update([
                    'status'=>1,
                ]);
                Log::info('合泰代付失败:'.json_encode($data));
            }
        }else{
            Log::info('合泰代付签名校验失败:'.json_encode($data));
        }

    }
    public function qrPay($order_no,$order_amount){
        $notify_url=env('PAY.NOTIFY_URL');
        $data['version']='3.0';
        $data['mch_id']=$this->mch_id;
        $data['notify_url']=$notify_url.url('htpay_notify');
        $data['mch_order_no']=$order_no;
        $data['trade_amount']=$order_amount;
        $data['order_date']=date('Y-m-d H:i:s');
        $data['point']=2;
        $data['pay_css']=rand(1,2);
        $data['mch_return_msg']=$order_amount;
        $data['sign_type']='md5';
        $data['sign']=$this->generateQueryString($data,$this->sign_key);
        $header = array("Content-Type:application/x-www-form-urlencoded");
        $http_url="https://api.hetaivip.com/pay/qr";
        $res=$this->curlPost($http_url,$data,10,$header);
        $result=json_decode($res,true);
        return $result;
    }
    //代付
    public function arPay($order_no,$order_amount,$bank_code,$bank_account,$nike_name){
        $notify_url=env('PAY.NOTIFY_URL');
        $data['sign_type']='md5';
        $data['mch_id']=$this->mch_id;
        $data['back_url']=$notify_url.url('arpay_notify');
        $data['resultid']=$order_no;
        $data['money']=$order_amount;
        $data['apply_date']=date('Y-m-d H:i:s');
        $data['nikename']=$nike_name;
        $data['banktype']=$bank_code;
        $data['account']=$bank_account;
        $data['sign']=$this->generateQueryString($data,$this->sign_key);
        $header = array("Content-Type:application/x-www-form-urlencoded");
        $http_url="https://api.hetaivip.com/pay/ar";
        $res=$this->curlPost($http_url,$data,10,$header);
        $result=json_decode($res,true);
        if($result['status']==1){
            return [
                'code'=>200,
                'msg'=>'ok',
                'order_idx'=>$result['orderNo'],
                'content'=>$res
            ];
        }else{
            return [
                'code'=>300,
                'msg'=>$result['message'],
                'order_idx'=>'',
                'content'=>$res
            ];
        }
    }
    public function getHtBalance(){
        $data['sign_type']='md5';
        $data['mch_id']=$this->mch_id;
        $data['sign']=$this->generateQueryString($data,$this->sign_key);
        $header = array("Content-Type:application/x-www-form-urlencoded");
        $http_url="https://api.hetaivip.com/query/balance";
        $res=$this->curlPost($http_url,$data,10,$header);
        $result=json_decode($res,true);
        if($result['status']==1){
            return [
                'amount'=>$result['availableAmount']
            ];
        }else{
            return [
                'amount'=>0
            ];
        }
    }

    /**
     * 传入数组进行HTTP POST请求
     */
    public function curlPost($url, $post_data = array(), $timeout = 5, $header = "", $data_type = "") {
        $header = empty($header) ? '' : $header;
        //支持json数据数据提交
        if($data_type == 'json'){
            $post_string = json_encode($post_data);
        }elseif($data_type == 'array') {
            $post_string = $post_data;
        }elseif(is_array($post_data)){
            $post_string = http_build_query($post_data, '', '&');
        }

        $ch = curl_init();    // 启动一个CURL会话
        curl_setopt($ch, CURLOPT_URL, $url);     // 要访问的地址
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // 对认证证书来源的检查   // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        //curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);     // Post提交的数据包
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);     // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        //curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     // 获取的信息以文件流的形式返回
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    private function generateQueryString($fields, $privateKey) {
        // 按ASCII顺序对字段名进行自然排序
        ksort($fields);

        // 将字段按 k=v 格式拼接成字符串
        $query = '';
        foreach($fields as $key => $value) {
            if($key!='sign'){
                $query .= $key . '=' . $value . '&';
            }
        }
        // 去除末尾的 '&'
        $query = rtrim($query, '&');
        // 在字符串末尾拼接私钥
        $query .= '&key=' . $privateKey;
        $sign=strtoupper(md5($query));
        return $sign;
    }

    private function getPayConfig($key)
    {
        $config=[
            'test'=>[
                'mch_id'=>'79385824',
                'sign_key'=>'3c3c71a4722e04d31e9e938dd0a927d0',
            ],
            'stock'=>[
                'mch_id'=>'79385824',
                'sign_key'=>'3c3c71a4722e04d31e9e938dd0a927d0',
            ],

        ];
        return $config[$key];
    }



}