PaymentController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Requests\Admin\PaymentSave;
  4. use App\Services\PaymentService;
  5. use App\Utils\Helper;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. use App\Models\Payment;
  9. use Illuminate\Support\Facades\DB;
  10. class PaymentController extends Controller
  11. {
  12. public function getPaymentMethods()
  13. {
  14. $methods = [];
  15. foreach (glob(base_path('app//Payments') . '/*.php') as $file) {
  16. array_push($methods, pathinfo($file)['filename']);
  17. }
  18. return response([
  19. 'data' => $methods
  20. ]);
  21. }
  22. public function fetch()
  23. {
  24. $payments = Payment::orderBy('sort', 'ASC')->get();
  25. foreach ($payments as $k => $v) {
  26. $notifyUrl = url("/api/v1/guest/payment/notify/{$v->payment}/{$v->uuid}");
  27. if ($v->notify_domain) {
  28. $parseUrl = parse_url($notifyUrl);
  29. $notifyUrl = $v->notify_domain . $parseUrl['path'];
  30. }
  31. $payments[$k]['notify_url'] = $notifyUrl;
  32. }
  33. return response([
  34. 'data' => $payments
  35. ]);
  36. }
  37. public function getPaymentForm(Request $request)
  38. {
  39. $paymentService = new PaymentService($request->input('payment'), $request->input('id'));
  40. return response([
  41. 'data' => $paymentService->form()
  42. ]);
  43. }
  44. public function show(Request $request)
  45. {
  46. $payment = Payment::find($request->input('id'));
  47. if (!$payment) abort(500, '支付方式不存在');
  48. $payment->enable = !$payment->enable;
  49. if (!$payment->save()) abort(500, '保存失败');
  50. return response([
  51. 'data' => true
  52. ]);
  53. }
  54. public function save(Request $request)
  55. {
  56. if (!config('v2board.app_url')) {
  57. abort(500, '请在站点配置中配置站点地址');
  58. }
  59. $params = $request->validate([
  60. 'name' => 'required',
  61. 'icon' => 'nullable',
  62. 'payment' => 'required',
  63. 'config' => 'required',
  64. 'notify_domain' => 'nullable|url',
  65. 'handling_fee_fixed' => 'nullable|integer',
  66. 'handling_fee_percent' => 'nullable|numeric|between:0.1,100'
  67. ], [
  68. 'name.required' => '显示名称不能为空',
  69. 'payment.required' => '网关参数不能为空',
  70. 'config.required' => '配置参数不能为空',
  71. 'notify_domain.url' => '自定义通知域名格式有误',
  72. 'handling_fee_fixed.integer' => '固定手续费格式有误',
  73. 'handling_fee_percent.between' => '百分比手续费范围须在0.1-100之间'
  74. ]);
  75. if ($request->input('id')) {
  76. $payment = Payment::find($request->input('id'));
  77. if (!$payment) abort(500, '支付方式不存在');
  78. try {
  79. $payment->update($params);
  80. } catch (\Exception $e) {
  81. abort(500, $e->getMessage());
  82. }
  83. return response([
  84. 'data' => true
  85. ]);
  86. }
  87. $params['uuid'] = Helper::randomChar(8);
  88. if (!Payment::create($params)) {
  89. abort(500, '保存失败');
  90. }
  91. return response([
  92. 'data' => true
  93. ]);
  94. }
  95. public function drop(Request $request)
  96. {
  97. $payment = Payment::find($request->input('id'));
  98. if (!$payment) abort(500, '支付方式不存在');
  99. return response([
  100. 'data' => $payment->delete()
  101. ]);
  102. }
  103. public function sort(Request $request)
  104. {
  105. $request->validate([
  106. 'ids' => 'required|array'
  107. ], [
  108. 'ids.required' => '参数有误',
  109. 'ids.array' => '参数有误'
  110. ]);
  111. DB::beginTransaction();
  112. foreach ($request->input('ids') as $k => $v) {
  113. if (!Payment::find($v)->update(['sort' => $k + 1])) {
  114. DB::rollBack();
  115. abort(500, '保存失败');
  116. }
  117. }
  118. DB::commit();
  119. return response([
  120. 'data' => true
  121. ]);
  122. }
  123. }