Pay.php 7.9 KB

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