forked from intacctaaron/intacctws-php
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi_session.php
More file actions
executable file
·241 lines (203 loc) · 9.89 KB
/
api_session.php
File metadata and controls
executable file
·241 lines (203 loc) · 9.89 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
include_once('api_post.php');
class api_session {
public $sessionId;
public $endPoint;
public $companyId;
public $entityId;
public $userId;
public $senderId;
public $senderPassword;
public $transaction = false;
const XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<request>
<control>
<senderid>{4%}</senderid>
<password>{5%}</password>
<controlid>foobar</controlid>
<uniqueid>false</uniqueid>
<dtdversion>3.0</dtdversion>
</control>
<operation>
<authentication>";
const XML_FOOTER = "</authentication>
<content>
<function controlid=\"foobar\"><getAPISession></getAPISession></function>
</content>
</operation>
</request>";
const XML_FOOTER_2 = "</authentication>
<content>
<function controlid=\"foobar\"><getAPISession><locationid>{6%}</locationid></getAPISession></function>
</content>
</operation>
</request>";
const XML_LOGIN = "<login>
<userid>{1%}</userid>
<companyid>{2%}</companyid>
<password>{3%}</password>
{%entityid%}
</login>";
const XML_SESSIONID = "<sessionid>{1%}</sessionid>";
const DEFAULT_LOGIN_URL = "https://api.intacct.com/ia/xml/xmlgw.phtml";
const PRV_LOGIN_URL = "https://preview.intacct.com/ia/xml/xmlgw.phtml";
public function __toString() {
$temp = array (
'sessionId' => $this->sessionId,
'endPoint' => $this->endPoint,
'companyId' => $this->companyId,
'entityId' => $this->entityId,
'userId' => $this->userId,
'senderId' => $this->senderId,
'entityId' => $this->entityId,
'senderPassword' => 'REDACTED',
'transaction' => $this->transaction
);
return json_encode($temp);
}
/**
* Connect to the Intacct Web Service using a set of user credntials for a subentity
* @param String $companyId company to connect to
* @param String $userId user
* @param String $password The users's password
* @param String $senderId Your Intacct Partner sender id
* @param String $senderPassword Your Intacct Partner password
* @param String $entityType location || client
* @param String $entityId The sub entity id
* @throws Exception this method returns no value, but will raise any connection exceptions
*/
private function buildHeaderXML($companyId, $userId, $password, $senderId, $senderPassword, $entityType = null, $entityId = null )
{
$xml = self::XML_HEADER . self::XML_LOGIN . self::XML_FOOTER;
$xml = str_replace("{1%}", $userId, $xml);
$xml = str_replace("{2%}", $companyId, $xml);
$xml = str_replace("{3%}", $password, $xml);
$xml = str_replace("{4%}", $senderId, $xml);
$xml = str_replace("{5%}", htmlspecialchars($senderPassword), $xml);
// hack for backward compat
if ($entityType == 'location' || $entityType === null) {
$xml = str_replace("{%entityid%}", "<locationid>$entityId</locationid>", $xml);
} else if ($entityType == 'client') {
$xml = str_replace("{%entityid%}", "<clientid>$entityId</clientid>", $xml);
} else if (!empty($entityType) || !empty($entityId)) {
$xml = str_replace("{%entityid%}", "<clientid>$entityType</clientid><locationid>$entityId</locationid>", $xml);
} else {
$xml = str_replace("{%entityid%}", "", $xml);
}
return $xml;
}
/**
* Connect to the Intacct Web Service using a set of user credntials
* @param String $companyId company to connect to
* @param String $userId user
* @param String $password The users's password
* @param String $senderId Your Intacct Partner sender id
* @param String $senderPassword Your Intacct Partner password
* @throws Exception this method returns no value, but will raise any connection exceptions
*/
public function connectCredentials($companyId, $userId, $password, $senderId, $senderPassword, $entityType=null, $entityId=null) {
$xml = $this->buildHeaderXML($companyId, $userId, $password, $senderId, $senderPassword, $entityType, $entityId);
$endpoint = strpos($companyId,"-prv") === FALSE ? self::DEFAULT_LOGIN_URL : self::PRV_LOGIN_URL;
$response = api_post::execute($xml, $endpoint);
self::validateConnection($response);
$responseObj = simplexml_load_string($response);
$this->sessionId = (string)$responseObj->operation->result->data->api->sessionid;
$this->endPoint = (string)$responseObj->operation->result->data->api->endpoint;
$this->entityId = (string)$responseObj->operation->authentication->locationid;
$this->companyId = $companyId;
$this->userId = $userId;
$this->senderId = $senderId;
$this->senderPassword = $senderPassword;
}
/**
* Connect to the Intacct Web Service using a set of user credntials for a subentity
* @param String $companyId company to connect to
* @param String $userId user
* @param String $password The users's password
* @param String $senderId Your Intacct Partner sender id
* @param String $senderPassword Your Intacct Partner password
* @param String $entityType location || client
* @param String $clientid The sub entity id
* @throws Exception this method returns no value, but will raise any connection exceptions
*/
public function connectCredentialsEntity($companyId, $userId, $password, $senderId, $senderPassword,$entityType, $entityId) {
$xml = $this->buildHeaderXML($companyId, $userId, $password, $senderId, $senderPassword,$entityType, $entityId);
$endpoint = strpos($companyId,"-prv") === FALSE ? self::DEFAULT_LOGIN_URL : self::PRV_LOGIN_URL;
$response = api_post::execute($xml, $endpoint);
self::validateConnection($response);
$responseObj = simplexml_load_string($response);
$this->sessionId = (string)$responseObj->operation->result->data->api->sessionid;
$this->endPoint = (string)$responseObj->operation->result->data->api->endpoint;
$this->entityId = (string)$responseObj->operation->authentication->locationid;
$this->companyId = $companyId;
$this->userId = $userId;
$this->senderId = $senderId;
$this->senderPassword = $senderPassword;
}
/**
* Create a session with the Intacct Web Services with an existing session.
* You'll normally get the sessionid using a merge field (or injection parameter)
* in an HTTP trigger or integration link
* @param String $sessionId a valid Intacct session Id
* @param String $senderId Your Intacct partner sender id
* @param String $senderPassword Your Intacct partner password
* @throws Exception This method returns no values, but will raise an exception if there's a connection error
*/
public function connectSessionId($sessionId, $senderId, $senderPassword, $entityId = null) {
if ($entityId === null) {
// we are passing NO entity/location. do not add locationid to the XML
$xml = self::XML_HEADER . self::XML_SESSIONID . self::XML_FOOTER;
} else {
// we are passing entity/location ('' for top-level flip from entity). add locationid to the XML
$xml = self::XML_HEADER . self::XML_SESSIONID . self::XML_FOOTER_2;
}
$xml = str_replace("{1%}", $sessionId, $xml);
$xml = str_replace("{4%}", $senderId, $xml);
$xml = str_replace("{5%}", $senderPassword, $xml);
if ($entityId !== null) {
$xml = str_replace("{6%}", $entityId, $xml);
}
$endpoint = ($this->companyId === null || strpos($this->companyId,"-prv") === FALSE) ? self::DEFAULT_LOGIN_URL : self::PRV_LOGIN_URL;
$response = api_post::execute($xml, $endpoint);
self::validateConnection($response);
$responseObj = simplexml_load_string($response);
$this->sessionId = (string)$responseObj->operation->result->data->api->sessionid;
$this->companyId = (string)$responseObj->operation->authentication->companyid;
$this->userId = (string)$responseObj->operation->authentication->userid;
$this->endPoint = (string)$responseObj->operation->result->data->api->endpoint;
$this->entityId = (string)$responseObj->operation->authentication->locationid;
$this->senderId = $senderId;
$this->senderPassword = $senderPassword;
}
private static function validateConnection($response) {
$simpleXml = simplexml_load_string($response);
if ($simpleXml === false) {
throw new Exception("Invalid XML response: \n" . var_export($response, true));
}
if ((string)$simpleXml->control->status == 'failure') {
throw new Exception(api_util::xmlErrorToString($simpleXml->errormessage));
}
if (!isset($simpleXml->operation)) {
if (isset($simpleXml->errormessage)) {
throw new Exception(api_util::xmlErrorToString($simpleXml->errormessage->error[0]));
}
}
if (isset($simpleXml->operation->authentication->status)) {
if ($simpleXml->operation->authentication->status != 'success') {
print_r($simpleXml);
$error = $simpleXml->operation->errormessage;
$desc2 = (string)$error->error[0]->description2 ?? '';
throw new Exception(" [Error] " . $desc2);
}
}
$status = $simpleXml->operation->result->status;
if ((string)$status != 'success') {
$error = $simpleXml->operation->result->errormessage;
throw new Exception(" [Error] " . (string)$error->error[0]->description2);
}
else {
return; // no error found.
}
}
}
?>