上一篇文章我们说到项目里需要用到美团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://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'];
}
}
/** 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);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
以上的代码是抖音卡券核销的功能。有需要的小伙伴记得关注、收藏。