<?php namespace Pay\Lib;
use Dever;

class Core
{
	public $order_id;

	/**
	 * 更新订单状态
	 */
	public function updateOrder($order_id, $cash, $desc = '')
	{
		$project_id = $this->checkOrder($order_id);
		if ($project_id) {
			$info = array();
			$info['id'] = 1;
			$info['status'] = 1;
			$info['order_id'] = $order_id;
			$info['project_id'] = $project_id;
		} else {
			$db = Dever::db('pay/order');
			$info = $db->one(array('order_id' => $order_id, 'rand' => time() . rand(1,1000)));
		}
		
		if ($info && $info['status']) {

			$param['status'] = 2;
			$msg = '支付成功';
			if ($desc) {
				$param['status'] = 3;
				$param['status_desc'] = $desc;
				$msg = '支付失败||' . $desc;
			}
			$this->log($msg, $info);

			if (isset($info['id'])) {
				$param['where_id'] = $info['id'];
				if ($param['status']) {
					$param['pay_time'] = time();
				}
				
				$db->update($param);
			}

			$notify = false;
			$key = false;
			if (isset($info['project_id']) && $info['project_id']) {
				$project = Dever::db('pay/project')->one($info['project_id']);
				if ($project && $project['notify']) {
					$notify = $project['notify'];
					//$key = md5($project['key']);
					$key = $project['key'];
				}
			}

			if (!$notify) {
				$notify = Dever::config('base', 'project')->pay_notify;
				//$key = md5($notify);
				$key = $notify;
			}

			if ($notify && $key) {
				
				$send = array();
				$send['pay_product_id'] = $info['product_id'];
				$send['pay_uid'] = $info['uid'];
				$send['pay_cash'] = $info['cash'];
				$send['pay_order_id'] = $order_id;
				$send['pay_status'] = $param['status'];
				$send['pay_msg'] = $msg;
				$send['pay_id'] = $info['id'];
				# 2020-08-20 取消之前的加密方式,使用自动加密
				/*
				$send['pay_time'] = time();
				$send['pay_nonce'] = Dever::nonce();
				ksort($send);
				$send['signature'] = md5($key . '&' . http_build_query($send));
				*/
				$send['dever_token'] = $key;
				$this->log($notify, $send);

				if (strstr($notify, 'http://') || strstr($notify, 'https://')) {
					Dever::curl($notify, $send);
				} else {
					Dever::load($notify, $send);
				}
			}
		} else {
			$this->log('支付失败', '错误的订单id:' . $order_id);
		}
	}

	/**
	 * 更新订单的支付信息
	 */
	protected function updateOrderParam($order_id, $data)
	{
		if ($this->checkOrder($order_id)) {
			return false;
		}
		$db = Dever::db('pay/order');
		$info = $db->one(array('order_id' => $order_id, 'status' => 1));
		if ($info) {
			$param['where_id'] = $info['id'];
			$param['param'] = Dever::array_encode($data);
			$db->update($param);
		}
	}

	/**
	 * 创建订单
	 */
	protected function createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $type, $order_id = false)
	{
		if ($this->checkOrder($order_id)) {
			return $this->order_id = $order_id;
		}
		
		$state = $order_id;
		$db = Dever::db('pay/order');
		if (!$state) {
			$order_id = Dever::order();
		}
		$this->order_id = $order_id;
		$info = $db->one(array('order_id' => $order_id));
		if ($info) {
			if (!$state) {
				return $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $type);
			} else {
				return $order_id;
			}
		} else {

			if (!$username && Dever::project('passport')) {
				$user = Dever::db('passport/user')->one($uid);
				if ($user) {
					$username = $user['username'];
				}
			}
			$add['status'] = 1;
			$add['uid'] = $uid;
			$add['username'] = $username;
			$add['account_id'] = $account_id;
			$add['project_id'] = $project_id;
			$add['name'] = $name;
			$add['cash'] = $cash;
			$add['type'] = $type;
			$add['order_id'] = $order_id;
			$add['product_id'] = $product_id;
			$add['id'] = $db->insert($add);
			$msg = '发起支付';
			$this->log($msg, $add);
		}
		return $order_id;
	}

	public function checkOrder($order_id)
	{
		if (strstr($order_id, 'O')) {
			$array = explode('O', $order_id);
			return $array[1];
		}

		return false;
	}

	/**
	 * 获取回调url
	 */
	protected function url($type, $account_id)
	{
		//$project = Dever::project('pay');
		//return $project['url'] . 'daemon/notify/'.$type.'.php';
		return str_replace('?', '', Dever::url('api.notify?account_id=' . $account_id, 'pay'));
	}

	# 退款
	public function refundByOrder($order_id, $refund_order_id = false, $refund_cash = false, $other = false)
	{
		$info = Dever::db('pay/order')->one(array('order_id' => $order_id));
		if ($info && ($info['status'] == 1 || $info['status'] == 2 || $info['status'] == 6)) {
			if ($info['param']) {
				$info['param'] = Dever::array_decode($info['param']);
				if (isset($info['param']['other']) && $info['param']['other'] && $other) {
					$info['param']['other'] = $other;
				}
			}
			$cash = $refund_cash ? $refund_cash : $info['cash'];
			$refund_data = array();
			if ($refund_order_id) {
				# 检测该订单是否退款过
				$refund_data['order_id'] = $info['id'];
				$refund_data['order_num'] = $refund_order_id;
				if ($refund_cash) {
					$refund_data['cash'] = $refund_cash;
				}
				$refund_data['clear'] = true;
				$state = Dever::db('pay/refund')->one($refund_data);
				if ($state) {
					return false;
				}
				$refund_order_id = $refund_order_id . '_' . Dever::order();
			}
			$state = $this->refund($info['order_id'], $cash, $info, $refund_order_id, $other);
			if ($state) {
				if ($refund_order_id) {
					$status = 6;
					if ($refund_data) {
						Dever::db('pay/refund')->insert($refund_data);
					}
				} else {
					$status = 5;
				}
				Dever::db('pay/order')->update(array('where_id' => $info['id'], 'status' => $status, 'refund_cash' => $cash));

				return 'yes';
			} else {
				return false;
			}
		}

		return false;
	}

	/**
	 * 写日志
	 */
	public function log($msg, $data = array())
	{
		if ($data) {
			$data = Dever::json_encode($data);
			$msg .= '||' . $data;
			/*
			$insert = $data;
			$insert['cdate'] = time();
			Dever::db('pay/order_log')->insert($insert);
			*/
		}

		Dever::log($msg, 'pay');
	}
}