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.

257 lines
8.1 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
  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\Controller\BaseController;
  7. use App\Model\v3\OrderMain;
  8. use App\Service\v3\Interfaces\CouponRebateServiceInterface;
  9. use App\Service\v3\Interfaces\DeviceServiceInterface;
  10. use App\Service\v3\Interfaces\FeiePrintServiceInterface;
  11. use App\Service\v3\Interfaces\MiniprogramServiceInterface;
  12. use App\Service\v3\Interfaces\MqttServiceInterface;
  13. use App\Service\v3\Interfaces\OrderOfflineServiceInterface;
  14. use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
  15. use App\Service\v3\Interfaces\SeparateAccountsServiceInterface;
  16. use App\Service\v3\Interfaces\UserServiceInterface;
  17. use EasyWeChat\Factory;
  18. use Hyperf\DbConnection\Db;
  19. use Hyperf\Guzzle\CoroutineHandler;
  20. use Exception;
  21. use Hyperf\Di\Annotation\Inject;
  22. use Hyperf\HttpMessage\Stream\SwooleStream;
  23. use Symfony\Component\HttpFoundation\Request;
  24. class NotifyController extends BaseController
  25. {
  26. /**
  27. * @Inject
  28. * @var MqttServiceInterface
  29. */
  30. protected $mqttService;
  31. /**
  32. * @Inject
  33. * @var DeviceServiceInterface
  34. */
  35. protected $deviceService;
  36. /**
  37. * @Inject
  38. * @var MiniprogramServiceInterface
  39. */
  40. protected $miniprogramService;
  41. /**
  42. * @Inject
  43. * @var FeiePrintServiceInterface
  44. */
  45. protected $feiePrintService;
  46. /**
  47. * @Inject
  48. * @var UserServiceInterface
  49. */
  50. protected $userService;
  51. /**
  52. * @Inject
  53. * @var CouponRebateServiceInterface
  54. */
  55. protected $couponRebateService;
  56. /**
  57. * @Inject
  58. * @var OrderOnlineServiceInterface
  59. */
  60. protected $orderOnlineService;
  61. /**
  62. * @Inject
  63. * @var OrderOfflineServiceInterface
  64. */
  65. protected $orderOfflineService;
  66. /**
  67. * @Inject
  68. * @var SeparateAccountsServiceInterface
  69. */
  70. protected $separateAccountsService;
  71. public function wxminiOnline()
  72. {
  73. $config = config('wxpay');
  74. $app = Factory::payment($config);
  75. $app['guzzle_handler'] = CoroutineHandler::class;
  76. $get = $this->request->getQueryParams();
  77. $post = $this->request->getParsedBody();
  78. $cookie = $this->request->getCookieParams();
  79. $files = $this->request->getUploadedFiles();
  80. $server = $this->request->getServerParams();
  81. $xml = $this->request->getBody()->getContents();
  82. $app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
  83. // 通知回调,进行业务处理
  84. $response = $app->handlePaidNotify(function ($message, $fail) use ($app) {
  85. Db::beginTransaction();
  86. try {
  87. // 支付失败或者通知失败
  88. if (
  89. empty($message)
  90. || $message['return_code'] != 'SUCCESS'
  91. || !isset($message['result_code'])
  92. || $message['result_code'] != 'SUCCESS'
  93. ) {
  94. $this->log->event(
  95. LogLabel::ORDER_OFFLINE_PAY_NOTIFY_LOG,
  96. $message
  97. );
  98. Db::rollBack();
  99. return $fail('Unknown error but FAIL');
  100. }
  101. // 查询订单
  102. $orderMain = OrderMain::query()
  103. ->where(['global_order_id' => $message['out_trade_no'],'type' => OrderType::ONLINE,'state' => OrderState::UNPAID])
  104. ->first();
  105. // 订单不存在
  106. if (empty($orderMain)) {
  107. $this->log->event(
  108. LogLabel::ORDER_OFFLINE_PAY_NOTIFY_LOG,
  109. ['global_order_id_fail' => $message['out_trade_no']]
  110. );
  111. Db::rollBack();
  112. return true;
  113. }
  114. $this->orderOnlineService->doByPaid($orderMain->id);
  115. $this->separateAccountsService->orderOnlinePaid($orderMain->id);
  116. // 优惠券返券
  117. $this->couponRebateService->couponRebateInTask($orderMain->id);
  118. // 喇叭通知,兼容旧音响,MQTT+IOT
  119. $res = $this->mqttService->speakToStore($orderMain->id);
  120. $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->id);
  121. // 公众号模板消息
  122. $res = $this->miniprogramService->sendTemMsgForOnlineOrder($orderMain->id);
  123. // 打印订单,自动打印
  124. $res = $this->feiePrintService->feiePrint($orderMain->id);
  125. Db::commit();
  126. return true;
  127. } catch (Exception $e) {
  128. $this->log->event(
  129. LogLabel::ORDER_ONLINE_PAY_NOTIFY_LOG,
  130. ['exception_fail' => $e->getMessage()]
  131. );
  132. Db::rollBack();
  133. return $fail('Exception');
  134. }
  135. });
  136. return $this->response
  137. ->withHeader('Content-Type', 'text/xml')
  138. ->withStatus(200)
  139. ->withBody(new SwooleStream($response->getContent()));
  140. }
  141. public function wxminiOffline()
  142. {
  143. $config = config('wxpay');
  144. $app = Factory::payment($config);
  145. $app['guzzle_handler'] = CoroutineHandler::class;
  146. $get = $this->request->getQueryParams();
  147. $post = $this->request->getParsedBody();
  148. $cookie = $this->request->getCookieParams();
  149. $files = $this->request->getUploadedFiles();
  150. $server = $this->request->getServerParams();
  151. $xml = $this->request->getBody()->getContents();
  152. $app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
  153. // 通知回调,进行业务处理
  154. $response = $app->handlePaidNotify(function ($message, $fail) use ($app) {
  155. Db::beginTransaction();
  156. try {
  157. // 支付失败或者通知失败
  158. if (
  159. empty($message)
  160. || $message['return_code'] != 'SUCCESS'
  161. || !isset($message['result_code'])
  162. || $message['result_code'] != 'SUCCESS'
  163. ) {
  164. $this->log->event(
  165. LogLabel::ORDER_OFFLINE_PAY_NOTIFY_LOG,
  166. $message
  167. );
  168. Db::rollBack();
  169. return $fail('Unknown error but FAIL');
  170. }
  171. // 查询订单
  172. $orderMain = OrderMain::query()
  173. ->where([
  174. 'global_order_id' => $message['out_trade_no'],
  175. 'type' => OrderType::OFFLINE,
  176. 'state' => OrderState::UNPAID
  177. ])
  178. ->first();
  179. // 订单不存在
  180. if (empty($orderMain)) {
  181. $this->log->event(
  182. LogLabel::ORDER_OFFLINE_PAY_NOTIFY_LOG,
  183. ['global_order_id_fail' => $message['out_trade_no']]
  184. );
  185. Db::rollBack();
  186. return true;
  187. }
  188. $orderPaid = $this->orderOfflineService->doPaid($orderMain->id);
  189. $separate = $this->separateAccountsService->orderOfflinePaid($orderMain->id);
  190. // 喇叭通知,兼容旧音响,MQTT+IOT
  191. $res = $this->mqttService->speakToStore($orderMain->id);
  192. $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->id);
  193. // 公众号模板消息
  194. $res = $this->miniprogramService->sendTemMsgForOfflineOrder($orderMain->id);
  195. Db::commit();
  196. return true;
  197. } catch (Exception $e) {
  198. $this->log->event(
  199. LogLabel::ORDER_OFFLINE_PAY_NOTIFY_LOG,
  200. ['exception_fail' => $e->getMessage()]
  201. );
  202. Db::rollBack();
  203. return $fail('Exception');
  204. }
  205. });
  206. return $this->response
  207. ->withHeader('Content-Type', 'text/xml')
  208. ->withStatus(200)
  209. ->withBody(new SwooleStream($response->getContent()));
  210. }
  211. }