-
Notifications
You must be signed in to change notification settings - Fork 5
/
SagePayAdminApi.class.php
81 lines (73 loc) · 2.18 KB
/
SagePayAdminApi.class.php
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
<?php
/**
* @author Colin Burn-Murdoch <[email protected]>
*
**/
class SagePayAdminApi {
private $vendor;
private $username;
private $password;
private $xml;
private $url;
private $sslverify;
public $curlTimeout = 90;
public function __construct($vendor, $username, $password, $live = true, $sslverify = true) {
$this->vendor = $vendor;
$this->username = $username;
$this->password = $password;
$this->url = 'https://' . ($live ? 'live' : 'test') . '.sagepay.com/access/access.htm';
$this->sslverify = $sslverify;
}
public function __call($name, $args) {
$this->xml = $this->xmlise($name, $args[0]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,'XML='.$this->xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->curlTimeout);
if (!$this->sslverify) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
return new SimpleXMLElement($response);
}
private function xmlise($command, $elements) {
$xml = "<command>{$command}</command><vendor>{$this->vendor}</vendor><user>{$this->username}</user>";
foreach($elements as $key => $value) {
$xml .= $this->recursiveKeyValue($key, $value);
}
$signature = md5($xml . "<password>{$this->password}</password>");
$xml .= "<signature>{$signature}</signature>";
return "<vspaccess>{$xml}</vspaccess>";
}
// This is just for debugging.
public function getXml() {
return $this->xml;
}
public function recursiveKeyValue($key, $value) {
$return = "<{$key}>";
if (is_array($value)){
if ($this->is_assoc($value)) {
foreach ($value as $sub_key => $sub_value) {
$return .= $this->recursiveKeyValue($sub_key, $sub_value);
}
} else {
$return = '';
foreach ($value as $sub_value) {
$return .= $this->recursiveKeyValue($key, $sub_value);
}
return $return;
}
} else {
$return .= $value;
}
$return .= "</{$key}>";
return $return;
}
// Checks if array is associative
private function is_assoc($a) {
$a = array_keys($a);
return ($a !== array_keys($a));
}
}