<?php namespace Pay\Src;

use Dever;

class Api
{
	/**
	 * 发起跳转
	 *
	 * @return mixed
	 */
	public function jump()
	{
		$url = Dever::input('refer');
		$url = urldecode($url);
		Dever::location($url);
	}

	# 获取支付渠道
	public function channel()
	{
		$data = Dever::db('pay/channel')->getData();
		return $data;
	}

	/**
	 * 发起支付 下单 获取预支付信息
	 *
	 * @return mixed
	 */
	public function get($type = 1, $param = array())
	{
		if (!$type) {
			$type = 1;
		}
		$this->init($param);
		if ($this->order_id > 0) {
			$pay = Dever::db('pay/order')->one(array('order_id' => $this->order_id));
			if ($pay && $pay['status'] == 1 && $pay['param']) {
				$order = Dever::array_decode($pay['param']);
				return $order;
			}
		}
		return $this->method->order($this->account_id, $this->project_id, $this->uid, $this->username, $this->product_id, $this->name, $this->cash, $this->openid, $type, $this->order_id);
	}

	/**
	 * notify
	 *
	 * @return mixed
	 */
	public function notify($param = array())
	{
		$this->account_id = Dever::input('account_id', false);
		if (!$this->account_id) {
			Dever::alert('没有账户信息');
		}
		$this->handle();
		return $this->method->notify();
	}

	/**
	 * 发起支付 通用的发起支付方法
	 *
	 * @return mixed
	 */
	public function pay($param = array())
	{
		$source = $this->getParam($param, 'source');
		$receipt = $this->getParam($param, 'receipt');

		$config = Dever::config('base', 'pay')->method;

		if (isset($config[$source])) {
			$param['account_id'] = $source;
			$method = $config[$source];
			if ($source == 3 && $receipt) {
				$method = $method[1];
			}
			return $this->$method($param);
		} else {
			Dever::alert('错误的source');
		}
	}

	/**
	 * 发起支付 用于小程序支付
	 *
	 * @return mixed
	 */
	public function applet($param = array())
	{
		$param['source'] = 4;
		$this->init($param);
		return $this->method->applet($this->get(1));
	}

	/**
	 * 发起支付 用于app支付
	 *
	 * @return mixed
	 */
	public function app($param = array())
	{
		$param['source'] = 1;
		$this->init($param);
		$this->openid = -1;
		return $this->method->app($this->get(3));
	}

	/**
	 * 发起支付 用于苹果内购支付
	 *
	 * @return mixed
	 */
	public function apple($param = array())
	{
		$this->init($param);
		# 只需验证苹果过来的参数即可
		return Dever::load('pay/lib/apple')->check($this->other, $this->account_id, $this->project_id, $this->uid, $this->username, $this->product_id, $this->name, $this->cash, $this->order_id);
	}

	/**
	 * 发起支付 用于页面支付
	 *
	 * @return mixed
	 */
	public function page($param = array())
	{
		$this->init($param);
		if (!$this->refer) {
			Dever::alert('没有回调refer');
		}
		$type = 1;
		if ($this->h5 == 1) {
			$type = 4;
		}
		return $this->method->page($this->get($type), $this->refer);
	}

	/**
	 * 发起支付 用于扫码支付
	 *
	 * @return mixed
	 */
	public function qrcode($param = array())
	{
		$this->init($param);
		$url = $this->method->qrcode($this->get(2), $this->refer);
		Dever::apply('sdk/qrcode');
		return \QRcode::png($url);
	}

	/**
	 * 查询支付
	 *
	 * @return mixed
	 */
	public function search($param = array())
	{
		$this->account_id = Dever::input('account_id', false);
		if (!$this->account_id) {
			Dever::alert('没有账户信息');
		}
		$this->handle();
		$this->order_id = $this->getParam($param, 'order_id');
		return $this->method->search($this->order_id);
	}

	/**
	 * 退款
	 *
	 * @return mixed
	 */
	public function refund($param = array())
	{
		$this->account_id = Dever::input('account_id', false);
		if (!$this->account_id) {
			Dever::alert('没有账户信息');
		}
		$this->handle();
		$this->order_id = $this->getParam($param, 'order_id');
		return $this->method->refundByOrder($this->order_id);
	}

	/**
	 * 初始化 设置参数
	 *
	 * @return mixed
	 */
	private function init($param = array())
	{
		if (isset($this->account_id)) {
			return;
		}
		$this->channel_id = $this->getParam($param, 'channel_id');
		$this->system_source = $this->getParam($param, 'system_source');
		$this->account_id = $this->getParam($param, 'account_id');
		$this->project_id = $this->getParam($param, 'project_id');
		$this->uid = $this->getParam($param, 'uid');
		$this->openid = $this->getParam($param, 'openid');
		$this->username = $this->getParam($param, 'username');
		$this->product_id = $this->getParam($param, 'product_id');
		$this->name = $this->getParam($param, 'name');
		$this->cash = $this->getParam($param, 'cash');
		$this->refer = $this->getParam($param, 'refer');
		$this->order_id = $this->getParam($param, 'order_id');
		$this->other = $this->getParam($param, 'other');
		$this->h5 = $this->getParam($param, 'h5');
		$this->ip = $this->getParam($param, 'ip');

		if (!$this->project_id) {
			$this->project_id = false;
		}

		if (!$this->order_id) {
			$this->order_id = false;
		}

		if (!$this->account_id && !$this->channel_id && !$this->system_source) {
			Dever::alert('没有账户信息');
		}

		if (!$this->uid) {
			Dever::alert('没有用户信息');
		}

		if (!$this->product_id) {
			Dever::alert('没有产品信息');
		}

		if (!$this->name) {
			Dever::alert('没有支付信息');
		}

		if (!$this->cash) {
			Dever::alert('没有支付金额');
		}

		if (!$this->ip) {
			$this->ip = Dever::ip();
		}

		return $this->handle();
	}

	/**
	 * 初始化
	 *
	 * @return mixed
	 */
	private function getParam($param, $key)
	{
		if (isset($param[$key])) {
			return $param[$key];
		}
		return Dever::input($key, false);
	}

	/**
	 * 获取支付类
	 *
	 * @return mixed
	 */
	private function handle()
	{
		$pay = false;
		if ($this->account_id > 0) {
			$pay = Dever::db('pay/account')->one($this->account_id);
		} elseif ($this->channel_id && $this->system_source) {
			$pay = Dever::db('pay/account')->one(array('channel_id' => $channel_id, 'system_source' => $this->system_source));
		}

		if (!$pay || ($pay && $pay['state'] != 1)) {
			Dever::alert('没有账户信息');
		}

		$this->account_id = $pay['id'];
		/*
		$this->channel_id = $pay['channel_id'];
		$this->system_source = $pay['system_source'];
		*/
		
		$method = '\\Pay\\Lib\\' . ucwords($pay['type']);
		if (isset($this->refer) && $this->refer) {
			$pay['refer'] = $this->refer;
		}
		if (isset($this->ip) && $this->ip) {
			$pay['ip'] = $this->ip;
		}
		
		$this->method = new $method($pay);
		return $this;
	}
}
