<?php

namespace Payment\Src;

use Dever;

class Api
{
    /**
     * 发起支付 下单 获取预支付信息
     *
     * @return mixed
     */
    public function get($type = 1, $param = array())
    {
        $this->init($param);
        if ($this->pay && $this->pay['status'] == 1 && $this->pay['param']) {
            $order = Dever::array_decode($this->pay['param']);
            return array($this->order_id, $order);
        }
        return $this->method->request($type, $this->uid, $this->account, $this->product_name, $this->amount, $this->currency, $this->data);
    }

    /**
     * 回调
     *
     * @return mixed
     */
    public function notify()
    {
        $param = Dever::input();
        $param['product_name'] = 'notify';
        $this->init($param);
        if ($this->pay) {
            $param['amount'] = $this->pay['amount'];
            $param['type'] = $this->pay['type'];
        }
        $result = $this->method->notify($param);

        $refer = Dever::session('refer');
        if ($refer) {
            if (strpos($refer, '?')) {
                $refer .= '&';
            } else {
                $refer .= '?';
            }
            $refer .= http_build_query($result);
            
            Dever::location($refer);
        }
    }

    /**
     * 初始化 设置参数
     *
     * @return mixed
     */
    private function init($param = array())
    {
        $this->account = $this->getParam($param, 'account');
        $this->uid = $this->getParam($param, 'uid');
        $this->product_name = $this->getParam($param, 'product_name');
        $this->amount = $this->getParam($param, 'amount');
        $this->currency = $this->getParam($param, 'currency');
        $this->refer = $this->getParam($param, 'refer');
        $this->order_id = $this->getParam($param, 'order_id');
        $this->data = $this->getParam($param, 'data');
        $this->pay = false;

        if ($this->data) {
            $this->data = Dever::json_decode($this->data);
        }

        if (!$this->account) {
            $this->account = 'payfort';
        }

        if (!$this->product_name) {
            Dever::alert('no_product_name');
        }

        if ($this->order_id) {
            $this->pay = Dever::db('payment/order')->one(array('order_id' => $this->order_id));
            if ($this->pay) {
                $this->amount = $this->pay['amount'];
            }
        }

        if (!$this->amount) {
            Dever::alert('no_amount');
        }

        if (!$this->currency) {
            //Dever::alert('no_currency');
        }

        Dever::session('refer', $this->refer);

        $this->uid = Dever::session('uid');

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

        if ($this->uid) {
            Dever::session('uid', $this->uid);
        }

        Dever::setInput('json', 1);

        return $this->pay();
    }

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

    /**
     * 获取支付类
     *
     * @return mixed
     */
    private function pay()
    {
        $config = Dever::config('base')->account[$this->account];
        $method = '\\Payment\\Lib\\' . ucwords($this->account);
        $this->method = new $method($this->account, $config);
        return $this;
    }
}
