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.

122 lines
3.8 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. use Hyperf\DbConnection\Db;
  14. class NotifyPayRefundController extends BaseController
  15. {
  16. const AWARD_LIMIT_AMOUNT = 3;
  17. /**
  18. * @Inject
  19. * @var FinancialRecordServiceInterface
  20. */
  21. protected $financialService;
  22. /**
  23. * @Inject
  24. * @var OrderServiceInterface
  25. */
  26. protected $orderService;
  27. /**
  28. * @Inject
  29. * @var PurchaseLimitServiceInterface
  30. */
  31. protected $purchaseLimitService;
  32. /**
  33. * 微信退款回调
  34. */
  35. public function wxPayRefund()
  36. {
  37. $this->log->event(
  38. LogLabel::WX_NOTIFY_REFUND,
  39. '进入回调'
  40. );
  41. $config = config('wxpay');
  42. $app = Factory::payment($config);
  43. $app['guzzle_handler'] = CoroutineHandler::class;
  44. $get = $this->request->getQueryParams();
  45. $post = $this->request->getParsedBody();
  46. $cookie = $this->request->getCookieParams();
  47. $files = $this->request->getUploadedFiles();
  48. $server = $this->request->getServerParams();
  49. $xml = $this->request->getBody()->getContents();
  50. $app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
  51. /* 通知回调,进行业务处理 */
  52. $response = $app->handleRefundedNotify(function ($message, $fail) use ($app) {
  53. $this->log->event(
  54. LogLabel::WX_NOTIFY_REFUND,
  55. $message
  56. );
  57. try {
  58. /* --- 退款失败 --- */
  59. if (
  60. empty($message)
  61. || !isset($message['result_code'])
  62. || $message['result_code'] != 'SUCCESS'
  63. ) {
  64. // 错误日志
  65. $this->log->event(
  66. LogLabel::WX_NOTIFY_REFUND,
  67. $message
  68. );
  69. $fail('Unknown error but FAIL');
  70. return false;
  71. }
  72. /* --- 退款成功 --- */
  73. $orderMain = OrderMain::select('id','global_order_id','money','user_id')
  74. ->where('global_order_id',$message['out_trade_no'])
  75. ->where('state',OrderMain::ORDER_STATE_REFUNDED)
  76. ->where(Db::raw('refund_time is null'))
  77. ->first();
  78. if(!empty($orderMain)){
  79. // 添加退款时间
  80. $orderMain->refund_time = time();
  81. $orderMain->save();
  82. // 退款返还优惠券
  83. $this->couponService->orderRefundCoupons($orderMain->global_order_id);
  84. // 删除特价商品缓存
  85. $this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->id);
  86. // 添加用户的流水
  87. $this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
  88. }
  89. } catch (\Exception $e) {
  90. $this->log->event(
  91. LogLabel::WX_NOTIFY_REFUND,
  92. ['exception_fail' => $e->getMessage()]
  93. );
  94. $fail('Exception');
  95. }
  96. });
  97. return $this->response
  98. ->withHeader('Content-Type', 'text/xml')
  99. ->withStatus(200)
  100. ->withBody(new SwooleStream($response->getContent()));
  101. }
  102. }