V3PaymentConfig.php 5.2 KB

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