apiurl = isset($option['apiurl']) ? $option['apiurl'] : ''; $this->token = isset($option['token']) ? $option['token'] : ''; } /** * PASS * 获取积分余额 * @param $huayi_uid * @return bool */ public function getBalance($huayi_uid) { $url = $this->apiurl.$this->get_balance_url; $data = [ 'uid'=>$huayi_uid, 'timestamp'=>time() ]; $response = $this->postRequest($url, $data); if (!$response) {//请求失败 return false; } elseif ($response['body']['code'] == 1) {//请求成功 return $response['body']; } else {//请求成功但是返回错误 $this->error = $response['body']['msg']; return false; } } /** * TODO TEST * 支出积分 * @param $huayi_uid * @param $amount * @param $note * @return bool */ public function pay($huayi_uid, $amount, $note) { $url = $this->apiurl.$this->pay_url; $data = [ 'uid'=>$huayi_uid, 'amount'=>$amount, 'note'=>$note, 'timestamp'=>time() ]; $response = $this->postRequest($url, $data); if (!$response) {//请求失败 return false; } elseif ($response['body']['code'] == 1) {//请求成功 return $response['body']; } else {//请求成功但是返回错误 $this->error = $response['body']['msg']; return false; } } /** * TODO TEST * 退还积分 * @param $huayi_uid * @param $amount * @param $note * @return bool */ public function refund($huayi_uid, $amount, $note) { $url = $this->apiurl.$this->refund_url; $data = [ 'uid'=>$huayi_uid, 'amount'=>$amount, 'note'=>$note, 'timestamp'=>time() ]; $response = $this->postRequest($url, $data); if (!$response) {//请求失败 return false; } elseif ($response['body']['code'] == 1) {//请求成功 return $response['body']; } else {//请求成功但是返回错误 $this->error = $response['body']['msg']; return false; } } /** * 生成签名 * @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 */ public function getError() { return $this->error; } /** * 封装Post请求 * @param $url * @param $data * @return bool / array */ protected function postRequest($url, $data) { //生成签名 $data['signature'] = $this->getSignature($this->token, $data); //请求接口 $response = wp_remote_post($url, array( 'method' => 'POST', 'body' => array_merge($data), ) ); if ( is_wp_error($response) ) { $this->error = $response->get_error_message(); return false; } else { $response['body'] = json_decode($response['body'], true); return $response; } } }