<?php
namespace Biz\Pay\Payment;
/**
 * 智付接口
 */
class DinPayScanPayment extends \Biz\Pay\BasePayment {
	function init() {
		$this->privateCertPath = '';
		$this->publicCertPath = '';
		$this->sellerInfo = array(
			'busKey' => openssl_get_privatekey($this->privateCertPath),
			'busAccount' => '987858', //
		);
		# code...
	}
	protected $getWay = "https://api.dinpay.com/gateway/api/weixin";
	protected $paymentName = "DinPayScan";
	function toPay() {
		$data = array(
			'merchant_code' => $this->sellerInfo['busAccount'],
			'service_type' => 'wxpay',
			'interface_version' => 'V3.0',
			'notify_url' => $this->notifyUrl,
			'order_no' => $this->dataAccess->orderSn,
			'order_time' => date('Y-m-d H:i:s', time()), //
			'order_amount' => $this->dataAccess->money, //
			'product_name' => '在线充值支付', //
		);
		$sign = $this->sslMcrypt($data, $this->sellerInfo['busKey']);
		$data['sign'] = $sign;
		$data['sign_type'] = 'RSA-S';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->getWay);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode(http_build_query($postdata)));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($ch);
		curl_close($ch);
		$res = simplexml_load_string($response);
		$resp_code = $res->response->resp_code;
		if ($resp_code == "SUCCESS") {
			$qrcode = $res->response->trade->qrcode;
			$uri = $qrcode;
			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;
		} else {
			if ($this->returnType == 'json') {
				JsonReturn('', -2, $resp_code . '-支付错误，联系管理员');
			}
		}

	}
	function sslMcrypt($data, $key) {
		$data = $this->fileterData($data);
		$str = http_build_query($data);
		$str = urldecode($str);
		openssl_sign($str, $sign_info, $key, OPENSSL_ALGO_MD5);
		$sign = urlencode(base64_encode($sign_info));
		return $sign;
	}

	function sslcrypt($data, $sign, $key) {
		$data = $this->fileterData($data);
		$str = http_build_query($data);
		$str = urldecode($str);
		return openssl_verify($str, $sign, $key, OPENSSL_ALGO_MD5);
	}
	function fileterData($data) {
		if (is_array($data) && count($data) > 0) {
			ksort($data);
			foreach ($data as $k => $v) {
				if (empty($v)) {
					unset($data[$k]);
				}
			}
		}
		return $data;

	}

	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();
		$this->dataAccess->orderSn = $data['order_no'] = $_POST['order_no'];
		$data['order_time'] = $_POST['order_time'];
		$this->dataAccess->money = $data['order_amount'] = $_POST['order_amount'];
		$data['trade_no'] = $_POST['trade_no'];
		$data['trade_time'] = $_POST['trade_time'];
		$data['trade_status'] = $_POST['trade_status'];
		$data['notify_id'] = $_POST['notify_id'];
		$data['notify_type'] = $_POST['notify_type'];
		$data['interface_version'] = $_POST['interface_version'];
		$data['merchant_code'] = $_POST['merchant_code'];

		$sign = base64_decode($_POST["sign"]);
		$data['bank_seq_no'] = $_POST["bank_seq_no"];
		$data['extra_return_param'] = $_POST["extra_return_param"];

		// $data['sign_type'] = $_POST['sign_type'];

		if ($this->sslcrypt($data, $sign, openssl_get_publickey($this->publicCertPath), OPENSSL_ALGO_MD5)) {
			return 1;
		} else {
			return 0;
		}

	}

	function notifyMsg($key) {
		if ($key > 0) {
			echo 'SUCCESS';
		} else {
			echo '';
		}
	}
	public function getPaymentType() {
		# code...
		return array(
			'weixin' => 'weixin',
		);
	}

}