PaymentController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 show(Request $request)
  44. {
  45. $payment = Payment::find($request->input('id'));
  46. if (!$payment) abort(500, '支付方式不存在');
  47. $payment->enable = !$payment->enable;
  48. if (!$payment->save()) abort(500, '保存失败');
  49. return response([
  50. 'data' => true
  51. ]);
  52. }
  53. public function save(Request $request)
  54. {
  55. if (!config('v2board.app_url')) {
  56. abort(500, '请在站点配置中配置站点地址');
  57. }
  58. $params = $request->validate([
  59. 'name' => 'required',
  60. 'icon' => 'nullable',
  61. 'payment' => 'required',
  62. 'config' => 'required',
  63. 'notify_domain' => 'nullable|url',
  64. 'handling_fee_fixed' => 'nullable|integer',
  65. 'handling_fee_percent' => 'nullable|numeric|between:0.1,100'
  66. ], [
  67. 'name.required' => '显示名称不能为空',
  68. 'payment.required' => '网关参数不能为空',
  69. 'config.required' => '配置参数不能为空',
  70. 'notify_domain.url' => '自定义通知域名格式有误',
  71. 'handling_fee_fixed.integer' => '固定手续费格式有误',
  72. 'handling_fee_percent.between' => '百分比手续费范围须在0.1-100之间'
  73. ]);
  74. if ($request->input('id')) {
  75. $payment = Payment::find($request->input('id'));
  76. if (!$payment) abort(500, '支付方式不存在');
  77. try {
  78. $payment->update($params);
  79. } catch (\Exception $e) {
  80. abort(500, $e->getMessage());
  81. }
  82. return response([
  83. 'data' => true
  84. ]);
  85. }
  86. $params['uuid'] = Helper::randomChar(8);
  87. if (!Payment::create($params)) {
  88. abort(500, '保存失败');
  89. }
  90. return response([
  91. 'data' => true
  92. ]);
  93. }
  94. public function drop(Request $request)
  95. {
  96. $payment = Payment::find($request->input('id'));
  97. if (!$payment) abort(500, '支付方式不存在');
  98. return response([
  99. 'data' => $payment->delete()
  100. ]);
  101. }
  102. }