mirror of
https://gitee.com/zoujingli/WeChatDeveloper.git
synced 2026-09-04 23:12:09 +08:00
- 统一根 Client 工厂为 wechatXXX/alipayXXX,保持与配置语义一致。 - 规范微信 platform、wxapp、service、payment 等缓存逻辑键命名。 - 补齐 Config、Contract、Exception 与缓存实现的中文 PHPDoc,提升开源项目可读性。
77 lines
2.5 KiB
PHP
77 lines
2.5 KiB
PHP
<?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'] ?? ''),
|
||
);
|
||
}
|
||
}
|