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.

240 lines
8.3 KiB

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