-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiteroapi.php
More file actions
113 lines (99 loc) · 3.02 KB
/
iteroapi.php
File metadata and controls
113 lines (99 loc) · 3.02 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
<?php
class IteroAPI {
static $oAuth = "https://sandbox.billwerk.com/oauth/token";
static $iteroBase = "https://sandbox.billwerk.com/api/v1/";
static $contractResource = "contracts/";
static $customerResource = "customers/";
//Authorization string
static $auth;
function __construct($clientid,$secret)
{
//Get oauth token to access billwerk API
$verbose = fopen('php://stderr', 'w');
$curl = curl_init();
$data = http_build_query(array(
'grant_type' => 'client_credentials'
));
curl_setopt_array($curl, array(
CURLOPT_URL => self::$oAuth,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "$clientid:$secret",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_STDERR => $verbose)
);
$response = curl_exec($curl);
$this->exit_on_error($curl);
$token = json_decode($response);
self::$auth = "Authorization: Bearer ".$token->access_token;
}
// Fetch contract by id from billwerk
public function get_contract($contractId) {
return $this->get(self::$contractResource.$contractId);
}
// Fetch customer by id from billwerk
public function get_customer($customerId) {
return $this->get(self::$customerResource.$customerId);
}
// Update customer data in billwerk
public function put_customer($customer) {
return $this->put(self::$customerResource.$customer->Id,$customer);
}
public function get_selfservice_token($contractId) {
return $this->get(self::$contractResource.$contractId."/selfServiceToken");
}
// billwerk API GET request
private function get($resource)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array(self::$auth));
$response = $this->request($curl,$resource);
curl_close($curl);
return json_decode($response);
}
// billwerk API PUT request
private function put($resource, $data)
{
$json = json_encode($data);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json),
self::$auth),
CURLOPT_POSTFIELDS => $json,
CURLOPT_CUSTOMREQUEST => "PUT")
);
$response = $this->request($curl,$resource);
curl_close($curl);
return json_decode($response);
}
// Execute billwerk API request
private function request($curl,$resource)
{
$verbose = fopen('php://stderr', 'w');
curl_setopt_array($curl, array(
CURLOPT_URL => self::$iteroBase.$resource,
CURLOPT_HTTPAUTH => CURLAUTH_ANY,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_STDERR => $verbose));
$response = curl_exec($curl);
$this->exit_on_error($curl);
return $response;
}
// Exit if Http response code is not 200
private function exit_on_error($curl)
{
$responseInfo = curl_getinfo($curl);
$httpResponseCode = $responseInfo['http_code'];
if ($httpResponseCode!=200)
{
http_response_code(422);
exit;
}
}
}
?>