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

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