<?php
namespace Biz\Pay\Payment;
/**
 * 银宝支付
 */
class YinBaoPayment extends \Biz\Pay\BasePayment {

	protected $getWay = "http://webapi.9vpay.com/zfapi/users/paiditem";
	protected $paymentName = "YinBao";
	function init() {
		$this->privateCertPath = '';
		$this->publicCertPath = '';
		$this->sellerInfo = array(
			'busKey' => 'c96b2a7f77977aeaa7b281e30159d1ed',
			'busAccount' => '24332', //
		);
		# code...
	}
	public function toPay() {
		print_r($this->dataAccess);
		$des = new DES3($this->sellerInfo['busKey']);
		$data = array(
			'version' => 'v1.0.0.0',
			'mchtid' => $this->sellerInfo['busAccount'],
			'amount' => $des->encrypt($this->dataAccess->money),
			'bankaccountname' => $des->encrypt($this->dataAccess->extra['accoutName']),
			'bankaccountno' => $des->encrypt($this->dataAccess->extra['accountNo']), //360cd.cn
			'branchname' => $des->encrypt($this->dataAccess->extra['bankName']), //360cd.cn
			'bankcode' => $des->encrypt($this->dataAccess->bankCode), //360cd.cn
			'transtime' => time(), //360cd.cn
			'transid' => $this->dataAccess->orderSn, //360cd.cn
		);
		$sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
		$data['notifyurl'] = $this->notifyUrl;
		$data['sign'] = $sign;
		$data = json_encode($data, 320);
		print_r($data);
		$header = array();
		$header[] = "Content-Type: application/json; charset=utf-8";
		$header[] = "Content-Length: " . strlen($data);
		$data = $this->goPayCurl($this->getWay, 'POST', $data, $header);
		print_r($data);
		$data = json_decode($data, 1);
		$code = $row['code'];
		if ($code == 1) {
			$data = $row['data'];
			$md5 = $this->md5mcrypt($row['data'], $this->sellerInfo['busKey']);
			if ($md5 == $data['sign']) {
				echo '代付返回签名正确!  结果描述：' . $row['info'];
			} else {
				echo '返回签名错误!';
			}
		} else {
			echo '系统提示：' . $row['info'];
			exit();
		}
		exit;

	}

	function md5mcrypt($data, $key) {
		ksort($data);
		$str = http_build_query($data) . $key;
		$str = urldecode($str);
		return md5($str);
	}

	public function redirect($order) {

	}

	public function notify($order) {
		$re = $this->check($order);
		if ($re) {
			return 'ok';
		}
		return '';
	}

	function check($order) {
		$data = array();
		$des = new DES3($this->sellerInfo['busKey']);
		$data['mchtid'] = $_POST['mchtid'];
		$this->dataAccess->orderSn = $data['orderno'] = $_POST['orderno'];
		$data['state'] = $_POST['state'];
		$this->dataAccess->money = $data['amount'] = $des->decrypt($_POST['amount']);
		$sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
		$this->dataAccess->goodsName = '';
		$data['sign'] = $_POST['sign'];
		if ($sign == $data['sign']) {
			if ($data['state'] == '1') {
				return 1;
			} else {
				return 0;
			}
		}
	}

	public function getPaymentType() {
		# code...
		return array(
			"alipay" => "1", //"支付宝",
			"weixin" => "2", //"微信",
			"baidupay" => "4", //"百度钱包",
			"qqpay" => "8", //QQ钱包",
			"jdpay" => "16", //京东钱包",
			"union" => "32", //"银联钱包",
		);
	}
}

class DES3 {
	var $key = "111111111111111111111111";
	var $iv = "00000000";
	public function __construct($key) {
		$this->key = $key;
	}
	public function encrypt($input) {
		$size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
		$input = $this->pkcs5_pad($input, $size);
		$key = str_pad($this->key, 24, '0');
		$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
		if ($this->iv == '') {
			$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
		} else {
			$iv = $this->iv;
		}
		@mcrypt_generic_init($td, $key, $iv);
		$data = mcrypt_generic($td, $input);
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		$data = base64_encode($data);
		return $data;
	}
	public function decrypt($encrypted) {
		$encrypted = base64_decode($encrypted);
		$key = str_pad($this->key, 24, '0');
		$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
		if ($this->iv == '') {
			$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
		} else {
			$iv = $this->iv;
		}
		$ks = mcrypt_enc_get_key_size($td);
		@mcrypt_generic_init($td, $key, $iv);
		$decrypted = mdecrypt_generic($td, $encrypted);
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		$y = $this->pkcs5_unpad($decrypted);
		return $y;
	}
	function pkcs5_pad($text, $blocksize) {
		$pad = $blocksize - (strlen($text) % $blocksize);
		return $text . str_repeat(chr($pad), $pad);
	}
	function pkcs5_unpad($text) {
		$pad = ord($text{strlen($text) - 1});
		if ($pad > strlen($text)) {
			return false;
		}
		if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
			return false;
		}
		return substr($text, 0, -1 * $pad);
	}
	function PaddingPKCS7($data) {
		$block_size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
		$padding_char = $block_size - (strlen($data) % $block_size);
		$data .= str_repeat(chr($padding_char), $padding_char);
		return $data;
	}
}
?>