107 lines
2.6 KiB
PHP
107 lines
2.6 KiB
PHP
<?php
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit; // Exit if accessed directly
|
|
}
|
|
|
|
class HuayiScoreSdk
|
|
{
|
|
//接口地址
|
|
protected $apiurl = null;
|
|
//接口Token
|
|
protected $token = null;
|
|
// 错误信息
|
|
protected $error;
|
|
|
|
public function __construct($option)
|
|
{
|
|
$this->apiurl = isset($option['apiurl']) ? $option['apiurl'] : '';
|
|
$this->token = isset($option['token']) ? $option['token'] : '';
|
|
}
|
|
/**
|
|
* 获取积分余额
|
|
*/
|
|
public function getBalance($huayi_uid)
|
|
{
|
|
return $huayi_uid;
|
|
}
|
|
|
|
/**
|
|
* 支出积分
|
|
* @return string
|
|
*/
|
|
public function pay($huayi_uid,$amount,$note)
|
|
{
|
|
return $huayi_uid.$amount.$note;
|
|
}
|
|
|
|
/**
|
|
* 退还积分
|
|
* @return string
|
|
*/
|
|
public function refund($huayi_uid,$amount,$note)
|
|
{
|
|
return $huayi_uid.$amount.$note;
|
|
}
|
|
|
|
/**
|
|
* 生成签名
|
|
* @param $token
|
|
* @param $data
|
|
* @return string
|
|
*/
|
|
protected function getSignature($token, $data)
|
|
{
|
|
ksort($data);// 对数组的值按key排序
|
|
$params = http_build_query($data);// 生成url的形式
|
|
return md5($params . $token);// 生成signature
|
|
}
|
|
|
|
/**
|
|
* 签名验证
|
|
* @param $token
|
|
* @param $data array('uid'=>123,'timestamp'=>time(),'signature'='signature')
|
|
* @return bool
|
|
*/
|
|
protected function verifySignature($token, $data)
|
|
{
|
|
if (empty($data['signature'])) {// 验证参数中是否有签名
|
|
$this->error = '数据签名不存在';
|
|
return false;
|
|
}
|
|
if (empty($data['timestamp'])) {
|
|
$this->error = '发送的数据参数不合法';
|
|
return false;
|
|
}
|
|
if (time() - $data['timestamp'] > 600) {// 验证10分钟失效
|
|
$this->error = '验证失效, 请重新发送请求';
|
|
return false;
|
|
}
|
|
|
|
$tmpSignature = $data['signature'];//接收过来的签名
|
|
|
|
unset($data['signature']);
|
|
ksort($data);//对数组的值按key排序
|
|
$params = http_build_query($data);//生成url的形式
|
|
|
|
$signature = md5($params . $token);//计算签名
|
|
if ($signature == $tmpSignature) {//验证签名
|
|
$this->error = '验证通过';
|
|
return true;
|
|
} else {
|
|
$this->error = '签名无效';
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取错误信息
|
|
* @return mixed
|
|
*/
|
|
protected function getError()
|
|
{
|
|
return $this->error;
|
|
}
|
|
|
|
|
|
} |