抖音小程序担保支付(php + uniapp)

// 支付
public function ttpay($data)
    {
        $url = 'https://developer.toutiao.com/api/apps/ecpay/v1/create_order';
        $params['app_id'] = '你的小程序appid';
        $params['out_order_no'] = (string)$data['no'];
        $params['total_amount'] = (int)$data['amount'] * 100;
        $params['subject'] = 'subject';
        $params['body'] = 'body';
        $params['valid_time'] = 3600;
        $params['sign'] = $this->ttsign($params);
        $http = $this->httpPost($url, $params);
        return json_decode($http,true);
    }

    //curl 
public function httpPost($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!$data) return false;
        if (is_array($data)) {
            $data = json_encode($data);
        }
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length:' . strlen($data),
            'Cache-Control: no-cache',
            'Pragma: no-cache'
        ));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
// 签名
public function ttsign($params)
    {
        $rList = array();
        foreach ($params as $k => $v) {
            if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
                continue;
            $value = trim(strval($v));
            $len = strlen($value);
            if ($len > 1 && substr($value, 0, 1) == "\"" && substr($value, $len, $len - 1) == "\"")
                $value = substr($value, 1, $len - 1);
            $value = trim($value);
            if ($value == "" || $value == "null")
                continue;
            array_push($rList, $value);
        }
        array_push($rList, "fregSFjXnAaSZmc02UGxLJWeDv9AX02F7YFxpji9");
        sort($rList, 2);
        return md5(implode('&', $rList));
    }

//ps: 请求 ttpay() 方法, 抖音文档真垃圾,返回错误参数不明确,各位踩坑的码友注意传递参数类型

//前端代码
tt.pay({
  orderInfo: {
    order_id: "6819903302604491021",
    order_token:
      "CgsIARCABRgBIAQoARJOCkx+WgXqCUIwTel2V3siEGZ0++poigIM+SMMxtMx798Vj0ZYzoTYBqeNslodUC9X5KAOHkR1YbSBz6I6pXATh5faIGy7R72A9vwm0OczGgA=",
  },
  service: 5,
  success(res) {
    if (res.code == 0) {
      // 支付成功处理逻辑,只有res.code=0时,才表示支付成功
      // 但是最终状态要以商户后端结果为准
    }
  },
  fail(res) {
    // 调起收银台失败处理逻辑
  },
});

给TA打赏
共{{data.count}}人
人已打赏
PHP

php 抖音支付

2022-8-30 9:29:36

PHP

php对接抖音小程序担保支付/头条小程序担保支付

2022-8-30 9:59:24

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
搜索