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.

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