<?php

namespace Pay\Lib;

use Dever;

class Set
{
	# 可以自定义token的key
	public function updateStatus_secure_api_token()
	{
		$project_id = Dever::input('pay_project_id');
		$info = Dever::db('pay/project')->one($project_id);
		if ($info) {
			return $info['key'];
		}
		return false;
	}

	# 带有安全验证的接口 需要传入dever_token，可以参考下边的test_order例子
    public function updateStatus_secure_api($param = array())
    {
    	$send = $param ? $param : Dever::preInput('pay_');
        $tk_pic = $send['pay_tk_pic'];
        $tk_time = $send['pay_tk_time'];
        $tk_desc = $send['pay_tk_desc'];
        $order_id = $send['pay_order_id'];
        $account_id = $send['pay_account_id'];
        $status = $send['pay_status'];
        $uid = $send['pay_uid'];
        $cash = $send['pay_cash'];

        $project_id = Dever::load('pay/lib/core')->checkOrder($order_id);
        if ($project_id) {
            $order = array();
            $order['account_id'] = $account_id;
        } else {
            $order = Dever::db('pay/order')->one(array('order_id' => $order_id, 'uid' => $uid));
            if ($order && $order['cash']) {
                $cash = $order['cash'];
            }
        }

        if ($order) {

            if ($status == 5) {
                $pay = Dever::db('pay/account')->one($order['account_id']);
                $method = '\\Pay\\Lib\\' . ucwords($pay['type']);
                $method = new $method($pay);
                $result = $method->refund($order_id, $cash);
            } 
            

            if (isset($order['id'])) {
                $update['where_id'] = $order['id'];
                $update['status'] = $status;
                $update['tk_pic'] = $tk_pic;
                $update['tk_time'] = $tk_time;
                $update['tk_desc'] = $tk_desc;
                Dever::db('pay/order')->update($update);
            }
        }

        return 'ok';
    }

    # 这是一个带有安全验证的请求例子
    private function test_order($id, $name, $data)
    {
        $status = Dever::param('status', $data);

        if ($status > 0 && $id > 0) {
            $send = array();
            $info = Dever::db('journal/order')->one($id);
            $send['pay_project_id'] = 1;
            $send['pay_uid'] = $info['uid'];
            $send['pay_order_id'] = $info['order_id'];
            $send['pay_tk_pic'] = $info['tk_pic'];
            $send['pay_tk_time'] = $info['tk_time'];
            $send['pay_tk_desc'] = $info['tk_desc'];
            $send['pay_status'] = $status;
            $send['dever_token'] = 'test';//自定义key，为空则使用系统的token

            # 发送请求
            Dever::load('pay/lib/set.updateStatus', $send);
        }
    }
}