123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php namespace Pay\Lib;
- use Dever;
- class Core
- {
- /**
- * 更新订单状态
- */
- 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'];
- $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']);
- }
- }
- if (!$notify) {
- $notify = Dever::config('base', 'project')->pay_notify;
- $key = md5($notify);
- }
- if ($notify && $key) {
-
- $send = array();
- if (isset($info['product_id'])) {
- $send['pay_product_id'] = $info['product_id'];
- }
- if (isset($info['uid'])) {
- $send['pay_uid'] = $info['uid'];
- }
- if (isset($info['cash'])) {
- $send['pay_cash'] = $info['cash'];
- }
- $send['pay_order_id'] = $order_id;
- $send['pay_status'] = $param['status'];
- $send['pay_msg'] = $msg;
- $send['pay_time'] = time();
- $send['pay_nonce'] = Dever::nonce();
- ksort($send);
- $send['signature'] = md5($key . '&' . http_build_query($send));
- $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 $order_id;
- }
-
- $state = $order_id;
- $db = Dever::db('pay/order');
- if (!$state) {
- $order_id = Dever::order();
- }
- $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 {
- $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 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');
- }
- }
|