上一篇文章我们说到项目里需要用到美团API接口来实现卡券核销的功能。
那么这篇文章就讲一下,如何实现抖音团购券码的核销功能。
近期项目需要,抖音团购的券码核销,话不多说,直接上代码吧!
凑合着用吧,反正能销,具体业务需要自己写
<?php/*
* 抖音店铺团购券核销
* author: Mr. YuYang
*
*/
class DouyinTest
{
protected $Appid = '';//appid
protected $AppSecret = '';//秘钥
protected $pro_id = ''; // 门店id /*
* 这里是核销的方法
*/
public function DouyinAfterVerification(){
$encrypted_data = '100000000000000'; // code 格式 100000000000000
$url = 'https://open.douyin.com/goodlife/v1/fulfilment/certificate/prepare/';
$client_token = $this->getClientToken();
if(!$client_token){
die('token 获取失败');
}
$res = $this->prepareCertificate($client_token,'',$encrypted_data);
$v = $res['data']['verify_token'];
$code = $res['data']['certificates'][0]['encrypted_code'];
$hexiao = $this->verifyCoupon($v,$client_token,$this->pro_id,[$code]);
//核销成功后的所有信息都在这里 打印自己看就行
//所有的逻辑根据自己项目需要来自己写。
print_r($hexiao);
} /**
* @param $access_token
* @param $encrypted_data
* @param null $code
* @return mixed
* 通过code 核销券码准备
*/
public function prepareCertificate($access_token, $encrypted_data, $code = null) {
$url = 'https://open.douyin.com/goodlife/v1/fulfilment/certificate/prepare/'; $params = [];
if (!empty($encrypted_data)) {
$params['encrypted_data'] = urlencode($encrypted_data);
} elseif (!empty($code)) {
$params['code'] = $code;
} else {
throw new Exception("encrypted_data 和 code 参数不能同时为空");
}
$back = $this->douyinRequest($url,$params,$access_token,0);
return $back;
} /**
* @param $verifyToken
* @param $client_token
* @param $poiId
* @param $encryptedCodes
* @param null $codes
* @param null $orderId
* @param null $codeWithTimeList
* @param null $voucher
* @return mixed
*/
public function verifyCoupon($verifyToken,$client_token, $poiId, $encryptedCodes, $codes = null, $orderId = null, $codeWithTimeList = null, $voucher = null) {
$url = 'https://open.douyin.com/goodlife/v1/fulfilment/certificate/verify/';
$data = array(
'verify_token' => $verifyToken,
'poi_id' => $poiId,
'encrypted_codes' => $encryptedCodes,
'codes' => $codes,
); $response = $this->douyinRequest($url,$data,$client_token);
return $response;
} /**
* @return mixed
* 获取token
*/
public function getClientToken(){
//正式环境:https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/updatecrm/php/20231004/span
//沙盒环境:https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/updatecrm/php/20231004/span
$url = 'https://baitexiaoyuan.oss-cn-zhangjiakou.aliyuncs.com/updatecrm/php/20231004/& class="token punctuation">;
$param = [
'client_key' => $this->Appid,
'client_secret' => $this->AppSecret,
'grant_type' => 'client_credential'
]; $res = $this->requestUrl($url,$param);
$back = json_decode($res,true);
if($back['data']['error_code'] == 0){
return $back['data']['access_token'];
} } /** curl 提交*/
public function requestUrl($url,$data=null,$https=true,$method='post'){
//1.初始化url
$ch = curl_init($url);
//2.设置相关的参数
//字符串不直接输出,进行一个变量的存储
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//判断是否为https请求
if($https === true){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
//判断是否为post请求
if($method == 'post'){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:multipart/form-data'));
//3.发送请求
$str = curl_exec($ch);
//4.关闭连接
curl_close($ch);
//6.返回请求到的结果
return $str;
}
/**
* 抖音请求方法
* @param $url
* @param $client_token
* @param array $data
* @param int $is_post
* @return mixed
*/
public function douyinRequest($url,$data = [],$client_token = '',$is_post = 1){
$headers = [
'Content-Type: application/json',
'access-token: ' . $client_token,
];
$curl = curl_init();
if($is_post){
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => $headers,
));
}else{
$data = http_build_query($data);
curl_setopt_array($curl, [
CURLOPT_URL => "$url?$data",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
]);
}
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response,true);
}
}
以上的代码是抖音卡券核销的功能。有需要的小伙伴记得关注、收藏。
发表评论 取消回复