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

	protected $getWay = "http://saascashier.mobaopay.com/cgi-bin/netpayment/pay_gate.cgi";
	protected $paymentName = "MoBao";
	function init() {
		$this->privateCertPath = '';
		$this->publicCertPath = '';
		$this->sellerInfo = array(
			'busKey' => '2e1528883db134b55217a318d825ab6e',
			'busId' => '2111641497951153',
			'busAccount' => '2111641497951153',
		);
	}

	public function toPay() {
		$paymentTypes = $this->getPaymentType();
		if (array_key_exists($this->dataAccess->payType, $paymentTypes)) {
			$payType = $paymentTypes[$this->dataAccess->payType];
		} else {
			$payType = 1;
		}

		$data = array(
			'apiName' => 'WEB_PAY_B2C', //接口名称
			'apiVersion' => '1.0.0.0', //接口版本
			'platformID' => $this->sellerInfo['busId'], //商户ID
			'merchNo' => $this->sellerInfo['busAccount'], //商户账号
			'orderNo' => $this->dataAccess->orderSn, //商户订单号
			'tradeDate' => date('Ymd', time()), //交易日期
			'amt' => $this->dataAccess->money, //订单金额
			'merchUrl' => $this->notifyUrl, //支付结果通知地址
			'merchParam' => '', //支付结果通知地址
			'tradeSummary' => 'user recharge', //支付结果通知地址
		);
		$sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
		$data['choosePayType'] = $payType; //支付方式
		$data['bankCode'] = $this->dataAccess->bankCode;
		//银行代码
		$data['merchParam'] = '';
		$data['tradeSummary'] = 'user recharge';
		$data['signMsg'] = $sign; //签名
		$data = $this->goPay($data);
	}

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

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

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

	function check($order) {
		$data = array();
		$data['apiName'] = $_POST["apiName"];
		// 通知时间
		$data['notifyTime'] = $_POST["notifyTime"];
		// 支付金额(单位元，显示用)
		$data['tradeAmt'] = $_POST["tradeAmt"];
		// 商户号
		$data['merchNo'] = $_POST["merchNo"];
		// 商户参数，支付平台返回商户上传的参数，可以为空
		$data['merchParam'] = $_POST["merchParam"];
		// 商户订单号
		$data['orderNo'] = $_POST["orderNo"];
		// 商户订单日期
		$data['tradeDate'] = $_POST["tradeDate"];
		// Mo宝支付订单号
		$data['accNo'] = $_POST["accNo"];
		// Mo宝支付账务日期
		$data['accDate'] = $_POST["accDate"];
		// 订单状态，0-未支付，1-支付成功，2-失败，4-部分退款，5-退款，9-退款处理中
		$data['orderStatus'] = $_POST["orderStatus"];

		$this->dataAccess->orderSn = $data['orderNo'];
		$this->dataAccess->money = $data['tradeAmt'];

		// 准备准备验签数据
		$sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
		$data['sign'] = $_POST['signMsg'];
		if ($sign == $data['sign']) {
			if ('1' == $_POST["orderStatus"]) {
				return 1;
			}
			return 0;
		} else {
			// 签名验证失败
			return 0;
		}
	}

	//摩宝支持支付方式
	public function getPaymentType() {
		return array(
			"alipay" => "4", //支付宝扫描,
			"weixin" => "5", //微信扫码,
		);
	}

	//摩宝支持银行及其对应代码
	public function getPayBankCode() {
		return array(
			'ICBC' => '工行',
			'ABC' => '农行',
			'BOC' => '中行',
			'CCB' => '建行',
			'COMM' => '交行',
			'CMB' => '招行',
			'SPDB' => '浦发',
			'CIB' => '兴业',
			'CMBC' => '民生',
			'GDB' => '广发',
			'CNCB' => '中信',
			'CEB' => '光大',
			'HXB' => '华夏',
			'PSBC' => '邮储',
			'PAB' => '平安',
		);
	}
	function notifyMsg($key) {
		if ($key > 0) {
			echo 'SUCCESS';
		} else {
			echo '';
		}
	}
}

?>