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;
    }
}
相关推荐
AC赳赳老秦2 小时前
司法公开数据采集应用:OpenClaw 抓取裁判文书公开信息,批量整理同类参考案例
java·大数据·python·数据挖掘·数据分析·php·openclaw
通玄2 小时前
Jetpack Compose 入门系列(十):Paging 3 分页加载
android
hunterandroid2 小时前
DataStore 工程化实践:迁移、并发更新与异常恢复
android·前端
EasyGBS4 小时前
ONVIF设备接入国标GB28181视频平台EasyGBS:不用国标也能一键拉流!
服务器·php·音视频
柒星栈4 小时前
PHP 源码怎么加密防破解?三套方案实战指南
开发语言·php·android studio
程序员-珍5 小时前
报错下载android sdk失败
android·java
小小代码狗5 小时前
SQLi-Labs 基础注入实战教程(Less-1 ~ Less-5and Less-9)
服务器·python·php
小码哥0686 小时前
从三甲综合医院到连锁诊所:医院管理小程序的场景化突围逻辑(陪诊系统-陪诊APP-陪诊小程序)
小程序·医院管理系统·陪诊小程序·陪诊系统·陪诊app
齊家治國平天下6 小时前
AAOS 电源管理深度解析:休眠/唤醒/功耗优化
android·车载系统·aaos·aosp·电源管理·休眠唤醒·carpowermanager
shencz20006 小时前
在windows10里的apache2.4加php7.2的环境下上安装Laravel5.8框架
php·laravel