Kuaishou.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. $res = $this->post(
  59. $url,
  60. $data,
  61. 'json'
  62. );
  63. return [
  64. 'order_id' => $res['order_info']['order_no'],
  65. 'order_token' => $res['order_info']['order_info_token']
  66. ];
  67. }
  68. /**
  69. * @param array $data
  70. * @return string
  71. */
  72. public function getSign(array $data)
  73. {
  74. $filterArray = ['sign','access_token'];
  75. $rList = array();
  76. foreach ($data as $k => $v) {
  77. if (in_array($k, $filterArray))
  78. continue;
  79. $value = trim(strval($v));
  80. $len = strlen($value);
  81. if ($len > 1 && substr($value, 0, 1) == "\"" && substr($value, $len, $len - 1) == "\"")
  82. $value = substr($value, 1, $len - 1);
  83. $value = trim($value);
  84. if ($value == "" || $value == "null")
  85. continue;
  86. array_push($rList, "$k=$value");
  87. }
  88. sort($rList, SORT_STRING);
  89. $str = implode('&', $rList);
  90. $str .= $this->secret;
  91. return md5($str);
  92. }
  93. protected function getAccessToken(): string
  94. {
  95. $res = $this->post($this->API::ACCESS_TOKEN, [
  96. 'app_id' => $this->appId,
  97. 'app_secret' => $this->secret,
  98. 'grant_type' => 'client_credentials',
  99. ]);
  100. if (!isset($res['result']) || $res['result'] != 1) {
  101. throw new \Exception('获取access token 错误');
  102. }
  103. file_put_contents($this->accessTokenFile, json_encode([
  104. 'access_token' => $res['access_token'],
  105. 'expires_at' => Carbon::now()->timestamp + $res['expires_in']
  106. ]));
  107. return $res['access_token'];
  108. }
  109. protected function setAccessFileDir(): void
  110. {
  111. $this->accessTokenDir = storage_path('app/kuaishou');
  112. }
  113. protected function setAccessFilePath(): void
  114. {
  115. $this->accessTokenFile = storage_path('app/kuaishou/kuaishou_access_token.json');
  116. }
  117. public function getNotifySign(array $data)
  118. {
  119. $req = file_get_contents('php://input');
  120. $str = $req.$this->secret;
  121. return md5($str);
  122. }
  123. /**
  124. * @param string $uri
  125. * @param array $data
  126. * @param string $type
  127. * @return array
  128. * @throws \Exception
  129. */
  130. protected function post($uri = '', $data = [], $type = 'urlencoded'): array
  131. {
  132. try {
  133. $client = new Client();
  134. if($type == 'urlencoded'){
  135. $url = $uri.'?'.http_build_query($data);
  136. $options = [
  137. 'verify' => false,
  138. 'headers' => ['Content-Type' => 'x-www-form-urlencoded'],
  139. ];
  140. }else{
  141. $url = $uri;
  142. $options = [
  143. 'verify' => false,
  144. 'headers' => ['Content-Type' => 'application/json'],
  145. 'body' => json_encode($data)
  146. ];
  147. }
  148. $res = $client->post($url, $options);
  149. $stringBody = (string)$res->getBody();
  150. $res = json_decode($stringBody, true);
  151. if(!isset($res['result']) || $res['result'] != 1){
  152. throw new \Exception("请求快手API接口错误,错误码:{$res['result']},错误信息:{$res['error_msg']}");
  153. }
  154. return $res;
  155. } catch (GuzzleException $e) {
  156. \Log::error($e->getMessage());
  157. throw new \Exception($e->getMessage());
  158. }
  159. }
  160. protected function setNoticeUrl(): void
  161. {
  162. $this->noticeUrl = env('APP_URL').'/api/pay/kuaishou/notify';
  163. }
  164. }