网站小程序接入抖音团购核销
第一步:注册抖音开放平台和抖音来客
根据这个文档注册设置
整一个php文件吧,放在extend文件夹下,然后接口调用
php
/***
* 验券准备接口
*/
public function prepareCertificate()
{
$code = $this->request->request('code');
//循环查是哪个平台
$res = Douyin::prepareCertificate('', $code);//抖音平台
if (!$res || $res['extra']['error_code'] != 0) {
//抖音没有查出来
$this->error('该券码无效,请上传真实券码');//, $res['extra']['description']
} else {
if (!isset($res['data']['certificates'][0])) {
$this->error('该券码无效,请上传真实券码');
}
$platform = 3;
$sku_id = $res['data']['certificates'][0]['sku']['sku_id'];
//看询券码属于哪个商品
$goodsitem = Goodsitem::where(['douyin' => $sku_id])->find();
$parties = [
'encrypted_code' => $res['data']['certificates'][0]['encrypted_code'],
'sku_id' => $sku_id,
'platform' => $platform,
'verify_token' => $res['data']['verify_token'],
];
}
if (!$goodsitem) {
$this->error('商品不存在,请重输入兑换');
}
$goods = Goods::where('id', $goodsitem['goods_id'])->find();
if (!$goods) {
$this->error('商品不存在,请重输入兑换');
}
$goods['parties'] = $parties;
$this->success('成功', $goods);
}
/***
* 最终验券接口
*/
public function certificate()
{
//抖音核销
$verifyToken = $this->request->request('verify_token', '');
if (empty($verifyToken) || $verifyToken == '') {
$this->error(__('请选择核销券'));
}
$encryptedCodes = $this->request->request('encryptedCodes', '');
if (empty($encryptedCodes) || $encryptedCodes == '') {
$this->error(__('请选择核销券'));
}
//先查询是否已经核验
$checkVerify = Douyin::checkVerify($encryptedCodes);
if (!$checkVerify || $checkVerify['extra']['error_code'] != 0) {
$this->error($checkVerify['extra']['description']);
} else {
// $this->success('成功', $checkVerify);
$status = $checkVerify['data']['certificate']['status'];
// dump($status);exit;
if ($status != 1) {
$this->error(__('该券已核销'));
}
}
$encryptedCodes = [$encryptedCodes];
$res = Douyin::verifyCoupon($verifyToken, $encryptedCodes);
$this->success('成功', $res);
}
}
php
<?php
namespace fast;
use think\Request;
use think\Config;
use fast\Random;
class Douyin
{
/**
* 验券
*/
const VERIFY = 'https://api.jianjianyouzhi.com/tuangouuserservice/mt/consume';
/**
* 查询已验券信息
*/
const CHECKVERIFY = 'https://api.jianjianyouzhi.com/tuangouuserservice/mt/fetchconsumed';
public function __construct()
{
// $this->Appid = Config::get('douyin.appid');
// $this->AppSecret = Config::get('douyin.appsecret');
// $this->pro_id = Config::get('douyin.pro_id');
$this->Appid = 'a*******a';// 抖音开放平台应用的AppID
$this->AppSecret = 'a********4';// 抖音开放平台应用的AppSecret
$this->pro_id = '7**************************5';// 抖音门店ID
}
/***
* 验券准备接口
*/
public static function prepareCertificate($encrypted_data, $code = null)
{
$access_token = self::getClientToken();
$poi_id = '7***************3';
$url = 'https://open.douyin.com/goodlife/v1/fulfilment/certificate/prepare/?poi_id=' . $poi_id . '&code=' . $code;
$back = doGet($url,$access_token);
$back = json_decode($back, true);
return $back;
}
/***
* 实际验券接口
*/
public static function verifyCoupon($verifyToken,$encryptedCodes)
{
$client_token = self::getClientToken();
$poiId = '***************';
$url = 'https://open.douyin.com/goodlife/v1/fulfilment/certificate/verify/';
$data = [
'verify_token' => $verifyToken,
'poi_id' => $poiId,
'encrypted_codes' => $encryptedCodes
];
$res = doPost($url, $data, $client_token);
$response = json_decode($res, true);
return $response;
}
/***
* 查询券状态
*/
public static function checkVerify($encrypted_data){
$access_token = self::getClientToken();
$poi_id = '***************';
//转义
$encrypted_data = urlencode($encrypted_data);
$url = 'https://open.douyin.com/goodlife/v1/fulfilment/certificate/get/?encrypted_code=' . $encrypted_data;
$back = doGet($url,$access_token);
$back = json_decode($back, true);
return $back;
}
/***
* 获取access_token
*/
public static function getClientToken()
{
$url = 'https://open.douyin.com/oauth/client_token/';
$Appid = '***************';// 抖音开放平台应用的AppID
$AppSecret = '***************';// 抖音开放平台应用的AppSecret
$pro_id = '***************';// 抖音门店ID
$param = [
'client_key' => $Appid,
'client_secret' => $AppSecret,
'grant_type' => 'client_credential'
];
$res = \fast\Http::post($url, $param);
$back = json_decode($res, true);
// dump($back);
// exit;
if ($back['data']['error_code'] == 0) {
return $back['data']['access_token'];
}
return false;
}
}