PaymentController.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. class PaymentController extends Controller
  10. {
  11. public function getPaymentMethods()
  12. {
  13. $methods = [];
  14. foreach (glob(base_path('app//Payments') . '/*.php') as $file) {
  15. array_push($methods, pathinfo($file)['filename']);
  16. }
  17. return response([
  18. 'data' => $methods
  19. ]);
  20. }
  21. public function fetch()
  22. {
  23. $payments = Payment::all();
  24. foreach ($payments as $k => $v) {
  25. $payments[$k]['notify_url'] = url("/api/v1/guest/payment/notify/{$v->payment}/{$v->uuid}");
  26. }
  27. return response([
  28. 'data' => $payments
  29. ]);
  30. }
  31. public function getPaymentForm(Request $request)
  32. {
  33. $paymentService = new PaymentService($request->input('payment'), $request->input('id'));
  34. return response([
  35. 'data' => $paymentService->form()
  36. ]);
  37. }
  38. public function save(Request $request)
  39. {
  40. if (!config('v2board.app_url')) {
  41. abort(500, '请在站点配置中配置站点地址');
  42. }
  43. if ($request->input('id')) {
  44. $payment = Payment::find($request->input('id'));
  45. if (!$payment) abort(500, '支付方式不存在');
  46. try {
  47. $payment->update($request->input());
  48. } catch (\Exception $e) {
  49. abort(500, '更新失败');
  50. }
  51. return response([
  52. 'data' => true
  53. ]);
  54. }
  55. $request->validate([
  56. 'name' => 'required',
  57. 'payment' => 'required',
  58. 'config' => 'required'
  59. ], [
  60. 'name.required' => '显示名称不能为空',
  61. 'payment.required' => '网关参数不能为空',
  62. 'config.required' => '配置参数不能为空'
  63. ]);
  64. if (!Payment::create([
  65. 'name' => $request->input('name'),
  66. 'payment' => $request->input('payment'),
  67. 'config' => $request->input('config'),
  68. 'uuid' => Helper::randomChar(8)
  69. ])) {
  70. abort(500, '保存失败');
  71. }
  72. return response([
  73. 'data' => true
  74. ]);
  75. }
  76. public function drop(Request $request)
  77. {
  78. $payment = Payment::find($request->input('id'));
  79. if (!$payment) abort(500, '支付方式不存在');
  80. return response([
  81. 'data' => $payment->delete()
  82. ]);
  83. }
  84. }