PHP教程

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

本文主要是介绍php对接抖音小程序担保支付/头条小程序担保支付,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

字节跳动小程序担保支付简介

担保交易是字节跳动为小程序开发者提供的小程序内支付能力,让用户能够在电商、生活服务、知识付费等多种场景下,在小程序完成支付闭环,提升用户的支付体验,保障资金安全。 开发者在小程序平台进行微信/支付宝进件后,字节跳动将会为开发者在微信和支付宝申请商户号,开发者可以调用小程序的下单、结算(分账)、退款等接口,完成支付的业务逻辑。

**

接入准备

**
文档上写的非常详细,可参照文档接入https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/server/ecpay/TE

小程序担保支付接入-服务端

<?php

  

  

/**
 * 支付下单
 * 服务端预下单
 * @param $data
 */
public function dy_pay($data)
{
    $url = 'https://developer.toutiao.com/api/apps/ecpay/v1/create_order';
    $postdata['app_id'] = '您的appid';
    $postdata['out_order_no'] = $data['order_num'];//订单号
    $postdata['total_amount'] = $data['all_price'];//支付金额
    $postdata['subject'] = '中文';
    $postdata['body'] = $data['body'];//备注
    $postdata['valid_time'] = 86400;//订单有效时间
    $postdata['cp_extra'] = urlencode(json_encode(['aaa'=>234,'bbb'=>'aaa']));
    $postdata['notify_url'] = '回调地址';
    $postdata['thirdparty_id'] = '';
    $postdata['disable_msg'] = 0;
    $postdata['msg_page'] = 'asger';
    $postdata['store_uid'] = '1';
    $postdata['sign'] = $this->sign($postdata);//获取签名
    $res = $this->json_post($url, $postdata);
    print_r($res);
}


function sign($map) {
    $rList = array();
    foreach($map 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, "您的担保支付密钥 在支付里的担保交易设置里的SALT获取");
    sort($rList, 2);
    return md5(implode('&', $rList));
}

function json_post($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 'data is null';
    }
    if (is_array($data)) {
        $data = json_encode($data,320);
    }
    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;
}
?>

返回值

{
"err_no": 0,
"err_tips": "",
"data": {
"order_id": "6819903302604491021",
"order_token": "CgwIARDiDRibDiABKAESTgpMbBhsCG7V1MPGAvpICgUSyGcuNOVb/BnCOi9EXgAxIxDqLTwCA6Hd3tNrCde28o0qjmAJQsmLrD18ifr5QktalszSSmTpHCqEm3h55xoA"
}
}

小程序端

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

支付完成

这篇关于php对接抖音小程序担保支付/头条小程序担保支付的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!