Cert.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WePayV3;
  16. use WeChat\Exceptions\InvalidResponseException;
  17. use WePayV3\Contracts\BasicWePay;
  18. use WePayV3\Contracts\DecryptAes;
  19. /**
  20. * 平台证书管理
  21. * Class Cert
  22. * @package WePayV3
  23. */
  24. class Cert extends BasicWePay
  25. {
  26. /**
  27. * 自动配置平台证书
  28. * @var bool
  29. */
  30. protected $autoCert = false;
  31. /**
  32. * 商户平台下载证书
  33. * @return void
  34. * @throws \WeChat\Exceptions\InvalidResponseException
  35. */
  36. public function download()
  37. {
  38. try {
  39. $aes = new DecryptAes($this->config['mch_v3_key']);
  40. $result = $this->doRequest('GET', '/v3/certificates');
  41. $certs = [];
  42. foreach ($result['data'] as $vo) {
  43. $certs[$vo['serial_no']] = [
  44. 'expire' => strtotime($vo['expire_time']),
  45. 'content' => $aes->decryptToString(
  46. $vo['encrypt_certificate']['associated_data'],
  47. $vo['encrypt_certificate']['nonce'],
  48. $vo['encrypt_certificate']['ciphertext']
  49. )
  50. ];
  51. }
  52. $this->tmpFile("{$this->config['mch_id']}_certs", $certs);
  53. } catch (\Exception $exception) {
  54. throw new InvalidResponseException($exception->getMessage(), $exception->getCode());
  55. }
  56. }
  57. }