123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace crmeb\services\wechat\config;
- use crmeb\services\wechat\contract\ConfigHandlerInterface;
- use crmeb\services\wechat\DefaultConfig;
- /**
- * 支付配置
- * Class PaymentConfig
- * @package crmeb\services\wechat\config
- */
- class PaymentConfig implements ConfigHandlerInterface
- {
- /**
- * appid
- * @var string
- */
- protected $appId;
- /**
- * 商户密钥
- * @var string
- */
- protected $mchId;
- /**
- * 小程序商户号
- * @var string
- */
- protected $routineMchId;
- /**
- * API密钥
- * @var string
- */
- protected $key;
- /**
- * 证书cert
- * @var string
- */
- protected $certPath;
- /**
- * 证书key
- * @var string
- */
- protected $keyPath;
- /**
- * 支付异步回调地址
- * @var string
- */
- protected $notifyUrl;
- /**
- * 退款异步通知
- * @var string
- */
- protected $refundUrl;
- /**
- * @var LogCommonConfig
- */
- protected $logConfig;
- /**
- * @var HttpCommonConfig
- */
- protected $httpConfig;
- /**
- * @var bool
- */
- protected $init = false;
- /**
- * PaymentConfig constructor.
- * @param LogCommonConfig $config
- * @param HttpCommonConfig $commonConfig
- */
- public function __construct(LogCommonConfig $config, HttpCommonConfig $commonConfig)
- {
- $this->logConfig = $config;
- $this->httpConfig = $commonConfig;
- }
- protected function init()
- {
- if ($this->init) {
- return;
- }
- $this->init = true;
- $this->appId = $this->appId ?: $this->httpConfig->getConfig(DefaultConfig::OFFICIAL_APPID, '');
- $this->mchId = $this->mchId ?: $this->httpConfig->getConfig(DefaultConfig::PAY_MCHID, '');
- $this->routineMchId = $this->routineMchId ?: $this->httpConfig->getConfig('pay.routine_mchid', '');
- $this->key = $this->key ?: $this->httpConfig->getConfig('pay.key', '');
- $this->certPath = $this->certPath ?: str_replace('//', '/', public_path() . $this->httpConfig->getConfig('pay.client_cert', ''));
- $this->keyPath = $this->keyPath ?: str_replace('//', '/', public_path() . $this->httpConfig->getConfig('pay.client_key', ''));
- $this->notifyUrl = $this->notifyUrl ?: trim($this->httpConfig->getConfig(DefaultConfig::COMMENT_URL)) . DefaultConfig::value('pay.notifyUrl');
- $this->refundUrl = $this->refundUrl ?: trim($this->httpConfig->getConfig(DefaultConfig::COMMENT_URL)) . DefaultConfig::value('pay.refundUrl');
- }
- /**
- * 获取配置
- * @param string $key
- * @param null $default
- * @return mixed
- */
- public function getConfig(string $key, $default = null)
- {
- return $this->httpConfig->getConfig($key, $default);
- }
- /**
- * 设置单个配置
- * @param string $key
- * @param $value
- * @return $this|mixed
- */
- public function set(string $key, $value)
- {
- $this->{$key} = $value;
- return $this;
- }
- /**
- * 获取单个配置
- * @param string|null $key
- * @return array|mixed
- */
- public function get(string $key = null)
- {
- $this->init();
- if ('log' === $key) {
- return $this->logConfig->all();
- }
- if ('http' === $key) {
- return $this->httpConfig->all();
- }
- return $this->{$key};
- }
- /**
- * 全部配置
- * @return array
- */
- public function all(): array
- {
- $this->init();
- return [
- 'app_id' => $this->appId,
- 'mch_id' => $this->mchId,
- 'key' => $this->key,
- 'cert_path' => $this->certPath,
- 'key_path' => $this->keyPath,
- 'notify_url' => $this->notifyUrl,
- 'log' => $this->logConfig->all(),
- 'http' => $this->httpConfig->all()
- ];
- }
- }
|