Skip to content

Commit

Permalink
feat: 增加获取微信平台公钥证书方法 (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda authored Jan 7, 2023
1 parent 53aa073 commit 4c1893d
Show file tree
Hide file tree
Showing 7 changed files with 527 additions and 418 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v3.2.12

### added

- feat: 增加获取微信平台公钥证书方法(#733)

## v3.2.11

### docs
Expand Down
26 changes: 26 additions & 0 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,32 @@ function reload_wechat_public_certs(array $params, ?string $serialNo = null): st
}
}

if (!function_exists('get_wechat_public_certs')) {
/**
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\InvalidConfigException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
* @throws \Yansongda\Pay\Exception\InvalidParamsException
* @throws \Yansongda\Pay\Exception\InvalidResponseException
*/
function get_wechat_public_certs(array $params = [], ?string $path = null): void
{
reload_wechat_public_certs($params);

$config = get_wechat_config($params);

if (empty($path)) {
var_dump($config['wechat_public_cert_path']);

return;
}

foreach ($config['wechat_public_cert_path'] as $serialNo => $cert) {
file_put_contents($path.'/'.$serialNo.'.crt', $cert);
}
}
}

if (!function_exists('decrypt_wechat_resource')) {
/**
* @throws \Yansongda\Pay\Exception\ContainerException
Expand Down
41 changes: 41 additions & 0 deletions tests/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use function Yansongda\Pay\get_unipay_config;
use function Yansongda\Pay\get_wechat_base_uri;
use function Yansongda\Pay\get_wechat_config;
use function Yansongda\Pay\get_wechat_public_certs;
use function Yansongda\Pay\get_wechat_sign;
use function Yansongda\Pay\reload_wechat_public_certs;
use function Yansongda\Pay\should_do_http_request;
Expand Down Expand Up @@ -278,6 +279,46 @@ public function testReloadWechatPublicCerts()
self::assertIsArray(Pay::get(ConfigInterface::class)->get('wechat.default'));
}

public function testGetWechatPublicCerts()
{
$response = new Response(
200,
[],
json_encode([
'data' => [
[
'effective_time' => '2021-07-16T17:51:10+08:00',
'encrypt_certificate' => [
'algorithm' => 'AEAD_AES_256_GCM',
'associated_data' => 'certificate',
'ciphertext' => 'kbbHAUhBwdjYZkHPW149MW/8WNpxQo1Gyp4kVNVjd+zrXnyOFhgZic2U2+tobFAgfdr93zr0JZF3FdbxgkaOAV2NAeCfU8jsUYXSfn7fM8487jXMVXKKEneGiiv1/bDLkz7KFsTfu2y5Rv+igWQ+bvCUQAwoNzjupTXnnDR5hBiofZcFLHL45govyYE2o0qD5SLiJHcFS4pg/IOx8SIqUFNepr3piKXUxKowU8/kNxXyRzL8yp7XnhrzAzclupvjveNwZyiw3TqlLZdR5TbEFLCogWaRHZRqz3vKEfgRaUYUtXCtQVrm+adbSDBFIq34v+XfeIHMz9pKhH/m80N5Hx69hPzbvIdBhzwaEDyN3h8gaeYKFyW9xIAs5jCrzzUEkKyMzOKzx7XA+1HRakSyvs6RlkRTa/ztBy6aZL0nxK6XMZ9tA7zdf2VnBX/7WPQYRzoky0cVyH1KRZxI7In2hfvpjSvl6P7Adzp+EZXYM/dINTrrg+RQRe60tPy7vgE8PZZf+SAWzSZPWIm7Lx6GksJX0vnT4gOeTAPw6EeFsYU/ZD7fYslJOEbA14yHBrJFkwDpSI8aSHp2nZYbruM0y8IKr0p3vjN80Ko3jiRPxj4uNdJliR9WDCV22b9JeadAaJhO9+oSNbbtFnFTCZjXbf8rMz5KCGVrGRvUyB70zhRxYIOdTYKAEkmbU7jcMLd0aufuQqIw0WviQHB+ztrkjBCFwPu5/hlRVj9opNFnzYNltfVGrA1XW3NQ4FaMNah95ahomAG/+S7zJqq4Gvk1O/PgQ9kMP0adY3GlrHUNqr2zC709IervMQ1pEdcuNEln3V5TSDiE0x7BjoMoN2m+MKAIhw59VxzHGNmJELbkKsZUhKKXFFyEXFsw143/9IYOyanmHQxujdIBKI0rxYkVz9QgaajisCzdnRf0ymnkceGGnYsP7VTYBnuCncjgHxbEn3emlTRygEjgj/epupsQL2tfW+snxnafEM+Pc079pUYmKeCUEUoX/FUmdFIf8hlSHBTjEVMGsNUI/u2W781RBDfk2X/2QQQm3NOjgZ3le6hxEQqc12yANTvdq7cFVllWqMHBsXPCjpHWIHcS5BMkImoD7s6WItq60yJA8ioGJf3Rba+Yb/YeBBNxjDnXtAmX/2hJIsxEFLTYGUvdmFC5jeb5ifrOuxnLciKM8y4nLZ28dDsvVsaBBAMAFYfWb5NymKUDhhngR5bDuW4sKccZ6DmYQeStHT1fn2yoSneGA70HctQSWZ2roTdNihPTCs7rYD0dFeQ+SfLOJzMN4c5GbJ6n5tdCjERcLGIaXEKacfySo7e4VZtHeHowvlvBclS9pooZqzHd+EFlJEYywEs9jURgsJY2yHJt2zTZeIdsvM8KK5v0NkH8FiPbWqFG8LaRmUrqhJGLuLLRTcJnt6YVYESxUVTb3pmriUbXfg/ThHF/y0THyrM6bVDNOwNWZOpMYPPNaVmOTX39JdYayWl2HX0n8AsIRmevXzD4N9iDh2HGwie4gh92Qdcogwua++uhkhSsLFuWBpJiaPdxVtzz3E3jHfy+yryfh6msaXc/jmhwqBm/ii3j76lDP5YaRv4+JWZmom72+pmZuKD8qPKrPRxI2/aGiKEqgs25knpLLnbAhWAEYeIzVK1sQkjc5JFss1Std8FdDrHeM6agAB+MWncK1LloXZmiwz/6WmlwSDepnGHqLEciXThAZq6FwunJZTcHY9LamJgIY81c9t/KHlSFqlc/9mW4OZHM4BOZQ5sTj5PWE+OP2Aq9CKdJqoK3OmphBg2ewjrZt5/tSn9jpk6NlVrHD7MsJcKi5a0he4qvNPh1cHqUqWcF4rBFmfPptdHIBV77LXnizJZMUAwf16KsmJpwJg==',
'nonce' => '4196a5b75276',
],
'expire_time' => '2026-07-15T17:51:10+08:00',
'serial_no' => 'test-45F59D4DABF31918AFCEC556D5D2C6E376675D57',
]
]
])
);

$http = Mockery::mock(Client::class);
$http->shouldReceive('sendRequest')->andReturn($response);

Pay::set(HttpClientInterface::class, $http);

$path = sys_get_temp_dir();

get_wechat_public_certs([], $path);

$crtPathName = $path . '/test-45F59D4DABF31918AFCEC556D5D2C6E376675D57.crt';

self::assertFileExists($crtPathName);
self::assertTrue(false !== strpos(file_get_contents($crtPathName), '-----BEGIN CERTIFICATE-----'));

self::expectOutputRegex('/.*-----BEGIN CERTIFICATE-----/');
get_wechat_public_certs();
}

public function testDecryptWechatResource()
{
$resource = [
Expand Down
1 change: 1 addition & 0 deletions web/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.vitepress/dist/
.vitepress/cache/
yarn-error.log
36 changes: 36 additions & 0 deletions web/docs/v3/quick-start/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,39 @@ $result = Pay::alipay()->find($order);
::: warning PHP-FPM 模式
如果您在 php-fpm 进程下使用 Pay,强烈建议您手动配置 `wechat_public_cert_path` 参数。当然,您也可以不用配置,不过,您每次支付将从微信服务器获取最新的证书并验证,这将有性能上的损耗。
:::

### 获取方式

Pay 中内置了非常简单的获取微信平台公钥证书的方式,只需三两行代码即可搞定微信平台证书,再也不用下载着下载那,搞的一头雾水了!

如果您感兴趣,您可以参考 `\Yansongda\Pay\get_wechat_public_certs` 方法了解详细细节。

#### 直接将获取到的证书保存为文件

```php
<?php

\Yansongda\Pay\Pay::config($config);

$params = [
'_config' => 'default' // 多租户配置时使用
];

\Yansongda\Pay\get_wechat_public_certs($params, storage_path('pay/wechat_default'));
```

通过简单的代码,即可将证书保存至指定目录,这样,我们就可以在配置文件中直接使用了。

#### 直接获取证书内容

以下代码运行后,将会直接将证书内容 `var_dump` 出来,方便您自由使用。

```php
\Yansongda\Pay\Pay::config($config);

$params = [
'_config' => 'default' // 多租户配置时使用
];

\Yansongda\Pay\get_wechat_public_certs($params);
```
3 changes: 1 addition & 2 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"devDependencies": {
"@types/node": "^18.7.8",
"sass": "^1.54.5",
"vitepress": "^1.0.0-alpha.27",
"vue": "^3.2.37"
"vitepress": "^1.0.0-alpha.35"
}
}
Loading

0 comments on commit 4c1893d

Please sign in to comment.