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;
    }
}
相关推荐
科技道人39 分钟前
Android 禁止使用ipv6 测试
android·禁用ipv6
AlexMaybeBot1 小时前
巧用 OpenClaw 为 Android 开发电脑瘦身
android·github·ai编程
神仙别闹4 小时前
基于PHP+MySQL实现在线考试系统
开发语言·mysql·php
王者鳜錸4 小时前
企业解决方案十一-各类小程序定制开发
图像处理·人工智能·小程序·大模型·语音处理·定制开发
pengyu4 小时前
【Kotlin 协程修仙录 · 金丹境 · 中阶】 | 启动密法:CoroutineStart 四种模式与底层调度玄机
android·kotlin
互联科技报4 小时前
商城小程序选择哪家平台比较好?预算有限也能选对!
大数据·小程序
Android小码家5 小时前
Xposed之雷电5+Android 7.1.2 Xposed 89(古早安装)
android·xposed
棒棒的唐5 小时前
配置 VSCode 的 PHP Intelephense 插件,去掉因php版本不同导至的红色波浪线误判
ide·vscode·php
ooseabiscuit5 小时前
Laravel2.x核心特性全解析
android
UXbot5 小时前
AI一次生成iOS和Android双端原型功能详解
android·前端·ios·kotlin·交互·swift