tp5/tp6 抖音支付

后端代码

<?php

namespace app\controller;

use app\BaseController;

class Pay extends BaseController
{

    public function test()
    {
        $timestamp = time();
        $order['app_id'] = "ttb905cfb8263a12dc01";
        $order['out_order_no'] = "noncestr$timestamp";
        $order['total_amount'] = 1;
        $order['subject'] = "抖音商品";
        $order['body'] = "抖音商品";
        $order['valid_time'] = 300;
        $order['notify_url'] = "https://app.mujuapi.cn/api/pay/notify";
        $order['sign'] = $this->sign($order);
        $jsonData = json_encode($order);

        $options = [
            'http' => [
                'method'  => 'POST',
                'header'  => 'Content-Type: application/json',
                'content' => $jsonData
            ]
        ];

        $context = stream_context_create($options);
        $response = file_get_contents('https://developer.toutiao.com/api/apps/ecpay/v1/create_order', false, $context);
        $responseData = json_decode($response, true);
        return json($responseData);

    }


    function sign($map)
    {
        $rList = [];
        foreach ($map as $k => $v) {
            if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
                continue;

            $value = trim(strval($v));
            if (is_array($v)) {
                $value = $this->arrayToStr($v);
            }

            $len = strlen($value);
            if ($len > 1 && substr($value, 0, 1) == "\"" && substr($value, $len - 1) == "\"")
                $value = substr($value, 1, $len - 1);
            $value = trim($value);
            if ($value == "" || $value == "null")
                continue;
            $rList[] = $value;
        }
        $rList[] = "JEwfiklDXIfPw4nl0lT15cpmJlreMPIMQOJbTOpT";
        sort($rList, SORT_STRING);
        return md5(implode('&', $rList));
    }

    function arrayToStr($map)
    {
        $isMap = $this->isArrMap($map);

        $result = "";
        if ($isMap) {
            $result = "map[";
        }

        $keyArr = array_keys($map);
        if ($isMap) {
            sort($keyArr);
        }

        $paramsArr = array();
        foreach ($keyArr as  $k) {
            $v = $map[$k];
            if ($isMap) {
                if (is_array($v)) {
                    $paramsArr[] = sprintf("%s:%s", $k, $this->arrayToStr($v));
                } else {
                    $paramsArr[] = sprintf("%s:%s", $k, trim(strval($v)));
                }
            } else {
                if (is_array($v)) {
                    $paramsArr[] = $this->arrayToStr($v);
                } else {
                    $paramsArr[] = trim(strval($v));
                }
            }
        }

        $result = sprintf("%s%s", $result, join(" ", $paramsArr));
        if (!$isMap) {
            $result = sprintf("[%s]", $result);
        } else {
            $result = sprintf("%s]", $result);
        }

        return $result;
    }

    function isArrMap($map)
    {
        foreach ($map as $k => $v) {
            if (is_string($k)) {
                return true;
            }
        }

        return false;
    }
    
}

使用

tp5/tp6 class里内容一样

只需要替换我画横线的内容即可完成测试

开源地址

gitee php_抖音_pay: tp5/tp6 抖音小程序支付icon-default.png?t=O83Ahttps://gitee.com/alsark/php-tiktok-pay.git

github https://github.com/alsarkc/php-tiktok-pay.giticon-default.png?t=O83Ahttps://github.com/alsarkc/php-tiktok-pay.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值