<?php
namespace Biz\Pay\Payment;
/**
 * 赢通宝支付
 */
class ETongBaoPayment extends \Biz\Pay\BasePayment {

	protected $getWay = "http://api.ytbao.net/passivePay";
	protected $paymentName = "ETongBao";
	protected $charset = "GBK";

	function init() {
		$this->sellerInfo = array(
			'busKey' => "C7388B4909B19274E219DECE40FD6E42",
			'busAccount' => '900371455510001', //360cd.cn
		);
	}

	public function toPay() {
		$payType = 0;
		$payment_list = $this->getPaymentType();
		if (isset($payment_list[$this->dataAccess->payType])) {
			$payType = $payment_list[$this->dataAccess->payType];
		} else {
			JsonReturn('', -3, '支付类型不正确');
		}
		$data = array();

		$data['merchno'] = $this->sellerInfo['busAccount'];
		$data['amount'] = $this->dataAccess->money;
		$data['traceno'] = $this->dataAccess->orderSn;
		$data['payType'] = $payType;
		$data['goodsName'] = iconv('utf-8', 'gbk', $this->dataAccess->goodsName);
		$data['notifyUrl'] = $this->notifyUrl;

		$data['signature'] = $this->md5mcrypt($data, $this->sellerInfo['busKey']);

		// print_r($data);
		$data = $this->goPayCurl($this->getWay, 'POST', $data);

		$data = iconv('gb2312', 'utf-8', $data);
		if (empty($data)) {
			JsonReturn(array(), -1, "支付异常请联系管理员");
		}
		$data = json_decode($data, 1);
		if ($data['respCode'] == '00') {
			$uri = $data['barCode'];
			if ($this->returnType == 'json') {
				JsonReturn(array('qrcode' => 'http://' . WEB_URI . '/Home/PayNotify/showQr?value=' . urldecode($uri)), 1, 'success');
			}
			$qrcode_url = 'http://' . WEB_URI . '/Home/PayNotify/showQr?value=' . urldecode($uri);
			$orderSn = $this->dataAccess->orderSn;
			include View();
			return;
		}
		if ($this->returnType == 'json') {
			JsonReturn(array(), -2, $data['message'] . "-支付异常请联系管理员");
		}
		exit($data['message'] . "-支付异常请联系管理员");

	}

	public function notify($order) {
		# code...
		$re = $this->check($order);
		return $re;
	}
	public function redirect($order) {
		$re = $this->check($order);
		return $re;
	}

	function check() {
		$data = array();
		$data['transDate'] = $_POST['transDate'];
		$data['transTime'] = $_POST['transTime'];
		$data['merchno'] = $_POST['merchno'];
		$data['merchName'] = $_POST['merchName'];

		$data['amount'] = $_POST['amount'];
		$data['traceno'] = $_POST['traceno'];
		$data['payType'] = $_POST['payType'];
		$data['orderno'] = $_POST['orderno'];
		$data['channelOrderno'] = $_POST['channelOrderno'];
		$data['channelTraceno'] = $_POST['channelTraceno'];
		$data['status'] = $_POST['status'];
		$md5sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
		$data['customerno'] = $_POST['customerno'];
		$data['openId'] = $_POST['openId'];

		$this->dataAccess->money = $data['amount'];
		$this->dataAccess->orderSn = $data['traceno'];
		$this->dataAccess->payType = $data['payType'];
		$this->dataAccess->goodsName = '';
		$data['signature'] = $_POST['signature'];
		if ($data['signature'] == $md5sign) {
			return 1;
		}
		return 0;
	}

	function md5mcrypt($data, $key) {
		$str = array();
		if (is_array($data) && count($data) > 0) {
			ksort($data);
			foreach ($data as $k => $v) {
				if (empty($v)) {
					unset($data[$k]);
				}
				$str[] = "{$k}={$v}";
			}
		}
		if (is_array($str) && count($str) > 0) {
			$str = implode('&', $str) . '&' . $key;
			//toLog($str);
			return md5($str);
		}
	}
	public function getPaymentType() {
		# code...
		return array(
			"alipay" => "1", //"支付宝",
			"weixin" => "2", //"微信",
			"baidupay" => "4", //"百度钱包",
			"qqpay" => "8", //QQ钱包",
			"jdpay" => "16", //京东钱包",
			"union" => "32", //"银联钱包",
		);
	}

	function notifyMsg($key) {
		if ($key > 0) {
			echo 'success';
		} else {
			echo 'fail';
		}
	}

}

?>