<?php namespace Pay\Lib;
use Dever;
Dever::apply('sdk/wechat', 'pay');

class Wechat extends Core
{
	public function __construct($config)
	{
		$project = Dever::project('pay');
		$this->channel_id = $config['channel_id'];
		$this->system_source = $config['system_source'];
		$this->config = new \WxPayConfig();
		# 通知接口
		$config['notify'] = $this->url($config['type'], $config['id']);
		# 证书
		$config['ssl'] = array
		(
			'cert' => Dever::local($config['file_cert']),
			'key' => Dever::local($config['file_key']),
		);

		$config['ip'] = (isset($config['ip']) && $config['ip']) ? $config['ip'] : Dever::ip();

		$this->config->set($config['appid'], $config['appsecret'], $config['mchid'], $config['notify'], $config['key'], $config['ssl'], $config['type'], $config['timeout'], $config['ip']);
	}

	/**
	 * 通知
	 */
	public function notify()
	{
		$this->log('支付回调-初始化', file_get_contents("php://input"));
		$callback = new Callback();
		$result = $callback->Handle($this->config, false);
	}

	# 查询订单
	public function search($order_id)
	{
		$info = Dever::db('pay/order')->one(array('order_id' => $order_id, 'status' => 2));
		if ($info) {
			$this->updateOrder($info['order_id'], 1);
			return $info;
		} else {
			$input = new \WxPayOrderQuery();
			$input->SetOut_trade_no($order_id);
			$result = \WxPayApi::orderQuery($this->config, $input);

			if (isset($result['transaction_id']) && isset($result['out_trade_no']) && isset($result['trade_state_desc'])) {
				$this->updateOrder($result['out_trade_no'], $result['cash_fee']);
			}

			return $result;
		}
	}

	# 退款
	public function refund($order_id, $cash, $order, $refund_order_id = false)
	{
		$out_trade_no = $order_id;
		$cash = $cash * 100;
		$total_fee = $cash;
		$refund_fee = $cash;
		$input = new \WxPayRefund();
		$input->SetOut_trade_no($out_trade_no);
		$input->SetTotal_fee($total_fee);
		$input->SetRefund_fee($refund_fee);

	    $input->SetOut_refund_no($out_trade_no . '_' . time());
	    $input->SetOp_user_id($this->config->GetMerchantId());
		$result = \WxPayApi::refund($this->config, $input);
		return $result;
	}

	/**
	 * 获取统一下单的基本信息
	 */
	public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false, $other = false, $refer = false)
	{
		$trade_type = $this->getType($type);
		$order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);
		$tools = new \JsApiPay($this->config);

		$input = new \WxPayUnifiedOrder();
		$input->SetBody($name);
		$input->SetAttach($name);
		$input->SetOut_trade_no($order_id);
		$input->SetTotal_fee($cash * 100);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + $this->config->GetTimeOut()));
		//$input->SetGoods_tag($name);
		$input->SetNotify_url($this->config->GetNotifyUrl());
		$input->SetTrade_type($trade_type);
		$input->SetProduct_id($product_id);
		if ($type == 1 && $openid != -1) {

			if (!$openid && Dever::project('passport')) {
				$where = array();
				$where['uid'] = $uid;
				$where['system_source'] = $this->system_source;
				$where['system_id'] = $this->channel_id;
				$wechat = Dever::db('passport/wechat')->one($where);
				if ($wechat) {
					$openid = $wechat['openid'];
				}
			}
			$openid = $openid ? $openid : $tools->GetOpenid();
			$input->SetOpenid($openid);
		}

		if ($type == 2) {
			# 下单信息
			$this->updateOrderParam($order_id, $input);
			return $input;
		} else {
			$order = \WxPayApi::unifiedOrder($this->config, $input);
			# 下单信息
			$order['time'] = '' . time() . '';
			$order['order_id'] = $order_id;
			$order['sign_type'] = $this->config->GetSignType();
			unset($order['mch_id']);
			$this->updateOrderParam($order_id, $order);
			return $order;
		}
	}

	/**
	 * 获取二维码支付
	 */
	public function qrcode($order, $refer)
	{
		$notify = new \NativePay($this->config);
		$result = $notify->GetPayUrl($order);
		if (isset($result['code_url'])) {
			$url = $result['code_url'];
			return array
			(
				'url' => $url,
				'type' => 'qrcode',
			);
		}

		return '';
	}

	/**
	 * 获取小程序支付
	 */
	public function applet($order)
	{
		if (isset($order['prepay_id'])) {

			$string = 'appId='.$this->config->GetAppId().'&nonceStr='.$order['nonce_str'].'&package=prepay_id='.$order['prepay_id'].'&signType='.$order['sign_type'].'&timeStamp='.$order['time'].'&key='.$this->config->GetKey();
			$this->log('支付签名-applet', $string);
			if($order['sign_type'] == "MD5"){
				$string = md5($string);
			} else {
				$string = hash_hmac("sha256", $string, $this->config->GetKey());
			}

			$order['sign'] = $string;
			$order['type'] = 'applet';
		}
		return $order;
	}

	/**
	 * 获取app支付
	 */
	public function app($order)
	{
		if (isset($order['prepay_id'])) {
			$order['partnerid'] = $this->config->GetMerchantId();
			$order['package_string'] = 'Sign=WXPay';

			$string = array();
			$string['appid'] = $this->config->GetAppId();
			$string['partnerid'] = $order['partnerid'];
			$string['prepayid'] = $order['prepay_id'];
			$string['package'] = $order['package_string'];
			$string['noncestr'] = $order['nonce_str'];
			$string['timestamp'] = $order['time'];
			ksort($string);
			$string = str_replace('%3D', '=', http_build_query($string));
			$string .= '&key=' . $this->config->GetKey();
			$this->log('支付签名-app', $string);
			if($order['sign_type'] == "MD5"){
				$string = md5($string);
			} else {
				$string = hash_hmac("sha256", $string, $this->config->GetKey());
			}

			$order['sign'] = $string;
			$order['type'] = 'app';
		}
		return $order;
	}


	/**
	 * 获取页面支付
	 */
	public function page($order, $refer)
	{
		if (isset($order['mweb_url'])) {
			$url = $order['mweb_url'] . '&redirect_url=' . $refer;
			if (!$url) {
				$location = 'window.open("' . $url . '")';
			} else {
				$location = 'location.href="' . $url . '"';
			}
			
			return '<script type="text/javascript">'.$location.'</script>';
		}
		$refer = urldecode($refer);

		if (strstr($refer, 'callback.')) {
			$callback = str_replace('callback.', '', $refer);
			$callback = $callback . '()';
		} else {
			$callback = 'location.href = "'.$refer.'";';
		}
		$tools = new \JsApiPay($this->config);
		$info = $tools->GetJsApiParameters($order);

		if ($refer == 'param') {
			return json_decode($info, true);
		}

		$html = '<script type="text/javascript">
		function jsApiCall()
		{
			WeixinJSBridge.invoke(
				"getBrandWCPayRequest",
				'.$info.',
				function(res){
					//WeixinJSBridge.log(res.err_msg);
					if(res.err_msg == "get_brand_wcpay_request:ok")
					{
						'.$callback.'
					} else {
						alert("支付失败");
						//alert(res.err_code+res.err_desc+res.err_msg);
					}
				}
			);
		}

		function callpay()
		{
			if (typeof WeixinJSBridge == "undefined"){
			    if( document.addEventListener ){
			        document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
			    }else if (document.attachEvent){
			        document.attachEvent("WeixinJSBridgeReady", jsApiCall); 
			        document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
			    }
			}else{
			    jsApiCall();
			}
		}
		callpay();
		</script>';

		return $html;
	}

	private function getType($type)
	{
		switch ($type) {
			case 1:
				$type = 'JSAPI';
				break;
			
			case 2:
				$type = 'NATIVE';
				break;

			case 3:
				$type = 'APP';
				break;

			case 4:
				$type = 'MWEB';
				break;
		}

		return $type;
	}
}

class Callback extends \WxPayNotify
{
	public function NotifyProcess($objData, $config, &$msg)
	{
		$data = $objData->GetValues();
		$obj = Dever::load('pay/lib/core');
		$obj->log('支付回调-获取数据', $data);
		$callback = function($msg = '') use ($obj, $data) {
			if ($msg) {
				$msg = $data['transaction_id'] . ':' . $msg;
			}
			$obj->updateOrder($data['out_trade_no'], $data['cash_fee'], $msg);
		};

		if(!array_key_exists("transaction_id", $data)){
			$msg = '输入参数不正确';
			$callback($msg);
			return false;
		}

		# 参数校验
		if(!array_key_exists("return_code", $data) 
			||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {
			$msg = $data['return_code'] . '(异常)';
			$callback($msg);
			return false;
		}

		# 进行签名验证
		try {
			$checkResult = $objData->CheckSign($config);
			if($checkResult == false){
				$msg = '签名错误';
				$callback($msg);
				return false;
			}
		} catch(Exception $e) {
			$msg = '签名异常';
			$callback($msg);
			return false;
		}


		# 查询订单，判断订单真实性
		/*
		if(!$this->Queryorder($data["transaction_id"])){
			$msg = '订单查询失败';
			$callback($msg);
			return false;
		}
		*/

		# 处理业务逻辑
		$callback();

		return true;
	}
}