<?php

require_once "lib/WxPay.Api.php";
require_once "WxPay.Config.php";
require_once "WxPay.JsApiPay.php";

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 *
 * 刷卡支付实现类
 * @author widyhu
 *
 */
class NativePay
{

	protected $ci;

	public function __construct()
	{
	    $this->ci =& get_instance();
	}

	/**
	 *
	 * 生成扫描支付URL,模式一
	 * @param BizPayUrlInput $bizUrlInfo
	 */
	public function GetPrePayUrl($productId)
	{
		$biz = new WxPayBizPayUrl();
		$biz->SetProduct_id($productId);
		try{
			$config = new WxPayConfig();
			$values = WxpayApi::bizpayurl($config, $biz);
		} catch(Exception $e) {
			// Log::ERROR(json_encode($e));
		}
		$url = "weixin://wxpay/bizpayurl?" . $this->ToUrlParams($values);
		return $url;
	}

	/**
	 *
	 * 参数数组转换为url参数
	 * @param array $urlObj
	 */
	private function ToUrlParams($urlObj)
	{
		$buff = "";
		foreach ($urlObj as $k => $v)
		{
			$buff .= $k . "=" . $v . "&";
		}

		$buff = trim($buff, "&");
		return $buff;
	}

	/**
	 *
	 * 生成直接支付url，支付url有效期为2小时,模式二
	 * @param UnifiedOrderInput $input
	 */
	public function GetPayUrl($input)
	{
		// if($input->GetTrade_type() == "NATIVE")
		// {
			try{
				$config = new WxPayConfig();
				$result = WxPayApi::unifiedOrder($config, $input);
				// log_message('error',var_export($input, 1));
				// Log::ERROR(var_export($input, 1));
				return $result;
			} catch(Exception $e) {
				log_message('error','testlog 2' . var_export($e, 1));
				// Log::ERROR(var_export($e, 1));
				// Log::ERROR(json_encode($e));
			}
		// }
		// return false;
	}
}
