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.

53 lines
1.4 KiB

  1. <?php
  2. namespace App\Service;
  3. use App\Model\OrderMain;
  4. use EasyWeChat\Factory;
  5. use Hyperf\DbConnection\Db;
  6. use App\Constants\ErrorCode;
  7. use App\Commons\Log;
  8. class WxRefundService implements WxRefundServiceInterface
  9. {
  10. /**
  11. * @Inject
  12. * @var Log
  13. */
  14. protected $log;
  15. /**
  16. * 微信支付退款
  17. */
  18. public function wxPayRefund($global_order_id)
  19. {
  20. $config = config('wxpay');
  21. $app = Factory::payment($config);
  22. $app['guzzle_handler'] = CoroutineHandler::class;
  23. // 查询订单
  24. $orderMain = OrderMain::query()
  25. ->select('id','global_order_id','order_num','money','state')
  26. ->where('global_order_id',$global_order_id)
  27. ->where('pay_type',OrderMain::ORDER_PAY_WX)
  28. ->where(Db::raw('refund_time is null'))
  29. ->first();
  30. if(empty($orderMain)){
  31. return false;
  32. };
  33. $options = [
  34. 'refund_desc' => '',
  35. 'notify_url' => config('site_host') . '/wechat/notify/wxpayrefund'
  36. ];
  37. $result = $app->refund->byOutTradeNumber(
  38. $orderMain->global_order_id,
  39. $orderMain->global_order_id,
  40. $orderMain->money * 100,
  41. $orderMain->money * 100,
  42. $options
  43. );
  44. $this->log->event('wx_pay_refund',$result);
  45. return $result;
  46. }
  47. }