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

82 lines
2.2 KiB
PHP
Raw Normal View History

2021-04-28 16:56:08 +08:00
<?php
namespace App\Http\Controllers\Admin;
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;
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()
{
2021-04-28 17:48:18 +08:00
$payments = Payment::all();
foreach ($payments as $k => $v) {
2021-05-07 00:18:38 +08:00
$payments[$k]['notify_url'] = url("/api/v1/guest/payment/notify/{$v->payment}/{$v->uuid}");
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()
]);
}
public function save(Request $request)
{
2021-07-30 15:00:20 +08:00
if (!config('v2board.app_url')) {
2021-07-29 20:12:11 +08:00
abort(500, '请在站点配置中配置站点地址');
}
2021-04-28 16:56:08 +08:00
if ($request->input('id')) {
$payment = Payment::find($request->input('id'));
if (!$payment) abort(500, '支付方式不存在');
try {
$payment->update($request->input());
} catch (\Exception $e) {
abort(500, '更新失败');
}
return response([
'data' => true
]);
}
if (!Payment::create([
'name' => $request->input('name'),
'payment' => $request->input('payment'),
2021-05-07 00:17:55 +08:00
'config' => $request->input('config'),
2021-08-20 22:42:07 +08:00
'uuid' => Helper::randomChar(8)
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()
]);
}
2021-04-28 16:56:08 +08:00
}