BaseUniPlatform.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Helper\UniPlatform;
  3. /**
  4. * Class ByteDance.
  5. *
  6. * @property string $appId
  7. * @property string $slat
  8. * @property string $secret
  9. * @property string $token
  10. * @property string $accessTokenDir
  11. * @property string $accessTokenFile
  12. * @property string $accessToken
  13. * @property string $noticeUrl
  14. * @property string $validTimestamp
  15. * @property BaseAPI $API
  16. */
  17. abstract class BaseUniPlatform
  18. {
  19. protected $appId;
  20. protected $mchId;
  21. protected $mchKey;
  22. protected $secret;
  23. protected $slat;
  24. protected $token;
  25. protected $accessTokenDir;
  26. protected $accessTokenFile;
  27. protected $accessToken;
  28. protected $noticeUrl;
  29. // 订单过期时间(秒);
  30. protected $validTimestamp = 24 * 60 * 60;
  31. protected $API;
  32. /**
  33. * @return $this
  34. */
  35. public function factory(array $config = [])
  36. {
  37. $this->appId = $config['app_id'];
  38. $this->secret = $config['app_secret'];
  39. $this->slat = $config['slat'] ?? null;
  40. $this->token = $config['token'] ?? null;
  41. $this->mchId = $config['mch_id'] ?? null;
  42. $this->mchKey = $config['mch_key'] ?? null;
  43. $this->setAccessFileDir();
  44. $this->setAccessFilePath();
  45. $this->setNoticeUrl();
  46. $this->accessToken = $this->checkAccessToken();
  47. return $this;
  48. }
  49. /**
  50. * @throws \Exception
  51. */
  52. public function decryptData(string $sessionKey, string $iv, string $encrypted): array
  53. {
  54. $decrypted = openssl_decrypt(
  55. base64_decode($encrypted, true),
  56. 'AES-128-CBC',
  57. base64_decode($sessionKey),
  58. OPENSSL_RAW_DATA,
  59. base64_decode($iv)
  60. );
  61. $decrypted = json_decode($decrypted, true);
  62. if (empty($decrypted)) {
  63. throw new \Exception('解密数据错误');
  64. }
  65. return $decrypted;
  66. }
  67. /**
  68. * 校验access token 是否过期
  69. */
  70. protected function checkAccessToken()
  71. {
  72. try {
  73. $dir = $this->accessTokenDir;
  74. if (!is_dir($dir)) {
  75. mkdir($dir, 0755);
  76. }
  77. if (!is_file($this->accessTokenFile)) {
  78. touch($this->accessTokenFile);
  79. }
  80. $accessToken = file_get_contents($this->accessTokenFile);
  81. $accessToken = $accessToken ? json_decode($accessToken, true) : null;
  82. // 改为 还有5分钟过期就刷新,不然会出现没有过期但是抖音会返回过期的信息
  83. if (empty($accessToken) || $accessToken['expires_at'] < time() + 300) {
  84. $accessToken = $this->getAccessToken();
  85. } else {
  86. $accessToken = $accessToken['access_token'];
  87. }
  88. return $accessToken;
  89. } catch (\Exception $e) {
  90. }
  91. }
  92. /**
  93. * 登陆.
  94. */
  95. abstract protected function login($code): array;
  96. /**
  97. * 接口请求
  98. *
  99. * @param string $uri
  100. * @param array $data
  101. */
  102. abstract protected function post($uri = '', $data = []): array;
  103. /**
  104. * 获取Access token.
  105. */
  106. abstract protected function getAccessToken(): string;
  107. /**
  108. * 创建支付订单.
  109. */
  110. abstract public function createOrder($outOrderNo, $totalAmount, $openId): array;
  111. /**
  112. * 设置 access token 目录.
  113. */
  114. abstract protected function setAccessFileDir(): void;
  115. /**
  116. * 设置 access token路径.
  117. */
  118. abstract protected function setAccessFilePath(): void;
  119. abstract protected function setNoticeUrl(): void;
  120. }