上一篇文章我们说到项目里需要用到美团api接口来实现卡券核销的功能。 那么这篇文章就讲一下,如何实现抖音团购券码的核销功能。 近期项目需要,抖音团购的券码核销,话不多说,直接上代码吧! 凑合着用吧,反
上一篇文章我们说到项目里需要用到美团api接口来实现卡券核销的功能。
那么这篇文章就讲一下,如何实现抖音团购券码的核销功能。
近期项目需要,抖音团购的券码核销,话不多说,直接上代码吧!
凑合着用吧,反正能销,具体业务需要自己写
PHPclass 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); } 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; } 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; } public function getClientToken(){ //正式环境:https://open.douyin.com/oauth/client_token/ //沙盒环境:https://open-sandbox.douyin.com/oauth/client_token/ $url = 'https://open.douyin.com/oauth/client_token/'; $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']; } } 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; } 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); }}
以上的代码是抖音卡券核销的功能。有需要的小伙伴记得关注、收藏。
来源地址:https://blog.csdn.net/weixin_47736740/article/details/132087789
--结束END--
本文标题: PHP Thinkphp5.0实现抖音团购券码核销
本文链接: https://lsjlt.com/news/392717.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0