當前位置:首頁 > IT技術 > 微信平臺 > 正文

php對接抖音小程序擔保支付/頭條小程序擔保支付
2021-09-10 18:14:55

字節(jié)跳動小程序擔保支付簡介

擔保交易是字節(jié)跳動為小程序開發(fā)者提供的小程序內支付能力,讓用戶能夠在電商、生活服務、知識付費等多種場景下,在小程序完成支付閉環(huán),提升用戶的支付體驗,保障資金安全。 開發(fā)者在小程序平臺進行微信/支付寶進件后,字節(jié)跳動將會為開發(fā)者在微信和支付寶申請商戶號,開發(fā)者可以調用小程序的下單、結算(分賬)、退款等接口,完成支付的業(yè)務邏輯。

?

?

接入流程

接入準備

文檔上寫的非常詳細,可參照文檔接入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時,才表示支付成功
// 但是最終狀態(tài)要以商戶后端結果為準
}
},
fail(res) {
// 調起收銀臺失敗處理邏輯
},
});

支付完成

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務立即開通 >