<?php
namespace Cmbc\Api;

class UnifiedOrder extends Core
{
	protected function init()
	{
		$this->api = 'https://ipaynow.acquire.cmbchina.com/';
		$this->param['funcode'] = 'WP001';
		$this->param['version'] = '1.0.0';
	}

	/**
	 * mhtOrderNo 订单号
	 * mhtOrderName 商品名
	 * mhtOrderType 交易类型：01普通消费 05代理消费
	 * mhtCurrencyType 156人民币
	 * mhtOrderAmt 用户实付金额 单位(人民币)：分 整数，无小数点
	 * oriMhtOrderAmt 订单原始金额
	 * discountAmt 订单优惠金额
	 * mhtOrderDetail 商户订单详情
	 * mhtOrderTimeOut 商户订单超时时间
	 * mhtOrderStartTime 商户订单开始时间
	 * notifyUrl 商户后台通知URL HTTP协议或者HTTPS协议，POST方式提交报文。
	 * frontNotifyUrl 商户前台通知URL HTTP协议或者HTTPS协议，POST方式提交报文。outputType=0时必填
	 * mhtCharset 商户字符编码
	 * deviceType 设备类型 14小程序
	 * payChannelType 用户所选渠道类型 13微信
	 * mhtReserved 商户保留域
	 * outputType 输出格式 0 直接调起支付 1 返回支付凭证
	 * mhtSubAppId 商户appId
	 * consumerId 消费者ID
	 * mhtLimitPay 是否支持信用卡支付 定值：
0表示支付不限制卡类型
1表示不能使用信用卡支付 

	 * mhtGoodsTag 商户签名方法
	 * mhtSignType 商户保留域

	 */
	public function request($request, $tool = false)
	{
		$this->data = $request;
		$this->setParam('mhtOrderNo');
		$this->setParam('mhtOrderName');
		$this->setParam('mhtOrderType', '01');
		$this->setParam('mhtCurrencyType', '156');
		$this->setParam('mhtOrderAmt');
		$this->setParam('oriMhtOrderAmt', false, false);
		$this->setParam('discountAmt', false, false);
		if (!isset($this->param['oriMhtOrderAmt']) && isset($this->param['mhtOrderAmt'])) {
			$this->param['oriMhtOrderAmt'] = $this->param['mhtOrderAmt'];
		}
		if (!isset($this->param['discountAmt']) && isset($this->param['mhtOrderAmt'])) {
			$this->param['discountAmt'] = $this->param['mhtOrderAmt'];
		}

		$this->setParam('mhtOrderDetail');
		$this->setParam('mhtOrderTimeOut', $this->setting->getTimeOut());
		$this->setParam('mhtOrderStartTime', date("YmdHis"));
		$this->setParam('notifyUrl');
		$this->setParam('frontNotifyUrl', false, false);
		$this->setParam('mhtCharset', 'UTF-8');
		$this->setParam('deviceType', 14);
		$this->setParam('payChannelType', 13);
		$this->setParam('mhtReserved', false, false);
		$this->setParam('outputType', 0);
		if (isset($this->param['outputType']) && $this->param['outputType'] == 1 && $this->param['payChannelType'] == 13) {
			$this->setParam('mhtSubAppId');
		} else {
			$this->setParam('mhtSubAppId', false, false);
		}
		if (isset($this->param['outputType']) && $this->param['outputType'] == 1) {
			$this->setParam('consumerId');
		} else {
			$this->setParam('consumerId', false, false);
		}
		$this->setParam('mhtLimitPay', 1);
		$this->setParam('mhtGoodsTag', false, false);
		$this->setParam('mhtSignType', 'MD5');
		$error = $this->getError();
		if ($error) {
			return $error;
		}
		$this->param['mhtSignature'] = $this->createSignature($this->param['mhtSignType']);
		$response = $this->curl();
		return $this->response($response);
	}

	public function response($response)
	{
		if (isset($response['tn']) && $response['tn']) {

			//$tn = urldecode($response['tn']);
			//\Dever::log('招商银行支付响应' . '||' . $response['tn'], 'pay');
			parse_str($response['tn'], $result);
			return $result;
		} else {
			return 'error';
		}
		return $response;
	}
}