PaymentController.php 3.1 KB

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