Core.php 4.2 KB

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