快手小程序预下单
快手官方文档中只有java的demo,没有php的,只能按照文档中的流程自己写了一个demo,主要注意的是,预下单中的app_id 、access_token 参数并不是一起post过去的,而是拼接到url中 生产sign 是把所有必要签名的参数,去除值为空的参数,然后ASCII 码升序排列处理,具体参考getSign function
控制器代码
<?php
namespace app\controller\api;
use app\BaseController;
use app\model\Banner;
use app\model\Video;
use think\facade\Cache;
use think\facade\Env;
class Ks extends BaseController
{
public function access_token(){
$ks_access_token = Cache::store('redis')->get('ks_access_token');
if(empty($ks_access_token)){
$url = 'https://open.kuaishou.com/oauth2/access_token';
$app_id = env('KS.AppID');
$app_secret = env('KS.AppSecret');
$data = array(
'app_id'=>$app_id,
'app_secret'=>$app_secret,
'grant_type'=>'client_credentials',
);
$result = curl_post($url,$data);
$result = json_decode($result,true);
if($result['result'] == 1){
$ks_access_token = $result['access_token'];
$expires_in = $result['expires_in'];
Cache::store('redis')->set('ks_access_token',$ks_access_token,$expires_in);
}
}
$data = array(
'access_token' => $ks_access_token
);
return $this->success('请求成功',$data);
}
public function create_order(){
$video_id = $this->request->param('video_id/d');
$open_id = $this->request->param('open_id/s');
$access_token = $this->getAccessToken();
$app_id = env('KS.AppID');
$order_no = time();
$open_id = "f19d3e538ff1eeef14bf57247a22c2c0";
$notify_url = 'https://ksvideo.fanshengyun.com';
$total_amount = 1;
$data = array(
'app_id'=>$app_id,
'access_token'=>$access_token,
'out_order_no'=> $order_no,
'open_id' =>$open_id,
'total_amount'=>$total_amount,
'subject' => '商品描述',
'detail' => '商品详情',
'type'=> 1233,
'expire_time'=>172800,
'attach'=>'',
'notify_url'=>$notify_url,
'goods_id'=>1,
'goods_detail_url'=>'',
);
$data['sign'] = $this->getSign($data);
$url = 'https://open.kuaishou.com/openapi/mp/developer/epay/create_order';
$url .= '?app_id='.$app_id.'&access_token='.$access_token;
unset($data['app_id']);
unset($data['access_token']);
$result = curl_post_json($url,$data);
$data = json_decode($result,true);
return $this->success('请求成功',$data);
}
public function getAccessToken(){
$ks_access_token = Cache::store('redis')->get('ks_access_token');
if(empty($ks_access_token)){
$url = 'https://open.kuaishou.com/oauth2/access_token';
$app_id = env('KS.AppID');
$app_secret = env('KS.AppSecret');
$data = array(
'app_id'=>$app_id,
'app_secret'=>$app_secret,
'grant_type'=>'client_credentials',
);
$result = curl_post($url,$data);
$result = json_decode($result,true);
if($result['result'] == 1){
$ks_access_token = $result['access_token'];
$expires_in = $result['expires_in'];
Cache::store('redis')->set('ks_access_token',$ks_access_token,$expires_in);
}
}
$data = array(
'access_token' => $ks_access_token
);
return $ks_access_token;
}
public function getSign($param){
$app_secret = env('KS.AppSecret');
$sign_data = [];
foreach ($param as $k=>$v){
if(!in_array($k,array('access_token','sign')) && !empty($v)){
$sign_data[$k] = $v;
}
}
ksort($sign_data);
$sign_str = '';
$i = 1;
foreach ($sign_data as $k=>$v){
if($i == 1){
$sign_str .= $k.'='.$v;
}else{
$sign_str .= '&'.$k.'='.$v;
}
$i++;
}
$sign_str .= $app_secret;
$sign = MD5($sign_str);
return $sign;
}
}
env配置文件代码
[KS]
AppID = you AppID
AppSecret = you AppSecret
|