-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBillmate.php
More file actions
170 lines (160 loc) · 5.93 KB
/
Copy pathBillmate.php
File metadata and controls
170 lines (160 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Billmate
*
* Billmate API - PHP Class
*
* LICENSE: This source file is part of Billmate API, that is fully owned by Billmate AB
* This is not open source. For licensing queries, please contact Billmate AB at info@billmate.se.
*
* @category Billmate
* @package Billmate
* @author Yuksel Findik <yuksel@billmate.se>
* @copyright 2013-2014 Billmate AB
* @license Proprietary and fully owned by Billmate AB
* @version 2.1.6
* @link http://www.billmate.se
*
* History:
* 2.0 20140625 Yuksel Findik: Second Version
* 2.0.8 20141125 Yuksel Findik: Url is updated. Some variables are updated
* 2.0.9 20141204 Yuksel Findik: Returns array and verifies the data is safe
* 2.1.0 20141215 Yuksel Findik: Unnecessary variables are removed
* 2.1.1 20141218 Yuksel Findik: If response can not be json_decoded, will return actual response
* 2.1.2 20150112 Yuksel Findik: verify_hash function is added.
* 2.1.4 20150115 Yuksel Findik: verify_hash is improved. The serverdata is added instead of useragent
* 2.1.5 20150122 Yuksel Findik: Will make a utf8_decode before it returns the result
* 2.1.6 20150129 Yuksel Findik: Language is added as an optional paramater in credentials, version_compare is added for Curl setup
*/
class BillMate {
var $ID = "";
var $KEY = "";
var $URL = "api.billmate.se";
var $MODE = "CURL";
var $SSL = true;
var $TEST = false;
var $DEBUG = false;
var $REFERER = false;
function __construct($id, $key, $ssl=true, $test=false, $debug=false, $referer=array())
{
$this->ID = $id;
$this->KEY = $key;
defined('BILLMATE_CLIENT') || define('BILLMATE_CLIENT', "BillMate:2.1.7" );
defined('BILLMATE_SERVER') || define('BILLMATE_SERVER', "2.0.6" );
defined('BILLMATE_LANGUAGE') || define('BILLMATE_LANGUAGE', "" );
$this->SSL = $ssl;
$this->DEBUG = $debug;
$this->TEST = $test;
$this->REFERER = $referer;
}
public function __call($name, $args)
{
if(count($args)==0) return; //Function call should be skipped
return $this->call($name,$args[0]);
}
function call($function, $params)
{
$values = array(
"credentials" => array(
"id" => $this->ID,
"hash" => $this->hash(json_encode($params)),
"version" => BILLMATE_SERVER,
"client" => BILLMATE_CLIENT,
"serverdata" => array_merge(
$this->getServerData(),
$this->REFERER
),
"time" => microtime(true),
"test" => $this->TEST ? "1" : "0",
"language" => BILLMATE_LANGUAGE
),
"data" => $params,
"function" => $function,
);
$this->out("CALLED FUNCTION",$function);
$this->out("PARAMETERS TO BE SENT",$values);
switch ($this->MODE) {
case "CURL":
$response = $this->curl(json_encode($values));
break;
}
return $this->verify_hash($response);
}
public function getServerData()
{
$return = array();
$keys = array(
'HTTP_HOST',
'HTTP_USER_AGENT',
'REMOTE_ADDR',
'REQUEST_METHOD',
'REQUEST_URI',
'SERVER_ADDR',
'SERVER_NAME'
);
foreach ($keys as $key) {
if (isset($_SERVER[$key])) {
$return[$key] = $_SERVER[$key];
}
}
return $return;
}
function verify_hash($response)
{
$response_array = is_array($response)?$response:json_decode($response,true);
//If it is not decodable, the actual response will be returnt.
if(!$response_array && !is_array($response)) {
return $response;
}
if(is_array($response)) {
$response_array['credentials'] = json_decode($response['credentials'], true);
$response_array['data'] = json_decode($response['data'],true);
}
//If it is a valid response without any errors, it will be verified with the hash.
if(isset($response_array["credentials"])){
$hash = $this->hash(json_encode($response_array["data"]));
//If hash matches, the data will be returnt as array.
if ($response_array["credentials"]["hash"]==$hash) {
return $response_array["data"];
} else {
return array("code"=>9511,"message"=>"Verification error","hash"=>$hash,"hash_received"=>$response_array["credentials"]["hash"]);
}
}
return array_map("utf8_decode",$response_array);
}
function curl($parameters)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http".($this->SSL?"s":"")."://".$this->URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->SSL);
$vh = $this->SSL?((function_exists("phpversion") && function_exists("version_compare") && version_compare(phpversion(),'5.4','>=')) ? 2 : true):false;
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $vh);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($parameters))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
$data = curl_exec($ch);
if (curl_errno($ch)) {
$curlerror = curl_error($ch);
return json_encode(array("error"=>9510,"message"=>htmlentities($curlerror)));
} else {
curl_close($ch);
}
return $data;
}
function hash($args)
{
$this->out("TO BE HASHED DATA",$args);
return hash_hmac('sha512',$args,$this->KEY);
}
function out($name, $out)
{
if (!$this->DEBUG) return;
print "$name: '";
if(is_array($out) or is_object($out)) print_r($out);
else print $out;
print "'\n";
}
}