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

133 lines
4.0 KiB

5 years ago
5 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->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' => 'wxb35ef055a4dd8ad4',
  61. 'mch_id' => '1606181693',
  62. 'key' => 'lfyyhyz8888888888888888888888888',
  63. 'notify_url' => route('wxpay_refund', $agent->id),
  64. 'cert_path' => env('WECHAT_CERT'),
  65. 'key_path' => env('WECHAT_KEY'),
  66. ]);
  67. $app = Factory::payment($config);
  68. // 参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数
  69. foreach ($log as $k=>$v) {
  70. $refund_no = $order->refund_info['refund_no'] . '-' . $v['id'];
  71. $money = intval(0 - $v->money * 100);
  72. $result = $app->refund->byTransactionId($v->transaction_id, $refund_no, $money, $money);
  73. //存入UserMoneyLog
  74. if (isset($result['return_code'], $result['result_code']) && $result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  75. UserMoneyLog::query()->insert([
  76. 'user_id' => $order->user_id,
  77. 'agent_id' => $order->agent_id,
  78. 'money' => $result['refund_fee'] / 100,
  79. 'order_id' => $order->id,
  80. 'type' => 2,
  81. 'desc' => DB::raw("LEFT('退款:{$order->title}', 250)"),
  82. 'transaction_id' => $result['transaction_id'],
  83. 'created_at' => now(), //模型没有updated_at,无法自动写入时间
  84. ]);
  85. }
  86. }
  87. DB::commit();
  88. return $this->response()->success("操作成功,已向微信申请退款,款项将原路退还")->refresh();
  89. } catch (\Exception $e) {
  90. DB::rollBack();
  91. return $this->response()->error($e->getMessage());
  92. }
  93. }
  94. //拒绝退款
  95. private function refuse()
  96. {
  97. try {
  98. $order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => Admin::user()->id, 'status' => OrderStatus::REFUNDING]);
  99. if (!$order) {
  100. return $this->response()->error("退款订单不存在或已处理过了")->refresh();
  101. }
  102. $order->status = OrderStatus::REFUSED_REFUND;
  103. $order->save();
  104. return $this->response()->success("操作成功,退款已拒绝")->refresh();
  105. } catch (\Exception $e) {
  106. return $this->response()->error($e->getMessage());
  107. }
  108. }
  109. public function confirm()
  110. {
  111. return ['确定要'.$this->title.'该用户的退款吗?', ''];
  112. }
  113. public function parameters()
  114. {
  115. return ['action' => $this->action];
  116. }
  117. }