You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Controller;
  3. use App\Constants\LogLabel;
  4. use App\Model\OrderMain;
  5. use EasyWeChat\Factory;
  6. use Hyperf\DbConnection\Db;
  7. use Hyperf\Guzzle\CoroutineHandler;
  8. use Exception;
  9. class NotifyController extends BaseController
  10. {
  11. public function wxminiOnline()
  12. {
  13. $config = config('wxpay');
  14. $app = Factory::payment($config);
  15. $app['guzzle_handler'] = CoroutineHandler::class;
  16. // 通知回调,进行业务处理
  17. $response = $app->handlePaidNotify(function ($message, $fail) use ($app) {
  18. Db::beginTransaction();
  19. try {
  20. $this->log->event(
  21. LogLabel::PAY_NOTIFY_WXMINI,
  22. $message
  23. );
  24. // 查询订单
  25. $orderMain = OrderMain::query()
  26. ->where(['global_order_id' => $message['out_trade_no'], 'type' => OrderMain::ORDER_TYPE_ONLINE, 'state' => OrderMain::ORDER_STATE_UNPAY])
  27. ->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
  28. ->first();
  29. if (empty($orderMain)) {
  30. // 去查一下微信订单
  31. $wxOrder = $app->order->queryByOutTradeNumber($orderMain->global_order_id);
  32. $this->log->event(
  33. LogLabel::PAY_NOTIFY_WXMINI,
  34. $wxOrder
  35. );
  36. // return true;
  37. }
  38. // 修改订单、子订单状态
  39. $currentTime = time();
  40. $orderMain->state = OrderMain::ORDER_STATE_UNTAKE;
  41. $orderMain->time_pay = $currentTime;
  42. $orderMain->pay_time = date('Y-m-d H:i:s', $currentTime);
  43. $orderMain->save();
  44. // 更新销量、商品库存,新增月销数据
  45. // 喇叭通知,兼容旧音响,MQTT+IOT
  46. // 公众号模板消息
  47. // 打印订单
  48. Db::commit();
  49. return true;
  50. } catch (Exception $e) {
  51. Db::rollBack();
  52. }
  53. });
  54. $response->send();
  55. }
  56. public function wxminiOffline()
  57. {
  58. $config = config('wxpay');
  59. $app = Factory::payment($config);
  60. $app['guzzle_handler'] = CoroutineHandler::class;
  61. // 通知回调,进行业务处理
  62. $response = $app->handlePaidNotify(function ($message, $fail) use ($app) {
  63. $this->log->event(
  64. LogLabel::PAY_NOTIFY_WXMINI,
  65. $message
  66. );
  67. // 查询订单
  68. $orderMain = OrderMain::query()
  69. ->where(['global_order_id' => $message['out_trade_no'], 'type' => OrderMain::ORDER_TYPE_OFFLINE, 'state' => OrderMain::ORDER_STATE_UNPAY])
  70. ->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
  71. ->first();
  72. if (empty($orderMain)) {
  73. // 去查一下微信订单
  74. $wxOrder = $app->order->queryByOutTradeNumber($orderMain->global_order_id);
  75. $this->log->event(
  76. LogLabel::PAY_NOTIFY_WXMINI,
  77. $wxOrder
  78. );
  79. // return true;
  80. }
  81. });
  82. $response->send();
  83. }
  84. }