feat(stripe): 实现了 stripe 获取配置信息的服务方法

This commit is contained in:
李东云
2023-09-20 16:30:54 +08:00
parent a50216f2fc
commit 64dd159f9c
3 changed files with 69 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/**
* StripeConfiguration.php@Pay
*
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
* Powered by PhpStorm
* Created on 2023/9/20
*/
namespace Singularity\HDK\Pay\Resource;
use Hyperf\Resource\Json\JsonResource;
/**
* Singularity\HDK\Pay\Resource\StripeConfiguration@Pay
*
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
* Powered by PhpStorm
* Created on 2023/9/20
*
* @property-read string $id The unique identifier for this stripe account
* @property-read string $pk The publishable key for this stripe account
*/
final class StripeConfiguration extends JsonResource
{
public ?string $wrap = null;
public function toArray(): array
{
return [
'id' => $this->resource['id'],
'pk' => $this->resource['pk'],
'sk' => $this->when(isset($this->resource['sk']), fn() => $this->resource['sk']),
'webhookSecret' => $this->when(isset($this->webhookSecret), fn() =>$this->resource['webhookSecret']),
'notifyUrl' => $this->when(!empty($this->resource['notifyUrl']), fn() => $this->resource['notifyUrl'])
];
}
public function __get($key): ?string
{
return $this->resource[$key] ?? null;
}
}

View File

@@ -19,6 +19,8 @@ use Singularity\HDK\Core\Http\RequestService;
use Singularity\HDK\Core\Http\RequestServiceFactory;
use Singularity\HDK\Pay\Resource\Order;
use Singularity\HDK\Pay\Resource\StripeConfiguration;
use function Hyperf\Config\config;
/**
@@ -40,6 +42,15 @@ final class StripeRpc
]);
}
public function getConfigurations(): StripeConfiguration
{
$resp = $this->requestService->requestGet('/rpc/v1/stripe/configurations');
$content = $resp->getBody()->getContents();
$order = Json::decode($content);
return StripeConfiguration::make($order);
}
/**
* @param Money $money
* @param string $uid

View File

@@ -12,11 +12,12 @@
use Money\Money;
use Singularity\HDK\Pay\Resource\Order;
use Singularity\HDK\Pay\Resource\StripeConfiguration;
use Singularity\HDK\Pay\Sdk\StripeRpc;
test('能够正常创建 Stripe 订单', function () {
/** @var StripeRpc $service */
$service = \Hyperf\Support\make(StripeRpc::class,['baseUrl' => 'http://192.168.2.218:9611']);
$service = \Hyperf\Support\make(StripeRpc::class, ['baseUrl' => 'http://192.168.2.218:9611']);
$order = $service->createSession(
money: Money::USD(51),
uid: uniqid('NAT_'),
@@ -27,3 +28,14 @@ test('能够正常创建 Stripe 订单', function () {
);
expect($order)->toBeInstanceOf(Order::class);
});
test('能够正常获取 Stripe 配置信息', function () {
/** @var StripeRpc $service */
$service = \Hyperf\Support\make(StripeRpc::class, ['baseUrl' => 'http://192.168.2.218:9611']);
$configures = $service->getConfigurations();
expect($configures)
->toBeInstanceOf(StripeConfiguration::class)
->toHaveKeys(['id', 'pk'])
->and($configures->resolve())->toBeArray();
});