Certficates.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2023 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\easywechat\v3pay;
  14. use crmeb\exceptions\PayException;
  15. use crmeb\services\CacheService;
  16. /**
  17. * Class Certficates
  18. * @package crmeb\services\easywechat\v3pay
  19. */
  20. trait Certficates
  21. {
  22. /**
  23. * @param string|null $key
  24. * @return array|mixed|null
  25. * @throws \Psr\SimpleCache\InvalidArgumentException
  26. */
  27. public function getCertficatescAttr(string $key = null)
  28. {
  29. $cacheKey = '_wx_v3' . $this->app['config']['v3_payment']['serial_no'];
  30. if (CacheService::has($cacheKey)) {
  31. $res = CacheService::get($cacheKey);
  32. if ($key && $res) {
  33. return $res[$key] ?? null;
  34. } else {
  35. return $res;
  36. }
  37. }
  38. $certficates = $this->getCertficates();
  39. CacheService::set($cacheKey, $certficates, 3600 * 24 * 30);
  40. if ($key && $certficates) {
  41. return $certficates[$key] ?? null;
  42. }
  43. return $certficates;
  44. }
  45. /**
  46. * get certficates.
  47. *
  48. * @return array
  49. */
  50. public function getCertficates()
  51. {
  52. $response = $this->request('v3/certificates', 'GET', [], false);
  53. if (isset($response['code'])) {
  54. throw new PayException($response['message']);
  55. }
  56. $certificates = $response['data'][0];
  57. $certificates['certificates'] = $this->decrypt($certificates['encrypt_certificate']);
  58. unset($certificates['encrypt_certificate']);
  59. return $certificates;
  60. }
  61. }