<?php
namespace app\home\service;

use app\model\RechargeApplyModel;
use app\model\UserWithdrawalModel;
use think\facade\Log;


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

//    public $key         = "399fd368dbd84b3fbee7012f20ca0b45";
//    public $df_key      = 'UQBCGZ3IWBVWUUDXYU8ZDXZ2QOQAC2DN';
//    public $merchant    = "400029056";
    public $key = "";
    public $df_key = "";
    public $merchant = "";
    public $pay_url     = "https://payment.qeaepay.com/pay/web";//支付
    public $app_url     = "https://payment.qeaepay.com/pay/transfer";//代付
    public $bla_url     = "https://payment.qeaepay.com/query/balance";//查余额
    public $pay_type    = 122;// 商户后台通道编码

    public function __construct()
    {
        $this->key = env('QEAE_PAY.KEY');
        $this->df_key = env('QEAE_PAY.DF_KEY');
        $this->merchant = env('QEAE_PAY.MERCHANT');
    }


    /**
     * @param $orderId // 订单id
     * @param $amount
     * @return mixed
     */
    public function create_order($orderId,$amount)
    {
        // order_no
        $notify_url                     = env('PAY.NOTIFY_URL');
        $post_data['goods_name']        = $orderId; // 商品名称必传参数 不超过50字节
        $post_data['mch_id']            = $this->merchant; //商户号
        $post_data['mch_order_no']      = $orderId;  //商家订单号
        $post_data['order_date']        = date('Y-m-d H:i:s'); // 订单时间
        $post_data['pay_type']          = $this->pay_type; // 支付类型
        $post_data['trade_amount']      = $amount; //交易金额 当地货币 精确到元
        $post_data['version']           = '1.0'; //版本号
        $post_data['notify_url']        = $notify_url.url('qeaepay_notify');

        $sign                           = $this->generateQueryString($post_data,$this->key);
        $post_data['sign']              = $sign;
        $post_data['sign_type']         = 'MD5'; // 签名方式固定MD5
        $header                         = array("Content-Type:application/x-www-form-urlencoded");
        $res                            = $this->curlPost($this->pay_url,$post_data,10,$header);
        $result                         = json_decode($res,true);

        return $result;
    }
    // 异步回调处理
    public function qeaePayNotify($data){
        Log::info('收到QeaePay异步回调:'.json_encode($data));

        $sign=$this->generateQueryString($data,$this->key);

        if($sign==strtolower($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('QeaePay支付订单不存在:'.json_encode($data));
                }
            }else{
                Log::info('QeaePay支付订单支付失败:'.json_encode($data));
            }
        }else{
            Log::info('QeaePay签名校验失败:'.json_encode($data));
        }

    }
    // 代付
    public function apply_pay($orderId,$amount,$bank_account,$nike_name,$ifsc,$bank_code)
    {
        $notify_url=env('PAY.NOTIFY_URL');

        $post_data['sign_type']             = 'MD5'; // 加密方式
        $post_data['mch_id']                = $this->merchant; //商家编号
        $post_data['mch_transferId']        = $orderId; //商家转账订单号
        $post_data['transfer_amount']       = floor($amount); //转账金额
        $post_data['apply_date']            = date('Y-m-d H:i:s'); // 订单时间;
        $post_data['bank_code']             = $bank_code; // 收款银行代码
        $post_data['receive_name']          = $nike_name; // 收款银行户名
        $post_data['receive_account']       = $bank_account; // 收款银行账号
        $post_data['back_url']              = $notify_url.'/bs/qeaepay_appNotify';

        $post_data['remark']                = $ifsc;
        $sign=$this->generateQueryString($post_data,$this->df_key);
        $post_data['sign']=$sign;
        $header = array("Content-Type:application/x-www-form-urlencoded");
        $res=$this->curlPost($this->app_url,$post_data,10,$header);
        $result=json_decode($res,true);

        if($result['respCode']=='SUCCESS'){
            return [
                'code'=>200,
                'msg'=>'ok',
                'order_idx'=>$result['tradeNo'],
                'content'=>$res
            ];
        }else{
            return [
                'code'=>300,
                'msg'=>$result['errorMsg'],
                'order_idx'=>'',
                'content'=>$res
            ];
        }
    }
    public function qeaePayApplyNotify($data){
        Log::info('收到qeaepay代付异步回调:'.json_encode($data));
//        $data['reverse']=$data['reverse'] ? 'true':'false';

        $sign=$this->generateQueryString($data,$this->df_key);
        if($sign==strtolower($data['sign'])){
            if($data['tradeResult']==1){
                $order_info=UserWithdrawalModel::getUserDrawalInfo([
                    'order_no'=>$data['merTransferId']
                ]);
                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('qeaepay代付支付成功:'.json_encode($data));
                }else{
                    Log::info('qeaepay代付订单不存在:'.json_encode($data));
                }
            }else{
                UserWithdrawalModel::where([
                    'order_no'=>$data['orderId']
                ])->update([
                    'status'=>1,
                ]);
                Log::info('xdpay代付失败:'.json_encode($data));
            }
        }else{
            Log::info('xdpay代付签名校验失败:'.json_encode($data));
        }
    }
    public function getBalance()
    {
        $post_data['mch_id']=$this->merchant;
        $sign=$this->generateQueryString($post_data,$this->key);
        $post_data['sign']=$sign;
        $post_data['sign_type']='MD5';
        $header = array("Content-Type:application/x-www-form-urlencoded");
        $res=$this->curlPost($this->bla_url,$post_data,10,$header);
        $result=json_decode($res,true);
        if($result['respCode']=='SUCCESS'){
            return [
                'amount'=>$result['availableAmount']
            ];
        }else{
            return [
                'amount'=>0
            ];
        }
    }
    private 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) {
            // sign 和signType 不参与签名
            if($key!='sign' && $key != 'sign_type'){
                $query .= $key . '=' . strval($value) . '&';
            }
        }
        // 去除末尾的 '&'
        $query = rtrim($query, '&');
        // 在字符串末尾拼接私钥
        $query .= '&key=' . $privateKey;
        $sign=md5($query);
        return $sign;
    }


}