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.

106 lines
3.1 KiB

  1. <?php
  2. namespace App\Controller;
  3. use App\Constants\LogLabel;
  4. use App\Model\OrderMain;
  5. use App\Model\Users;
  6. use App\Service\CouponRebateServiceInterface;
  7. use App\Service\DeviceServiceInterface;
  8. use App\Service\FeiePrintServiceInterface;
  9. use App\Service\MiniprogramServiceInterface;
  10. use App\Service\MqttServiceInterface;
  11. use App\Service\OrderServiceInterface;
  12. use App\Service\SeparateAccountsServiceInterface;
  13. use App\Service\UserServiceInterface;
  14. use EasyWeChat\Factory;
  15. use Hyperf\DbConnection\Db;
  16. use Hyperf\Guzzle\CoroutineHandler;
  17. use Exception;
  18. use Hyperf\Di\Annotation\Inject;
  19. use Hyperf\HttpMessage\Stream\SwooleStream;
  20. use Symfony\Component\HttpFoundation\Request;
  21. class NotifyPayRefundController extends BaseController
  22. {
  23. const AWARD_LIMIT_AMOUNT = 3;
  24. /**
  25. * @Inject
  26. * @var UserServiceInterface
  27. */
  28. protected $userService;
  29. /**
  30. * @Inject
  31. * @var CouponRebateServiceInterface
  32. */
  33. protected $couponRebateService;
  34. /**
  35. * @Inject
  36. * @var OrderServiceInterface
  37. */
  38. protected $orderService;
  39. /**
  40. * 微信退款回调
  41. */
  42. public function wxPayRefund()
  43. {
  44. $config = config('wxpay');
  45. $app = Factory::payment($config);
  46. $app['guzzle_handler'] = CoroutineHandler::class;
  47. $get = $this->request->getQueryParams();
  48. $post = $this->request->getParsedBody();
  49. $cookie = $this->request->getCookieParams();
  50. $files = $this->request->getUploadedFiles();
  51. $server = $this->request->getServerParams();
  52. $xml = $this->request->getBody()->getContents();
  53. $app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
  54. /* 通知回调,进行业务处理 */
  55. $response = $app->handleRefundedNotify(function ($message, $fail) use ($app) {
  56. try {
  57. /* --- 退款失败 --- */
  58. if (
  59. empty($message)
  60. || !isset($message['result_code'])
  61. || $message['result_code'] != 'SUCCESS'
  62. ) {
  63. // 错误日志
  64. $this->log->event(
  65. LogLabel::PAY_NOTIFY_REFUND,
  66. $message
  67. );
  68. $fail('Unknown error but FAIL');
  69. }
  70. /* --- 退款成功 --- */
  71. // 退款返还优惠券
  72. $this->couponService->orderRefundCoupon($message['out_trade_no']);
  73. // 添加用户的流水
  74. $orderMain = OrderMain::select('id','global_order_id')->where('code',$message['out_trade_no'])->first();
  75. } catch (Exception $e) {
  76. $this->log->event(
  77. LogLabel::PAY_NOTIFY_REFUND,
  78. ['exception_fail' => $e->getMessage()]
  79. );
  80. $fail('Exception');
  81. }
  82. });
  83. return $this->response
  84. ->withHeader('Content-Type', 'text/xml')
  85. ->withStatus(200)
  86. ->withBody(new SwooleStream($response->getContent()));
  87. }
  88. }