Application.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace EasyWeChat\MicroMerchant;
  11. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  12. use EasyWeChat\Kernel\ServiceContainer;
  13. use EasyWeChat\Kernel\Support;
  14. use EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException;
  15. /**
  16. * Class Application.
  17. *
  18. * @author liuml <liumenglei0211@gmail.com>
  19. *
  20. * @property \EasyWeChat\MicroMerchant\Certficates\Client $certficates
  21. * @property \EasyWeChat\MicroMerchant\Material\Client $material
  22. * @property \EasyWeChat\MicroMerchant\MerchantConfig\Client $merchantConfig
  23. * @property \EasyWeChat\MicroMerchant\Withdraw\Client $withdraw
  24. * @property \EasyWeChat\MicroMerchant\Media\Client $media
  25. *
  26. * @method mixed submitApplication(array $params)
  27. * @method mixed getStatus(string $applymentId, string $businessCode = '')
  28. * @method mixed upgrade(array $params)
  29. * @method mixed getUpgradeStatus(string $subMchId = '')
  30. */
  31. class Application extends ServiceContainer
  32. {
  33. /**
  34. * @var array
  35. */
  36. protected $providers = [
  37. // Base services
  38. Base\ServiceProvider::class,
  39. Certficates\ServiceProvider::class,
  40. MerchantConfig\ServiceProvider::class,
  41. Material\ServiceProvider::class,
  42. Withdraw\ServiceProvider::class,
  43. Media\ServiceProvider::class,
  44. ];
  45. /**
  46. * @var array
  47. */
  48. protected $defaultConfig = [
  49. 'http' => [
  50. 'base_uri' => 'https://api.mch.weixin.qq.com/',
  51. ],
  52. 'log' => [
  53. 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
  54. 'channels' => [
  55. // 测试环境
  56. 'dev' => [
  57. 'driver' => 'single',
  58. 'path' => '/tmp/easywechat.log',
  59. 'level' => 'debug',
  60. ],
  61. // 生产环境
  62. 'prod' => [
  63. 'driver' => 'daily',
  64. 'path' => '/tmp/easywechat.log',
  65. 'level' => 'info',
  66. ],
  67. ],
  68. ],
  69. ];
  70. /**
  71. * @return string
  72. *
  73. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  74. */
  75. public function getKey()
  76. {
  77. $key = $this['config']->key;
  78. if (empty($key)) {
  79. throw new InvalidArgumentException('config key connot be empty.');
  80. }
  81. if (32 !== strlen($key)) {
  82. throw new InvalidArgumentException(sprintf("'%s' should be 32 chars length.", $key));
  83. }
  84. return $key;
  85. }
  86. /**
  87. * set sub-mch-id and appid.
  88. *
  89. * @param string $subMchId Identification Number of Small and Micro Businessmen Reported by Service Providers
  90. * @param string $appId Public Account ID of Service Provider
  91. *
  92. * @return $this
  93. */
  94. public function setSubMchId(string $subMchId, string $appId = '')
  95. {
  96. $this['config']->set('sub_mch_id', $subMchId);
  97. if ($appId) {
  98. $this['config']->set('appid', $appId);
  99. }
  100. return $this;
  101. }
  102. /**
  103. * setCertificate.
  104. *
  105. * @return $this
  106. */
  107. public function setCertificate(string $certificate, string $serialNo)
  108. {
  109. $this['config']->set('certificate', $certificate);
  110. $this['config']->set('serial_no', $serialNo);
  111. return $this;
  112. }
  113. /**
  114. * Returning true indicates that the verification is successful,
  115. * returning false indicates that the signature field does not exist or is empty,
  116. * and if the signature verification is wrong, the InvalidSignException will be thrown directly.
  117. *
  118. * @return bool
  119. *
  120. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  121. * @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException
  122. */
  123. public function verifySignature(array $data)
  124. {
  125. if (!isset($data['sign']) || empty($data['sign'])) {
  126. return false;
  127. }
  128. $sign = $data['sign'];
  129. unset($data['sign']);
  130. $signType = strlen($sign) > 32 ? 'HMAC-SHA256' : 'MD5';
  131. $secretKey = $this->getKey();
  132. $encryptMethod = Support\get_encrypt_method($signType, $secretKey);
  133. if (Support\generate_sign($data, $secretKey, $encryptMethod) === $sign) {
  134. return true;
  135. }
  136. throw new InvalidSignException('return value signature verification error');
  137. }
  138. /**
  139. * @param string $name
  140. * @param array $arguments
  141. *
  142. * @return mixed
  143. */
  144. public function __call($name, $arguments)
  145. {
  146. return call_user_func_array([$this['base'], $name], $arguments);
  147. }
  148. }