<?php
namespace Biz\Pay\Payment;
/**
 * 汇付天下接口
 */
class XinHuiScanPayment extends \Biz\Pay\BasePayment {
	function init() {
		$this->sellerInfo = array(
			'busKey' => "b73ca2925a4c8fd3134ac377d3163f98",
			'busAccount' => '667371',
		);
		# code...
	}
	protected $getWay = "http://spdo.zjwee.cn/Online/GateWay";
	protected $paymentName = "XinHuiScan";
	function toPay() {
		$this->checkMoney($this->dataAccess->money);
		$payType = 0;
		$payment_list = $this->getPaymentType();
		if (isset($payment_list[$this->dataAccess->payType])) {
			$payType = $payment_list[$this->dataAccess->payType];
		} else {
			$this->showNotice("支付类型不正确", -3);
		}
		$data = array(
			'version' => '3.0',
			'method' => 'Xh.online.pay',
			'partner' => $this->sellerInfo['busAccount'],
			'banktype' => $payType,
			'paymoney' => $this->dataAccess->money,
			'ordernumber' => $this->dataAccess->orderSn,
			'callbackurl' => $this->notifyUrl,
		);
		$sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
		$data['hrefbackurl'] = $this->redirectUrl;
		$data['isshow'] = 0;
		$data['sign'] = $sign;
		$data = $this->goPayCurl($this->getWay, 'POST', $data);
		// print_r($data);
		if (empty($data)) {
			$this->showNotice($data . "支付异常请联系管理员", -1);
		}
		$index = json_decode($data, 1);
		if ($this->dataAccess->payType == 'weixin') {
			$uri = $index['qrurl'];
		} else {
			$uri = 'http://spdo.zjwee.cn' . $index['qrurl'];
			$uri = str_replace('http://spdo.zjwee.cnhttp://spdo.zjwee.cn', 'http://spdo.zjwee.cn', $uri);
		}

		$this->jsonSuccess(array('qrcode' => $uri));
		$qrcode_url = $uri;
		$orderSn = $this->dataAccess->orderSn;
		include View();
		return;

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

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

	public function check($order) {
		$data = array();
		$data['partner'] = $_GET['partner'];
		$this->dataAccess->orderSn = $data['ordernumber'] = $_GET['ordernumber'];
		$data['orderstatus'] = $_GET['orderstatus'];
		$this->dataAccess->money = $data['paymoney'] = $_GET['paymoney'];

		$sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
		$this->dataAccess->goodsName = '';
		$data['sysnumber'] = $_GET['sysnumber'];
		$data['attach'] = $_GET['attach'];
		$data['sign'] = $_GET['sign'];
		if ($sign == $data['sign']) {
			if ($data['orderstatus'] == '1') {
				return 1;
			} else {
				return 0;
			}
		}

	}

	function notifyMsg($key) {
		if ($key > 0) {
			echo 'ok';
		} else {
			echo '';
		}
	}
	public function getPaymentType() {
		# code...
		return array(
			"alipay" => "ALIPAY", //"支付宝",
			"weixin" => "WEIXIN", //"微信",
			"qqpay" => "QQ ", //QQ钱包",
			// 'tenpay'=>'TENPAY',//财付通
			'jdpay' => 'JD', //京东钱包
		);
	}

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

}