feat : Proxy payment
This commit is contained in:
54
app/Aspect/Test/Proxy/LoggingAspect.php
Normal file
54
app/Aspect/Test/Proxy/LoggingAspect.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Aspect\Test\Proxy;
|
||||||
|
|
||||||
|
use App\Interface\Test\Decorator\LoggerInterface;
|
||||||
|
use App\Service\Test\Proxy\Payment\PaymentService;
|
||||||
|
use Hyperf\Di\Annotation\Aspect;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
use Hyperf\Di\Aop\AbstractAspect;
|
||||||
|
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||||
|
use Hyperf\Di\Exception\Exception;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
#[Aspect]
|
||||||
|
class LoggingAspect extends AbstractAspect
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var LoggerInterface
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected LoggerInterface $logger;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array|string[]
|
||||||
|
*/
|
||||||
|
public array $classes = [
|
||||||
|
PaymentService::class , '::pay',
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ProceedingJoinPoint $proceedingJoinPoint
|
||||||
|
* @return mixed
|
||||||
|
* @throws Throwable
|
||||||
|
*/
|
||||||
|
public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed
|
||||||
|
{
|
||||||
|
$amount = $proceedingJoinPoint->arguments['keys']['amount'];
|
||||||
|
|
||||||
|
$this->logger->log('Attempting payment of '.$amount.' for '.$proceedingJoinPoint->methodName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $proceedingJoinPoint->process();
|
||||||
|
|
||||||
|
$this->logger->log('Payment of '.$amount.'succeeded');
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
$this->logger->error('Payment of '.$amount.' failed: '.$e->getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
53
app/Aspect/Test/Proxy/SecurityAspect.php
Normal file
53
app/Aspect/Test/Proxy/SecurityAspect.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Aspect\Test\Proxy;
|
||||||
|
|
||||||
|
use App\Service\Test\Proxy\Payment\PaymentService;
|
||||||
|
use Hyperf\Di\Annotation\Aspect;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
use Hyperf\Di\Aop\AbstractAspect;
|
||||||
|
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||||
|
use Hyperf\Di\Exception\Exception;
|
||||||
|
use Hyperf\HttpServer\Request;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
#[Aspect]
|
||||||
|
class SecurityAspect extends AbstractAspect
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Request
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected Request $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array|string[]
|
||||||
|
*/
|
||||||
|
public array $classes = [
|
||||||
|
PaymentService::class , '::pay',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 安全验证切面
|
||||||
|
* @param ProceedingJoinPoint $proceedingJoinPoint
|
||||||
|
* @return mixed
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed
|
||||||
|
{
|
||||||
|
$token = $this->request->header('Authorization');
|
||||||
|
|
||||||
|
if (!$this->validateToken($token)) throw new RuntimeException('Unauthorized');
|
||||||
|
|
||||||
|
return $proceedingJoinPoint->process();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|null $token
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function validateToken(?string $token): bool
|
||||||
|
{
|
||||||
|
return $token !== 'valid-token';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,8 +4,10 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Controller\Test;
|
namespace App\Controller\Test;
|
||||||
|
|
||||||
|
use App\Service\Test\Proxy\AopService;
|
||||||
use App\Service\Test\Proxy\BasicSubjectService;
|
use App\Service\Test\Proxy\BasicSubjectService;
|
||||||
use App\Service\Test\Proxy\DynamicProxyService;
|
use App\Service\Test\Proxy\DynamicProxyService;
|
||||||
|
use App\Service\Test\Proxy\Payment\PaymentService;
|
||||||
use Hyperf\HttpServer\Annotation\Controller;
|
use Hyperf\HttpServer\Annotation\Controller;
|
||||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
@@ -33,4 +35,13 @@ class ProxyController
|
|||||||
{
|
{
|
||||||
return (new DynamicProxyService)->handle();
|
return (new DynamicProxyService)->handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
#[RequestMapping(path: 'payment', methods: 'GET')]
|
||||||
|
public function payment(): array
|
||||||
|
{
|
||||||
|
return (new AopService)->handle();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
13
app/Interface/Test/Proxy/PaymentInterface.php
Normal file
13
app/Interface/Test/Proxy/PaymentInterface.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Interface\Test\Proxy;
|
||||||
|
|
||||||
|
interface PaymentInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 代理支付接口
|
||||||
|
* @param float $amount
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function pay(float $amount) : array;
|
||||||
|
}
|
||||||
@@ -41,3 +41,8 @@ Content-Type: application/x-www-form-urlencoded
|
|||||||
### Proxy dynamic test
|
### Proxy dynamic test
|
||||||
GET {{host}}/proxy/test/dynamic
|
GET {{host}}/proxy/test/dynamic
|
||||||
Content-Type: application/x-www-form-urlencoded
|
Content-Type: application/x-www-form-urlencoded
|
||||||
|
|
||||||
|
|
||||||
|
### Proxy payment test
|
||||||
|
GET {{host}}/proxy/test/payment
|
||||||
|
Content-Type: application/x-www-form-urlencoded
|
||||||
Reference in New Issue
Block a user