Skip to content

Commit

Permalink
feat: 请求头增加withSerialHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
lyt8384 committed Jan 6, 2025
1 parent 97117db commit 6888b87
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 11 deletions.
52 changes: 45 additions & 7 deletions docs/src/6.x/pay/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

> 👏🏻 欢迎点击本页下方 "帮助我们改善此页面!" 链接参与贡献更多的使用示例!


<details>
<summary>JSAPI 下单</summary>

Expand All @@ -30,7 +28,6 @@ $response = $app->getClient()->postJson("v3/pay/transactions/jsapi", [

</details>


<details>
<summary>Native 下单</summary>

Expand All @@ -49,8 +46,8 @@ $response = $app->getClient()->postJson('v3/pay/transactions/native', [

print_r($response->toArray(false));
```
</details>

</details>

<details>
<summary>查询订单(商户订单号)</summary>
Expand All @@ -66,8 +63,8 @@ $response = $app->getClient()->get("v3/pay/transactions/out-trade-no/{$outTradeN

print_r($response->toArray());
```
</details>

</details>

<details>
<summary>查询订单(微信订单号)</summary>
Expand All @@ -82,6 +79,7 @@ $response = $app->getClient()->get("pay/transactions/id/{$transactionId}", [

print_r($response->toArray());
```

</details>

<details>
Expand Down Expand Up @@ -118,6 +116,7 @@ Route::post('payment_notify', function () {
return $server->serve();
});
```

</details>

<details>
Expand All @@ -141,13 +140,13 @@ $response = $api->post('/mmpaymkttransfers/promotion/transfers', [

print_r($response->toArray());
```
</details>

</details>

<details>
<summary>JSAPI下单(服务商)</summary>

> 官方文档:<[https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml](https://pay.weixin.qq.com/docs/partner/apis/partner-jsapi-payment/partner-jsons/partner-jsapi-prepay.html)>
> 官方文档:<[https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml](https://pay.weixin.qq.com/docs/partner/apis/partner-jsapi-payment/partner-jsons/partner-jsapi-prepay.html)>
```php
$response = $app->getClient()->postJson("v3/pay/partner/transactions/jsapi", [
Expand All @@ -170,6 +169,45 @@ print_r($response->toArray());

print_r($response->toArray());
```

</details>

<details>
<summary>敏感信息加密</summary>

> 官方文档:<https://pay.weixin.qq.com/doc/v3/merchant/4013053257>
> 使用默认公钥 ID
```php
$utils = $app->getUtils();
$response = $app->getClient()->withSerialHeader()->postJson("v3/applyment4sub/applyment/", [
"business_code" => "12345678",
'contact_info' => [
'contact_name' => $utils->createRsaEncrypt('张三'),
//...
],
//...
]);

print_r($response->toArray());
```

或指定公钥 ID

```php
$utils = $app->getUtils();
$response = $app->getClient()->withSerialHeader("PUB_KEY_ID_123456")->postJson("v3/applyment4sub/applyment/", [
"business_code" => "12345678",
'contact_info' => [
'contact_name' => $utils->createRsaEncrypt("张三","PUB_KEY_ID_123456"),
//...
],
//...
]);

print_r($response->toArray());
```

</details>

<!--
Expand Down
23 changes: 23 additions & 0 deletions src/Pay/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ protected function isV3Request(string $url): bool
return false;
}

public function withSerialHeader(?string $serial = null)
{
$platformCerts = $this->merchant->getPlatformCerts();
if (empty($platformCerts)) {
throw new InvalidConfigException('Missing platform certificate.');
}

$serial = $serial ?? array_key_first($platformCerts);
$this->withHeader('Wechatpay-Serial', $serial);
return $this;
}

/**
* @param array<int, mixed> $arguments
*
Expand Down Expand Up @@ -242,6 +254,17 @@ public static function createMockClient(MockHttpClient $mockHttpClient): HttpCli
Mockery::mock(PublicKey::class),
'mock-v3-key',
'mock-v2-key',
[
'PUB_KEY_ID_MOCK' => '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlReZ1YnfAohRIfUqIeyP
aO0PlkMw1RLPdZbEZmldbGrIrOh/0XqSzNZ+mtB6H0eB7TSaoGFtdp/AWy3tb67m
1T62OrEhz6bnSKMcZkYVmODyxZvcwsCZ3zqCaFo7FrGmh1o9M0/Xfa5SOX4jVGni
3iM7r7YD/NiW2RCYDtjMoLTmVgrzv45Mzu2XpJqtNbUJIRRhVSnjsAZRC6spWH+b
QpYIkVd4qmYE0qdpIQBMYOV1w7v1pYn6Z5QdKG4keemADTn4QaZZHrryTcHNYVsZ
2OZ3aybrevSV3wDGnYGk2nt2xtkdfaNfFn4dGW+p4an5M4fRK+CnYpeTgI6POABk
pwIDAQAB
-----END PUBLIC KEY-----'
]
);

return Mockery::mock(static::class, [$mockMerchant, $mockHttpClient])
Expand Down
12 changes: 12 additions & 0 deletions tests/Pay/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,16 @@ public function test_v3_upload_media()

$this->assertSame($response, $client->uploadMedia('/v3/merchant/media/upload', './tests/fixtures/files/image.jpg'));
}

public function test_v3_with_serial_header()
{
$client = Client::mock();
$client->shouldReceive('createSignature')->never();
$client->shouldReceive('attachLegacySignature')->with([
'foo' => 'bar',
])->andReturn(['foo' => 'bar', 'sign' => 'mock-signature']);

$client->withSerialHeader()->post('certificates', ['body' => Xml::build(['foo' => 'bar'])]);
$this->assertSame('Wechatpay-Serial: PUB_KEY_ID_MOCK', $client->getRequestOptions()['headers'][0]);
}
}
10 changes: 6 additions & 4 deletions tests/Pay/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function test_create_rsa_encrypt()
secretKey: 'v3SecretKey',
v2SecretKey: 'v2SecretKey',
platformCerts: [
'PUB_KEY_ID_123456' => '-----BEGIN PUBLIC KEY-----
'PUB_KEY_ID_MOCK' => '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlReZ1YnfAohRIfUqIeyP
aO0PlkMw1RLPdZbEZmldbGrIrOh/0XqSzNZ+mtB6H0eB7TSaoGFtdp/AWy3tb67m
1T62OrEhz6bnSKMcZkYVmODyxZvcwsCZ3zqCaFo7FrGmh1o9M0/Xfa5SOX4jVGni
Expand All @@ -81,12 +81,14 @@ public function test_create_rsa_encrypt()
]
);
$utils = new Utils(merchant: $merchant);
$this->assertIsString($utils->createRsaEncrypt('mock-plaintext', 'PUB_KEY_ID_123456'));
$this->assertIsString($utils->createRsaEncrypt('mock-plaintext', 'PUB_KEY_ID_MOCK'));
$this->assertIsString($utils->createRsaEncrypt('mock-plaintext'));

$this->expectException(InvalidConfigException::class);
$utils->createRsaEncrypt('mock-plaintext', 'PUB_KEY_ID_456789');
$utils->createRsaEncrypt('mock-plaintext', 'PUB_KEY_ID_OTHER');

$this->expectException(EncryptionFailureException::class);
$utils->createRsaEncrypt('', 'PUB_KEY_ID_123456');
$utils->createRsaEncrypt('', 'PUB_KEY_ID_MOCK');

}
}

0 comments on commit 6888b87

Please sign in to comment.