PaymentController.php 3.2 KB

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