-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreports.php
More file actions
547 lines (472 loc) · 21.8 KB
/
Copy pathreports.php
File metadata and controls
547 lines (472 loc) · 21.8 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<?php
declare(strict_types=1);
/**
* Global Payments Reporting API Endpoint
*
* This script provides RESTful API endpoints for accessing Global Payments
* reporting functionality including transaction search, details, settlement
* reports, and data export capabilities.
*
* PHP version 7.4 or higher
*
* @category API_Endpoint
* @package GlobalPayments_Reporting
* @author Global Payments
* @license MIT License
* @link https://github.com/globalpayments
*/
require_once 'reporting-service.php';
ini_set('display_errors', '0');
/**
* Set JSON response headers
*/
function setJsonHeaders(): void
{
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
// Handle preflight OPTIONS request
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
}
/**
* Send JSON response
*
* @param array $data Response data
* @param int $statusCode HTTP status code
*/
function sendJsonResponse(array $data, int $statusCode = 200): void
{
http_response_code($statusCode);
echo json_encode($data, JSON_PRETTY_PRINT);
exit;
}
/**
* Handle error responses
*
* @param string $message Error message
* @param int $statusCode HTTP status code
* @param string $errorCode Error code
*/
function handleError(string $message, int $statusCode = 400, string $errorCode = 'API_ERROR'): void
{
sendJsonResponse([
'success' => false,
'error' => [
'code' => $errorCode,
'message' => $message,
'timestamp' => date('Y-m-d H:i:s')
]
], $statusCode);
}
/**
* Get request parameters (supports both GET and POST)
*
* @return array Combined request parameters
*/
function getRequestParams(): array
{
$params = $_GET;
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
$postData = json_decode(file_get_contents('php://input'), true);
if (is_array($postData)) {
$params = array_merge($params, $postData);
} else {
$params = array_merge($params, $_POST);
}
}
return $params;
}
/**
* Validate required parameters
*
* @param array $params Request parameters
* @param array $required Required parameter names
* @throws InvalidArgumentException If required parameters are missing
*/
function validateRequiredParams(array $params, array $required): void
{
foreach ($required as $param) {
if (!isset($params[$param]) || $params[$param] === '') {
throw new \InvalidArgumentException("Missing required parameter: {$param}");
}
}
}
/**
* Validate date format
*
* @param string $date Date string
* @param string $format Expected date format
* @return bool True if valid
*/
function validateDateFormat(string $date, string $format = 'Y-m-d'): bool
{
$dateObj = \DateTime::createFromFormat($format, $date);
return $dateObj && $dateObj->format($format) === $date;
}
// Set response headers
setJsonHeaders();
try {
// Initialize the reporting service
$reportingService = new GlobalPaymentsReportingService();
// Get request parameters
$params = getRequestParams();
$action = $params['action'] ?? '';
// Route requests based on action parameter
switch ($action) {
case 'search':
// Search transactions
$filters = [
'page' => (int)($params['page'] ?? 1),
'page_size' => min((int)($params['page_size'] ?? 10), 100), // Limit page size
'start_date' => $params['start_date'] ?? '',
'end_date' => $params['end_date'] ?? '',
'transaction_id' => $params['transaction_id'] ?? '',
'payment_type' => $params['payment_type'] ?? '',
'status' => $params['status'] ?? '',
'amount_min' => $params['amount_min'] ?? '',
'amount_max' => $params['amount_max'] ?? '',
'card_last_four' => $params['card_last_four'] ?? ''
];
// Validate date formats if provided
if ($filters['start_date'] && !validateDateFormat($filters['start_date'])) {
throw new \InvalidArgumentException('Invalid start_date format. Use YYYY-MM-DD.');
}
if ($filters['end_date'] && !validateDateFormat($filters['end_date'])) {
throw new \InvalidArgumentException('Invalid end_date format. Use YYYY-MM-DD.');
}
// Remove empty filters
$filters = array_filter($filters, function($value) {
return $value !== '' && $value !== null;
});
$result = $reportingService->searchTransactions($filters);
sendJsonResponse($result);
break;
case 'detail':
// Get transaction details
validateRequiredParams($params, ['transaction_id']);
$result = $reportingService->getTransactionDetails($params['transaction_id']);
sendJsonResponse($result);
break;
case 'settlement':
// Get settlement report
$settlementParams = [
'page' => (int)($params['page'] ?? 1),
'page_size' => min((int)($params['page_size'] ?? 50), 100),
'start_date' => $params['start_date'] ?? '',
'end_date' => $params['end_date'] ?? ''
];
// Validate date formats if provided
if ($settlementParams['start_date'] && !validateDateFormat($settlementParams['start_date'])) {
throw new \InvalidArgumentException('Invalid start_date format. Use YYYY-MM-DD.');
}
if ($settlementParams['end_date'] && !validateDateFormat($settlementParams['end_date'])) {
throw new \InvalidArgumentException('Invalid end_date format. Use YYYY-MM-DD.');
}
// Remove empty filters
$settlementParams = array_filter($settlementParams, function($value) {
return $value !== '' && $value !== null;
});
$result = $reportingService->getSettlementReport($settlementParams);
sendJsonResponse($result);
break;
case 'export':
// Export transaction data
$exportFilters = [
'start_date' => $params['start_date'] ?? '',
'end_date' => $params['end_date'] ?? '',
'transaction_id' => $params['transaction_id'] ?? '',
'payment_type' => $params['payment_type'] ?? '',
'status' => $params['status'] ?? '',
'amount_min' => $params['amount_min'] ?? '',
'amount_max' => $params['amount_max'] ?? '',
'card_last_four' => $params['card_last_four'] ?? ''
];
$format = $params['format'] ?? 'json';
if (!in_array($format, ['json', 'csv'])) {
throw new \InvalidArgumentException('Invalid format. Supported formats: json, csv');
}
// Validate date formats if provided
if ($exportFilters['start_date'] && !validateDateFormat($exportFilters['start_date'])) {
throw new \InvalidArgumentException('Invalid start_date format. Use YYYY-MM-DD.');
}
if ($exportFilters['end_date'] && !validateDateFormat($exportFilters['end_date'])) {
throw new \InvalidArgumentException('Invalid end_date format. Use YYYY-MM-DD.');
}
// Remove empty filters
$exportFilters = array_filter($exportFilters, function($value) {
return $value !== '' && $value !== null;
});
$result = $reportingService->exportTransactions($exportFilters, $format);
if ($format === 'csv') {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . ($result['filename'] ?? 'transactions.csv') . '"');
echo $result['data'];
exit;
}
sendJsonResponse($result);
break;
case 'summary':
// Get summary statistics
$summaryParams = [
'start_date' => $params['start_date'] ?? '',
'end_date' => $params['end_date'] ?? ''
];
// Validate date formats if provided
if ($summaryParams['start_date'] && !validateDateFormat($summaryParams['start_date'])) {
throw new \InvalidArgumentException('Invalid start_date format. Use YYYY-MM-DD.');
}
if ($summaryParams['end_date'] && !validateDateFormat($summaryParams['end_date'])) {
throw new \InvalidArgumentException('Invalid end_date format. Use YYYY-MM-DD.');
}
// Remove empty filters
$summaryParams = array_filter($summaryParams, function($value) {
return $value !== '' && $value !== null;
});
$result = $reportingService->getSummaryStats($summaryParams);
sendJsonResponse($result);
break;
case 'disputes':
// Get dispute report
$disputeFilters = [
'page' => (int)($params['page'] ?? 1),
'page_size' => min((int)($params['page_size'] ?? 10), 100),
'start_date' => $params['start_date'] ?? '',
'end_date' => $params['end_date'] ?? '',
'stage' => $params['stage'] ?? '',
'status' => $params['status'] ?? ''
];
// Validate date formats if provided
if ($disputeFilters['start_date'] && !validateDateFormat($disputeFilters['start_date'])) {
throw new \InvalidArgumentException('Invalid start_date format. Use YYYY-MM-DD.');
}
if ($disputeFilters['end_date'] && !validateDateFormat($disputeFilters['end_date'])) {
throw new \InvalidArgumentException('Invalid end_date format. Use YYYY-MM-DD.');
}
// Remove empty filters
$disputeFilters = array_filter($disputeFilters, function($value) {
return $value !== '' && $value !== null;
});
$result = $reportingService->getDisputeReport($disputeFilters);
sendJsonResponse($result);
break;
case 'dispute_detail':
// Get dispute details
validateRequiredParams($params, ['dispute_id']);
$result = $reportingService->getDisputeDetails($params['dispute_id']);
sendJsonResponse($result);
break;
case 'deposits':
// Get deposit report
$depositFilters = [
'page' => (int)($params['page'] ?? 1),
'page_size' => min((int)($params['page_size'] ?? 10), 100),
'start_date' => $params['start_date'] ?? '',
'end_date' => $params['end_date'] ?? '',
'deposit_id' => $params['deposit_id'] ?? '',
'status' => $params['status'] ?? ''
];
// Validate date formats if provided
if ($depositFilters['start_date'] && !validateDateFormat($depositFilters['start_date'])) {
throw new \InvalidArgumentException('Invalid start_date format. Use YYYY-MM-DD.');
}
if ($depositFilters['end_date'] && !validateDateFormat($depositFilters['end_date'])) {
throw new \InvalidArgumentException('Invalid end_date format. Use YYYY-MM-DD.');
}
// Remove empty filters
$depositFilters = array_filter($depositFilters, function($value) {
return $value !== '' && $value !== null;
});
$result = $reportingService->getDepositReport($depositFilters);
sendJsonResponse($result);
break;
case 'deposit_detail':
// Get deposit details
validateRequiredParams($params, ['deposit_id']);
$result = $reportingService->getDepositDetails($params['deposit_id']);
sendJsonResponse($result);
break;
case 'batches':
// Get batch report
$batchFilters = [
'start_date' => $params['start_date'] ?? '',
'end_date' => $params['end_date'] ?? ''
];
// Validate date formats if provided
if ($batchFilters['start_date'] && !validateDateFormat($batchFilters['start_date'])) {
throw new \InvalidArgumentException('Invalid start_date format. Use YYYY-MM-DD.');
}
if ($batchFilters['end_date'] && !validateDateFormat($batchFilters['end_date'])) {
throw new \InvalidArgumentException('Invalid end_date format. Use YYYY-MM-DD.');
}
// Remove empty filters
$batchFilters = array_filter($batchFilters, function($value) {
return $value !== '' && $value !== null;
});
$result = $reportingService->getBatchReport($batchFilters);
sendJsonResponse($result);
break;
case 'declines':
// Get declined transactions report
$declineFilters = [
'page' => (int)($params['page'] ?? 1),
'page_size' => min((int)($params['page_size'] ?? 10), 100),
'start_date' => $params['start_date'] ?? '',
'end_date' => $params['end_date'] ?? '',
'payment_type' => $params['payment_type'] ?? '',
'amount_min' => $params['amount_min'] ?? '',
'amount_max' => $params['amount_max'] ?? '',
'card_last_four' => $params['card_last_four'] ?? ''
];
// Validate date formats if provided
if ($declineFilters['start_date'] && !validateDateFormat($declineFilters['start_date'])) {
throw new \InvalidArgumentException('Invalid start_date format. Use YYYY-MM-DD.');
}
if ($declineFilters['end_date'] && !validateDateFormat($declineFilters['end_date'])) {
throw new \InvalidArgumentException('Invalid end_date format. Use YYYY-MM-DD.');
}
// Remove empty filters
$declineFilters = array_filter($declineFilters, function($value) {
return $value !== '' && $value !== null;
});
$result = $reportingService->getDeclinedTransactionsReport($declineFilters);
sendJsonResponse($result);
break;
case 'date_range':
// Get comprehensive date range report
$dateRangeParams = [
'start_date' => $params['start_date'] ?? '',
'end_date' => $params['end_date'] ?? '',
'transaction_limit' => min((int)($params['transaction_limit'] ?? 100), 1000),
'settlement_limit' => min((int)($params['settlement_limit'] ?? 50), 500),
'dispute_limit' => min((int)($params['dispute_limit'] ?? 25), 100),
'deposit_limit' => min((int)($params['deposit_limit'] ?? 25), 100)
];
// Validate date formats if provided
if ($dateRangeParams['start_date'] && !validateDateFormat($dateRangeParams['start_date'])) {
throw new \InvalidArgumentException('Invalid start_date format. Use YYYY-MM-DD.');
}
if ($dateRangeParams['end_date'] && !validateDateFormat($dateRangeParams['end_date'])) {
throw new \InvalidArgumentException('Invalid end_date format. Use YYYY-MM-DD.');
}
// Remove empty filters
$dateRangeParams = array_filter($dateRangeParams, function($value) {
return $value !== '' && $value !== null;
});
$result = $reportingService->getDateRangeReport($dateRangeParams);
sendJsonResponse($result);
break;
case 'config':
// Get configuration status
$configStatus = getSdkConfigStatus();
$envValidation = validateEnvironmentConfig();
sendJsonResponse([
'success' => true,
'data' => [
'sdk_status' => $configStatus,
'environment_validation' => $envValidation,
'api_endpoints' => [
'search' => '/reports.php?action=search',
'detail' => '/reports.php?action=detail&transaction_id={id}',
'settlement' => '/reports.php?action=settlement',
'disputes' => '/reports.php?action=disputes',
'dispute_detail' => '/reports.php?action=dispute_detail&dispute_id={id}',
'deposits' => '/reports.php?action=deposits',
'deposit_detail' => '/reports.php?action=deposit_detail&deposit_id={id}',
'batches' => '/reports.php?action=batches',
'declines' => '/reports.php?action=declines',
'date_range' => '/reports.php?action=date_range',
'export' => '/reports.php?action=export&format={json|csv}',
'summary' => '/reports.php?action=summary',
'config' => '/reports.php?action=config'
]
],
'timestamp' => date('Y-m-d H:i:s')
]);
break;
case '':
// Default action - show API documentation
sendJsonResponse([
'success' => true,
'data' => [
'name' => 'Global Payments Reporting API',
'version' => '1.0.0',
'description' => 'RESTful API for Global Payments transaction reporting and analytics',
'endpoints' => [
'search' => [
'url' => '/reports.php?action=search',
'method' => 'GET/POST',
'description' => 'Search transactions with filters and pagination',
'parameters' => [
'page' => 'Page number (default: 1)',
'page_size' => 'Results per page (default: 10, max: 100)',
'start_date' => 'Start date (YYYY-MM-DD)',
'end_date' => 'End date (YYYY-MM-DD)',
'transaction_id' => 'Specific transaction ID',
'payment_type' => 'Payment type (sale, refund, authorize, capture)',
'status' => 'Transaction status',
'amount_min' => 'Minimum amount',
'amount_max' => 'Maximum amount',
'card_last_four' => 'Last 4 digits of card'
]
],
'detail' => [
'url' => '/reports.php?action=detail&transaction_id={id}',
'method' => 'GET',
'description' => 'Get detailed transaction information',
'parameters' => [
'transaction_id' => 'Transaction ID (required)'
]
],
'settlement' => [
'url' => '/reports.php?action=settlement',
'method' => 'GET/POST',
'description' => 'Get settlement report',
'parameters' => [
'page' => 'Page number (default: 1)',
'page_size' => 'Results per page (default: 50, max: 100)',
'start_date' => 'Start date (YYYY-MM-DD)',
'end_date' => 'End date (YYYY-MM-DD)'
]
],
'export' => [
'url' => '/reports.php?action=export&format={json|csv}',
'method' => 'GET/POST',
'description' => 'Export transaction data',
'parameters' => [
'format' => 'Export format (json or csv)',
'...filters' => 'Same filters as search endpoint'
]
],
'summary' => [
'url' => '/reports.php?action=summary',
'method' => 'GET/POST',
'description' => 'Get summary statistics',
'parameters' => [
'start_date' => 'Start date (YYYY-MM-DD)',
'end_date' => 'End date (YYYY-MM-DD)'
]
],
'config' => [
'url' => '/reports.php?action=config',
'method' => 'GET',
'description' => 'Get API configuration and status'
]
]
],
'timestamp' => date('Y-m-d H:i:s')
]);
break;
default:
throw new \InvalidArgumentException("Invalid action: {$action}");
}
} catch (\InvalidArgumentException $e) {
handleError($e->getMessage(), 400, 'VALIDATION_ERROR');
} catch (\GlobalPayments\Api\Entities\Exceptions\ApiException $e) {
handleError($e->getMessage(), 400, 'API_ERROR');
} catch (\Exception $e) {
handleError('An unexpected error occurred: ' . $e->getMessage(), 500, 'INTERNAL_ERROR');
}