Config.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\WechatPay;
  4. use SixShop\System\ExtensionManager;
  5. use WeChatPay\Crypto\Rsa;
  6. /**
  7. * 微信支付配置
  8. * @package SixShop\WechatPay
  9. * @property string $mchid 商户号
  10. * @property string $appid 应用ID
  11. * @property string $serial_no 商户证书序列号
  12. * @property string $public_key_id 微信支付公钥id
  13. * @property string $api_v3_key APIv3密钥
  14. * @property string $notify_url 微信支付回调地址
  15. * @property string $apiclient_cert 微信支付证书 用于服务端API请求报文签名
  16. * @property string $apiclient_key 微信支付证书密钥 用于生成接口请求签名
  17. * @property string $public_key 微信支付公钥
  18. * @property string $platform_no 平台证书序列号
  19. * @property string $platform_cert 平台证书
  20. * @property string $refund_notify_url 退款回调地址
  21. * @property bool $debug 调试模式
  22. *
  23. */
  24. class Config
  25. {
  26. public function __construct(
  27. private readonly ExtensionManager $extensionManager,
  28. private ?array $options = null
  29. )
  30. {
  31. }
  32. public function __get(string $name)
  33. {
  34. if ($this->options === null) {
  35. $this->options = $this->extensionManager->getExtensionConfig('wechatpay');
  36. if (empty($this->options)) {
  37. throw new \RuntimeException('微信支付配置不能为空');
  38. }
  39. }
  40. return match ($name) {
  41. 'debug' => $this->options['debug'] ?? false,
  42. 'apiclient_cert' => $this->getKeyPath($this->options['apiclient_cert'][0]),
  43. 'apiclient_key' => Rsa::from($this->getKeyPath($this->options['apiclient_key'][0])),
  44. 'public_key' => Rsa::from($this->getKeyPath($this->options['public_key'][0]), Rsa::KEY_TYPE_PUBLIC),
  45. 'platform_cert' => isset($this->options['platform_cert'][0]) ? Rsa::from($this->getKeyPath($this->options['platform_cert'][0]), Rsa::KEY_TYPE_PUBLIC) : null,
  46. default => $this->options[$name] ?? null,
  47. };
  48. }
  49. public function getKeyPath(string $name): string
  50. {
  51. return 'file://' . public_path() . $name;
  52. }
  53. }