Pay.php 10 KB

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