<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/31 0031
 * Time: 下午 4:51
 */

namespace app\lib\utils\WXPay;

use app\admin\model\users\Withdraw;
use think\Config;
use think\Log;

class WXPayConstants
{
    //'配置'
    public $APPID = ""; //appid
    public $AppSecret = ""; //appid密钥
    public $mch_id = ""; //商户账号
    public $secret = ""; //商户密钥
    public $wxpayCert = ""; //商户证书
    public $wxPayCertKey = ""; //商户证书密钥
    public $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //微信支付
    public $refund_url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //微信退款
    public $pay_url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; //微信企业支付零钱
    public $pay_bank_url = "https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank"; //微信企业支付银行卡
    public $public = "";
    public $public_pkcs8 = "";

    /**
     * 配置
     * WXPayConstants constructor.
     */
    public function __construct()
    {
        //        $appconfig = SettingModel::getConfigValue('miniconfig');
        $appconfig = Config::get('testConfig');
        $this->APPID = $appconfig['app_id'];
        $this->AppSecret = $appconfig['appSecret'];
        $this->mch_id = $appconfig['mch_id'];
        $this->secret = $appconfig['key'];
        $this->wxpayCert = $appconfig['cert_path'];
        $this->wxPayCertKey = $appconfig['key_path'];
        $this->public = $appconfig['public'];
        $this->public_pkcs8 = $appconfig['public_pkcs8'];
        //        $this->wxpayCert=Config::get("appConfig.wxpayCert");
        //        $this->wxPayCertKey=Config::get("appConfig.wxPayCertKey");
    }

    public  function getOppid($code)
    {
        $data = array(
            'appid' => $this->APPID,
            'secret' => $this->AppSecret,
            'js_code' => $code,
            'grant_type' => 'authorization_code',
        );
        $data = http_build_query($data);
        $opts = array(
            'http' => array(
                'method' => 'POST',
                'header' => "Content-type: application/x-www-form-urlencodedrn",
                "Content-Length: " . strlen($data) . "rn",
                'content' => $data
            )
        );
        $context = stream_context_create($opts);
        $html = file_get_contents('https://api.weixin.qq.com/sns/jscode2session', false, $context);
        return $html;
    }

    /**获取access_token
     * @return string
     */
    public function getAccessToken()
    {
        //获取access_token
        $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->APPID&secret=$this->AppSecret";
        //        session_start();
        $_SESSION['access_token'] = "";
        $_SESSION['expires_in'] = 0;
        $ACCESS_TOKEN = "";
        if (!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() > $_SESSION['expires_in'])) {
            $json = WXPayUtils::httpRequest($access_token);
            $json = json_decode($json, true);
            $_SESSION['access_token'] = $json['access_token'];
            $_SESSION['expires_in'] = time() + 7200;
            return $ACCESS_TOKEN = $json["access_token"];
        } else {
            return $ACCESS_TOKEN =  $_SESSION["access_token"];
        }
    }

    /**获取电话号码
     * @return string
     */
    public static function getuserphonenumber($code, $error)
    {
        $that = new static();
        $access_token = $that->getAccessToken();
        $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $access_token;
        if ($code) {
            $json = patchCurl($url, ['code' => $code]);
            if ($json->errcode == 0) {
                return $json->phone_info->purePhoneNumber;
            }
            $error->code = -3;
            $error->msg = $json->errmsg;
            return $error;
        }
        $error->code = -3;
        $error->msg = "获取code失败";
        return $error;
    }

    /**微信企业支付零钱
     * @param $openid
     * @param $out_trade_no
     * @param $total_fee
     * @param $re_user_name
     * @param $error
     * @return string
     */
    public static function enterprisePayment($openid, $body, $out_trade_no, $total_fee, $re_user_name, $error, $login_type = '')
    {
        $that = new static();
        $nonce_str = WXPayUtils::nonce_str();
        if (empty($openid)) {
            $error->code = -4;
            $error->msg = "获取openid失败";
            return $error;
        }
        $params = [
            "mch_appid"          =>  $that->APPID,
            "mchid"         =>  $that->mch_id,
            "nonce_str"     =>  $nonce_str, // 随机串，32字符以内
            "partner_trade_no" =>  $out_trade_no,
            "openid"        =>  $openid,
            "check_name"    => "FORCE_CHECK",
            "re_user_name" => $re_user_name,
            "amount"    =>   floatval($total_fee), //floatval($total_fee * 100),// 订单费用，单位：分
            "spbill_create_ip" =>    $_SERVER['SERVER_ADDR'],
            "desc"      => $body,
        ];
        //按照要求计算sign
        $params['sign'] =  WXPayUtils::sign($params, $that->secret);
        // 给微信发出的请求，整个参数是个XML
        $post_xml = WXPayUtils::post_xml($params);
        //请求是否成功
        $output = WXPayUtils::http_request($that->pay_url, $post_xml, $that->wxpayCert, $that->wxPayCertKey);
        if (false === $output) {
            $error->code = -10;
            $error->msg = "请求第三方支付失败";
            return $error;
        }
        $array = WXPayUtils::xml($output); //全要大写
        Log::write("返回值" . json_encode($array));
        $tmp = []; //临时数组用于签名
        if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS') {
            $time = time();
            $tmp['partner_trade_no'] = $array['PARTNER_TRADE_NO'];
            $tmp['payment_no'] = $array['PAYMENT_NO'];
            return $tmp;
        } else if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'FAIL') {
            if ($array["ERR_CODE"] == "NO_AUTH") {
                $error->code = -3;
                $error->msg = $_SERVER['REMOTE_ADDR'] . "没有该接口权限";
                return $error;
            } else if ($array["ERR_CODE"] == "AMOUNT_LIMIT") {
                $error->code = -3;
                $error->msg = "提现金额超过限制";
                return $error;
            } else if ($array["ERR_CODE"] == "PARAM_ERROR") {
                $error->code = -3;
                $error->msg = "参数错误";
                return $error;
            } else if ($array["ERR_CODE"] == "OPENID_ERROR") {
                $error->code = -3;
                $error->msg = "Openid错误，请重新登录后提现";
                return $error;
            } else if ($array["ERR_CODE"] == "NOTENOUGH") {
                $error->code = -3;
                $error->msg = "余额不足";
                return $error;
            } else if ($array["ERR_CODE"] == "SYSTEMERROR") {
                $error->code = -3;
                $error->msg = "系统繁忙，请稍后再试。";
                return $error;
            } else if ($array["ERR_CODE"] == "NAME_MISMATCH") {
                $error->code = -3;
                $error->msg = "填写姓名与微信真实姓名不一致";
                return $error;
            } else if ($array["ERR_CODE"] == "SIGN_ERROR") {
                $error->code = -3;
                $error->msg = "签名错误，请重新登录后提现";
                return $error;
            } else if ($array["ERR_CODE"] == "MONEY_LIMIT") {
                $error->code = -3;
                $error->msg = "已经达到今日付款总额上限";
                return $error;
            } else {
                $error->code = -3;
                $error->msg = $array["ERR_CODE_DES"];
                return $error;
            }
        }
    }

    /*
     * 企业付款到银行卡接口
      * @params string $out_trade_no : 商户订单号
      * @params int $amount : 付款金额，单位分
      * @params string $enc_bank_no : 收款方银行卡号
      * @params string $enc_true_name : 收款方用户名
      * @params string $bank_name : 收款方开户行,根据银行名称获取银行编号bank_code
      * @params string $desc : 付款备注
      * return string $payment_no ：支付成功的订单号
     */
    public static function payForBank($out_trade_no, $amount, $enc_bank_no, $enc_true_name, $bank_code, $desc = '企业付款到银行卡', $error)
    {
        $error->code = -10;
        $error->msg = "暂不支持打款到银行卡";
        return $error;
        $that = new static();
        $data['amount'] = $amount;
        $data['bank_code'] = $bank_code;
        $data['desc'] = $desc;
        $data['enc_bank_no'] = $that->publicEncrypt($enc_bank_no);
        $data['enc_true_name'] = $that->publicEncrypt($enc_true_name);
        $data['mch_id'] = $that->mch_id;
        $data['nonce_str'] = $that->random(12);
        $data['partner_trade_no'] = $out_trade_no;
        //按照要求计算sign
        $data['sign']  =  WXPayUtils::sign($data, $that->secret);
        // 给微信发出的请求，整个参数是个XML
        $post_xml = WXPayUtils::post_xml($data);
        //请求是否成功
        $output = WXPayUtils::http_request($that->pay_bank_url, $post_xml, $that->wxpayCert, $that->wxPayCertKey);
        if (false === $output) {
            $error->code = -10;
            $error->msg = "请求第三方支付失败";
            return $error;
        }
        $array = WXPayUtils::xml($output); //全要大写
        Log::write("返回值" . json_encode($array));
        $tmp = []; //临时数组用于签名
        if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS') {
            $time = time();
            $tmp['partner_trade_no'] = $array['PARTNER_TRADE_NO'];
            $tmp['payment_no'] = $array['PAYMENT_NO'];
            return $tmp;
        } else if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'FAIL') {
            $error->code = -3;
            $error->RETURN_CODE = $array['RETURN_CODE'];
            $error->RESULT_CODE = $array['RESULT_CODE'];
            $error->msg = $array["ERR_CODE_DES"];
            return $error;
        }
    }
    //微信退款功能
    public function wxminiOutRefund($out_trade_no, $total_fee, $error, $login_type = '')
    {
        $config = SettingModel::getConfigValue('miniconfig');
        $wxRefundResponseURl = $config['NotifyUrl'];
        if ($login_type == 'app') {
            $config = SettingModel::getConfigValue('appconfig');
            $this->APPID = $config['app_id'];
            $wxRefundResponseURl = $config['NotifyUrl'];
        }
        $nonce_str = WXPayUtils::nonce_str();
        $params = [
            "appid"          =>  $this->APPID,
            "mch_id"         =>  $this->mch_id,
            "nonce_str"     =>  $nonce_str, // 随机串，32字符以内
            "out_trade_no" =>  $out_trade_no,
            "out_refund_no" =>  $out_trade_no,
            "total_fee"    =>   floatval($total_fee), //floatval($total_fee * 100),// 订单费用，单位：分
            "refund_fee" =>  floatval($total_fee),
            "notify_url"   => $wxRefundResponseURl,
        ];
        //按照要求计算sign
        $params['sign'] =  WXPayUtils::sign($params, $this->secret); //商户秘钥
        // 给微信发出的请求，整个参数是个XML
        $post_xml = WXPayUtils::post_xml($params);
        //请求是否成功
        $output = WXPayUtils::http_request($this->refund_url, $post_xml, $this->wxpayCert, $this->wxPayCertKey);
        if (false === $output) {
            $error->code = -10;
            $error->msg = "请求第三方支付失败";
            echo $error->msg;
            return $error;
        }
        $array = WXPayUtils::xml($output); //全要大写
        $data = []; //临时数组用于签名
        //Log::record("==app支付调起 <pre>".json_encode($array). "<pre>" . "失败", 'debug');//记录错误
        if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS') {
            $data['appid'] = $array['APPID'];
            $data['mch_id'] = $array['MCH_ID'];
            $data['refund_fee'] = $array['REFUND_FEE'];
            $data['total_fee'] = $array['TOTAL_FEE'];
            return $data;
        } else if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'FAIL') {
            if ($array["ERR_CODE"] == "BIZERR_NEED_RETRY") {
                $error->code = -3;
                $error->msg = "并发情况下，业务被拒绝，商户重试即可解决";
                //echo $error->msg;
                return $error;
            } else if ($array["ERR_CODE"] == "TRADE_OVERDUE") {
                $error->code = -3;
                $error->msg = "	订单已经超过可退款的最大期限(支付后一年内可退款)";
                //echo $error->msg;
                return $error;
            } else if ($array["ERR_CODE"] == "ERROR") {
                $error->code = -3;
                $error->msg = "申请退款业务发生错误";
                //echo $error->msg;
                return $error;
            } else if ($array["ERR_CODE"] == "USER_ACCOUNT_ABNORMAL") {
                $error->code = -3;
                $error->msg = "用户帐号注销";
                //echo $error->msg;
                return $error;
            } else if ($array["ERR_CODE"] == "SIGNERROR") {
                $error->code = -3;
                $error->msg = "签名错误";
                //echo $error->msg;
                return $error;
            } else if ($array["ERR_CODE"] == "NOTENOUGH") {
                $error->code = -3;
                $error->msg = "商户可用退款余额不足";
                //echo $error->msg;
                return $error;
            } else {
                $error->code = -3;
                $error->msg = "对不起，微信退款接口调用错误!";
                //echo $error->msg;
                return $error;
            }
        }
    }


    /**
     * 公钥加密，银行卡号和姓名需要RSA算法加密
     * @param string $data  需要加密的字符串，银行卡/姓名
     * @return null|string  加密后的字符串
     */
    private function publicEncrypt($data)
    {
        //        $public_Pkcs8=SettingModel::getConfigChildValue('appconfig',"publicPkcs8");
        //        $publicPkcs8=APP_PATH."admin/utils/".$public_Pkcs8;
        $publicPkcs8 = $this->public_pkcs8;
        // 进行加密
        $pubkey = openssl_pkey_get_public(file_get_contents($publicPkcs8));
        $encrypt_data = '';
        $encrypted = '';
        $r = openssl_public_encrypt($data, $encrypt_data, $pubkey, OPENSSL_PKCS1_OAEP_PADDING);
        if ($r) { //加密成功，返回base64编码的字符串
            return base64_encode($encrypted . $encrypt_data);
        } else {
            return false;
        }
    }
    /*
     * 获取公钥,格式为PKCS#1 转PKCS#8
     * openssl rsa -RSAPublicKey_in -in  <filename> -out <out_put_filename>
     * */

    private function get_pub_key()
    {
        $config = SettingModel::getConfigValue('miniconfig');
        $rsafile = $config['rsafile'];
        if (!is_file($rsafile)) {
            $data['mch_id'] = $this->mch_id;
            $data['nonce_str'] = $this->random(12);
            $data['sign']  =  WXPayUtils::sign($data, $this->secret);
            $post_xml = WXPayUtils::post_xml($data);

            $url = 'https://fraud.mch.weixin.qq.com/risk/getpublickey';
            $output = WXPayUtils::http_request($url, $post_xml, $this->wxpayCert, $this->wxPayCertKey);
            $ret = WXPayUtils::xml($output); //全要大写
            if ($ret['RETURN_CODE'] == 'SUCCESS' && isset($ret['PUB_KEY'])) {
                file_put_contents($rsafile, $ret['PUB_KEY']);
                return $ret['pub_key'];
            } else {
                return null;
            }
        } else {
            return file_get_contents($rsafile);
        }
    }

    /*
    *  随机字符串
     * @param int $length 长度
     * @param string $type 类型
     * @param int $convert 转换大小写 1大写 0小写
     * @return string
    */
    private function random($length = 10, $type = 'letter', $convert = false)
    {
        $config = array(
            'number' => '1234567890',
            'letter' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            'string' => 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
            'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
        );

        if (!isset($config[$type])) $type = 'letter';
        $string = $config[$type];

        $code = '';
        $strlen = strlen($string) - 1;
        for ($i = 0; $i < $length; $i++) {
            $code .= $string[mt_rand(0, $strlen)];
        }
        if (!empty($convert)) {
            $code = ($convert > 0) ? strtoupper($code) : strtolower($code);
        }
        return $code;
    }


    /**
     * @notes 商家转账到零钱
     */
    public static function transfer($openid, $body, $out_trade_no, $total_fee, $re_user_name, $error, $login_type = '')
    {
        $that = new static();
        //请求URL
        $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
        //请求方式
        $http_method = 'POST';
        //请求参数
        $data = [
            'appid' => $that->APPID, //申请商户号的appid或商户号绑定的appid（企业号corpid即为此appid）
            'out_batch_no' => 'ycz' . date('Ymd') . mt_rand(100000, 999999), //商户系统内部的商家批次单号，要求此参数只能由数字、大小写字母组成，在商户系统内部唯一
            'batch_name' => $body, //该笔批量转账的名称
            'batch_remark' => $body, //转账说明，UTF8编码，最多允许32个字符
            'total_amount' => floatval($total_fee), //转账金额单位为“分”。转账总金额必须与批次内所有明细转账金额之和保持一致，否则无法发起转账操作
            'total_num' => 1, //一个转账批次单最多发起三千笔转账。转账总笔数必须与批次内所有明细之和保持一致，否则无法发起转账操作
            'transfer_detail_list' => [
                [ //发起批量转账的明细列表，最多三千笔
                    'out_detail_no' => $out_trade_no, //商户系统内部区分转账批次单下不同转账明细单的唯一标识，要求此参数只能由数字、大小写字母组成
                    'transfer_amount' => floatval($total_fee), //转账金额单位为分
                    'transfer_remark' => $body, //单条转账备注（微信用户会收到该备注），UTF8编码，最多允许32个字符
                    'openid' => $openid, //openid是微信用户在公众号appid下的唯一用户标识（appid不同，则获取到的openid就不同），可用于永久标记一个用户
                ]
            ]
        ];

        $token  = self::token($url, $http_method, $data); //获取token
        Log::write("token:" . $token);
        $result = self::https_request($url, json_encode($data), $token); //发送请求
        $result_arr = json_decode($result, true);
        if (!isset($result_arr['create_time'])) { //批次受理失败
            Log::write("提现异常:" . $result_arr['message']);
            $error->code = -3;
            $error->msg = $result_arr['message'];
            return $error;
        }

        Withdraw::where(['cash_sn' =>$out_trade_no])->update(['out_batch_no' =>$data['out_batch_no']]);//更新商家批次单号

        return true;
    }

    /**
     * @notes 获取签名
     */
    public static function token($url, $http_method, $data)
    {
        $that = new static();
        $timestamp   = time(); //请求时间戳
        $url_parts   = parse_url($url); //获取请求的绝对URL
        $nonce       = $timestamp . rand('10000', '99999'); //请求随机串
        $body        = empty($data) ? '' : json_encode((object)$data); //请求报文主体
        $stream_opts = [
            "ssl" => [
                "verify_peer" => false,
                "verify_peer_name" => false,
            ]
        ];

        $apiclient_cert_arr = openssl_x509_parse(file_get_contents($that->wxpayCert, false, stream_context_create($stream_opts)));
        $serial_no          = $apiclient_cert_arr['serialNumberHex']; //证书序列号
        $mch_private_key    = file_get_contents($that->wxPayCertKey, false, stream_context_create($stream_opts)); //密钥
        $merchant_id = $that->mch_id; //商户id
        $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
        $message = $http_method . "\n" .
            $canonical_url . "\n" .
            $timestamp . "\n" .
            $nonce . "\n" .
            $body . "\n";
        openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
        $sign = base64_encode($raw_sign); //签名
        $schema = 'WECHATPAY2-SHA256-RSA2048';
        $token = sprintf(
            'mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
            $merchant_id,
            $nonce,
            $timestamp,
            $serial_no,
            $sign
        ); //微信返回token
        return $schema . ' ' . $token;
    }

    /**
     * @notes 发送请求
     */
    public static function https_request($url, $data, $token)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, (string)$url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //添加请求头
        $headers = [
            'Authorization:' . $token,
            'Accept: application/json',
            'Content-Type: application/json; charset=utf-8',
            'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
        ];
        if (!empty($headers)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * @notes 敏感信息加解密
     */
    public static function getEncrypt($str)
    {
        $that = new static();
        //$str是待加密字符串
        $public_key = file_get_contents($that->wxpayCert);
        $encrypted = '';
        if (openssl_public_encrypt($str, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING)) {
            //base64编码
            $sign = base64_encode($encrypted);
        } else {
            throw new \Exception('encrypt failed');
        }
        return $sign;
    }

    /**
     * @notes 商家明细单号查询
     */
    public static function detailsQuery($withdrawApply,$error)
    {
        //请求URL
        $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches/out-batch-no/' . $withdrawApply['out_batch_no'] . '/details/out-detail-no/' . $withdrawApply['cash_sn'];
        //请求方式
        $http_method = 'GET';
        //请求参数
        $data = [];
        $token  = self::token($url, $http_method, $data); //获取token
        $result = self::https_request($url, $data, $token); //发送请求
        $result_arr = json_decode($result, true);
        if(isset($result_arr['code']) && !empty($result_arr['code'])){
            $error->fail_reason = $result_arr['code'];
            $error->code = -3;
            $error->msg = $result_arr['message'];
            return $error;
        }else if(isset($result_arr['fail_reason']) && !empty($result_arr['fail_reason'])){
            $error->fail_reason = $result_arr['fail_reason'];
            if($result_arr['fail_reason'] === "ACCOUNT_FROZEN"){
                $error->code = -3;
                $error->msg = "该用户账户被冻结";
                return $error;
            }else if($result_arr['fail_reason'] === "REAL_NAME_CHECK_FAIL"){
                $error->code = -3;
                $error->msg = "收款人未实名认证，需要用户完成微信实名认证";
                return $error;
            }else if($result_arr['fail_reason'] === "NAME_NOT_CORRECT"){
                $error->code = -3;
                $error->msg = "收款人姓名校验不通过，请核实信息";
                return $error;
            }else if($result_arr['fail_reason'] === "OPENID_INVALID"){
                $error->code = -3;
                $error->msg = "Openid格式错误或者不属于商家公众账号";
                return $error;
            }else if($result_arr['fail_reason'] === "TRANSFER_QUOTA_EXCEED"){
                $error->code = -3;
                $error->msg = "超过用户单笔收款额度，核实产品设置是否准确";
                return $error;
            }else if($result_arr['fail_reason'] === "DAY_RECEIVED_QUOTA_EXCEED"){
                $error->code = -3;
                $error->msg = "超过用户单日收款额度，核实产品设置是否准确";
                return $error;
            }else if($result_arr['fail_reason'] === "DAY_RECEIVED_COUNT_EXCEED"){
                $error->code = -3;
                $error->msg = "超过用户单日收款次数，核实产品设置是否准确";
                return $error;
            }else if($result_arr['fail_reason'] === "TRANSFER_RISK"){
                $error->code = -3;
                $error->msg = "该笔转账可能存在风险，已被微信拦截";
                return $error;
            }else {
                $error->code = -3;
                $error->msg = $result_arr['fail_reason'];
                //$error->msg = "对不起，查询微信转账明细接口调用错误!";
                return $error;
            }
        }
        return true;
    }
}
