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.

101 lines
3.1 KiB

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