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.

108 lines
3.3 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. use App\Service\PurchaseLimitServiceInterface;
  13. class NotifyPayRefundController extends BaseController
  14. {
  15. const AWARD_LIMIT_AMOUNT = 3;
  16. /**
  17. * @Inject
  18. * @var FinancialRecordServiceInterface
  19. */
  20. protected $financialService;
  21. /**
  22. * @Inject
  23. * @var OrderServiceInterface
  24. */
  25. protected $orderService;
  26. /**
  27. * @Inject
  28. * @var PurchaseLimitServiceInterface
  29. */
  30. protected $purchaseLimitService;
  31. /**
  32. * 微信退款回调
  33. */
  34. public function wxPayRefund()
  35. {
  36. $config = config('wxpay');
  37. $app = Factory::payment($config);
  38. $app['guzzle_handler'] = CoroutineHandler::class;
  39. $get = $this->request->getQueryParams();
  40. $post = $this->request->getParsedBody();
  41. $cookie = $this->request->getCookieParams();
  42. $files = $this->request->getUploadedFiles();
  43. $server = $this->request->getServerParams();
  44. $xml = $this->request->getBody()->getContents();
  45. $app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
  46. /* 通知回调,进行业务处理 */
  47. $response = $app->handleRefundedNotify(function ($message, $fail) use ($app) {
  48. try {
  49. /* --- 退款失败 --- */
  50. if (
  51. empty($message)
  52. || !isset($message['result_code'])
  53. || $message['result_code'] != 'SUCCESS'
  54. ) {
  55. // 错误日志
  56. $this->log->event(
  57. LogLabel::PAY_NOTIFY_REFUND,
  58. $message
  59. );
  60. $fail('Unknown error but FAIL');
  61. }
  62. /* --- 退款成功 --- */
  63. $orderMain = OrderMain::select('id','global_order_id','money','user_id')
  64. ->where('global_order_id',$message['out_trade_no'])
  65. ->where('state',OrderMain::ORDER_STATE_REFUNDED)
  66. ->first();
  67. // 退款返还优惠券
  68. $this->couponService->orderRefundCoupon($message['out_trade_no']);
  69. // 删除特价商品缓存
  70. $this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->id);
  71. // 添加用户的流水
  72. if(!empty($orderMain)){
  73. $this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
  74. }
  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. }