Skip to content

Commit

Permalink
feat: 增加加密时自动携带Wechatpay-Serial处理逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lyt8384 committed Jan 6, 2025
1 parent b3da704 commit 6a79ad1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Pay/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public function request(string $method, string $url, array $options = []): Respo
if (! empty($this->prependHeaders)) {
$options['headers'] = array_merge($this->prependHeaders, $options['headers']);
}

//如果有加密字段,需要携带
if(! isset($options['headers']['Wechatpay-Serial']) && $serial = $this->merchant->getPlatformCertSerial()){
$options['headers']['Wechatpay-Serial'] = $serial;
}

return new Response(
$this->client->request($method, $url, $options),
Expand Down
4 changes: 4 additions & 0 deletions src/Pay/Contracts/Merchant.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public function getPlatformCert(string $serial): ?PublicKey;
* @return PublicKey[]
*/
public function getPlatformCerts(): array;

public function getPlatformCertSerial(): ?string;

public function setPlatformCertSerial(string $serial): static;
}
17 changes: 17 additions & 0 deletions src/Pay/Merchant.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Merchant implements MerchantInterface
*/
protected array $platformCerts = [];

/**
* @var string|null
*/
protected ?string $platCertSerial = null;

/**
* @param array<int|string, string|PublicKey> $platformCerts
*
Expand Down Expand Up @@ -63,6 +68,11 @@ public function getV2SecretKey(): ?string
return $this->v2SecretKey;
}

public function getPlatformCertSerial(): ?string
{
return $this->platCertSerial;
}

public function getPlatformCert(string $serial): ?PublicKey
{
return $this->platformCerts[$serial] ?? null;
Expand Down Expand Up @@ -98,4 +108,11 @@ protected function normalizePlatformCerts(array $platformCerts): array

return $certs;
}

public function setPlatformCertSerial(string $serial): static
{
$this->platCertSerial = $serial;

return $this;
}
}
4 changes: 4 additions & 0 deletions src/Pay/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,19 @@ protected function createSignature(string $message): string
*/
public function createRsaEncrypt(string $plaintext, ?string $serial = null): string
{
$serial = $serial ?? $this->merchant->getPlatformCertSerial();
$platformCerts = $this->merchant->getPlatformCerts();
$platformCert = $serial ? $this->merchant->getPlatformCert($serial) : reset($platformCerts);

if (empty($platformCert)) {
throw new InvalidConfigException('Missing platform certificate.');
}

if (!openssl_public_encrypt($plaintext, $encrypted, $platformCert, OPENSSL_PKCS1_OAEP_PADDING)) {
throw new EncryptionFailureException('Encrypt failed.');
}

$this->merchant->setPlatformCertSerial($serial ?? key($platformCerts));

return base64_encode($encrypted);
}
Expand Down
12 changes: 9 additions & 3 deletions tests/Pay/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,18 @@ public function test_create_rsa_encrypt()
]
);
$utils = new Utils(merchant: $merchant);
$this->assertSame(null,$merchant->getPlatformCertSerial());
$this->assertIsString($utils->createRsaEncrypt('mock-plaintext', 'PUB_KEY_ID_123456'));
$this->assertSame('PUB_KEY_ID_123456',$merchant->getPlatformCertSerial());

$this->assertIsString($utils->createRsaEncrypt('mock-plaintext'));
$this->assertSame('PUB_KEY_ID_123456',$merchant->getPlatformCertSerial());

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


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

}
}

0 comments on commit 6a79ad1

Please sign in to comment.