Certficates.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\v3pay;
  14. use crmeb\exceptions\PayException;
  15. use think\facade\Cache;
  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. $driver = Cache::store('file');
  30. $cacheKey = '_wx_v3' . $this->app['config']['v3_payment']['serial_no'];
  31. if ($driver->has($cacheKey)) {
  32. $res = $driver->get($cacheKey);
  33. if ($key && $res) {
  34. return $res[$key] ?? null;
  35. } else {
  36. return $res;
  37. }
  38. }
  39. $certficates = $this->getCertficates();
  40. $driver->set($cacheKey, $certficates, 3600 * 24 * 30);
  41. if ($key && $certficates) {
  42. return $certficates[$key] ?? null;
  43. }
  44. return $certficates;
  45. }
  46. /**
  47. * get certficates.
  48. *
  49. * @return array
  50. */
  51. public function getCertficates()
  52. {
  53. $response = $this->request('v3/certificates', 'GET', [], false);
  54. if (isset($response['code'])) {
  55. throw new PayException($response['message']);
  56. }
  57. $certificates = $response['data'][0];
  58. $certificates['certificates'] = $this->decrypt($certificates['encrypt_certificate']);
  59. unset($certificates['encrypt_certificate']);
  60. return $certificates;
  61. }
  62. }