WeChatDeveloper/src/Config/WechatPaymentConfig.php
Anyon 05cb47d3fd refactor(core): 统一核心配置缓存命名与注释
- 统一根 Client 工厂为 wechatXXX/alipayXXX,保持与配置语义一致。

- 规范微信 platform、wxapp、service、payment 等缓存逻辑键命名。

- 补齐 Config、Contract、Exception 与缓存实现的中文 PHPDoc,提升开源项目可读性。
2026-05-08 00:29:04 +08:00

77 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* 微信支付 APIv3 商户配置对象。
*/
namespace We\Config;
use We\Contract\ConfigInterface;
use We\Exception\WechatException;
/**
* 微信支付 APIv3 商户配置。
*
* 商户号、商户 API 证书序列号和商户私钥用于生成 APIv3 请求签名APIv3 密钥用于回调资源解密;微信支付平台证书或平台公钥用于通知验签。
*/
final class WechatPaymentConfig implements ConfigInterface
{
/**
* 创建微信支付 APIv3 配置并执行必填项校验。
*/
public function __construct(
public string $appid,
public string $mchId,
public string $apiV3Key,
public string $merchantSerial,
public string $merchantPrivateKey,
/** 微信支付平台证书 PEM未配置 platformPublicKey 时用于回调验签。 */
public string $platformCertificate = '',
/** 微信支付平台公钥 PEM优先用于回调验签。 */
public string $platformPublicKey = '',
/** 微信支付平台证书/公钥序列号;非空时会校验回调头 Wechatpay-Serial。 */
public string $platformSerial = '',
) {
$this->validate();
}
/**
* 校验微信支付 appid、商户号、APIv3 密钥、商户证书序列号和商户私钥。
*/
public function validate(): void
{
foreach ([
'appid' => $this->appid,
'mchId' => $this->mchId,
'apiV3Key' => $this->apiV3Key,
'merchantSerial' => $this->merchantSerial,
'merchantPrivateKey' => $this->merchantPrivateKey,
] as $name => $value) {
if (trim($value) === '') {
throw new WechatException($name . ' 不能为空');
}
}
}
/**
* 从数组创建微信支付 APIv3 商户配置。
*
* @param array<string,mixed> $data
*/
public static function fromArray(array $data): static
{
return new static(
(string)($data['appid'] ?? ''),
(string)($data['mch_id'] ?? $data['mchid'] ?? ''),
(string)($data['api_v3_key'] ?? $data['mch_v3_key'] ?? ''),
(string)($data['merchant_serial'] ?? $data['cert_serial'] ?? ''),
(string)($data['merchant_private_key'] ?? $data['cert_private'] ?? ''),
(string)($data['platform_certificate'] ?? $data['cert_public'] ?? ''),
(string)($data['platform_public_key'] ?? ''),
(string)($data['platform_serial'] ?? ''),
);
}
}