Paybank.php 5.4 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use addons\epay\library\Service;
use app\admin\model\Porder;
use think\Db;
use Yansongda\Pay\Pay;
use app\admin\model\Account;
use app\admin\model\User;
use app\admin\model\Message;
use app\admin\model\Car;
use app\admin\model\Product;
use think\Log;
use fast\Http;
use think\Validate;
use Exception;
/**
 * 企业付款接口**
 */
class Paybank extends Api
{
    protected  $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $user_id = '';//token存贮user_id
    protected $KEY = 'cccccccccccccccccccccccccccccc00'; //商户秘钥
    private $params;
    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->getUserId();
    }
    //获取公钥
    public function getPuyKey(){
        $this->params = [
            'mch_id'    => '1515355581',//商户ID
            'nonce_str' => md5(time()),
            'sign_type' => 'MD5',
        ];
        //将数据发送到接口地址
        $res = $this->send("https://fraud.mch.weixin.qq.com/risk/getpublickey");
        var_dump($res);exit;
    }

//    public function payBank(){
//        if($this->request->isPost()){
//            $rsa = new Rsa(file_get_contents(url('/addons/epay/certs/apiclient_cert.pem','','',true)), '');
//            $data = [
//                'enc_bank_no'         => $rsa->public_encrypt('1234342343234234'),//收款方银行卡号RSA加密
//                'enc_true_name'       => $rsa->public_encrypt('李明'),//收款方姓名RSA加密
//                'bank_code'           => '1002',//收款方开户行
//                'amount'              => '1',//付款金额
//            ];
//            $res = $this->bankPay($data);
//            var_dump($res);exit;
//        }else{
//            $this->error('请求方式错误');
//        }
//    }
//
//    private function bankPay($data){
//        //构建原始数据
//        $this->params = [
//            'mch_id'    => '1515355581',//商户号
//            'partner_trade_no'   => $this->auth->genPayOrderSn(),//商户付款单号
//            'nonce_str'           => md5(time()), //随机串
//            'enc_bank_no'         => $data['enc_bank_no'],//收款方银行卡号RSA加密
//            'enc_true_name'       => $data['enc_true_name'],//收款方姓名RSA加密
//            'bank_code'           => $data['bank_code'],//收款方开户行
//            'amount'              => $data['amount'],//付款金额
//        ];
//        //将数据发送到接口地址
//        return $this->send('https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank');
//    }

    public function send($url){
        $res = $this->sign();
        $xml = $this->ArrToXml($res);
        $returnData = $this->postData($url, $xml);
        var_dump($returnData);exit;
        return $this->XmlToArr($returnData);
    }
    public function sign(){
        return $this->setSign($this->params);
    }
    //数组转xml
    public function ArrToXml($arr)
    {
        if(!is_array($arr) || count($arr) == 0) return '';

        $xml = "<xml>";
        foreach ($arr as $key=>$val)
        {
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml;
    }
    /**
     * 获取签名
     * @param array $arr
     * @return string
     */
    public function getSign($arr){
        //去除空值
        $arr = array_filter($arr);
        if(isset($arr['sign'])){
            unset($arr['sign']);
        }
        //按照键名字典排序
        ksort($arr);
        //生成url格式的字符串
        $str = $this->arrToUrl($arr) . '&key=' .$this->KEY;
        return strtoupper(md5($str));
    }
    /**
     * 获取带签名的数组
     * @param array $arr
     * @return array
     */
    public function setSign($arr){
        $arr['sign'] = $this->getSign($arr);
        return $arr;
    }
    /**
     * 数组转URL格式的字符串
     * @param array $arr
     * @return string
     */
    public function arrToUrl($arr){
        return urldecode(http_build_query($arr));
    }

    //Xml转数组
    public function XmlToArr($xml){
        if($xml == '') return '';
        libxml_disable_entity_loader(true);
        $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $arr;
    }
    //发送数据
    public function postData($url,$postfields){

        $ch = curl_init();
        $params[CURLOPT_URL] = $url;    //请求url地址
        $params[CURLOPT_HEADER] = false; //是否返回响应头信息
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $postfields;
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        //以下是证书相关代码
        $params[CURLOPT_SSLCERTTYPE] = 'PEM';
        $params[CURLOPT_SSLCERT] = '../certs/apiclient_cert.pem';
        $params[CURLOPT_SSLKEYTYPE] = 'PEM';
        $params[CURLOPT_SSLKEY] = '../certs/apiclient_key.pem';

        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }
}