PaymentController.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. $notifyUrl = url("/api/v1/guest/payment/notify/{$v->payment}/{$v->uuid}");
  26. if ($v->notify_domain) {
  27. $parseUrl = parse_url($notifyUrl);
  28. $notifyUrl = $v->notify_domain . $parseUrl['path'];
  29. }
  30. $payments[$k]['notify_url'] = $notifyUrl;
  31. }
  32. return response([
  33. 'data' => $payments
  34. ]);
  35. }
  36. public function getPaymentForm(Request $request)
  37. {
  38. $paymentService = new PaymentService($request->input('payment'), $request->input('id'));
  39. return response([
  40. 'data' => $paymentService->form()
  41. ]);
  42. }
  43. public function save(Request $request)
  44. {
  45. if (!config('v2board.app_url')) {
  46. abort(500, '请在站点配置中配置站点地址');
  47. }
  48. if ($request->input('id')) {
  49. $payment = Payment::find($request->input('id'));
  50. if (!$payment) abort(500, '支付方式不存在');
  51. try {
  52. $payment->update($request->input());
  53. } catch (\Exception $e) {
  54. abort(500, '更新失败');
  55. }
  56. return response([
  57. 'data' => true
  58. ]);
  59. }
  60. $params = $request->validate([
  61. 'name' => 'required',
  62. 'icon' => 'nullable',
  63. 'payment' => 'required',
  64. 'config' => 'required',
  65. 'notify_domain' => 'nullable|url'
  66. ], [
  67. 'name.required' => '显示名称不能为空',
  68. 'payment.required' => '网关参数不能为空',
  69. 'config.required' => '配置参数不能为空',
  70. 'notify_domain.url' => '自定义通知域名格式有误'
  71. ]);
  72. $params['uuid'] = Helper::randomChar(8);
  73. if (!Payment::create($params)) {
  74. abort(500, '保存失败');
  75. }
  76. return response([
  77. 'data' => true
  78. ]);
  79. }
  80. public function drop(Request $request)
  81. {
  82. $payment = Payment::find($request->input('id'));
  83. if (!$payment) abort(500, '支付方式不存在');
  84. return response([
  85. 'data' => $payment->delete()
  86. ]);
  87. }
  88. }