Skip to content

Commit

Permalink
fix: 未配置微信证书时,自动获取证书后仍然使用之前的微信配置 (#1026)
Browse files Browse the repository at this point in the history
* 修复自动获取微信平台公钥证书相关接口提示 参数异常: 微信公钥序列号为找到 错误

* 优化代码,增加单测

---------

Co-authored-by: yansongda <me@yansongda.cn>
  • Loading branch information
Wsmallnews and yansongda authored Sep 11, 2024
1 parent 2ea20b7 commit 0aea5b2
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v3.7.10

### fixed

- fix: 未配置微信证书时,自动获取证书后仍然使用之前的微信配置(#1026)

## v3.7.9

### added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ protected function service(Collection $payload, array $params, array $config): a
protected function encryptSensitiveData(array $params, array $config, Collection $payload): array
{
$data['_serial_no'] = get_wechat_serial_no($params);

$config = get_provider_config('wechat', $params);
$publicKey = get_wechat_public_key($config, $data['_serial_no']);

$data['name'] = encrypt_wechat_contents($payload->get('name'), $publicKey);
Expand Down
2 changes: 2 additions & 0 deletions src/Plugin/Wechat/V3/Extend/ProfitSharing/CreatePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ protected function service(Collection $payload, array $params, array $config): a
protected function encryptSensitiveData(array $params, array $config, Collection $payload): array
{
$data['_serial_no'] = get_wechat_serial_no($params);

$config = get_provider_config('wechat', $params);
$publicKey = get_wechat_public_key($config, $data['_serial_no']);

foreach ($payload->get('receivers', []) as $key => $list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function assembly(Rocket $rocket, Closure $next): Rocket
protected function encryptSensitiveData(?Collection $payload, array $params, array $config): array
{
$data['_serial_no'] = get_wechat_serial_no($params);

$config = get_provider_config('wechat', $params);
$publicKey = get_wechat_public_key($config, $data['_serial_no']);

$phone = $payload?->get('buyer_information.phone') ?? null;
Expand Down
2 changes: 2 additions & 0 deletions src/Plugin/Wechat/V3/Marketing/Transfer/CreatePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ protected function encryptSensitiveData(array $params, array $config, Collection
{
$data['transfer_detail_list'] = $payload->get('transfer_detail_list', []);
$data['_serial_no'] = get_wechat_serial_no($params);

$config = get_provider_config('wechat', $params);
$publicKey = get_wechat_public_key($config, $data['_serial_no']);

foreach ($data['transfer_detail_list'] as $key => $list) {
Expand Down
2 changes: 2 additions & 0 deletions src/Plugin/Wechat/V3/Pay/Refund/RefundAbnormalPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ protected function encryptSensitiveData(array $params, array $config, Collection
{
if ($payload->has('bank_account') && $payload->has('real_name')) {
$data['_serial_no'] = get_wechat_serial_no($params);

$config = get_provider_config('wechat', $params);
$publicKey = get_wechat_public_key($config, $data['_serial_no']);

$data['real_name'] = encrypt_wechat_contents($payload->get('real_name'), $publicKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace Yansongda\Pay\Tests\Plugin\Wechat\V3\Extend\ProfitSharing;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mockery;
use Yansongda\Artful\Contract\HttpClientInterface;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Artful\Exception\InvalidParamsException;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\V3\Extend\ProfitSharing\AddReceiverPlugin;
use Yansongda\Artful\Rocket;
use Yansongda\Pay\Tests\TestCase;
Expand Down Expand Up @@ -151,4 +156,50 @@ public function testWithSubAppId()
'type' => 'PERSONAL_SUB_OPENID',
], $result->getPayload()->all());
}

public function testServiceWithNameEmptyWechatCert()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'empty_wechat_public_cert'])->setPayload(new Collection([
"name" => "yansongda",
]));

$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);

$result = $this->plugin->assembly($rocket, function ($rocket) {
return $rocket;
});

$payload = $result->getPayload()->all();
self::assertEquals('POST', $payload['_method']);
self::assertEquals('v3/profitsharing/receivers/add', $payload['_url']);
self::assertEquals('v3/profitsharing/receivers/add', $payload['_service_url']);
self::assertEquals('wx55955316af4ef13', $payload['appid']);
self::assertArrayHasKey('_serial_no', $payload);
self::assertArrayHasKey('name', $payload);
self::assertNotEquals('yansongda', $payload['name']);
}
}
55 changes: 55 additions & 0 deletions tests/Plugin/Wechat/V3/Extend/ProfitSharing/CreatePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace Yansongda\Pay\Tests\Plugin\Wechat\V3\Extend\ProfitSharing;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mockery;
use Yansongda\Artful\Contract\HttpClientInterface;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Artful\Exception\InvalidParamsException;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\V3\Extend\ProfitSharing\CreatePlugin;
use Yansongda\Artful\Rocket;
use Yansongda\Pay\Tests\TestCase;
Expand Down Expand Up @@ -167,4 +172,54 @@ public function testWithSubAppId()
],
], $result->getPayload()->all());
}

public function testServiceWithNameEmptyWechatCert()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'empty_wechat_public_cert'])->setPayload(new Collection([
'receivers' => [
[
'name' => 'yansongda',
],
],
]));

$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/k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GIaXEKacfySo7e4VZtHeHowvlvBclS9pooZqzHd+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);

$result = $this->plugin->assembly($rocket, function ($rocket) {
return $rocket;
});

$payload = $result->getPayload()->all();
self::assertEquals('POST', $payload['_method']);
self::assertEquals('v3/profitsharing/orders', $payload['_url']);
self::assertEquals('v3/profitsharing/orders', $payload['_service_url']);
self::assertEquals('wx55955316af4ef13', $payload['appid']);
self::assertArrayHasKey('_serial_no', $payload);
self::assertArrayHasKey('name', $payload['receivers'][0]);
self::assertNotEquals('yansongda', $payload['receivers'][0]['name']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

namespace Yansongda\Pay\Tests\Plugin\Wechat\V3\Marketing\Fapiao\Blockchain;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mockery;
use Yansongda\Artful\Contract\HttpClientInterface;
use Yansongda\Artful\Rocket;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\V3\Marketing\Fapiao\Blockchain\CreatePlugin;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;
Expand Down Expand Up @@ -55,4 +60,51 @@ public function testNormalWithSensitiveData()
self::assertNotEquals('123', $payload['buyer_information']['phone']);
self::assertNotEquals('456', $payload['buyer_information']['email']);
}

public function testNormalWithSensitiveDataEmptyWechatCert()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'empty_wechat_public_cert'])->setPayload(new Collection( [
"test" => "yansongda",
'buyer_information' => [
'phone' => '123',
'email' => '456',
]
]));

$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+SAWzSZPWIm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rPRxI2/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);

$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload()->all();

self::assertEquals('POST', $payload['_method']);
self::assertEquals('v3/new-tax-control-fapiao/fapiao-applications', $payload['_url']);
self::assertEquals('yansongda', $payload['test']);
self::assertArrayHasKey('_serial_no', $payload);
self::assertNotEquals('123', $payload['buyer_information']['phone']);
self::assertNotEquals('456', $payload['buyer_information']['email']);
}
}
Loading

0 comments on commit 0aea5b2

Please sign in to comment.