mirror of
https://gitee.com/technical-laohu/mpay_v2_webman.git
synced 2026-04-21 17:44:27 +08:00
重构初始化
This commit is contained in:
138
app/http/api/controller/trade/PayController.php
Normal file
138
app/http/api/controller/trade/PayController.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace app\http\api\controller\trade;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\exception\ResourceNotFoundException;
|
||||
use app\http\api\validation\PayCallbackValidator;
|
||||
use app\http\api\validation\PayCloseValidator;
|
||||
use app\http\api\validation\PayPrepareValidator;
|
||||
use app\http\api\validation\PayTimeoutValidator;
|
||||
use app\service\merchant\security\MerchantApiCredentialService;
|
||||
use app\service\payment\config\PaymentTypeService;
|
||||
use app\service\payment\order\PayOrderService;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
/**
|
||||
* 支付接口控制器。
|
||||
*
|
||||
* 负责支付预下单、支付查询、支付关闭和渠道回调入口。
|
||||
*/
|
||||
class PayController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 构造函数,注入支付单相关依赖。
|
||||
*/
|
||||
public function __construct(
|
||||
protected PayOrderService $payOrderService,
|
||||
protected MerchantApiCredentialService $merchantApiCredentialService,
|
||||
protected PaymentTypeService $paymentTypeService
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付预下单。
|
||||
*/
|
||||
public function prepare(Request $request): Response
|
||||
{
|
||||
$data = $this->validated(
|
||||
$this->normalizePreparePayload($request, $request->all()),
|
||||
PayPrepareValidator::class,
|
||||
'prepare'
|
||||
);
|
||||
|
||||
return $this->success($this->payOrderService->preparePayAttempt($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询支付单详情。
|
||||
*/
|
||||
public function show(Request $request, string $payNo): Response
|
||||
{
|
||||
try {
|
||||
return $this->success($this->payOrderService->detail($payNo));
|
||||
} catch (ResourceNotFoundException) {
|
||||
return $this->fail('支付单不存在', 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭支付单。
|
||||
*/
|
||||
public function close(Request $request, string $payNo): Response
|
||||
{
|
||||
$data = $this->validated(
|
||||
array_merge($request->all(), ['pay_no' => $payNo]),
|
||||
PayCloseValidator::class,
|
||||
'close'
|
||||
);
|
||||
|
||||
return $this->success($this->payOrderService->closePayOrder($payNo, $data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记支付单超时。
|
||||
*/
|
||||
public function timeout(Request $request, string $payNo): Response
|
||||
{
|
||||
$data = $this->validated(
|
||||
array_merge($request->all(), ['pay_no' => $payNo]),
|
||||
PayTimeoutValidator::class,
|
||||
'timeout'
|
||||
);
|
||||
|
||||
return $this->success($this->payOrderService->timeoutPayOrder($payNo, $data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理渠道回调。
|
||||
*/
|
||||
public function callback(Request $request, string $payNo = ''): Response|string
|
||||
{
|
||||
if ($payNo !== '') {
|
||||
return $this->payOrderService->handlePluginCallback($payNo, $request);
|
||||
}
|
||||
|
||||
$data = $this->validated($request->all(), PayCallbackValidator::class, 'callback');
|
||||
|
||||
return $this->success($this->payOrderService->handleChannelCallback($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 归一化外部支付下单参数并完成签名校验。
|
||||
*
|
||||
* 这层逻辑保留在控制器内,避免中间件承担业务验签职责。
|
||||
*/
|
||||
private function normalizePreparePayload(Request $request, array $payload): array
|
||||
{
|
||||
$this->merchantApiCredentialService->verifyMd5Sign($payload);
|
||||
|
||||
$typeCode = trim((string) ($payload['type'] ?? ''));
|
||||
$paymentType = $this->paymentTypeService->resolveEnabledType($typeCode);
|
||||
$typeCode = (string) $paymentType->code;
|
||||
|
||||
$money = (string) ($payload['money'] ?? '0');
|
||||
$amount = (int) round(((float) $money) * 100);
|
||||
|
||||
return [
|
||||
'merchant_id' => (int) ($payload['pid'] ?? 0),
|
||||
'merchant_order_no' => (string) ($payload['out_trade_no'] ?? ''),
|
||||
'pay_type_id' => (int) $paymentType->id,
|
||||
'pay_amount' => $amount,
|
||||
'subject' => (string) ($payload['name'] ?? ''),
|
||||
'body' => (string) ($payload['name'] ?? ''),
|
||||
'ext_json' => [
|
||||
'type_code' => $typeCode,
|
||||
'notify_url' => (string) ($payload['notify_url'] ?? ''),
|
||||
'return_url' => (string) ($payload['return_url'] ?? ''),
|
||||
'param' => $payload['param'] ?? null,
|
||||
'clientip' => (string) ($payload['clientip'] ?? ''),
|
||||
'device' => (string) ($payload['device'] ?? ''),
|
||||
'sign_type' => (string) ($payload['sign_type'] ?? 'MD5'),
|
||||
'channel_callback_base_url' => (string) sys_config('site_url') . '/api/pay',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
93
app/http/api/controller/trade/RefundController.php
Normal file
93
app/http/api/controller/trade/RefundController.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace app\http\api\controller\trade;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\exception\ResourceNotFoundException;
|
||||
use app\http\api\validation\RefundActionValidator;
|
||||
use app\http\api\validation\RefundCreateValidator;
|
||||
use app\service\payment\order\RefundService;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
/**
|
||||
* 退款接口控制器。
|
||||
*
|
||||
* 负责退款单创建与退款单查询。
|
||||
*/
|
||||
class RefundController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 构造函数,注入退款相关依赖。
|
||||
*/
|
||||
public function __construct(
|
||||
protected RefundService $refundService,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建退款单。
|
||||
*/
|
||||
public function create(Request $request): Response
|
||||
{
|
||||
$data = $this->validated($request->all(), RefundCreateValidator::class, 'store');
|
||||
|
||||
return $this->success($this->refundService->createRefund($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询退款单详情。
|
||||
*/
|
||||
public function show(Request $request, string $refundNo): Response
|
||||
{
|
||||
try {
|
||||
return $this->success($this->refundService->detail($refundNo));
|
||||
} catch (ResourceNotFoundException) {
|
||||
return $this->fail('退款单不存在', 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记退款处理中。
|
||||
*/
|
||||
public function processing(Request $request, string $refundNo): Response
|
||||
{
|
||||
$data = $this->validated(
|
||||
array_merge($request->all(), ['refund_no' => $refundNo]),
|
||||
RefundActionValidator::class,
|
||||
'processing'
|
||||
);
|
||||
|
||||
return $this->success($this->refundService->markRefundProcessing($refundNo, $data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款重试。
|
||||
*/
|
||||
public function retry(Request $request, string $refundNo): Response
|
||||
{
|
||||
$data = $this->validated(
|
||||
array_merge($request->all(), ['refund_no' => $refundNo]),
|
||||
RefundActionValidator::class,
|
||||
'retry'
|
||||
);
|
||||
|
||||
return $this->success($this->refundService->retryRefund($refundNo, $data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记退款失败。
|
||||
*/
|
||||
public function markFail(Request $request, string $refundNo): Response
|
||||
{
|
||||
$data = $this->validated(
|
||||
array_merge($request->all(), ['refund_no' => $refundNo]),
|
||||
RefundActionValidator::class,
|
||||
'fail'
|
||||
);
|
||||
|
||||
return $this->success($this->refundService->markRefundFailed($refundNo, $data));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user