快手小程序预下单
快手官方文档中只有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
{
/**
* 获取access_token
* @param
* @date 2022/2/22
*/
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);
}
/**
* 预下单
* @param
* @date 2022/2/22
*/
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, //小程序 AppID
'access_token'=>$access_token, //拥有小程序支付权限的access token
'out_order_no'=> $order_no, //商户系统内部订单号
'open_id' =>$open_id, //快手用户在当前小程序的open_id
'total_amount'=>$total_amount, //用户支付金额,单位为[分]。不允许传非整数的数值
'subject' => '商品描述', //商品描述。注:1汉字=2字符。
'detail' => '商品详情', //商品详情。注:1汉字=2字符。
'type'=> 1233, //商品类型,不同商品类目的编号见 担保支付商品类目编号
'expire_time'=>172800, //订单过期时间,单位秒,300s - 172800s
'attach'=>'', //开发者自定义字段,回调原样回传.
'notify_url'=>$notify_url, //通知URL必须为直接可访问的URL,不允许携带查询串。
'goods_id'=>1, // 下单商品id,长度限制256个英文字符,1个汉字=2个英文字符;
'goods_detail_url'=>'', // 订单详情页跳转path。长度限制500个英文字符,1个汉字=2个英文字符; 示例值:/page/index/anima
);
$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);
}
/**
* 获取access_token
* @param
* @date 2022/2/22
*/
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;
}
/**
* 获取access_token
* @param
* @date 2022/2/22
*/
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