<?php
namespace Biz\Pay\Payment;
/**
 * 汇付天下接口
 */
class OnlineScanPayment extends \Biz\Pay\BasePayment {
	function init() {
		$this->sellerInfo = array(
			'busKey' => "1E09D11FB5A71BED5D21B79FC2FD9E95",
			'busAccount' => '666310144570001',
		);
		# code...
	}
	protected $getWay = "http://p.1wpay.com/a/passivePay";
	protected $paymentName = "OnlineScan";
	function toPay() {
		$paymentTypes = $this->getPaymentType();
		if (array_key_exists($this->dataAccess->payType, $paymentTypes)) {
			$payType = $paymentTypes[$this->dataAccess->payType];
		} else {
			$payType = 1;
		}

		$data = array(
			'amount' => $this->dataAccess->money, //交易金额
			'goodsName' => 'OnlineScan', //商品名称，可选
			'merchno' => $this->sellerInfo['busAccount'], //商户号
			'notifyUrl' => $this->notifyUrl, //支付结果异步通知地址，可选
			'payType' => $payType, //支付类型
			'traceno' => $this->dataAccess->orderSn, //商户订单号
		);
		$sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
		$data['signature'] = $sign;
		$data = $this->goPayCurl($this->getWay, $data);

		$data = iconv('gb2312', 'utf-8', $data);
		$row = json_decode($data, true);
		if ($row['respCode'] == '00') {
			$uri = $row['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, $row['msg'] . "-支付异常请联系管理员");
		}
		exit($row['msg'] . "-支付异常请联系管理员");
	}
	function md5mcrypt($data, $key) {
		$str = http_build_query($data) . '&' . $key;
		$str = urldecode($str);
		return md5($str);
	}

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

	public function check($order) {
		$data = array();
		$data['amount'] = $_POST['amount']; //交易金额
		$data['channelOrderno'] = $_POST['channelOrderno']; //渠道订单号
		$data['channelTraceno'] = $_POST['channelTraceno']; //渠道流水号
		if (isset($_POST['customerno']) && (!empty($_POST['customerno']))) {$data['customerno'] = $_POST['customerno'];} //客户号
		$data['merchName'] = $_POST['merchName']; //商户名称
		$data['merchno'] = $_POST['merchno']; //商户号
		if (isset($_POST['openId']) && (!empty($_POST['openId']))) {$data['openId'] = $_POST['openId'];} //用户OpenId
		$data['orderno'] = $_POST['orderno']; //系统订单号
		$data['payType'] = $_POST['payType']; //支付方式
		if (isset($_POST['remark']) && (!empty($_POST['remark']))) {$data['remark'] = $_POST['remark'];} //备注
		$data['status'] = $_POST['status']; //交易状态
		$data['traceno'] = $_POST['traceno']; //商户流水号
		$data['transDate'] = $_POST['transDate']; //交易日期
		$data['transTime'] = $_POST['transTime']; //交易时间

		$this->dataAccess->orderSn = $data['traceno'];
		$this->dataAccess->money = $data['amount'];
		$this->dataAccess->goodsName = '';

		$sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
		$data['signature'] = $_POST['signature']; //数据签名
		if ($sign == $data['signature']) {
			if ($data['status'] == '1') {
				return 1;
			} else {
				return 0;
			}
		}
		return 0;
	}

	function notifyMsg($key) {
		if ($key > 0) {
			echo 'ok';
		} else {
			echo '';
		}
	}
	public function getPaymentType() {
		return array(
			"alipay" => "1", //"支付宝",
			"weixin" => "2", //"微信",
			// "baidupay" => "3", //百度钱包
			// "qqpay" => "4 ", //QQ钱包",
			// 'jdpay' => '5', //京东钱包
		);
	}

	public function checkMoney($money) {
		if ($this->dataAccess->payType == 'qqpay') {
			if ($this->dataAccess->money < 10) {
				$this->showNotice('QQ钱包最小10元起付', -4);
			}
		}
	}

}