| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 | <?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['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;				# 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)	{		$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']);			}			$cash = $refund_cash ? $refund_cash : $info['cash'];			$state = $this->refund($info['order_id'], $cash, $info, $refund_order_id);			if ($state) {				if ($refund_order_id) {					$status = 6;				} 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');	}}
 |