You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace toutiao\pay;
class Base
{
protected $config;
// 正式地址
protected $url = 'https://developer.toutiao.com/api/apps/ecpay/v1/';
// 沙盒地址
// protected $url = '';
/**
* 构造函数
* @date 2022-10-20
*/
public function __construct(array $data = [])
{
// 基础配置参数
$this->config = $data;
}
/**
* 请求签名算法
* @param array $data 待签名的数据
* @date 2022-10-20
*/
public function getSign($data = [])
{
$value_list = [];
// 处理待签名数据
foreach ($data as $k => $v) {
// other_settle_params, sign, app_id , thirdparty_id 字段用于标识身份字段,不参与签名
if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id") {
continue;
}
// 去除value前后空格
$value = trim(strval($v));
// 空值不参与签名
if ($value == "" || $value == "null") {
continue;
}
array_push($value_list, $value);
}
// 拼接支付SALT
$salt = $this->config['salt'];
array_push($value_list, $salt);
// 把每一项作为字符串来处理 升序排序
sort($value_list, 2);
// 使用&符号链接
$str = implode('&', $value_list);
// 使用 md5 算法对该字符串计算摘要
return md5($str);
}
/**
* 回调签名算法
* @param array $data 待验签的数据
* @date 2022-10-20
*/
public function checkSign($data){
$msg_signature = $data['msg_signature'];
unset($data['msg_signature']);
unset($data['type']);
$value_list = [];
// 处理待验签数据
foreach ($data as $k => $v) {
// msg_signature, type字段不参与签名
if ($k == "msg_signature" || $k == "type") {
continue;
}
// 去除value前后空格
$value = trim(strval($v));
// 空值不参与签名
if ($value == "" || $value == "null") {
continue;
}
array_push($value_list, $value);
}
$token = $this->config['token'];
array_push($value_list,$token);
// 把每一项作为字符串来处理 升序排序
sort($value_list, 2);
// 拼接为字符串
$str = implode('', $value_list);
// 使用 md5 算法对该字符串计算摘要
$sign = sha1($str);
return $sign === $msg_signature;
}
}