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

	protected $getWay = "https://gateway.okfpay.com/Gate/payindex.aspx";
	//http://api.haiopay.com/GateWay/bank/DefaultV2.aspx
	protected $paymentName = "OkPay";
	function init() {
		$this->privateCertPath = '';
		$this->publicCertPath = '';
		$this->sellerInfo = array(
			'busKey' => '4ce8d34d7c854fda91b122e1b947a53c',
			'busId' => '5842',
		);
	}

	public function toPay() {

		$paymentTypes = $this->getPaymentType();
		if (array_key_exists($this->dataAccess->payType, $paymentTypes)) {
			$payType = $paymentTypes[$this->dataAccess->payType];
		} else {
			$this->isMobile = 0;
			$paymentTypes = $this->getPaymentType();
			if (array_key_exists($this->dataAccess->payType, $paymentTypes)) {
				$payType = $paymentTypes[$this->dataAccess->payType];
			} else {
				echo "<script>
					alert('支付类型不存在');
					window.history.back();
					</script>";
				//throw new \Exception($this->dataAccess->payType . '支付类型不存在', 1);
				//$payType = 'CCB';
			}
		}

		$data = array(
			'version' => '1.0', //当前接口版本号 1.0
			'partner' => $this->sellerInfo['busId'], //商户编码
			'orderid' => $this->dataAccess->orderSn, //商户订单号
			'payamount' => $this->dataAccess->money, //订单总金额（0.01－10000000.00，保留两位小数）
			'payip' => GETIP(), //客户端IP
			'notifyurl' => $this->notifyUrl, //支付结果异步通知地址
			'returnurl' => $this->redirectUrl, //同步跳转地址
			'paytype' => $payType, //支付类型
			'remark' => '',
		);
		$sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);

		$data['sign'] = $sign; //签名

		$data = $this->goPay($data);

	}

	function md5mcrypt($data, $key) {
		// ksort($data);
		$str = http_build_query($data);
		$str = urldecode($str);
		$str = $str . '&key=' . $key;
		//toLog('md5--' . $str);
		return md5($str);
	}

	public function redirect() {
		$re = $this->check();
		return $re;
	}

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

	function check() {
		$data = array();
		//toLog($_POST);
		//toLog('---get---');
		//toLog($_GET);

		//当前版本号
		$data['version'] = '1.0';
		//商户ID
		$data['partner'] = $this->sellerInfo['busId'];
		// 商户订单号
		$data['orderid'] = $_GET["orderid"];
		// 金额
		$data['payamount'] = $_GET["payamount"];
		// 订单状态 2支付成功，0处理中
		$data['opstate'] = $_GET["opstate"];
		// OK付交易号（订单号）
		$data['orderno'] = $_GET["orderno"];
		// 订单完成时间
		$data['okfpaytime'] = $_GET["okfpaytime"];
		// 订单交易结果说明
		$data['message'] = $_GET["message"];
		// 支付方式
		$data['paytype'] = $_GET["paytype"];
		// 支付方式
		$data['remark'] = $_GET["remark"];
		// 商户私钥

		$this->dataAccess->orderSn = $data['orderid'];
		$this->dataAccess->money = $data['payamount'];

		//toLog($this->dataAccess);
		// 准备准备验签数据
		$sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
		$data['key'] = $this->sellerInfo['busKey'];
		$data['sign'] = $_GET['sign'];
		//toLog($sign);
		//toLog($data);
		if ($sign == $data['sign']) {
			if ($data['opstate'] == 2) {
				return 1;
			}
			return 0;
		} else {
			// 签名验证失败
			return 0;
		}
	}

	function getPaymentType() {
		$getMobile = (!isset($_GET['ismobile']) || empty($_GET['ismobile'])) ? 1 : $_GET['ismobile'];
		if ($this->isMobile) {
			return array(
				'weixin' => 'WECHAT', //微信
				'alipay' => 'ALIPAY', //支付宝
			);
		}

		return array(
			//'ICBC' => 'ICBC', //工商银行
			// 'CMB' => 'CMB', //招商银行
			'CCB' => 'CCB', //建设银行
			// 'BOC' => 'BOC', //中国银行
			'ABC' => 'ABC', //农业银行
			// 'BOCO' => 'BOCM', //交通银行
			// 'SPDB' => 'SPDB', //浦发银行
			// 'GDB' => 'CGB', //广发银行
			// 'CTTIC' => 'CTITC', //中信银行
			'CEB' => 'CEB', //光大银行
			// 'CIB' => 'CIB', //兴业银行
			// 'PINGANBANK' => 'SDB', //平安银行
			// 'CMBC' => 'CMBC', //民生银行
			// 'HXB' => 'HXB', //华夏银行
			'PSBS' => 'PSBC', //邮储银行
			// 'BCCB' => 'BCCB', //北京银行
			// 'SHB' => 'SHBANK', //上海银行
			// 'CBHB' => 'BOHAI', //渤海银行
			// 'SRCB' => 'SHNS', //上海农商
			// 'union' => 'UNION', //银联支付
			'weixin' => 'WECHAT', //微信
			'alipay' => 'ALIPAY', //支付宝
			// 'tenpay' => 'TENPAY', //财付通
		);
	}
}

?>