php网站小程序接入抖音团购核销

网站小程序接入抖音团购核销

第一步:注册抖音开放平台和抖音来客

根据这个文档注册设置

https://developer.open-douyin.com/docs/resource/zh-CN/local-life/connect/developer/self-developed-merchant-guide

整一个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;
    }
}
相关推荐
杰建云1672 小时前
2026年第三方平台制作微信小程序多少钱?
微信小程序·小程序·小程序制作
weikecms2 小时前
外卖霸王餐CPS怎么对接接口
小程序·微客云
莫逸风2 小时前
【java-core-collections】B+ 树深度解析
android·java·开发语言
我命由我123452 小时前
Android 开发问题:无法从存储库 “D:\keys\MyNotifications.jks“ 中读取密钥 MyNotifications.
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
AI玫瑰助手2 小时前
Python基础:字符串的切片操作(含正向反向索引)
android·开发语言·python
落羽的落羽3 小时前
【算法札记】练习 | Week2
android·linux·服务器·c++·python·算法·机器学习
ROLL.73 小时前
同步与异步
android·java
BoomHe3 小时前
Android (AAOS) 13 编译中间产物(Wifi Jar)
android·android studio·android jetpack
niucloud-admin3 小时前
PHP SAAS 框架常见问题——安装应用时提示 “未找到 admin 源码所在目录”
php