Core.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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['id'] = 1;
  15. $info['status'] = 1;
  16. $info['order_id'] = $order_id;
  17. $info['project_id'] = $project_id;
  18. } else {
  19. $db = Dever::db('pay/order');
  20. $info = $db->one(array('order_id' => $order_id, 'rand' => time() . rand(1,1000)));
  21. }
  22. if ($info && $info['status']) {
  23. $param['status'] = 2;
  24. $msg = '支付成功';
  25. if ($desc) {
  26. $param['status'] = 3;
  27. $param['status_desc'] = $desc;
  28. $msg = '支付失败||' . $desc;
  29. }
  30. $this->log($msg, $info);
  31. if (isset($info['id'])) {
  32. $param['where_id'] = $info['id'];
  33. if ($param['status']) {
  34. $param['pay_time'] = time();
  35. }
  36. $db->update($param);
  37. }
  38. $notify = false;
  39. $key = false;
  40. if (isset($info['project_id']) && $info['project_id']) {
  41. $project = Dever::db('pay/project')->one($info['project_id']);
  42. if ($project && $project['notify']) {
  43. $notify = $project['notify'];
  44. //$key = md5($project['key']);
  45. $key = $project['key'];
  46. }
  47. }
  48. if (!$notify) {
  49. $notify = Dever::config('base', 'project')->pay_notify;
  50. //$key = md5($notify);
  51. $key = $notify;
  52. }
  53. if ($notify && $key) {
  54. $send = array();
  55. $send['pay_product_id'] = $info['product_id'];
  56. $send['pay_uid'] = $info['uid'];
  57. $send['pay_cash'] = $info['cash'];
  58. $send['pay_order_id'] = $order_id;
  59. $send['pay_status'] = $param['status'];
  60. $send['pay_msg'] = $msg;
  61. $send['pay_id'] = $info['id'];
  62. # 2020-08-20 取消之前的加密方式,使用自动加密
  63. /*
  64. $send['pay_time'] = time();
  65. $send['pay_nonce'] = Dever::nonce();
  66. ksort($send);
  67. $send['signature'] = md5($key . '&' . http_build_query($send));
  68. */
  69. $send['dever_token'] = $key;
  70. $this->log($notify, $send);
  71. if (strstr($notify, 'http://') || strstr($notify, 'https://')) {
  72. Dever::curl($notify, $send);
  73. } else {
  74. Dever::load($notify, $send);
  75. }
  76. }
  77. } else {
  78. $this->log('支付失败', '错误的订单id:' . $order_id);
  79. }
  80. }
  81. /**
  82. * 更新订单的支付信息
  83. */
  84. protected function updateOrderParam($order_id, $data)
  85. {
  86. if ($this->checkOrder($order_id)) {
  87. return false;
  88. }
  89. $db = Dever::db('pay/order');
  90. $info = $db->one(array('order_id' => $order_id, 'status' => 1));
  91. if ($info) {
  92. $param['where_id'] = $info['id'];
  93. $param['param'] = Dever::array_encode($data);
  94. $db->update($param);
  95. }
  96. }
  97. /**
  98. * 创建订单
  99. */
  100. protected function createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $type, $order_id = false)
  101. {
  102. if ($this->checkOrder($order_id)) {
  103. return $this->order_id = $order_id;
  104. }
  105. $state = $order_id;
  106. $db = Dever::db('pay/order');
  107. if (!$state) {
  108. $order_id = Dever::order();
  109. }
  110. $this->order_id = $order_id;
  111. $info = $db->one(array('order_id' => $order_id));
  112. if ($info) {
  113. if (!$state) {
  114. return $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $type);
  115. } else {
  116. return $order_id;
  117. }
  118. } else {
  119. if (!$username && Dever::project('passport')) {
  120. $user = Dever::db('passport/user')->one($uid);
  121. if ($user) {
  122. $username = $user['username'];
  123. }
  124. }
  125. $add['status'] = 1;
  126. $add['uid'] = $uid;
  127. $add['username'] = $username;
  128. $add['account_id'] = $account_id;
  129. $add['project_id'] = $project_id;
  130. $add['name'] = $name;
  131. $add['cash'] = $cash;
  132. $add['type'] = $type;
  133. $add['order_id'] = $order_id;
  134. $add['product_id'] = $product_id;
  135. $add['id'] = $db->insert($add);
  136. $msg = '发起支付';
  137. $this->log($msg, $add);
  138. }
  139. return $order_id;
  140. }
  141. public function checkOrder($order_id)
  142. {
  143. if (strstr($order_id, 'O')) {
  144. $array = explode('O', $order_id);
  145. return $array[1];
  146. }
  147. return false;
  148. }
  149. /**
  150. * 获取回调url
  151. */
  152. protected function url($type, $account_id)
  153. {
  154. //$project = Dever::project('pay');
  155. //return $project['url'] . 'daemon/notify/'.$type.'.php';
  156. return str_replace('?', '', Dever::url('api.notify?account_id=' . $account_id, 'pay'));
  157. }
  158. # 退款
  159. public function refundByOrder($order_id, $refund_order_id = false, $refund_cash = false, $other = false)
  160. {
  161. $info = Dever::db('pay/order')->one(array('order_id' => $order_id));
  162. if ($info && ($info['status'] == 1 || $info['status'] == 2 || $info['status'] == 6)) {
  163. if ($info['param']) {
  164. $info['param'] = Dever::array_decode($info['param']);
  165. if (isset($info['param']['other']) && $info['param']['other'] && $other) {
  166. $info['param']['other'] = $other;
  167. }
  168. }
  169. $cash = $refund_cash ? $refund_cash : $info['cash'];
  170. $refund_data = array();
  171. if ($refund_order_id) {
  172. # 检测该订单是否退款过
  173. $refund_data['order_id'] = $info['id'];
  174. $refund_data['order_num'] = $refund_order_id;
  175. if ($refund_cash) {
  176. $refund_data['cash'] = $refund_cash;
  177. }
  178. $refund_data['clear'] = true;
  179. $state = Dever::db('pay/refund')->one($refund_data);
  180. if ($state) {
  181. return false;
  182. }
  183. $refund_order_id = $refund_order_id . '_' . Dever::order();
  184. }
  185. $state = $this->refund($info['order_id'], $cash, $info, $refund_order_id, $other);
  186. if ($state) {
  187. if ($refund_order_id) {
  188. $status = 6;
  189. if ($refund_data) {
  190. Dever::db('pay/refund')->insert($refund_data);
  191. }
  192. } else {
  193. $status = 5;
  194. }
  195. Dever::db('pay/order')->update(array('where_id' => $info['id'], 'status' => $status, 'refund_cash' => $cash));
  196. return 'yes';
  197. } else {
  198. return false;
  199. }
  200. }
  201. return false;
  202. }
  203. /**
  204. * 写日志
  205. */
  206. public function log($msg, $data = array())
  207. {
  208. if ($data) {
  209. $data = Dever::json_encode($data);
  210. $msg .= '||' . $data;
  211. /*
  212. $insert = $data;
  213. $insert['cdate'] = time();
  214. Dever::db('pay/order_log')->insert($insert);
  215. */
  216. }
  217. Dever::log($msg, 'pay');
  218. }
  219. }