PaymentController.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Services\PaymentService;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Payment;
  7. class PaymentController extends Controller
  8. {
  9. public function getPaymentMethods()
  10. {
  11. $methods = [];
  12. foreach (glob(base_path('app//Payments') . '/*.php') as $file) {
  13. array_push($methods, pathinfo($file)['filename']);
  14. }
  15. return response([
  16. 'data' => $methods
  17. ]);
  18. }
  19. public function fetch()
  20. {
  21. $payments = Payment::all();
  22. foreach ($payments as $k => $v) {
  23. $payments[$k]['notify_url'] = url("/api/v1/guest/payment/notify/{$v->payment}/{$v->id}");
  24. }
  25. return response([
  26. 'data' => $payments
  27. ]);
  28. }
  29. public function getPaymentForm(Request $request)
  30. {
  31. $paymentService = new PaymentService($request->input('payment'), $request->input('id'));
  32. return response([
  33. 'data' => $paymentService->form()
  34. ]);
  35. }
  36. public function save(Request $request)
  37. {
  38. if ($request->input('id')) {
  39. $payment = Payment::find($request->input('id'));
  40. if (!$payment) abort(500, '支付方式不存在');
  41. try {
  42. $payment->update($request->input());
  43. } catch (\Exception $e) {
  44. abort(500, '更新失败');
  45. }
  46. return response([
  47. 'data' => true
  48. ]);
  49. }
  50. if (!Payment::create([
  51. 'name' => $request->input('name'),
  52. 'payment' => $request->input('payment'),
  53. 'config' => $request->input('config')
  54. ])) {
  55. abort(500, '保存失败');
  56. }
  57. return response([
  58. 'data' => true
  59. ]);
  60. }
  61. }