Client.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\easywechat\certficates;
  12. use crmeb\exceptions\WechatException;
  13. use crmeb\services\easywechat\BaseClient;
  14. use EasyWeChat\Core\AbstractAPI;
  15. use think\exception\InvalidArgumentException;
  16. use think\facade\Cache;
  17. class Client extends BaseClient
  18. {
  19. public function get()
  20. {
  21. //自动分账
  22. if ($this->isService) {
  23. $pay_routine_public_id = $pay_routine_public_key = '';
  24. } else { //普通支付
  25. $pay_routine_public_id = $this->app['config']['payment']['pay_weixin_public_id'] ?? '';
  26. $pay_routine_public_key = $this->app['config']['payment']['pay_weixin_public_key'] ?? '';
  27. }
  28. if ($pay_routine_public_key && $pay_routine_public_id) {
  29. $certficates = [
  30. 'serial_no' => $pay_routine_public_id,
  31. 'certificates' => $pay_routine_public_key
  32. ];
  33. } else {
  34. $driver = Cache::store('file');
  35. $cacheKey = '_wx_v3' . ($this->isService ? $this->app['config']['service_payment']['serial_no'] : $this->app['config']['payment']['serial_no']);
  36. if ($driver->has($cacheKey)) {
  37. return $driver->get($cacheKey);
  38. }
  39. $certficates = $this->getCertficates();
  40. $driver->set($cacheKey, $certficates, 3600 * 24 * 30);
  41. }
  42. return $certficates;
  43. }
  44. /**
  45. * get certficates.
  46. *
  47. * @return array
  48. */
  49. public function getCertficates()
  50. {
  51. $response = $this->request('/v3/certificates', 'GET', [], false);
  52. if (isset($response['code'])) throw new WechatException($response['message']);
  53. $certificates = $response['data'][0];
  54. $certificates['certificates'] = $this->decrypt($certificates['encrypt_certificate']);
  55. unset($certificates['encrypt_certificate']);
  56. return $certificates;
  57. }
  58. }