Repository navigation
Expand file tree
/
Copy pathGLMXHttpClient.php
More file actions
305 lines (259 loc) · 10.6 KB
/
Copy pathGLMXHttpClient.php
File metadata and controls
305 lines (259 loc) · 10.6 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
namespace DPRMC\GLMXHttpClient;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
class GMLXHttpClient {
private Client $httpClient;
private string $sharedSecret;
/**
* @param string $baseUrl
* @param string $sharedSecret
* @param string|NULL $pathToClientPem Optional: If client certificates are used
* @param string|NULL $pathToClientKey Optional: If client certificates are used
*/
public function __construct( string $baseUrl,
string $sharedSecret,
string $pathToClientPem = NULL,
string $pathToClientKey = NULL ) {
$options = [
'base_uri' => $baseUrl,
'timeout' => 30.0, // Adjust as needed
'http_errors' => FALSE, // We handle errors manually
'verify' => FALSE, // In production, set this to true and configure CA certificates
'curl' => [
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, // Ensure TLSv1.2+
],
];
if ( $pathToClientPem ):
$options[ 'curl' ][ CURLOPT_SSLCERT ] = $pathToClientPem;
endif;
if ( $pathToClientKey ):
$options[ 'curl' ][ CURLOPT_SSLKEY ] = $pathToClientKey;
endif;
$this->httpClient = new Client( $options );
$this->sharedSecret = base64_decode( $sharedSecret ); // Decode the Base64 secret
}
private function generateDigest( string $dateHeader, string $bodyContent ): string {
$hmac = hash_hmac( 'sha256', $dateHeader . $bodyContent, $this->sharedSecret, TRUE );
return base64_encode( $hmac );
}
private function verifyResponse( ResponseInterface $response, string $requestBody ): void {
$responseDateHeader = $response->getHeaderLine( 'Date' );
$responseDigest = $response->getHeaderLine( 'X-GLMX-Message-Digest' );
$responseBody = (string)$response->getBody();
// Timestamp verification
$responseTimestamp = strtotime( $responseDateHeader );
if ( abs( time() - $responseTimestamp ) > 10 ) {
throw new Exception( 'Timestamp in response is out of sync by more than 10 seconds.' );
}
// Digest verification
$expectedResponseDigest = $this->generateDigest( $responseDateHeader, $responseBody );
if ( $responseDigest !== $expectedResponseDigest ) {
// Uncomment in production after thorough testing of digest generation/verification
// throw new Exception('Response digest mismatch. Expected: ' . $expectedResponseDigest . ' Got: ' . $responseDigest);
}
if ( $response->getStatusCode() >= 500 ) {
$errorDetails = json_decode( $responseBody, TRUE );
throw new Exception( 'GLMX server error: ' . json_encode( $errorDetails ) );
}
}
public function sendRequest( string $endpoint, array $data, string $method = 'POST' ): array {
$requestBody = json_encode( $data );
$date = gmdate( 'D, d M Y H:i:s T' );
$digest = $this->generateDigest( $date, $requestBody );
$headers = [
'Content-Type' => 'application/json',
'Date' => $date,
'X-GLMX-Message-Digest' => $digest,
];
try {
$response = $this->httpClient->request( $method, $endpoint, [
'headers' => $headers,
'body' => $requestBody,
] );
$this->verifyResponse( $response, $requestBody );
return json_decode( (string)$response->getBody(), TRUE );
} catch ( RequestException $e ) {
throw new Exception( 'Guzzle HTTP error: ' . $e->getMessage() );
}
}
/**
* Sends an Inquiry Create message to GLMX.
* This endpoint is implemented by GLMX.
*
* @param array $inquiryData The data for the inquiry.
*
* @return array The response from GLMX.
*/
public function sendInquiryCreate( array $inquiryData ): array {
return $this->sendRequest( '/Inquiry Create', $inquiryData );
}
/**
* Sends a Price Source Create message to GLMX.
* This endpoint is implemented by GLMX.
*
* @param array $priceSourceData The data for the price source.
*
* @return array The response from GLMX.
*/
public function sendPriceListCreate( array $priceSourceData ): array {
return $this->sendRequest( '/PriceListCreate', $priceSourceData );
}
/**
* Sends a List Create message to GLMX.
* This endpoint is implemented by GLMX.
*
* @param array $listData The data for the list.
*
* @return array The response from GLMX.
*/
public function sendListCreate( array $listData ): array {
return $this->sendRequest( '/ListCreate', $listData );
}
/**
* Sends a List Update message to GLMX.
* This endpoint is implemented by GLMX.
*
* @param array $listData The data for updating the list.
*
* @return array The response from GLMX.
*/
public function sendListUpdate( array $listData ): array {
return $this->sendRequest( '/ListUpdate', $listData );
}
/**
* Sends a List Delete message to GLMX.
* This endpoint is implemented by GLMX.
*
* @param array $listData The data for deleting the list.
*
* @return array The response from GLMX.
*/
public function sendListDelete( array $listData ): array {
return $this->sendRequest( '/ListDelete', $listData );
}
/**
* Sends an Inquiry Update message to the customer.
* This endpoint is implemented by the customer.
*
* @param array $inquiryData The data for updating the inquiry.
*
* @return array The response from the customer's endpoint.
*/
public function sendInquiryUpdate( array $inquiryData ): array {
// This endpoint is implemented by the customer, so the base_uri would need to point to the customer's URL.
// For demonstration, it's assumed to be part of the GLMX client's operations.
return $this->sendRequest( '/Inquiry Update', $inquiryData );
}
/**
* Sends an Inquiry Stage message to GLMX.
* This endpoint is implemented by GLMX.
*
* @param array $inquiryStageData The data for staging changes to a particular inquiry.
*
* @return array The response from GLMX.
*/
public function sendInquiryStage( array $inquiryStageData ): array {
return $this->sendRequest( '/InquiryStage', $inquiryStageData );
}
/**
* Sends an Inquiry Stages message to GLMX.
* This endpoint is implemented by GLMX.
*
* @param array $inquiryStagesData The data for staging changes to a list of inquiries, usually used with packages.
*
* @return array The response from GLMX.
*/
public function sendInquiryStages( array $inquiryStagesData ): array {
return $this->sendRequest( '/InquiryStages', $inquiryStagesData );
}
/**
* Sends an Inquiry Action message to GLMX.
* This endpoint is implemented by GLMX.
*
* @param array $inquiryActionData The data for performing certain actions on inquiries.
*
* @return array The response from GLMX.
*/
public function sendInquiryAction( array $inquiryActionData ): array {
return $this->sendRequest( '/InquiryAction', $inquiryActionData );
}
/**
* Sends an Inquiry Counters message to GLMX.
* This endpoint is implemented by GLMX.
*
* @param array $inquiryCountersData The data for countering an inquiry (or package of inquiries).
*
* @return array The response from GLMX.
*/
public function sendInquiryCounters( array $inquiryCountersData ): array {
return $this->sendRequest( '/InquiryCounters', $inquiryCountersData );
}
/**
* Sends a Test message to the GLMX test endpoint.
* This endpoint is implemented by GLMX.
*
* @param array $testData The data to echo back.
*
* @return array The echoed response from GLMX.
*/
public function sendTest( array $testData ): array {
return $this->sendRequest( '/Test', $testData );
}
/**
* Retrieves pending STP jobs from GLMX.
* This endpoint is implemented by GLMX.
*
* @param int $limit The maximum number of pending jobs to return ($<=100.$, default 1).
*
* @return array An array of StpJobDetail objects.
*/
public function getPendingJobs( int $limit = 1 ): array {
$date = gmdate( 'D, d M Y H:i:s T' );
$digest = $this->generateDigest( $date, '' ); // GET requests have an empty body for digest calculation
$headers = [
'Date' => $date,
'X-GLMX-Message-Digest' => $digest,
];
try {
$response = $this->httpClient->request( 'GET', '/PendingJobs', [
'headers' => $headers,
'query' => [ 'limit' => $limit ],
] );
$this->verifyResponse( $response, '' ); // No request body for GET
$responseData = json_decode( (string)$response->getBody(), TRUE );
return $responseData[ 'jobs' ] ?? [];
} catch ( RequestException $e ) {
throw new Exception( 'Guzzle HTTP error fetching pending jobs: ' . $e->getMessage() );
}
}
/**
* Acknowledges pending jobs by sending a DELETE request to GLMX.
*
* @param array $jobIds An array of job IDs to acknowledge.
*
* @return bool True on success, false otherwise.
*/
public function acknowledgePendingJobs( array $jobIds ): bool {
$requestBody = http_build_query( [ 'id' => $jobIds ] );
$date = gmdate( 'D, d M Y H:i:s T' );
$digest = $this->generateDigest( $date, $requestBody );
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
'Date' => $date,
'X-GLMX-Message-Digest' => $digest,
];
try {
$response = $this->httpClient->request( 'DELETE', '/PendingJobs', [
'headers' => $headers,
'body' => $requestBody,
] );
$this->verifyResponse( $response, $requestBody ); // Verify the response from the DELETE request
return $response->getStatusCode() === 200; // Assuming 200 OK for successful deletion
} catch ( RequestException $e ) {
throw new Exception( 'Guzzle HTTP error acknowledging pending jobs: ' . $e->getMessage() );
}
}
}