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.

416 lines
14 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Controller\v3;
  3. use App\Constants\v3\LogLabel;
  4. use App\Constants\v3\OrderState;
  5. use App\Constants\v3\OrderType;
  6. use App\Constants\v3\Payment;
  7. use App\Controller\BaseController;
  8. use App\Model\v3\Goods;
  9. use App\Model\v3\GoodsActivity;
  10. use App\Model\v3\Order;
  11. use App\Model\v3\OrderGoods;
  12. use App\Model\v3\OrderMain;
  13. use App\Model\v3\User;
  14. use App\Service\v3\Interfaces\BadgeServiceInterface;
  15. use App\Service\v3\Interfaces\CouponRebateServiceInterface;
  16. use App\Service\v3\Interfaces\CouponServiceInterface;
  17. use App\Service\v3\Interfaces\DeviceServiceInterface;
  18. use App\Service\v3\Interfaces\FeiePrintServiceInterface;
  19. use App\Service\v3\Interfaces\FinancialRecordServiceInterface;
  20. use App\Service\v3\Interfaces\GoodsActivityServiceInterface;
  21. use App\Service\v3\Interfaces\MiniprogramServiceInterface;
  22. use App\Service\v3\Interfaces\MqttServiceInterface;
  23. use App\Service\v3\Interfaces\OrderOfflineServiceInterface;
  24. use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
  25. use App\Service\v3\Interfaces\OrderStatisticsServiceInterface;
  26. use App\Service\v3\Interfaces\SeparateAccountsServiceInterface;
  27. use App\Service\v3\Interfaces\UserInfoServiceInterface;
  28. use EasyWeChat\Factory;
  29. use Hyperf\DbConnection\Db;
  30. use Hyperf\Guzzle\CoroutineHandler;
  31. use Exception;
  32. use Hyperf\Di\Annotation\Inject;
  33. use Hyperf\HttpMessage\Stream\SwooleStream;
  34. use Symfony\Component\HttpFoundation\Request;
  35. class NotifyController extends BaseController
  36. {
  37. /**
  38. * @Inject
  39. * @var MqttServiceInterface
  40. */
  41. protected $mqttService;
  42. /**
  43. * @Inject
  44. * @var DeviceServiceInterface
  45. */
  46. protected $deviceService;
  47. /**
  48. * @Inject
  49. * @var MiniprogramServiceInterface
  50. */
  51. protected $miniprogramService;
  52. /**
  53. * @Inject
  54. * @var FeiePrintServiceInterface
  55. */
  56. protected $feiePrintService;
  57. /**
  58. * @Inject
  59. * @var CouponRebateServiceInterface
  60. */
  61. protected $couponRebateService;
  62. /**
  63. * @Inject
  64. * @var OrderOnlineServiceInterface
  65. */
  66. protected $orderOnlineService;
  67. /**
  68. * @Inject
  69. * @var OrderOfflineServiceInterface
  70. */
  71. protected $orderOfflineService;
  72. /**
  73. * @Inject
  74. * @var SeparateAccountsServiceInterface
  75. */
  76. protected $separateAccountsService;
  77. /**
  78. * @Inject
  79. * @var CouponServiceInterface
  80. */
  81. protected $couponService;
  82. /**
  83. * @Inject
  84. * @var FinancialRecordServiceInterface
  85. */
  86. protected $financialService;
  87. /**
  88. * @Inject
  89. * @var BadgeServiceInterface
  90. */
  91. protected $badgeService;
  92. /**
  93. * @Inject
  94. * @var GoodsActivityServiceInterface
  95. */
  96. protected $goodsActivityService;
  97. /**
  98. * @Inject
  99. * @var OrderStatisticsServiceInterface
  100. */
  101. protected $orderStatisticsService;
  102. /**
  103. * @Inject
  104. * @var UserInfoServiceInterface
  105. */
  106. protected $userInfoService;
  107. public function wxminiOnline()
  108. {
  109. $config = config('wxpay');
  110. $app = Factory::payment($config);
  111. $app['guzzle_handler'] = CoroutineHandler::class;
  112. $get = $this->request->getQueryParams();
  113. $post = $this->request->getParsedBody();
  114. $cookie = $this->request->getCookieParams();
  115. $files = $this->request->getUploadedFiles();
  116. $server = $this->request->getServerParams();
  117. $xml = $this->request->getBody()->getContents();
  118. $app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
  119. // 通知回调,进行业务处理
  120. $response = $app->handlePaidNotify(function ($message, $fail) use ($app) {
  121. Db::beginTransaction();
  122. try {
  123. // 支付失败或者通知失败
  124. if (
  125. empty($message)
  126. || $message['return_code'] != 'SUCCESS'
  127. || !isset($message['result_code'])
  128. || $message['result_code'] != 'SUCCESS'
  129. ) {
  130. $this->log->event(
  131. LogLabel::ORDER_ONLINE_PAY_NOTIFY_LOG,
  132. $message
  133. );
  134. Db::rollBack();
  135. return $fail('Unknown error but FAIL');
  136. }
  137. // 查询订单
  138. $orderMain = OrderMain::query()
  139. ->where(['global_order_id' => $message['out_trade_no'],'type' => OrderType::ONLINE,'state' => OrderState::UNPAID])
  140. ->first();
  141. // 订单不存在
  142. if (empty($orderMain)) {
  143. $this->log->event(
  144. LogLabel::ORDER_ONLINE_PAY_NOTIFY_LOG,
  145. ['global_order_id_fail' => $message['out_trade_no']]
  146. );
  147. Db::rollBack();
  148. return true;
  149. }
  150. $this->orderOnlineService->doByPaid($orderMain->global_order_id);
  151. $this->separateAccountsService->orderOnlinePaid($orderMain->global_order_id);
  152. Db::commit();
  153. //记录当前市场的当天外卖订单数
  154. $this->orderStatisticsService->setForMarket($orderMain->market_id);
  155. // 优惠券返券
  156. $this->couponRebateService->couponRebateInTask($orderMain->global_order_id);
  157. // 喇叭通知,兼容旧音响,MQTT+IOT
  158. $res = $this->mqttService->speakToStore($orderMain->global_order_id);
  159. $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->global_order_id);
  160. // 打印订单,自动打印
  161. $res = $this->feiePrintService->feiePrint($orderMain->global_order_id);
  162. // 记录badge
  163. $orderChildIds = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->pluck('store_id');
  164. $this->badgeService->doByOrder($orderMain->user_id, $orderChildIds, $orderMain->global_order_id, OrderState::PAID);
  165. // 公众号模板消息
  166. $res = $this->miniprogramService->sendTemMsgForOnlineOrder($orderMain->global_order_id);
  167. return true;
  168. } catch (Exception $e) {
  169. $this->log->event(
  170. LogLabel::ORDER_ONLINE_PAY_NOTIFY_LOG,
  171. ['exception_fail' => $e->getMessage()]
  172. );
  173. Db::rollBack();
  174. return $fail('Exception');
  175. }
  176. });
  177. return $this->response
  178. ->withHeader('Content-Type', 'text/xml')
  179. ->withStatus(200)
  180. ->withBody(new SwooleStream($response->getContent()));
  181. }
  182. public function wxminiOffline()
  183. {
  184. $config = config('wxpay');
  185. $app = Factory::payment($config);
  186. $app['guzzle_handler'] = CoroutineHandler::class;
  187. $get = $this->request->getQueryParams();
  188. $post = $this->request->getParsedBody();
  189. $cookie = $this->request->getCookieParams();
  190. $files = $this->request->getUploadedFiles();
  191. $server = $this->request->getServerParams();
  192. $xml = $this->request->getBody()->getContents();
  193. $app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
  194. // 通知回调,进行业务处理
  195. $response = $app->handlePaidNotify(function ($message, $fail) use ($app) {
  196. Db::beginTransaction();
  197. try {
  198. // 支付失败或者通知失败
  199. if (
  200. empty($message)
  201. || $message['return_code'] != 'SUCCESS'
  202. || !isset($message['result_code'])
  203. || $message['result_code'] != 'SUCCESS'
  204. ) {
  205. $this->log->event(
  206. LogLabel::ORDER_OFFLINE_PAY_NOTIFY_LOG,
  207. $message
  208. );
  209. Db::rollBack();
  210. return $fail('Unknown error but FAIL');
  211. }
  212. // 查询订单
  213. $orderMain = OrderMain::query()
  214. ->where([
  215. 'global_order_id' => $message['out_trade_no'],
  216. 'type' => OrderType::OFFLINE,
  217. 'state' => OrderState::UNPAID
  218. ])
  219. ->first();
  220. // 订单不存在
  221. if (empty($orderMain)) {
  222. $this->log->event(
  223. LogLabel::ORDER_OFFLINE_PAY_NOTIFY_LOG,
  224. ['global_order_id_fail' => $message['out_trade_no']]
  225. );
  226. Db::rollBack();
  227. return true;
  228. }
  229. $orderPaid = $this->orderOfflineService->doByPaid($orderMain->global_order_id);
  230. $separate = $this->separateAccountsService->orderOfflinePaid($orderMain->global_order_id);
  231. Db::commit();
  232. // 喇叭通知,兼容旧音响,MQTT+IOT
  233. $res = $this->mqttService->speakToStore($orderMain->global_order_id);
  234. $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->global_order_id);
  235. // 公众号模板消息
  236. $res = $this->miniprogramService->sendTemMsgForOfflineOrder($orderMain->global_order_id);
  237. $openid = User::query()->where(['id' => $orderMain->user_id])->value('openid');
  238. $res = $this->userInfoService->getPaidUnionId($openid);
  239. $this->log->event('test_get_paid_unionid', ['res' => $res]);
  240. return true;
  241. } catch (Exception $e) {
  242. $this->log->event(
  243. LogLabel::ORDER_OFFLINE_PAY_NOTIFY_LOG,
  244. ['exception_fail' => $e->getMessage()]
  245. );
  246. Db::rollBack();
  247. return $fail('Exception');
  248. }
  249. });
  250. return $this->response
  251. ->withHeader('Content-Type', 'text/xml')
  252. ->withStatus(200)
  253. ->withBody(new SwooleStream($response->getContent()));
  254. }
  255. public function wxminiRefund()
  256. {
  257. $config = config('wxpay');
  258. $app = Factory::payment($config);
  259. $app['guzzle_handler'] = CoroutineHandler::class;
  260. $get = $this->request->getQueryParams();
  261. $post = $this->request->getParsedBody();
  262. $cookie = $this->request->getCookieParams();
  263. $files = $this->request->getUploadedFiles();
  264. $server = $this->request->getServerParams();
  265. $xml = $this->request->getBody()->getContents();
  266. $app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
  267. // 通知回调,进行业务处理
  268. $response = $app->handleRefundedNotify(function ($message, $reqInfo, $fail) use ($app) {
  269. Db::beginTransaction();
  270. try {
  271. // 支付失败或者通知失败
  272. if (
  273. empty($message)
  274. || $message['return_code'] != 'SUCCESS'
  275. || !isset($reqInfo['refund_status'])
  276. || $reqInfo['refund_status'] != 'SUCCESS'
  277. ) {
  278. $this->log->event(
  279. LogLabel::ORDER_REFUND_NOTIFY_LOG,
  280. array_merge($message, $reqInfo)
  281. );
  282. Db::rollBack();
  283. return $fail('Unknown error but FAIL');
  284. }
  285. // 查询订单
  286. $orderMain = OrderMain::query()
  287. ->whereNotIn('state', [OrderState::UNPAID])
  288. ->where(['global_order_id' => $reqInfo['out_trade_no'], 'pay_type' => Payment::WECHAT])
  289. ->first();
  290. // 订单不存在
  291. if (empty($orderMain)) {
  292. $this->log->event(
  293. LogLabel::ORDER_REFUND_NOTIFY_LOG,
  294. ['global_order_id_fail' => $reqInfo['out_trade_no']]
  295. );
  296. Db::rollBack();
  297. return true;
  298. }
  299. // 添加退款时间
  300. $orderMain->refund_time = time();
  301. $orderMain->state = OrderState::REFUNDED;
  302. $orderMain->save();
  303. // 退款返还优惠券
  304. $this->couponService->orderRefundCoupons($orderMain->global_order_id);
  305. // 处理特价商品缓存
  306. $orderChildren = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->get()->toArray();
  307. $orderGoods = OrderGoods::query()
  308. // ->where(['activity_type' => 2])
  309. ->whereIn('order_id', array_values(array_column($orderChildren, 'id')))->get()->toArray();
  310. foreach ($orderGoods as $key => &$item) {
  311. if ($item['activity_type'] == 2) { # 活动商品
  312. $this->goodsActivityService->clearCacheRecord($item['goods_id'], $item['number'], $orderMain->user_id);
  313. $goods = GoodsActivity::find($item['goods_id']);
  314. } else {
  315. $goods = Goods::find($item['goods_id']);
  316. }
  317. $goods->inventory = $goods->inventory + $item['number'];
  318. $goods->sales = $goods->sales - $item['number'];
  319. $goods->save();
  320. }
  321. // 添加用户的流水
  322. $this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
  323. Db::commit();
  324. // 记录badge
  325. $orderChildIds = array_values(array_column($orderChildren, 'store_id'));
  326. $this->badgeService->doByOrder($orderMain->user_id, $orderChildIds, $orderMain->global_order_id, OrderState::REFUNDED);
  327. return true;
  328. } catch (Exception $e) {
  329. $this->log->event(
  330. LogLabel::ORDER_REFUND_NOTIFY_LOG,
  331. ['exception_fail' => $e->getMessage()]
  332. );
  333. Db::rollBack();
  334. return $fail('Exception');
  335. }
  336. });
  337. return $this->response
  338. ->withHeader('Content-Type', 'text/xml')
  339. ->withStatus(200)
  340. ->withBody(new SwooleStream($response->getContent()));
  341. }
  342. }