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.

121 lines
3.8 KiB

  1. <?php
  2. /**
  3. * 本回调用不到 20200826
  4. */
  5. namespace App\Controller;
  6. use App\Constants\LogLabel;
  7. use App\Model\OrderMain;
  8. use App\Service\OrderServiceInterface;
  9. use App\Service\FinancialRecordServiceInterface;
  10. use EasyWeChat\Factory;
  11. use Hyperf\Guzzle\CoroutineHandler;
  12. use Hyperf\Di\Annotation\Inject;
  13. use Hyperf\HttpMessage\Stream\SwooleStream;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use App\Service\PurchaseLimitServiceInterface;
  16. use Hyperf\DbConnection\Db;
  17. class NotifyPayRefundController extends BaseController
  18. {
  19. const AWARD_LIMIT_AMOUNT = 3;
  20. /**
  21. * @Inject
  22. * @var FinancialRecordServiceInterface
  23. */
  24. protected $financialService;
  25. /**
  26. * @Inject
  27. * @var OrderServiceInterface
  28. */
  29. protected $orderService;
  30. /**
  31. * @Inject
  32. * @var PurchaseLimitServiceInterface
  33. */
  34. protected $purchaseLimitService;
  35. /**
  36. * 微信退款回调
  37. */
  38. public function wxPayRefund()
  39. {
  40. $this->log->event(
  41. LogLabel::WX_NOTIFY_REFUND,
  42. '进入回调'
  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. $this->log->event(
  57. LogLabel::WX_NOTIFY_REFUND,
  58. $message
  59. );
  60. try {
  61. /* --- 退款失败 --- */
  62. if (
  63. empty($message)
  64. || !isset($message['result_code'])
  65. || $message['result_code'] != 'SUCCESS'
  66. ) {
  67. // 错误日志
  68. $this->log->event(
  69. LogLabel::WX_NOTIFY_REFUND,
  70. $message
  71. );
  72. $fail('Unknown error but FAIL');
  73. return false;
  74. }
  75. /* --- 退款成功 --- */
  76. $orderMain = OrderMain::select('id','global_order_id','money','user_id')
  77. ->where('global_order_id',$message['out_trade_no'])
  78. ->where('state',OrderMain::ORDER_STATE_REFUNDED)
  79. ->where(Db::raw('refund_time is null'))
  80. ->first();
  81. if(!empty($orderMain)){
  82. // 添加退款时间
  83. $orderMain->refund_time = time();
  84. $orderMain->save();
  85. // 退款返还优惠券
  86. $this->couponService->orderRefundCoupons($orderMain->global_order_id);
  87. // 删除特价商品缓存
  88. $this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->id);
  89. // 添加用户的流水
  90. $this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
  91. }
  92. } catch (\Exception $e) {
  93. $this->log->event(
  94. LogLabel::WX_NOTIFY_REFUND,
  95. ['exception_fail' => $e->getMessage()]
  96. );
  97. $fail('Exception');
  98. }
  99. });
  100. return $this->response
  101. ->withHeader('Content-Type', 'text/xml')
  102. ->withStatus(200)
  103. ->withBody(new SwooleStream($response->getContent()));
  104. }
  105. }