海南旅游SAAS
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.

117 lines
3.3 KiB

  1. <?php
  2. namespace App\AdminAgent\Extensions\Grid;
  3. use App\Common\OrderStatus;
  4. use App\Models\Order;
  5. use App\Models\UserMoneyLog;
  6. use Dcat\Admin\Admin;
  7. use Dcat\Admin\Grid\RowAction;
  8. use EasyWeChat\Factory;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * 退款审核
  13. * Class AuditRefund
  14. * @package App\AdminAgent\Extensions\Grid
  15. */
  16. class AuditRefund extends RowAction
  17. {
  18. private $action;
  19. public function __construct($title = null, $action = 1)
  20. {
  21. parent::__construct($title);
  22. $this->action = $action; //$action:1=通过;2=拒绝
  23. $this->title = $action == 1 ? '通过' : '拒绝';
  24. }
  25. protected function html()
  26. {
  27. $class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger';
  28. $this->appendHtmlAttribute('class', $class);
  29. $this->defaultHtmlAttribute('href', 'javascript:;');
  30. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  31. }
  32. public function handle(Request $request)
  33. {
  34. return $request->action == 1 ? $this->pass() : $this->refuse();
  35. }
  36. //通过退款
  37. private function pass()
  38. {
  39. $agent = Admin::user();
  40. DB::beginTransaction();
  41. try {
  42. //修改订单状态
  43. $order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => $agent->id, 'status' => OrderStatus::REFUNDING]);
  44. if (!$order) {
  45. return $this->response()->error("退款订单不存在或已处理过了")->refresh();
  46. }
  47. $order->status = OrderStatus::REFUNDED;
  48. $order->save();
  49. //查看原来的支付信息,可能存在多条支付记录
  50. $log = UserMoneyLog::query()
  51. ->where(['user_id' => $order->user_id, 'order_id' => $order->id, 'type' => 1])
  52. ->where('transaction_id', '<>', '')
  53. ->get();
  54. if ($log->isEmpty()) {
  55. throw new \Exception('未查询到该笔订单的支付信息,退款失败');
  56. }
  57. //将微信发起退款申请
  58. $config = config('wechat.payment.default');
  59. $config = array_merge($config, [
  60. 'app_id' => $agent->appid,
  61. 'mch_id' => $agent->mchid,
  62. 'key' => $agent->mchkey,
  63. 'notify_url' => route('wxpay_refund', $agent->id),
  64. ]);
  65. $app = Factory::payment($config);
  66. // 参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数
  67. foreach ($log as $k=>$v) {
  68. $refund_no = $order->refund_info['refund_no'] . '-' . $v['id'];
  69. $app->refund->byTransactionId($v->transaction_id, $refund_no, $v->money, $v->money, $config);
  70. }
  71. //退款回调之后再存入UserMoneyLog
  72. DB::commit();
  73. return $this->response()->success("操作成功,已向微信申请退款,款项将原路退还")->refresh();
  74. } catch (\Exception $e) {
  75. DB::rollBack();
  76. return $this->response()->error($e->getMessage());
  77. }
  78. }
  79. //拒绝退款
  80. private function refuse()
  81. {
  82. try {
  83. $order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => Admin::user()->id, 'status' => OrderStatus::REFUNDING]);
  84. if (!$order) {
  85. return $this->response()->error("退款订单不存在或已处理过了")->refresh();
  86. }
  87. $order->status = OrderStatus::REFUSED_REFUND;
  88. $order->save();
  89. return $this->response()->success("操作成功,退款已拒绝")->refresh();
  90. } catch (\Exception $e) {
  91. return $this->response()->error($e->getMessage());
  92. }
  93. }
  94. public function confirm()
  95. {
  96. return ['确定要'.$this->title.'该用户的退款吗?', ''];
  97. }
  98. public function parameters()
  99. {
  100. return ['action' => $this->action];
  101. }
  102. }