v2board/app/Http/Controllers/Admin/PaymentController.php

134 lines
4.1 KiB
PHP
Raw Normal View History

2021-04-28 16:56:08 +08:00
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\PaymentSave;
2021-04-28 16:56:08 +08:00
use App\Services\PaymentService;
2021-05-07 00:17:55 +08:00
use App\Utils\Helper;
2021-04-28 16:56:08 +08:00
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Payment;
2022-09-24 02:37:02 +08:00
use Illuminate\Support\Facades\DB;
2021-04-28 16:56:08 +08:00
class PaymentController extends Controller
{
public function getPaymentMethods()
{
$methods = [];
foreach (glob(base_path('app//Payments') . '/*.php') as $file) {
array_push($methods, pathinfo($file)['filename']);
}
return response([
'data' => $methods
]);
}
public function fetch()
{
2022-09-24 02:37:02 +08:00
$payments = Payment::orderBy('sort', 'ASC')->get();
2021-04-28 17:48:18 +08:00
foreach ($payments as $k => $v) {
2022-05-09 23:26:59 +08:00
$notifyUrl = url("/api/v1/guest/payment/notify/{$v->payment}/{$v->uuid}");
if ($v->notify_domain) {
$parseUrl = parse_url($notifyUrl);
$notifyUrl = $v->notify_domain . $parseUrl['path'];
}
$payments[$k]['notify_url'] = $notifyUrl;
2021-04-28 17:48:18 +08:00
}
2021-04-28 16:56:08 +08:00
return response([
2021-04-28 17:48:18 +08:00
'data' => $payments
2021-04-28 16:56:08 +08:00
]);
}
public function getPaymentForm(Request $request)
{
$paymentService = new PaymentService($request->input('payment'), $request->input('id'));
return response([
'data' => $paymentService->form()
]);
}
2022-03-18 02:49:51 +08:00
public function show(Request $request)
{
$payment = Payment::find($request->input('id'));
if (!$payment) abort(500, '支付方式不存在');
$payment->enable = !$payment->enable;
if (!$payment->save()) abort(500, '保存失败');
return response([
'data' => true
]);
}
2021-10-22 00:00:49 +08:00
public function save(Request $request)
2021-04-28 16:56:08 +08:00
{
2021-07-30 15:00:20 +08:00
if (!config('v2board.app_url')) {
2021-07-29 20:12:11 +08:00
abort(500, '请在站点配置中配置站点地址');
}
2022-03-18 02:49:51 +08:00
$params = $request->validate([
2021-10-22 00:00:49 +08:00
'name' => 'required',
'icon' => 'nullable',
2021-10-22 00:00:49 +08:00
'payment' => 'required',
'config' => 'required',
2022-03-17 14:50:02 +08:00
'notify_domain' => 'nullable|url',
'handling_fee_fixed' => 'nullable|integer',
'handling_fee_percent' => 'nullable|numeric|between:0.1,100'
2022-03-18 02:49:51 +08:00
], [
'name.required' => '显示名称不能为空',
'payment.required' => '网关参数不能为空',
'config.required' => '配置参数不能为空',
'notify_domain.url' => '自定义通知域名格式有误',
'handling_fee_fixed.integer' => '固定手续费格式有误',
'handling_fee_percent.between' => '百分比手续费范围须在0.1-100之间'
]);
2022-03-17 14:50:02 +08:00
if ($request->input('id')) {
$payment = Payment::find($request->input('id'));
if (!$payment) abort(500, '支付方式不存在');
try {
2022-03-18 02:49:51 +08:00
$payment->update($params);
2022-03-17 14:50:02 +08:00
} catch (\Exception $e) {
abort(500, $e->getMessage());
}
return response([
'data' => true
]);
}
$params['uuid'] = Helper::randomChar(8);
if (!Payment::create($params)) {
2021-04-28 16:56:08 +08:00
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
2021-05-06 23:10:05 +08:00
public function drop(Request $request)
{
$payment = Payment::find($request->input('id'));
if (!$payment) abort(500, '支付方式不存在');
return response([
'data' => $payment->delete()
]);
}
2022-09-24 02:37:02 +08:00
public function sort(Request $request)
{
$request->validate([
'ids' => 'required|array'
], [
'ids.required' => '参数有误',
'ids.array' => '参数有误'
]);
DB::beginTransaction();
foreach ($request->input('ids') as $k => $v) {
if (!Payment::find($v)->update(['sort' => $k + 1])) {
DB::rollBack();
abort(500, '保存失败');
}
}
DB::commit();
return response([
'data' => true
]);
}
2021-04-28 16:56:08 +08:00
}