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

141 lines
4.4 KiB

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