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.

231 lines
7.9 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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\Goods;
  5. use App\Model\Order;
  6. use App\Model\OrderGoods;
  7. use App\Model\OrderMain;
  8. use App\Model\OrderSalesStatistic;
  9. use App\Model\SpecCombination;
  10. use App\Model\Store;
  11. use App\Model\SystemConfig;
  12. use App\Service\DeviceServiceInterface;
  13. use App\Service\FeiePrintServiceInterface;
  14. use App\Service\MiniprogramService;
  15. use App\Service\MqttServiceInterface;
  16. use EasyWeChat\Factory;
  17. use Hyperf\DbConnection\Db;
  18. use Hyperf\Guzzle\CoroutineHandler;
  19. use Exception;
  20. use Hyperf\Di\Annotation\Inject;
  21. use Symfony\Component\HttpFoundation\Request;
  22. class NotifyController extends BaseController
  23. {
  24. /**
  25. * @Inject
  26. * @var MqttServiceInterface
  27. */
  28. protected $mqttSpeakerService;
  29. /**
  30. * @Inject
  31. * @var DeviceServiceInterface
  32. */
  33. protected $deviceService;
  34. /**
  35. * @Inject
  36. * @var MiniprogramService
  37. */
  38. protected $miniprogramService;
  39. /**
  40. * @Inject
  41. * @var FeiePrintServiceInterface
  42. */
  43. protected $feiePrintService;
  44. public function wxminiOnline()
  45. {
  46. $config = config('wxpay');
  47. $app = Factory::payment($config);
  48. $app['guzzle_handler'] = CoroutineHandler::class;
  49. $get = $this->request->getQueryParams();
  50. $post = $this->request->getParsedBody();
  51. $cookie = $this->request->getCookieParams();
  52. $files = $this->request->getUploadedFiles();
  53. $server = $this->request->getServerParams();
  54. $xml = $this->request->getBody()->getContents();
  55. $app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
  56. var_dump('inside');
  57. // 通知回调,进行业务处理
  58. $response = $app->handlePaidNotify(function ($message, $fail) use ($app) {
  59. Db::beginTransaction();
  60. try {
  61. var_dump('message', $message);
  62. // 支付失败或者通知失败
  63. if (
  64. empty($message)
  65. || $message['return_code'] != 'SUCCESS'
  66. || !isset($message['result_code'])
  67. || $message['result_code'] != 'SUCCESS'
  68. ) {
  69. $this->log->event(
  70. LogLabel::PAY_NOTIFY_WXMINI,
  71. $message
  72. );
  73. Db::rollBack();
  74. $fail('Unknown error but FAIL');
  75. }
  76. // 查询订单
  77. $orderMain = OrderMain::query()
  78. ->where([
  79. 'global_order_id' => $message['out_trade_no'],
  80. 'type' => OrderMain::ORDER_TYPE_ONLINE
  81. ])
  82. ->first();
  83. var_dump('$orderMain', $orderMain);
  84. // 订单不存在
  85. if (empty($orderMain)) {
  86. $this->log->event(
  87. LogLabel::PAY_NOTIFY_WXMINI,
  88. ['global_order_id_fail' => $message['out_trade_no']]
  89. );
  90. Db::rollBack();
  91. return true;
  92. }
  93. // 修改订单、子订单状态
  94. $currentTime = time();
  95. $orderMain->state = OrderMain::ORDER_STATE_UNTAKE;
  96. $orderMain->time_pay = $currentTime;
  97. $orderMain->pay_time = date('Y-m-d H:i:s', $currentTime);
  98. $orderMain->save();
  99. $upOrder = Order::query()
  100. ->where(['order_main_id' => $orderMain->id])
  101. ->update(['state' => OrderMain::ORDER_STATE_UNTAKE, 'pay_time' => $orderMain->pay_time]);
  102. // 更新商户销量
  103. $upStoreScore = Store::query()
  104. ->whereIn('id', explode(',', $orderMain->store_ids))
  105. ->update(['score' => Db::raw('score+1')]);
  106. // 更新商品库存和销量
  107. $orders = Order::query()->select(['id', 'money', 'user_id', 'store_id', 'pay_time'])
  108. ->where(['order_main_id' => $orderMain->id])
  109. ->get()
  110. ->toArray();
  111. $orderGoods = OrderGoods::query()->select(['good_id AS id', 'number', 'combination_id'])
  112. ->whereIn('order_id', array_values(array_column($orders, 'id')))
  113. ->get()
  114. ->toArray();
  115. foreach ($orderGoods as $key => &$goodsItem) {
  116. $goods = Goods::find($goodsItem['id']);
  117. // 库存处理,有规格
  118. if ($goodsItem['combination_id']) {
  119. $combination = SpecCombination::find($goodsItem['combination_id']);
  120. $combination->number = $combination->number - $goodsItem['number'];
  121. $combination->save();
  122. } else {
  123. $goods->inventory = $goods->inventory - $goodsItem['number'];
  124. }
  125. $goods->sales = $goods->sales - $goodsItem['number'];
  126. $goods->save();
  127. }
  128. // 月销流水
  129. $statistics = [];
  130. foreach ($orders as $key => &$order) {
  131. $statistics[] = [
  132. 'money' => $order['money'],
  133. 'user_id' => $order['user_id'],
  134. 'store_id' => $order['store_id'],
  135. 'market_id' => $orderMain->market_id,
  136. 'order_id' => $order['id'],
  137. 'createtime' => strtotime($order['pay_time']),
  138. ];
  139. }
  140. if (is_array($statistics) && !empty($statistics)) {
  141. $inSalesStatistics = OrderSalesStatistic::query()->insert($statistics);
  142. }
  143. // 喇叭通知,兼容旧音响,MQTT+IOT
  144. $res = $this->mqttSpeakerService->speakToStore($orderMain->id);
  145. var_dump('speakToStore',$res);
  146. $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->id);
  147. var_dump('pubMsgToStoreByOrderMainId',$res);
  148. // 公众号模板消息
  149. // $res = $this->miniprogramService->sendTemMsgForOnlineOrder($orderMain->id);
  150. // var_dump('sendTemMsgForOnlineOrder',$res);
  151. // 打印订单,自动打印 TODO 后续优化调用逻辑
  152. $res = $this->feiePrintService->feiePrint($orderMain->order_num);
  153. var_dump('feiePrint',$res);
  154. Db::commit();
  155. return true;
  156. } catch (Exception $e) {
  157. var_dump($e->getMessage());
  158. Db::rollBack();
  159. $fail('Exception');
  160. }
  161. });
  162. $response->send();
  163. }
  164. public function wxminiOffline()
  165. {
  166. $config = config('wxpay');
  167. $app = Factory::payment($config);
  168. $app['guzzle_handler'] = CoroutineHandler::class;
  169. // 通知回调,进行业务处理
  170. $response = $app->handlePaidNotify(function ($message, $fail) use ($app) {
  171. $this->log->event(
  172. LogLabel::PAY_NOTIFY_WXMINI,
  173. $message
  174. );
  175. // 查询订单
  176. $orderMain = OrderMain::query()
  177. ->where(['global_order_id' => $message['out_trade_no'], 'type' => OrderMain::ORDER_TYPE_OFFLINE, 'state' => OrderMain::ORDER_STATE_UNPAY])
  178. ->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
  179. ->first();
  180. if (empty($orderMain)) {
  181. // 去查一下微信订单
  182. $wxOrder = $app->order->queryByOutTradeNumber($orderMain->global_order_id);
  183. $this->log->event(
  184. LogLabel::PAY_NOTIFY_WXMINI,
  185. $wxOrder
  186. );
  187. // return true;
  188. }
  189. });
  190. $response->send();
  191. }
  192. }