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

119 lines
3.4 KiB

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->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. $app->refund->byTransactionId($v->transaction_id, $refund_no, $v->money, $v->money, $config);
  72. }
  73. //退款回调之后再存入UserMoneyLog
  74. DB::commit();
  75. return $this->response()->success("操作成功,已向微信申请退款,款项将原路退还")->refresh();
  76. } catch (\Exception $e) {
  77. DB::rollBack();
  78. return $this->response()->error($e->getMessage());
  79. }
  80. }
  81. //拒绝退款
  82. private function refuse()
  83. {
  84. try {
  85. $order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => Admin::user()->id, 'status' => OrderStatus::REFUNDING]);
  86. if (!$order) {
  87. return $this->response()->error("退款订单不存在或已处理过了")->refresh();
  88. }
  89. $order->status = OrderStatus::REFUSED_REFUND;
  90. $order->save();
  91. return $this->response()->success("操作成功,退款已拒绝")->refresh();
  92. } catch (\Exception $e) {
  93. return $this->response()->error($e->getMessage());
  94. }
  95. }
  96. public function confirm()
  97. {
  98. return ['确定要'.$this->title.'该用户的退款吗?', ''];
  99. }
  100. public function parameters()
  101. {
  102. return ['action' => $this->action];
  103. }
  104. }