<?php

namespace App\Libs\Pay\Merchants;

use App\Libs\PayClient;
use GuzzleHttp\Client;
use Log;

/**
 通联支付
 @author zhoulj
 */
class AllinPay {
    /*
	const APPID = '00014994';
	const CUSID = '514331048163570';
	const APPKEY = 'zhuishuyun2017#123';
	const APIURL = "https://vsp.allinpay.com/apiweb/unitorder";//生产环境
	const APIVERSION = '11';
    */
    private $appid ;
    private $cusid;
    public  $appkey;
    private $api_url;
    private $api_version;
    /*
    const APPID = '00015244';
    const CUSID = '515331048168685';
    const APPKEY = 'zhuishuyun2017#124';
    const APIURL = "https://vsp.allinpay.com/apiweb/unitorder";//生产环境
    const APIVERSION = '11';
    */
	public $PayClient;
	
	function __construct($config)
	{
        $this->appid = $config['appid'];
        $this->cusid = $config['cusid'];
        $this->appkey = $config['appkey'];
        $this->api_url = $config['api_url'];
        $this->api_version = $config['api_version'];
		//$this->PayClient = new PayClient(self::APIURL,20);
		$this->PayClient = new PayClient($this->api_url,10);
	}

	//订单发送
	function send($data)
	{
        $data = [
            'cusid'=>$this->cusid,
            'appid'=>$this->appid,
            'version'=>$this->api_version,
            'trxamt'=>$data['price'],
            'reqsn'=>$data['trade_no'],
            'paytype'=>'W02',
            'randomstr'=>md5(time()),
            'body'=>$data['body'],
            'remark'=>$data['remark'],
            'acct'=>$data['openid'],
            'notify_url'=>env('ALLINPAY_PAY_NOFITY_URL'),
        ];

		Log::info('allinpay_client_request_data');
		Log::info($data);
		
		$data['sign'] = $this->SignArray($data,$this->appkey);//签名;
		$response = $this->PayClient->get($this->api_url."/pay",$data);

		return $this->getPayInfo($response);
	}



	//验签
	function validSign($array){
		if("SUCCESS"==$array["retcode"]){
			$signRsp = strtolower($array["sign"]);
			$array["sign"] = "";
			$sign =  strtolower($this->SignArray($array, $this->appkey));
			if($sign==$signRsp){
				return TRUE;
			}
			else {
				echo "验签失败:".$signRsp."--".$sign;
			}
		}
		else{
			echo $array["retmsg"];
		}
	
		return FALSE;
	}
	
	/**
	 * 校验签名
	 */
	public function NotifyValidSign(array $array,$appkey){
		$sign = $array['sign'];
		unset($array['sign']);
		//$array['key'] = $appkey;
		$mySign = $this->SignArray($array, $appkey);
		return strtolower($sign) == strtolower($mySign);
	}
	
	/**
	 * 将参数数组签名
	 */
	public  function SignArray(array $array,$appkey){
		$array['key'] = $appkey;// 将key放到数组中一起进行排序和组装
		ksort($array);
		$blankStr = $this->ToUrlParams($array);
		$sign = md5($blankStr);
		return $sign;
	}
	
	public  function ToUrlParams(array $array)
	{
		$buff = "";
		foreach ($array as $k => $v)
		{
			if($v != "" && !is_array($v)){
				$buff .= $k . "=" . $v . "&";
			}
		}
	
		$buff = trim($buff, "&");
		return $buff;
	}
	
	function getPayInfo($response)
	{
		Log::info($response);
		if($this->validSign($response)){
			Log::info('valid_success2');
            if(isset($response['payinfo'])){
                $payinfo = $response['payinfo'];
                Log::info($payinfo);
                $payinfo = json_decode($payinfo,true);
                Log::info($payinfo);
                return $payinfo;
            }else{
                return $response;
            }

		}else{
			Log::info('valid_fail');
			return null;
		}
	}

	

}