Pay.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. #新版本的异步支付
  3. namespace Act\Lib;
  4. use Dever;
  5. class Pay
  6. {
  7. private $key = 'jmss_2018';
  8. # 退款操作
  9. public function order($id, $name, $data)
  10. {
  11. $status = Dever::param('status', $data);
  12. if ($status == 5 && $id > 0) {
  13. $send = array();
  14. $info = Dever::db('act/order')->one($id);
  15. if ($info['system'] == 1) {
  16. if ($info['cate_id'] == 1) {
  17. $account_id = 1;
  18. }
  19. if ($info['cate_id'] == 2) {
  20. $account_id = 4;
  21. }
  22. if ($info['cate_id'] == 3) {
  23. $account_id = 5;
  24. }
  25. } elseif ($info['system'] == 2) {
  26. $account_id = 2;
  27. } else {
  28. $account_id = 3;
  29. }
  30. $send['pay_project_id'] = 3;
  31. $send['pay_uid'] = $info['uid'];
  32. $send['pay_order_id'] = $info['order_id'] ? $info['order_id'] : Dever::load('act/order')->createOrderId($info, $info['id']);
  33. $send['pay_tk_pic'] = Dever::param('tk_pic', $data);
  34. $send['pay_tk_time'] = Dever::param('tk_time', $data);
  35. $send['pay_tk_desc'] = Dever::param('tk_desc', $data);
  36. $send['pay_account_id'] = $account_id;
  37. $send['pay_cash'] = $info['cash'];
  38. $send['pay_status'] = $status;
  39. $send['dever_token'] = $this->key;
  40. Dever::load('pay/lib/set.updateStatus', $send);
  41. if ($info['buy_id'] > 0) {
  42. $buy = Dever::db('journal/buy_num')->one($info['buy_id']);
  43. if ($buy) {
  44. if ($info['buy_num'] > 0) {
  45. $num = $info['buy_num'];
  46. } else {
  47. $num = $buy['num'];
  48. }
  49. # 减少积分
  50. $score = $num * 20;
  51. $user = Dever::db('score/user')->one(array('uid' => $info['uid']));
  52. if ($user) {
  53. $user_id = $user['id'];
  54. $user_score = $user['score'];
  55. $user_score = $user_score - $score;
  56. if ($user_score < 0) {
  57. $user_score = 0;
  58. }
  59. Dever::db('score/user')->update(array('where_id' => $user_id, 'score' => $user_score));
  60. }
  61. # 减少排行榜
  62. $journal_num = Dever::db('act/journal_num')->one(array('uid' => $info['uid'], 'journal_id' => $info['product_id']));
  63. if ($journal_num) {
  64. $journal_num_id = $journal_num['id'];
  65. $journal_num = $journal_num['num'];
  66. $journal_num = $journal_num - $num;
  67. if ($journal_num < 0) {
  68. $journal_num = 0;
  69. }
  70. Dever::db('act/journal_num')->update(array('where_id' => $journal_num_id, 'num' => $journal_num));
  71. }
  72. # 减少订阅本数 这个定时跑就行了 不管了
  73. $journal = Dever::db('journal/info')->one($info['product_id']);
  74. if ($journal) {
  75. $journal_id = $journal['id'];
  76. $journal_num = $journal['num_ding'];
  77. $journal_num = $journal_num - $num;
  78. if ($journal_num < 0) {
  79. $journal_num = 0;
  80. }
  81. Dever::db('journal/info')->update(array('where_id' => $journal_id, 'num_ding' => $journal_num));
  82. }
  83. }
  84. }
  85. }
  86. return 'reload';
  87. }
  88. /**
  89. * 支付成功后,调取的接口 这里的安全以后再升级吧,升级成和pay/lib/set.updateStatus一样的
  90. *
  91. * @return mixed
  92. */
  93. public function act($param = array())
  94. {
  95. $this->act_action($param);
  96. return 'ok';
  97. $send = Dever::preInput('pay_');
  98. $send = $param;
  99. $key = md5($this->key);
  100. ksort($send);
  101. $send['signature'] = md5($key . '&' . http_build_query($send));
  102. $signature = Dever::input('signature');
  103. if ($send['signature'] == $signature) {
  104. $this->act_action($send);
  105. }
  106. return 'ok';
  107. }
  108. public function act_action($send)
  109. {
  110. //$product_id = $send['pay_product_id'];
  111. //$uid = $send['pay_uid'];
  112. //$cash = $send['pay_cash'];
  113. $order_id = $send['pay_order_id'];
  114. $status = $send['pay_status'];
  115. $msg = $send['pay_msg'];
  116. $temp = explode('D', $order_id);
  117. $id = $temp[1];
  118. //$order = Dever::db('act/order_temp')->one(array('order_id' => $order_id, 'uid' => $uid));
  119. $order = Dever::db('act/order_temp')->one($id);
  120. if ($send['pay_status'] == 2 && $order) {
  121. $update = $order;
  122. //$update['where_id'] = $order['id'];
  123. $update['status'] = 2;
  124. if ($order['type'] == 3 && !$order['code']) {
  125. if ($order['system'] == 2) {
  126. $code = Dever::load('code/lib/core')->createCodeByOrder($order);
  127. } else {
  128. $code = Dever::load('code/lib/core')->createCodeByOrder($order, -1);
  129. }
  130. if ($code) {
  131. $update['code'] = $code;
  132. }
  133. }
  134. Dever::db('act/order')->insert($update);
  135. $score = false;
  136. $num = false;
  137. $journal = Dever::load('act/order')->getJournal($order['product_id']);
  138. if ($order['buy_id'] > 0) {
  139. $buy = Dever::load('act/order')->getBuy($order['buy_id']);
  140. if ($buy && $buy['score'] > 0) {
  141. $score = $buy['num'] * $buy['score'];
  142. } else {
  143. if ($order['type'] == 1) {
  144. # 直接购买
  145. $col = 'score';
  146. } elseif ($order['type'] == 3) {
  147. $col = 'dh_score';
  148. }
  149. if ($journal && $buy && $journal[$col] > 0) {
  150. $score = $buy['num'] * $journal[$col];
  151. } elseif ($journal && $buy) {
  152. $num = $buy['num'];
  153. }
  154. }
  155. if ($buy && $buy['num']) {
  156. $active = Dever::load('act/order')->getActive($order['product_id']);
  157. $active_state = true;
  158. if ($active && $active['status'] == 1 && time() > $active['end']) {
  159. $active_state = false;
  160. }
  161. if ($active_state) {
  162. # 增加本数
  163. Dever::load('act/lib/num')->submit($order['product_id'], $order['uid'], $buy['num']);
  164. }
  165. # 增加订阅数
  166. //Dever::load('act/lib/num')->setCounter($order['product_id'], $buy['num']);
  167. }
  168. }
  169. # 积分
  170. //Dever::score($order['uid'], 'buy_journal', '购买小刊', '', $score, $num);
  171. if ($order['system'] == 2) {
  172. return;
  173. }
  174. # 发消息
  175. $journal['id'] = $order['product_id'];
  176. $journal['name'] = $order['name'];
  177. if (strstr('-', $journal['name'])) {
  178. $name = explode('-', $journal['name']);
  179. $journal['name'] = $name[0];
  180. }
  181. //$journal = Dever::db('journal/info')->one($order['product_id']);
  182. if (Dever::project('message')) {
  183. Dever::load('message/lib/data')->push(-1, $order['uid'], '购买提醒', '购买成功,您获得了 '.$journal['name'].' 的阅读资格!', 11, $order['cate_id'], 1, Dever::load('act/lib/note')->push(4, $journal['id'], $journal['name']));
  184. }
  185. $user = Dever::db('passport/user')->one($order['uid']);
  186. # 发短信
  187. /*
  188. if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms') && $order['cate_id'] == 1) {
  189. $send = array();
  190. $send['name'] = $journal['name'];
  191. Dever::load('sms/api.send', 'buy_journal', $user['mobile'], $send);
  192. }
  193. */
  194. # 发模板消息
  195. $wechat = Dever::db('passport/wechat')->one(array('uid' => $order['uid'], 'type' => 1, 'system_id' => $order['cate_id']));
  196. if ($wechat && Dever::project('wechat_applet')) {
  197. $send['key'] = 'buy_journal';
  198. $send['project_id'] = $order['cate_id'];
  199. $send['touser'] = $wechat['openid'];
  200. $send['page'] = Dever::config('base')->applet_index . '?scene=' . $order['uid'] . ',' . '4,' . $order['product_id'];
  201. $send['data'] = array
  202. (
  203. 'keyword1' => array('value' => date('Y年m月d日 H:i', $order['cdate'])),
  204. 'keyword2' => array('value' => '购买成功,您获得了 '.$journal['name'].' 的阅读资格!'),
  205. );
  206. $send['data'] = json_encode($send['data']);
  207. $send['form_id'] = Dever::load('act/lib/form')->get($order['uid'], 2, $order['cate_id']);
  208. if ($send['form_id']) {
  209. Dever::load('wechat_applet/msg.send', $send);
  210. }
  211. }
  212. } else {
  213. Dever::db('act/order_temp')->update(array('where_id' => $order['id'], 'status' => 3));
  214. }
  215. }
  216. public function send_api()
  217. {
  218. $uid = Dever::input('uid', 8);
  219. # 发模板消息
  220. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid));
  221. if ($wechat && Dever::project('wechat_applet')) {
  222. $send['key'] = 'buy_journal';
  223. $send['project_id'] = 2;
  224. $send['touser'] = $wechat['openid'];
  225. $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($uid) . ',' . '4,' . $order['product_id'];
  226. $send['data'] = array
  227. (
  228. 'keyword1' => array('value' => date('Y年m月d日 H:i', $order['cdate'])),
  229. 'keyword2' => array('value' => '购买成功,您获得了 '.$journal['name'].' 的阅读资格!'),
  230. );
  231. $send['data'] = json_encode($send['data']);
  232. $send['form_id'] = Dever::load('act/lib/form')->get($uid, 2, $order['cate_id']);
  233. if ($send['form_id']) {
  234. Dever::load('wechat_applet/msg.send', $send);
  235. }
  236. }
  237. }
  238. }