抖⾳担保⽀付PHPdemo附带详细解释担保⽀付闭坑说明:
1、关于前端发起⽀付报错CD0015
//仔细检查下前端 order_id order_token 格式,数据是由发布⽀付的接⼝回调得到的。
orderInfo: { // 订单信息
order_id: der_id,
order_token: der_token
}
2、关于后端,长常见就是发起⽀付有问题、回调有问题
这⾥的坑点是:仔细看开发⼿册你就发现抖⾳担保⽀付,发起⽀付跟回调签名算法不⼀样。
注意:发起⽀付签名加密是MD5、回调验证签名是SHA1加密
3、附PHP代码
保护地球的标语
(⼀)发起担保⽀付
* 抖⾳担保⽀付 - 处理数据
* @param $openId
* @param $out_order_no
* @param $total_amount
* @param string $body
* Ur: huweikeji
* Date: 2021/7/31 14:44
*/
public function DouyinPay($openId,$out_order_no,$total_amount,$body = '抖⾳担保⽀付'){
$outOrderNo = $out_order_no;//开发者侧的订单号
$totalAmount = $total_amount;//⽀付价格; 接⼝中参数⽀付⾦额单位为[分]
$subject = "订单号:".$out_order_no; //商品描述也就是标题
$body = $body;
$validTimestamp = 60 * 60;//订单过期时间(秒);
$notifyUrl = "域名/tt_notify"; //这⾥可以忽略,⾛字节跳动⼩程序 -⽀付 -担保配置-设置回调地址
//创建订单
心情的四字词语
$respon = $this->createOrder($outOrderNo, $totalAmount, $subject, $body, $validTimestamp, $notifyUrl); return $respon;
}爸爸痛
/**
* 创建订单
*/
public function CreateOrder($outOrderNo, $totalAmount, $subject, $body, $validTimestamp, $notifyUrl)
{
$params = [
'app_id' => $this->appid,
'out_order_no' => $outOrderNo,
'total_amount' => intval($totalAmount) * 100,
'subject' => $subject,
'body' => $body,
'valid_time' => $validTimestamp,
//'notify_url' => $notifyUrl,
//'cp_extra' => $cpExtra,
//'thirdparty_id' => $thirdPartyId,
//'disable_msg' => $disableMsg,
//'msg_page' => $msgPage,
//'store_uid' => $storeUid
];
$params = array_filter($params);
$params['sign'] = $this->getSign($params, $this->payment['cret']);
$res = $this->post(
'/api/apps/ecpay/v1/create_order',
$params
);
return $res;
}
(⼆)发起⽀付签名
* ⽀付签名
* @param array $body
* @param string $cret
* @return string
* Ur: huweikeji
* Date: 2021/8/1 16:14
*/
public function getSign(array $body, string $cret)
{
$filtered = [];
foreach ($body as $key => $value) {
if (in_array($key, ['sign', 'app_id', 'thirdparty_id'])) { continue;
}
$filtered[] =
is_string($value)
trim($value)
: $value;
}
$filtered[] = trim($cret);
sort($filtered, SORT_STRING);
return md5(trim(implode('&', $filtered)));
}
(三)验证回调签名
// **************** 以下⽅法是字节⼩程序⽀付签名处理⽅式
public function getNotifySign(array $body, string $cret) {
$filtered = [];
陶笛怎么吹
foreach ($body as $key => $value) {
if (in_array($key, ['msg_signature', 'type'])) {
continue;
}
$filtered[] =
is_string($value)
如何理发笼中的小鸟trim($value)
: $value;
}
$filtered[] = trim($cret);
sort($filtered, SORT_STRING);
$filtered = trim(implode('', $filtered));
珍惜的作文
return sha1($filtered);
}
(四)⽀付回调
public function notify()
{
$notify = \request()->param();
if ($notify['msg_signature'] !== $this->getNotifySign($notify, $this->payment['app_id'])) {
Log::info('回调验证错误');
} el {
//获取订单信息
$order = json_decode($notify['msg'], true);
//处理订单
$data = ['order_sn' => $order['cp_orderno']];
Log::info('抖⾳担保⽀付效验成功');
}
$data = ['err_no' => '0', 'err_tips' => 'success'];
return json($data);
}
(五)回调即CURL post提交
public function post(string $url, array $params = [], array $headers = [])
{
$headers[] = 'Content-type: application/json';
$ch = curl_init();
curl_topt($ch, CURLOPT_URL, $url);
curl_topt($ch, CURLOPT_POST, true);
大鱼海棠剧情
curl_topt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_topt($ch, CURLOPT_HTTPHEADER, $headers);
curl_topt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_topt($ch, CURLOPT_SSL_VERIFYPEER, fal);
curl_topt($ch, CURLOPT_SSL_VERIFYHOST, fal);
$output = curl_exec($ch);
curl_clo($ch);
return json_decode($output, true);
}
以上就是抖⾳担保⽀付 PHP版本demo,代码中关于⽀付秘钥、订单数据格式等⾃⾏替换修改即可。希望以上代码能够帮助你。
最后⼀次编辑于 2021年08⽉13⽇