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

156 lines
5.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Extensions\Grid;
  3. use App\Common\OrderStatus;
  4. use App\Common\PayType;
  5. use App\Models\AdminSetting;
  6. use App\Models\Order;
  7. use App\Models\UserMoneyLog;
  8. use Dcat\Admin\Admin;
  9. use Dcat\Admin\Grid\RowAction;
  10. use EasyWeChat\Factory;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\DB;
  13. /**
  14. * 退款审核
  15. * Class AuditRefund
  16. * @package App\AdminAgent\Extensions\Grid
  17. */
  18. class AuditRefund extends RowAction
  19. {
  20. private $action;
  21. public function __construct($title = null, $action = 1)
  22. {
  23. parent::__construct($title);
  24. $this->action = $action; //$action:1=通过;2=拒绝
  25. $this->title = $action == 1 ? '通过' : '拒绝';
  26. }
  27. protected function html()
  28. {
  29. $class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger';
  30. $this->appendHtmlAttribute('class', $class);
  31. $this->defaultHtmlAttribute('href', 'javascript:;');
  32. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  33. }
  34. public function handle(Request $request)
  35. {
  36. return $request->action == 1 ? $this->pass() : $this->refuse();
  37. }
  38. //通过退款
  39. private function pass()
  40. {
  41. $agent = Admin::user();
  42. DB::beginTransaction();
  43. try {
  44. //修改订单状态
  45. $order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => $agent->id, 'status' => OrderStatus::REFUNDING]);
  46. if (!$order) {
  47. throw new \Exception("退款订单不存在或已处理过了");
  48. }
  49. /**
  50. * 【注意】线下付款退款暂不支持,若要开启,需要同时扣回供应商的余额,否则供应商可以白嫖
  51. * 需要修改:App\AdminAgent\Extensions\Grid\ChangeOrderStatus
  52. * 需要修改:app/Http/Controllers/Api/VerificationController.php
  53. */
  54. if ($order->pay_type == PayType::OFFLINE) {
  55. throw new \Exception("线下支付退款功能暂未开放");
  56. }
  57. //查看原来的支付信息,可能存在多条支付记录
  58. $log = UserMoneyLog::query()
  59. ->where(['user_id' => $order->user_id, 'order_id' => $order->id, 'type' => 1])
  60. ->where('transaction_id', '<>', '')
  61. ->get();
  62. if ($log->isEmpty()) {
  63. throw new \Exception('未查询到该笔订单的支付信息,退款失败');
  64. }
  65. $setting = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey']);
  66. if (!isset($setting['payee_appid'], $setting['payee_mchid'], $setting['payee_mchkey'])) {
  67. throw new \Exception('获取系统配置失败');
  68. }
  69. //向微信发起退款申请
  70. $config = config('wechat.payment.default');
  71. $config = array_merge($config, [
  72. 'app_id' => $setting['payee_appid'],
  73. 'mch_id' => $setting['payee_mchid'],
  74. 'key' => $setting['payee_mchkey'],
  75. 'notify_url' => route('wxpay_refund', $agent->id),
  76. 'cert_path' => env('WECHAT_CERT'),
  77. 'key_path' => env('WECHAT_KEY'),
  78. ]);
  79. $app = Factory::payment($config);
  80. // 参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数
  81. $refund_money = 0;
  82. foreach ($log as $k=>$v) {
  83. $refund_money = $refund_money + $v->money;
  84. $refund_no = $order->refund_info['refund_no'] . '-' . $v['id'];
  85. $money = intval($v->money * 100);
  86. $result = $app->refund->byTransactionId($v->transaction_id, $refund_no, $money, $money);
  87. //存入UserMoneyLog
  88. if (isset($result['return_code'], $result['result_code']) && $result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  89. UserMoneyLog::query()->insert([
  90. 'user_id' => $order->user_id,
  91. 'agent_id' => $order->agent_id,
  92. 'money' => $result['refund_fee'] / 100,
  93. 'order_id' => $order->id,
  94. 'type' => 2,
  95. 'desc' => DB::raw("LEFT('退款:{$order->title}', 250)"),
  96. 'transaction_id' => $result['transaction_id'],
  97. 'created_at' => now(), //模型没有updated_at,无法自动写入时间
  98. ]);
  99. } else {
  100. throw new \Exception("操作失败,失败原因:" . ($result['return_msg']??'未知'));
  101. }
  102. }
  103. //保存到订单表
  104. $order->verify_code = ''; //清空核销码
  105. $order->status = OrderStatus::REFUNDED;
  106. $order->paid_money = DB::raw('`paid_money` - ' . $refund_money);
  107. $order->save();
  108. DB::commit();
  109. return $this->response()->success("操作成功,款项将原路退还")->refresh();
  110. } catch (\Exception $e) {
  111. DB::rollBack();
  112. return $this->response()->error($e->getMessage())->refresh();
  113. }
  114. }
  115. //拒绝退款
  116. private function refuse()
  117. {
  118. try {
  119. $order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => Admin::user()->id, 'status' => OrderStatus::REFUNDING]);
  120. if (!$order) {
  121. return $this->response()->error("退款订单不存在或已处理过了")->refresh();
  122. }
  123. $order->status = OrderStatus::REFUSED_REFUND;
  124. $order->save();
  125. return $this->response()->success("操作成功,退款已拒绝")->refresh();
  126. } catch (\Exception $e) {
  127. return $this->response()->error($e->getMessage());
  128. }
  129. }
  130. public function confirm()
  131. {
  132. return ['确定要'.$this->title.'该用户的退款吗?', ''];
  133. }
  134. public function parameters()
  135. {
  136. return ['action' => $this->action];
  137. }
  138. }