Client.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\Payment\Sandbox;
  11. use EasyWeChat\Kernel\Traits\InteractsWithCache;
  12. use EasyWeChat\Payment\Kernel\BaseClient;
  13. use EasyWeChat\Payment\Kernel\Exceptions\SandboxException;
  14. /**
  15. * Class Client.
  16. *
  17. * @author mingyoung <mingyoungcheung@gmail.com>
  18. */
  19. class Client extends BaseClient
  20. {
  21. use InteractsWithCache;
  22. /**
  23. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  24. * @throws \EasyWeChat\Payment\Kernel\Exceptions\SandboxException
  25. * @throws \Psr\SimpleCache\InvalidArgumentException
  26. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  27. * @throws \GuzzleHttp\Exception\GuzzleException
  28. */
  29. public function getKey(): string
  30. {
  31. if ($cache = $this->getCache()->get($this->getCacheKey())) {
  32. return $cache;
  33. }
  34. $response = $this->requestArray('sandboxnew/pay/getsignkey');
  35. if ('SUCCESS' === $response['return_code']) {
  36. $this->getCache()->set($this->getCacheKey(), $key = $response['sandbox_signkey'], 24 * 3600);
  37. return $key;
  38. }
  39. throw new SandboxException($response['retmsg'] ?? $response['return_msg']);
  40. }
  41. protected function getCacheKey(): string
  42. {
  43. return 'easywechat.payment.sandbox.'.md5($this->app['config']->app_id.$this->app['config']['mch_id']);
  44. }
  45. }