<?php
namespace Biz\Pay\Payment;
/**
 * 汇付天下接口
 */
class HuiPayPayment extends \Biz\Pay\BasePayment {
	function init() {
		$this->sellerInfo = array(
			'busKey' => "MHGDETHDER674SDCVG3456BNO9955BJ33366BNDCDFGRT",
			'busAccount' => '41711', //360cd.cn
		);
	}
	protected $getWay = "https://gwapi.yemadai.com/pay/sslpayment";
	protected $paymentName = "HuiPay";
	public function toPay() {
		$MD5key = $this->sellerInfo['busKey']; //"MHGDETHDER674SDCVG3456BNO9955BJ33366BNDCDFGRT"; //MD5私钥
		$MerNo = $this->sellerInfo['busAccount']; //"41711"; //商户号
		$BillNo = $this->dataAccess->orderSn; //[必填]订单号(商户自己产生：要求不重复)
		$Amount = $this->dataAccess->money; //[必填]订单金额
		$ReturnURL = $this->redirectUrl; //[必填]返回数据给商户的地址(商户自己填写):::注意请在测试前将该地址告诉我方人员;否则测试通不过
		$AdviceURL = $this->notifyUrl; //[必填]支付完成后，后台接收支付结果，可用来更新数据库值
		$Remark = ""; //[选填]升级。
		$OrderTime = date("YmdHis", time()); //[必填]交易时间YYYYMMDDHHMMSS
		$defaultBankNumber = $this->dataAccess->bankCode;
		$payType = $this->dataAccess->payType; //"quickPay";
		$products = $this->dataAccess->goodsName;
		$md5src = "MerNo=" . $MerNo . "&BillNo=" . $BillNo . "&Amount=" . $Amount . "&OrderTime=" . $OrderTime . "&ReturnURL=" . $ReturnURL . "&AdviceURL=" . $AdviceURL . "&" . $MD5key; //校验源字符串
		$SignInfo = strtoupper(md5($md5src)); //MD5检验结果
		$newOrder = array(
			'MerNo' => $MerNo,
			'BillNo' => $BillNo,
			'Amount' => $Amount,
			'ReturnURL' => $ReturnURL,
			'AdviceURL' => $AdviceURL,
			'Remark' => $Remark,
			'OrderTime' => $OrderTime,
			'products' => $products,
			'defaultBankNumber' => $defaultBankNumber,
			'SignInfo' => $SignInfo,
			'payType' => $payType,

		);
		// echo '<pre>';
		// print_r($newOrder);
		// echo '</pre>';exit;
		$this->goPay($newOrder);

	}

	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) {
		//商户号
		$MD5key = $this->sellerInfo['busKey']; //"MHGDETHDER674SDCVG3456BNO9955BJ33366BNDCDFGRT"; //MD5私钥
		$MerNo = $this->sellerInfo['busAccount']; //"41711"; //商户号
		//订单号
		$BillNo = trim($_POST["BillNo"]);
		//一麻袋支付订单号
		$OrderNo = trim($_POST["OrderNo"]);
		//金额
		$Amount = trim($_POST["Amount"]);
		//支付状态
		$Succeed = trim($_POST["Succeed"]);
		//支付结果
		$Result = trim($_POST["Result"]);
		//取得的MD5校验信息
		$SignInfo = trim($_POST["SignInfo"]);
		//备注
		$Remark = trim($_POST["Remark"]);
		$products = trim($_POST["products"]);
		$this->dataAccess->orderSn = $BillNo;
		$this->dataAccess->money = $Amount;
		$this->dataAccess->goodsName = $products;
		//校验源字符串
		$md5src = "MerNo=" . $MerNo . "&BillNo=" . $BillNo . "&OrderNo=" . $OrderNo . "&Amount=" . $Amount . "&Succeed=" . $Succeed . "&" . $MD5key;
		//MD5检验结果
		$md5sign = strtoupper(md5($md5src));
		if ($SignInfo == $md5sign) {
			if ($Succeed == "88") {
				return 1;
			} else {
				$err = intval($Succeed);
				return $err > 0 ? -$err : $err;
			}
		} else {
			return -1;
		}

	}

	function notifyMsg($key) {
		if ($key > 0) {
			echo 'ok';
		} else {
			echo 'error';
		}
	}

}

?>