PaymentController.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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(PaymentSave $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. if (!Payment::create([
  56. 'name' => $request->input('name'),
  57. 'payment' => $request->input('payment'),
  58. 'config' => $request->input('config'),
  59. 'uuid' => Helper::randomChar(8)
  60. ])) {
  61. abort(500, '保存失败');
  62. }
  63. return response([
  64. 'data' => true
  65. ]);
  66. }
  67. public function drop(Request $request)
  68. {
  69. $payment = Payment::find($request->input('id'));
  70. if (!$payment) abort(500, '支付方式不存在');
  71. return response([
  72. 'data' => $payment->delete()
  73. ]);
  74. }
  75. }