Core.php 5.3 KB

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