forked from jpcampbell/intacctws-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_session.php
More file actions
executable file
·213 lines (176 loc) · 8.37 KB
/
api_session.php
File metadata and controls
executable file
·213 lines (176 loc) · 8.37 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
<?php
include_once('api_post.php');
class api_session {
public $sessionId;
public $endPoint;
public $companyId;
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_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";
public function __toString() {
$temp = array (
'sessionId' => $this->sessionId,
'endPoint' => $this->endPoint,
'companyId' => $this->companyId,
'userId' => $this->userId,
'senderId' => $this->senderId,
'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') {
$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);
$response = api_post::execute($xml, self::DEFAULT_LOGIN_URL);
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->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);
$response = api_post::execute($xml, self::DEFAULT_LOGIN_URL);
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->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) {
$xml = self::XML_HEADER . self::XML_SESSIONID . self::XML_FOOTER;
$xml = str_replace("{1%}", $sessionId, $xml);
$xml = str_replace("{4%}", $senderId, $xml);
$xml = str_replace("{5%}", $senderPassword, $xml);
$response = api_post::execute($xml, self::DEFAULT_LOGIN_URL);
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->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') {
$error = $simpleXml->operation->errormessage;
throw new Exception(" [Error] " . (string)$error->error[0]->description2);
}
}
$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.
}
}
}
?>