Core.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php namespace Pay\Lib;
  2. use Dever;
  3. class Core
  4. {
  5. public $order_id;
  6. /**
  7. * 更新订单状态
  8. */
  9. public function updateOrder($order_id, $cash, $desc = '')
  10. {
  11. $project_id = $this->checkOrder($order_id);
  12. if ($project_id) {
  13. $info = array();
  14. $info['status'] = 1;
  15. $info['order_id'] = $order_id;
  16. $info['project_id'] = $project_id;
  17. } else {
  18. $db = Dever::db('pay/order');
  19. $info = $db->one(array('order_id' => $order_id, 'rand' => time() . rand(1,1000)));
  20. }
  21. if ($info && $info['status']) {
  22. $param['status'] = 2;
  23. $msg = '支付成功';
  24. if ($desc) {
  25. $param['status'] = 3;
  26. $param['status_desc'] = $desc;
  27. $msg = '支付失败||' . $desc;
  28. }
  29. $this->log($msg, $info);
  30. if (isset($info['id'])) {
  31. $param['where_id'] = $info['id'];
  32. $db->update($param);
  33. }
  34. $notify = false;
  35. $key = false;
  36. if (isset($info['project_id']) && $info['project_id']) {
  37. $project = Dever::db('pay/project')->one($info['project_id']);
  38. if ($project && $project['notify']) {
  39. $notify = $project['notify'];
  40. //$key = md5($project['key']);
  41. $key = $project['key'];
  42. }
  43. }
  44. if (!$notify) {
  45. $notify = Dever::config('base', 'project')->pay_notify;
  46. //$key = md5($notify);
  47. $key = $notify;
  48. }
  49. if ($notify && $key) {
  50. $send = array();
  51. $send['pay_product_id'] = $info['product_id'];
  52. $send['pay_uid'] = $info['uid'];
  53. $send['pay_cash'] = $info['cash'];
  54. $send['pay_order_id'] = $order_id;
  55. $send['pay_status'] = $param['status'];
  56. $send['pay_msg'] = $msg;
  57. # 2020-08-20 取消之前的加密方式,使用自动加密
  58. /*
  59. $send['pay_time'] = time();
  60. $send['pay_nonce'] = Dever::nonce();
  61. ksort($send);
  62. $send['signature'] = md5($key . '&' . http_build_query($send));
  63. */
  64. $send['dever_token'] = $key;
  65. $this->log($notify, $send);
  66. if (strstr($notify, 'http://') || strstr($notify, 'https://')) {
  67. Dever::curl($notify, $send);
  68. } else {
  69. Dever::load($notify, $send);
  70. }
  71. }
  72. } else {
  73. $this->log('支付失败', '错误的订单id:' . $order_id);
  74. }
  75. }
  76. /**
  77. * 更新订单的支付信息
  78. */
  79. protected function updateOrderParam($order_id, $data)
  80. {
  81. if ($this->checkOrder($order_id)) {
  82. return false;
  83. }
  84. $db = Dever::db('pay/order');
  85. $info = $db->one(array('order_id' => $order_id, 'status' => 1));
  86. if ($info) {
  87. $param['where_id'] = $info['id'];
  88. $param['param'] = Dever::array_encode($data);
  89. $db->update($param);
  90. }
  91. }
  92. /**
  93. * 创建订单
  94. */
  95. protected function createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $type, $order_id = false)
  96. {
  97. if ($this->checkOrder($order_id)) {
  98. return $this->order_id = $order_id;
  99. }
  100. $state = $order_id;
  101. $db = Dever::db('pay/order');
  102. if (!$state) {
  103. $order_id = Dever::order();
  104. }
  105. $this->order_id = $order_id;
  106. $info = $db->one(array('order_id' => $order_id));
  107. if ($info) {
  108. if (!$state) {
  109. return $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $type);
  110. } else {
  111. return $order_id;
  112. }
  113. } else {
  114. if (!$username && Dever::project('passport')) {
  115. $user = Dever::db('passport/user')->one($uid);
  116. if ($user) {
  117. $username = $user['username'];
  118. }
  119. }
  120. $add['status'] = 1;
  121. $add['uid'] = $uid;
  122. $add['username'] = $username;
  123. $add['account_id'] = $account_id;
  124. $add['project_id'] = $project_id;
  125. $add['name'] = $name;
  126. $add['cash'] = $cash;
  127. $add['type'] = $type;
  128. $add['order_id'] = $order_id;
  129. $add['product_id'] = $product_id;
  130. $add['id'] = $db->insert($add);
  131. $msg = '发起支付';
  132. $this->log($msg, $add);
  133. }
  134. return $order_id;
  135. }
  136. public function checkOrder($order_id)
  137. {
  138. if (strstr($order_id, 'O')) {
  139. $array = explode('O', $order_id);
  140. return $array[1];
  141. }
  142. return false;
  143. }
  144. /**
  145. * 获取回调url
  146. */
  147. protected function url($type, $account_id)
  148. {
  149. //$project = Dever::project('pay');
  150. //return $project['url'] . 'daemon/notify/'.$type.'.php';
  151. return str_replace('?', '', Dever::url('api.notify?account_id=' . $account_id, 'pay'));
  152. }
  153. /**
  154. * 写日志
  155. */
  156. public function log($msg, $data = array())
  157. {
  158. if ($data) {
  159. $data = Dever::json_encode($data);
  160. $msg .= '||' . $data;
  161. /*
  162. $insert = $data;
  163. $insert['cdate'] = time();
  164. Dever::db('pay/order_log')->insert($insert);
  165. */
  166. }
  167. Dever::log($msg, 'pay');
  168. }
  169. }