PaymentConfig.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\wechat\config;
  12. use crmeb\services\wechat\contract\ConfigHandlerInterface;
  13. use crmeb\services\wechat\DefaultConfig;
  14. /**
  15. * 支付配置
  16. * Class PaymentConfig
  17. * @package crmeb\services\wechat\config
  18. */
  19. class PaymentConfig implements ConfigHandlerInterface
  20. {
  21. /**
  22. * appid
  23. * @var string
  24. */
  25. protected $appId;
  26. /**
  27. * 商户密钥
  28. * @var string
  29. */
  30. protected $mchId;
  31. /**
  32. * 小程序商户号
  33. * @var string
  34. */
  35. protected $routineMchId;
  36. /**
  37. * API密钥
  38. * @var string
  39. */
  40. protected $key;
  41. /**
  42. * 证书cert
  43. * @var string
  44. */
  45. protected $certPath;
  46. /**
  47. * 证书key
  48. * @var string
  49. */
  50. protected $keyPath;
  51. /**
  52. * 支付异步回调地址
  53. * @var string
  54. */
  55. protected $notifyUrl;
  56. /**
  57. * 退款异步通知
  58. * @var string
  59. */
  60. protected $refundUrl;
  61. /**
  62. * @var LogCommonConfig
  63. */
  64. protected $logConfig;
  65. /**
  66. * @var HttpCommonConfig
  67. */
  68. protected $httpConfig;
  69. /**
  70. * @var bool
  71. */
  72. protected $init = false;
  73. /**
  74. * PaymentConfig constructor.
  75. * @param LogCommonConfig $config
  76. * @param HttpCommonConfig $commonConfig
  77. */
  78. public function __construct(LogCommonConfig $config, HttpCommonConfig $commonConfig)
  79. {
  80. $this->logConfig = $config;
  81. $this->httpConfig = $commonConfig;
  82. }
  83. protected function init()
  84. {
  85. if ($this->init) {
  86. return;
  87. }
  88. $this->init = true;
  89. $this->appId = $this->appId ?: $this->httpConfig->getConfig(DefaultConfig::OFFICIAL_APPID, '');
  90. $this->mchId = $this->mchId ?: $this->httpConfig->getConfig(DefaultConfig::PAY_MCHID, '');
  91. $this->routineMchId = $this->routineMchId ?: $this->httpConfig->getConfig('pay.routine_mchid', '');
  92. $this->key = $this->key ?: $this->httpConfig->getConfig('pay.key', '');
  93. $this->certPath = $this->certPath ?: str_replace('//', '/', public_path() . $this->httpConfig->getConfig('pay.client_cert', ''));
  94. $this->keyPath = $this->keyPath ?: str_replace('//', '/', public_path() . $this->httpConfig->getConfig('pay.client_key', ''));
  95. $this->notifyUrl = $this->notifyUrl ?: trim($this->httpConfig->getConfig(DefaultConfig::COMMENT_URL)) . DefaultConfig::value('pay.notifyUrl');
  96. $this->refundUrl = $this->refundUrl ?: trim($this->httpConfig->getConfig(DefaultConfig::COMMENT_URL)) . DefaultConfig::value('pay.refundUrl');
  97. }
  98. /**
  99. * 获取配置
  100. * @param string $key
  101. * @param null $default
  102. * @return mixed
  103. */
  104. public function getConfig(string $key, $default = null)
  105. {
  106. return $this->httpConfig->getConfig($key, $default);
  107. }
  108. /**
  109. * 设置单个配置
  110. * @param string $key
  111. * @param $value
  112. * @return $this|mixed
  113. */
  114. public function set(string $key, $value)
  115. {
  116. $this->{$key} = $value;
  117. return $this;
  118. }
  119. /**
  120. * 获取单个配置
  121. * @param string|null $key
  122. * @return array|mixed
  123. */
  124. public function get(string $key = null)
  125. {
  126. $this->init();
  127. if ('log' === $key) {
  128. return $this->logConfig->all();
  129. }
  130. if ('http' === $key) {
  131. return $this->httpConfig->all();
  132. }
  133. return $this->{$key};
  134. }
  135. /**
  136. * 全部配置
  137. * @return array
  138. */
  139. public function all(): array
  140. {
  141. $this->init();
  142. return [
  143. 'app_id' => $this->appId,
  144. 'mch_id' => $this->mchId,
  145. 'key' => $this->key,
  146. 'cert_path' => $this->certPath,
  147. 'key_path' => $this->keyPath,
  148. 'notify_url' => $this->notifyUrl,
  149. 'log' => $this->logConfig->all(),
  150. 'http' => $this->httpConfig->all()
  151. ];
  152. }
  153. }