mirror of
https://gitee.com/technical-laohu/mpay_v2_webman.git
synced 2026-04-21 01:14:31 +08:00
codex基础代码更新
This commit is contained in:
@@ -3,8 +3,7 @@
|
||||
namespace app\http\api\controller;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\services\api\EpayService;
|
||||
use app\validation\EpayValidator;
|
||||
use app\services\api\EpayProtocolService;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
@@ -14,43 +13,32 @@ use support\Response;
|
||||
class EpayController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
protected EpayService $epayService
|
||||
) {}
|
||||
protected EpayProtocolService $epayProtocolService
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面跳转支付
|
||||
*/
|
||||
public function submit(Request $request)
|
||||
{
|
||||
$data = match ($request->method()) {
|
||||
'GET' => $request->get(),
|
||||
'POST' => $request->post(),
|
||||
default => $request->all(),
|
||||
};
|
||||
|
||||
try {
|
||||
// 参数校验(使用自定义 Validator + 场景)
|
||||
$params = EpayValidator::make($data)
|
||||
->withScene('submit')
|
||||
->validate();
|
||||
$result = $this->epayProtocolService->handleSubmit($request);
|
||||
$type = $result['response_type'] ?? '';
|
||||
|
||||
// 业务处理:创建订单并获取支付参数
|
||||
$result = $this->epayService->submit($params, $request);
|
||||
$payParams = $result['pay_params'] ?? [];
|
||||
|
||||
// 根据支付参数类型返回响应
|
||||
if (($payParams['type'] ?? '') === 'redirect' && !empty($payParams['url'])) {
|
||||
return redirect($payParams['url']);
|
||||
if ($type === 'redirect' && !empty($result['url'])) {
|
||||
return redirect($result['url']);
|
||||
}
|
||||
|
||||
if (($payParams['type'] ?? '') === 'form') {
|
||||
if (!empty($payParams['html'])) {
|
||||
return response($payParams['html'])->withHeaders(['Content-Type' => 'text/html; charset=UTF-8']);
|
||||
}
|
||||
return $this->renderForm($payParams);
|
||||
if ($type === 'form_html') {
|
||||
return response((string)($result['html'] ?? ''))
|
||||
->withHeaders(['Content-Type' => 'text/html; charset=UTF-8']);
|
||||
}
|
||||
|
||||
if ($type === 'form_params') {
|
||||
return $this->renderForm((array)($result['form'] ?? []));
|
||||
}
|
||||
|
||||
// 如果没有匹配的类型,返回错误
|
||||
return $this->fail('支付参数生成失败');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->fail($e->getMessage());
|
||||
@@ -62,20 +50,12 @@ class EpayController extends BaseController
|
||||
*/
|
||||
public function mapi(Request $request)
|
||||
{
|
||||
$data = $request->post();
|
||||
|
||||
try {
|
||||
$params = EpayValidator::make($data)
|
||||
->withScene('mapi')
|
||||
->validate();
|
||||
|
||||
$result = $this->epayService->mapi($params, $request);
|
||||
|
||||
return json($result);
|
||||
return json($this->epayProtocolService->handleMapi($request));
|
||||
} catch (\Throwable $e) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => $e->getMessage(),
|
||||
'msg' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -85,33 +65,12 @@ class EpayController extends BaseController
|
||||
*/
|
||||
public function api(Request $request)
|
||||
{
|
||||
$data = array_merge($request->get(), $request->post());
|
||||
|
||||
try {
|
||||
$act = strtolower($data['act'] ?? '');
|
||||
|
||||
if ($act === 'order') {
|
||||
$params = EpayValidator::make($data)
|
||||
->withScene('api_order')
|
||||
->validate();
|
||||
$result = $this->epayService->api($params);
|
||||
} elseif ($act === 'refund') {
|
||||
$params = EpayValidator::make($data)
|
||||
->withScene('api_refund')
|
||||
->validate();
|
||||
$result = $this->epayService->api($params);
|
||||
} else {
|
||||
$result = [
|
||||
'code' => 0,
|
||||
'msg' => '不支持的操作类型',
|
||||
];
|
||||
}
|
||||
|
||||
return json($result);
|
||||
return json($this->epayProtocolService->handleApi($request));
|
||||
} catch (\Throwable $e) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => $e->getMessage(),
|
||||
'msg' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
namespace app\http\api\controller;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\services\PayOrderService;
|
||||
use app\services\PayNotifyService;
|
||||
use app\services\PluginService;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
/**
|
||||
* 支付控制器(OpenAPI)
|
||||
@@ -12,30 +14,61 @@ use support\Request;
|
||||
class PayController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
protected PayOrderService $payOrderService
|
||||
) {}
|
||||
protected PayNotifyService $payNotifyService,
|
||||
protected PluginService $pluginService
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建订单
|
||||
*/
|
||||
public function create(Request $request) {}
|
||||
public function create(Request $request)
|
||||
{
|
||||
return $this->fail('not implemented', 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询订单
|
||||
*/
|
||||
public function query(Request $request) {}
|
||||
public function query(Request $request)
|
||||
{
|
||||
return $this->fail('not implemented', 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭订单
|
||||
*/
|
||||
public function close(Request $request) {}
|
||||
public function close(Request $request)
|
||||
{
|
||||
return $this->fail('not implemented', 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单退款
|
||||
*/
|
||||
public function refund(Request $request) {}
|
||||
public function refund(Request $request)
|
||||
{
|
||||
return $this->fail('not implemented', 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步通知
|
||||
*/
|
||||
public function notify(Request $request) {}
|
||||
public function notify(Request $request, string $pluginCode)
|
||||
{
|
||||
try {
|
||||
$plugin = $this->pluginService->getPluginInstance($pluginCode);
|
||||
$result = $this->payNotifyService->handleNotify($pluginCode, $request);
|
||||
$ackSuccess = method_exists($plugin, 'notifySuccess') ? $plugin->notifySuccess() : 'success';
|
||||
$ackFail = method_exists($plugin, 'notifyFail') ? $plugin->notifyFail() : 'fail';
|
||||
|
||||
if (!($result['ok'] ?? false)) {
|
||||
return $ackFail instanceof Response ? $ackFail : response((string)$ackFail);
|
||||
}
|
||||
|
||||
return $ackSuccess instanceof Response ? $ackSuccess : response((string)$ackSuccess);
|
||||
} catch (\Throwable $e) {
|
||||
return response('fail');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user