BaseUniPlatform.php 3.7 KB

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