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.

67 lines
1.9 KiB

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