BaseUniPlatform.php 3.9 KB

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