-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvangateJsonrpcClient.php
More file actions
99 lines (83 loc) · 3.17 KB
/
AvangateJsonrpcClient.php
File metadata and controls
99 lines (83 loc) · 3.17 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
<?php
/**
* @method createCustomer($customerObject)
* @method getCustomerInformation($AvangateCustomerReference = 0, $ExternalCustomerReference = '')
* @method placeOrder($orderObject)
* @method getOrder($refNo)
* @method addProduct($productObject)
* @method getProductByCode($productCode)
* @method addSubscription($subscriptionObject)
* @method getSubscription($subscriptionCode)
*/
class AvangateJsonrpcClient
{
protected static $merchantCode;
protected static $loginDate;
protected static $hash;
protected static $baseUrl;
protected static $callCount = 0;
protected static $sessionId = '';
public static function setCredentials($code, $key)
{
static::$merchantCode = $code;
static::$loginDate = gmdate('Y-m-d H:i:s');
static::$hash = hash_hmac('md5', strlen($code) . $code . strlen(static::$loginDate) . static::$loginDate, $key);
static::$sessionId = static::login();
}
public static function setBaseUrl($url)
{
static::$baseUrl = $url;
}
public static function login()
{
$requestObject = new stdClass();
$requestObject->jsonrpc = '2.0';
$requestObject->method = 'login';
$requestObject->params = [static::$merchantCode, static::$loginDate, static::$hash];
$requestObject->id = static::$callCount++;
return static::doRequest($requestObject);
}
public static function __callStatic($name, $arguments = [])
{
$requestObject = static::getPreparedRequest($name, $arguments);
$responseObject = static::doRequest($requestObject);
return $responseObject;
}
public static function doRequest($requestObject)
{
$curl = curl_init(static::$baseUrl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSLVERSION, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_PROXY, '');
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json'
]);
$requestString = json_encode($requestObject);
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestString);
$responseString = curl_exec($curl);
if (!empty($responseString)) {
$response = json_decode($responseString);
if (isset($response->result)) {
return $response->result;
}
if (isset($response->error) && !is_null($response->error)) {
throw new \Exception($requestObject->method . ': ' . json_encode($response->error));
}
}
}
protected static function getPreparedRequest($name, array $arguments = [])
{
array_unshift($arguments, static::$sessionId);
$jsonRpcRequest = new stdClass();
$jsonRpcRequest->jsonrpc = '2.0';
$jsonRpcRequest->method = $name;
$jsonRpcRequest->params = $arguments;
$jsonRpcRequest->id = static::$callCount++;
return $jsonRpcRequest;
}
}