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

146 lines
4.5 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. throw new \Exception("退款订单不存在或已处理过了");
  47. }
  48. //查看原来的支付信息,可能存在多条支付记录
  49. $log = UserMoneyLog::query()
  50. ->where(['user_id' => $order->user_id, 'order_id' => $order->id, 'type' => 1])
  51. ->where('transaction_id', '<>', '')
  52. ->get();
  53. if ($log->isEmpty()) {
  54. throw new \Exception('未查询到该笔订单的支付信息,退款失败');
  55. }
  56. $setting = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey']);
  57. if (!isset($setting['payee_appid'], $setting['payee_mchid'], $setting['payee_mchkey'])) {
  58. throw new \Exception('获取系统配置失败');
  59. }
  60. //向微信发起退款申请
  61. $config = config('wechat.payment.default');
  62. $config = array_merge($config, [
  63. 'app_id' => $setting['payee_appid'],
  64. 'mch_id' => $setting['payee_mchid'],
  65. 'key' => $setting['payee_mchkey'],
  66. 'notify_url' => route('wxpay_refund', $agent->id),
  67. 'cert_path' => env('WECHAT_CERT'),
  68. 'key_path' => env('WECHAT_KEY'),
  69. ]);
  70. $app = Factory::payment($config);
  71. // 参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数
  72. $refund_money = 0;
  73. foreach ($log as $k=>$v) {
  74. $refund_money = $refund_money + $v->money;
  75. $refund_no = $order->refund_info['refund_no'] . '-' . $v['id'];
  76. $money = intval($v->money * 100);
  77. $result = $app->refund->byTransactionId($v->transaction_id, $refund_no, $money, $money);
  78. //存入UserMoneyLog
  79. if (isset($result['return_code'], $result['result_code']) && $result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  80. UserMoneyLog::query()->insert([
  81. 'user_id' => $order->user_id,
  82. 'agent_id' => $order->agent_id,
  83. 'money' => $result['refund_fee'] / 100,
  84. 'order_id' => $order->id,
  85. 'type' => 2,
  86. 'desc' => DB::raw("LEFT('退款:{$order->title}', 250)"),
  87. 'transaction_id' => $result['transaction_id'],
  88. 'created_at' => now(), //模型没有updated_at,无法自动写入时间
  89. ]);
  90. } else {
  91. throw new \Exception("操作失败,失败原因:" . ($result['return_msg']??'未知'));
  92. }
  93. }
  94. //保存到订单表
  95. $order->verify_code = ''; //清空核销码
  96. $order->status = OrderStatus::REFUNDED;
  97. $order->paid_money = DB::raw('`paid_money` - ' . $refund_money);
  98. $order->save();
  99. DB::commit();
  100. return $this->response()->success("操作成功,款项将原路退还")->refresh();
  101. } catch (\Exception $e) {
  102. DB::rollBack();
  103. return $this->response()->error($e->getMessage())->refresh();
  104. }
  105. }
  106. //拒绝退款
  107. private function refuse()
  108. {
  109. try {
  110. $order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => Admin::user()->id, 'status' => OrderStatus::REFUNDING]);
  111. if (!$order) {
  112. return $this->response()->error("退款订单不存在或已处理过了")->refresh();
  113. }
  114. $order->status = OrderStatus::REFUSED_REFUND;
  115. $order->save();
  116. return $this->response()->success("操作成功,退款已拒绝")->refresh();
  117. } catch (\Exception $e) {
  118. return $this->response()->error($e->getMessage());
  119. }
  120. }
  121. public function confirm()
  122. {
  123. return ['确定要'.$this->title.'该用户的退款吗?', ''];
  124. }
  125. public function parameters()
  126. {
  127. return ['action' => $this->action];
  128. }
  129. }