Kuaishou.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Helper;
  3. use App\Helper\UniPlatform\BaseAPI;
  4. use App\Helper\UniPlatform\BaseUniPlatform;
  5. use Carbon\Carbon;
  6. use GuzzleHttp\Client;
  7. use GuzzleHttp\Exception\GuzzleException;
  8. /**
  9. * Class Kuaishou
  10. *
  11. * @package App\Helper
  12. * @property-read string $appId
  13. * @property-read string $slat
  14. * @property-read string $secret
  15. * @property-read string $token
  16. * @property-read string $accessTokenFile
  17. * @property-read string $accessToken
  18. * @property-read string $noticeUrl
  19. * @property-read string $validTimestamp
  20. */
  21. class Kuaishou extends BaseUniPlatform
  22. {
  23. public function __construct(BaseAPI $api)
  24. {
  25. $this->API = $api;
  26. }
  27. /**
  28. * @param string $code
  29. * @return array|mixed
  30. * @throws \Exception
  31. */
  32. public function login($code = ''): array
  33. {
  34. return $this->post($this->API::LOGIN, [
  35. 'app_id' => $this->appId,
  36. 'app_secret' => $this->secret,
  37. 'js_code' => $code,
  38. ]);
  39. }
  40. public function createOrder($outOrderNo, $totalAmount, $openId): array
  41. {
  42. $data = [
  43. 'app_id' => $this->appId,
  44. 'out_order_no' => $outOrderNo,
  45. 'open_id' => $openId,
  46. 'total_amount' => intval($totalAmount * 100),
  47. 'subject' => "订单号:".$outOrderNo,
  48. 'detail' => '快手担保支付',
  49. 'type' => 1233, // @url https://mp.kuaishou.com/docs/operate/platformAgreement/epayServiceCharge.html
  50. 'expire_time' => $this->validTimestamp,
  51. 'notify_url' => $this->noticeUrl
  52. ];
  53. $data['sign'] = $this->getSign($data);
  54. $url = $this->API::CREATE_ORDER.'?'.http_build_query([
  55. 'app_id' => $this->appId,
  56. 'access_token' => $this->accessToken,
  57. ]);
  58. return $this->post(
  59. $url,
  60. $data,
  61. 'json'
  62. );
  63. }
  64. /**
  65. * @param array $data
  66. * @return string
  67. */
  68. public function getSign(array $data)
  69. {
  70. $filterArray = ['sign','access_token'];
  71. $rList = array();
  72. foreach ($data as $k => $v) {
  73. if (in_array($k, $filterArray))
  74. continue;
  75. $value = trim(strval($v));
  76. $len = strlen($value);
  77. if ($len > 1 && substr($value, 0, 1) == "\"" && substr($value, $len, $len - 1) == "\"")
  78. $value = substr($value, 1, $len - 1);
  79. $value = trim($value);
  80. if ($value == "" || $value == "null")
  81. continue;
  82. array_push($rList, "$k=$value");
  83. }
  84. sort($rList, SORT_STRING);
  85. $str = implode('&', $rList);
  86. $str .= $this->secret;
  87. return md5($str);
  88. }
  89. protected function getAccessToken(): string
  90. {
  91. $res = $this->post($this->API::ACCESS_TOKEN, [
  92. 'app_id' => $this->appId,
  93. 'app_secret' => $this->secret,
  94. 'grant_type' => 'client_credentials',
  95. ]);
  96. if (!isset($res['result']) || $res['result'] != 1) {
  97. throw new \Exception('获取access token 错误');
  98. }
  99. file_put_contents($this->accessTokenFile, json_encode([
  100. 'access_token' => $res['access_token'],
  101. 'expires_at' => Carbon::now()->timestamp + $res['expires_in']
  102. ]));
  103. return $res['access_token'];
  104. }
  105. protected function setAccessFileDir(): void
  106. {
  107. $this->accessTokenDir = storage_path('app/kuaishou');
  108. }
  109. protected function setAccessFilePath(): void
  110. {
  111. $this->accessTokenFile = storage_path('app/kuaishou/kuaishou_access_token.json');
  112. }
  113. public function getNotifySign(array $data)
  114. {
  115. $str = json_encode($data,JSON_UNESCAPED_UNICODE).$this->secret;
  116. return md5($str);
  117. }
  118. /**
  119. * @param string $uri
  120. * @param array $data
  121. * @param string $type
  122. * @return array
  123. * @throws \Exception
  124. */
  125. protected function post($uri = '', $data = [], $type = 'urlencoded'): array
  126. {
  127. try {
  128. $client = new Client();
  129. if($type == 'urlencoded'){
  130. $url = $uri.'?'.http_build_query($data);
  131. $options = [
  132. 'verify' => false,
  133. 'headers' => ['Content-Type' => 'x-www-form-urlencoded'],
  134. ];
  135. }else{
  136. $url = $uri;
  137. $options = [
  138. 'verify' => false,
  139. 'headers' => ['Content-Type' => 'application/json'],
  140. 'body' => json_encode($data)
  141. ];
  142. }
  143. $res = $client->post($url, $options);
  144. $stringBody = (string)$res->getBody();
  145. $res = json_decode($stringBody, true);
  146. if(!isset($res['result']) || $res['result'] != 1){
  147. throw new \Exception("请求快手API接口错误,错误码:{$res['result']},错误信息:{$res['error_msg']}");
  148. }
  149. return $res;
  150. } catch (GuzzleException $e) {
  151. \Log::error($e->getMessage());
  152. throw new \Exception($e->getMessage());
  153. }
  154. }
  155. protected function setNoticeUrl(): void
  156. {
  157. $this->noticeUrl = env('APP_URL').'/api/pay/kuaishou/notify';
  158. }
  159. }