Notify.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Goodorder;
  4. use app\admin\model\RecoveryBill;
  5. use app\admin\model\Recoveryorder;
  6. use app\admin\model\TablewareBill;
  7. use app\admin\model\TablewareOrder;
  8. use app\admin\model\transaction\Log;
  9. use app\admin\model\Userscore;
  10. use EasyWeChat\Factory;
  11. use app\common\controller\Api;
  12. use think\Db;
  13. class Notify extends Api
  14. {
  15. protected $noNeedLogin =['*'];
  16. protected $noNeedRight = ['*'];
  17. /**
  18. * @return void
  19. * @throws \EasyWeChat\Kernel\Exceptions\Exception
  20. * 商城购物回调
  21. */
  22. public function good_notify(){
  23. //必要配置
  24. $config = [
  25. // 必要配置
  26. 'app_id' => 'wxdc450b7c5d0a0f80',
  27. 'mch_id' => '1635096198',
  28. 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
  29. 'notify_url' => common_url().'/api/Notice/good_notify', // 你也可以在下单时单独设置来想覆盖它
  30. ];
  31. $app = Factory::payment($config);
  32. $response = $app->handlePaidNotify(function($message, $fail){
  33. file_put_contents('pay_log.log', json_encode($message));
  34. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  35. $pay_model= new \app\admin\model\Pay();
  36. $order_model = new Goodorder();
  37. $user_model = new \app\admin\model\User();
  38. $transaction_log_model = new Log();
  39. $order = $pay_model->where('pay_no',$message['out_trade_no'])->where('status',1)->find();
  40. if (!$order) { // 如果订单不存在 或者 订单已经支付过了
  41. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  42. }
  43. ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
  44. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  45. // 用户是否支付成功
  46. if ($message ['result_code'] === 'SUCCESS') {
  47. $username = $user_model->where('id',$order['user_id'])->value('username');
  48. $log_data = [
  49. 'user_id'=>$order['user_id'],
  50. 'username'=>$username,
  51. 'money'=>$order['price'],
  52. 'desc'=>'商城购物支付',
  53. 'create_time'=>time()
  54. ];
  55. $transaction_log_model->insert($log_data);
  56. $pay_model->save(['status'=>2,'pay_time'=>time()],['pay_no'=>$message['out_trade_no']]);
  57. $order_model->save(['status'=>2],['pay_no'=>$message['out_trade_no']]);
  58. // 用户支付失败
  59. }
  60. } else {
  61. return $fail('通信失败,请稍后再通知我');
  62. }
  63. return true; // 返回处理完成
  64. });
  65. }
  66. /**
  67. * @return void
  68. * @throws \EasyWeChat\Kernel\Exceptions\Exception
  69. * 月结回收支付回调
  70. */
  71. public function recovery_notify(){
  72. //必要配置
  73. $config = [
  74. // 必要配置
  75. 'app_id' => 'wxdc450b7c5d0a0f80',
  76. 'mch_id' => '1635096198',
  77. 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
  78. 'notify_url' => common_url().'/api/Notice/recovery_notify', // 你也可以在下单时单独设置来想覆盖它
  79. ];
  80. $app = Factory::payment($config);
  81. $response = $app->handlePaidNotify(function($message, $fail){
  82. file_put_contents('pay1_log.log', json_encode($message));
  83. $recovery_bill_model = new RecoveryBill();
  84. $user_model = new \app\admin\model\User();
  85. $transaction_log_model = new Log();
  86. file_put_contents('out_trade_no.log', $message['out_trade_no']);
  87. $order = $recovery_bill_model->where('order_no',$message['out_trade_no'])->where('status',1)->find();
  88. file_put_contents('order .log',json_encode($order));
  89. if (!$order) { // 如果订单不存在 或者 订单已经支付过了
  90. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  91. }
  92. ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
  93. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  94. // 用户是否支付成功
  95. file_put_contents('ceshi1_log.log', 11);
  96. if ($message ['result_code'] === 'SUCCESS') {
  97. file_put_contents('ceshi2_log.log', $message['out_trade_no']);
  98. $use_score = $user_model->where('id',$order['c_user_id'])->field('score,username')->find();
  99. $date = getMonthRange(date('Y-m',$order['date']));
  100. file_put_contents('date.log', $date);
  101. $pre = config('site.score_recovery')/100;
  102. $log_data = [
  103. 'user_id'=>$order['c_user_id'],
  104. 'username'=>$use_score['username'],
  105. 'money'=>$order['amount_money'],
  106. 'desc'=>'回收月结支付',
  107. 'create_time'=>time()
  108. ];
  109. $score = intval($order['service_money']*$pre);
  110. file_put_contents('score.log', $score);
  111. $score_num = $use_score['score']+$score;
  112. file_put_contents('end.log', $score_num);
  113. $user_collont = new Userscore();
  114. $user_collont->insert([
  115. 'user_id'=>$order['c_user_id'],
  116. 'number'=>$score,
  117. 'type'=>1,
  118. 'desc'=>'回收月结支付获得',
  119. 'create_time'=>time(),
  120. ]);
  121. Db::name('user')->where('id' , $order['c_user_id'])->update(['score' => $score_num]);
  122. $transaction_log_model->insert($log_data);
  123. $recovery_bill_model->save(['status'=>2,'pay_time'=>time(),'pay_no'=>$message['transaction_id']],['order_no'=>$message['out_trade_no']]);
  124. Db::name('recovery_order')->whereBetween('create_time',[strtotime($date['sdate']),strtotime($date['edate'])])->where('c_user_id',$order['c_user_id'])->update(['status'=>3,'updatetime'=>time()]);
  125. // 用户支付失败
  126. }
  127. } else {
  128. return $fail('通信失败,请稍后再通知我');
  129. }
  130. return true; // 返回处理完成
  131. });
  132. }
  133. public function tableware_notify(){
  134. //必要配置
  135. $config = [
  136. // 必要配置
  137. 'app_id' => 'wxdc450b7c5d0a0f80',
  138. 'mch_id' => '1635096198',
  139. 'key' => 'e3NkU3L7vmz84H92kfKT5d9qBCOmvdEB', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
  140. 'notify_url' => common_url().'/api/Notice/tableware_notify', // 你也可以在下单时单独设置来想覆盖它
  141. ];
  142. $app = Factory::payment($config);
  143. $response = $app->handlePaidNotify(function($message, $fail){
  144. file_put_contents('pay2_log.log', json_encode($message));
  145. $tableware_bill_model = new TablewareBill();
  146. $user_model = new \app\admin\model\User();
  147. $transaction_log_model = new Log();
  148. $order = $tableware_bill_model->where('order_no',$message['out_trade_no'])->where('status',1)->find();
  149. if (!$order) { // 如果订单不存在 或者 订单已经支付过了
  150. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  151. }
  152. ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
  153. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  154. // 用户是否支付成功
  155. if ($message ['result_code'] === 'SUCCESS') {
  156. $use_score = $user_model->where('id',$order['c_user_id'])->field('score,username')->find();
  157. $date = getMonthRange(date('Y-m',$order['date']));
  158. $pre = config('site.score_tableware')/100;
  159. $log_data = [
  160. 'user_id'=>$order['c_user_id'],
  161. 'username'=>$use_score['username'],
  162. 'money'=>$order['amount_money'],
  163. 'desc'=>'餐具配送月结支付',
  164. 'create_time'=>time()
  165. ];
  166. $score = intval($order['amount_money']*$pre);
  167. $score_num = $use_score['score']+$score;
  168. $user_collont = new Userscore();
  169. $user_collont->insert([
  170. 'user_id'=>$order['c_user_id'],
  171. 'number'=>$score,
  172. 'type'=>1,
  173. 'desc'=>'餐具月结支付获得',
  174. 'create_time'=>time(),
  175. ]);
  176. Db::name('user')->where('id' , $order['c_user_id'])->update(['score' => $score_num]);
  177. $transaction_log_model->insert($log_data);
  178. $tableware_bill_model->save(['status'=>2,'pay_time'=>time(),'pay_no'=>$message['transaction_id'],'pay_remarks'=>'微信支付'],['order_no'=>$message['out_trade_no']]);
  179. Db::name('tableware_order')->whereBetween('create_time',[strtotime($date['sdate']),strtotime($date['edate'])])->where('c_user_id',$order['c_user_id'])->update(['status'=>5,'pay_time'=>time()]);
  180. // 用户支付失败
  181. }
  182. } else {
  183. return $fail('通信失败,请稍后再通知我');
  184. }
  185. return true; // 返回处理完成
  186. });
  187. }
  188. public function text()
  189. {
  190. $message['out_trade_no'] = input('out_trade_no');
  191. $recovery_bill_model = new RecoveryBill();
  192. $user_model = new \app\admin\model\User();
  193. $transaction_log_model = new Log();
  194. $order = $recovery_bill_model->where('order_no',$message['out_trade_no'])->where('status',1)->find();
  195. if (!$order) { // 如果订单不存在 或者 订单已经支付过了
  196. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  197. }
  198. $use_score = $user_model->where('id',$order['c_user_id'])->field('score,username')->find();
  199. $date = getMonthRange(date('Y-m',$order['date']));
  200. $pre = config('site.score_recovery')/100;
  201. $log_data = [
  202. 'user_id'=>$order['c_user_id'],
  203. 'username'=>$use_score['username'],
  204. 'money'=>$order['amount_money'],
  205. 'desc'=>'回收月结支付',
  206. 'create_time'=>time()
  207. ];
  208. $score = intval(100*$pre);
  209. $score_num = $use_score['score']+$score;
  210. $user_collont = new Userscore();
  211. $user_collont->insert([
  212. 'user_id'=>$order['c_user_id'],
  213. 'number'=>$score,
  214. 'type'=>1,
  215. 'desc'=>'回收月结支付获得',
  216. 'create_time'=>time(),
  217. ]);
  218. Db::name('user')->where('id' , $order['c_user_id'])->update(['score' => $score_num]);
  219. $transaction_log_model->insert($log_data);
  220. $recovery_bill_model->save(['status'=>2,'pay_time'=>time(),'pay_no'=>1],['order_no'=>$message['out_trade_no']]);
  221. Db::name('recovery_order')->whereBetween('create_time',[strtotime($date['sdate']),strtotime($date['edate'])])->where('c_user_id',$order['c_user_id'])->update(['status'=>3,'updatetime'=>time()]);
  222. // 用户支付失败
  223. }
  224. /**
  225. * @return void
  226. * 我的厨余餐具订单
  227. */
  228. public function me_tab_order(){
  229. $page = input('page',1);
  230. $mon = input('date',date('Y-m'));
  231. $user = 40;
  232. $recovery_model = new Recoveryorder();
  233. $recovery_bill_model = new RecoveryBill();
  234. $date = getMonthRange($mon);
  235. $is_have = $recovery_bill_model->whereBetween('date',[strtotime($date['sdate']),strtotime($date['edate'])])->where('c_user_id',$user)->find();
  236. $list = $recovery_model->whereBetween('create_time',[strtotime($date['sdate']),strtotime($date['edate'])])->where('c_user_id',$user)->paginate('5','',['page'=>$page]);
  237. $sate = 1;
  238. $data['bill']=[];
  239. if($is_have){
  240. if($is_have['status']==1) {
  241. $sate = 2;
  242. }
  243. if($is_have['status']==2) {
  244. $sate = 3;
  245. }
  246. $data['bill']=$is_have;
  247. }
  248. $data['list']=$list;
  249. $data['sate']=$sate;
  250. $this->success('月份订单',$data);
  251. }
  252. }