-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReportingAndClearance.php
101 lines (73 loc) · 3.46 KB
/
ReportingAndClearance.php
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
<?php
require_once("Helpers/ApiHelper.php");
require_once("Helpers/InvoiceHelper.php");
require_once("Signer/EInvoiceSigner.php");
echo "\nRUN THIS CODE AFTER SUCCESSFULLY ONBOARDED\n";
echo "\nClearance & Reporting\n";
$certInfo = ApiHelper::loadJsonFromFile("certificate/certificateInfo.json");
$xmlTemplatePath = "Resources/Invoice.xml";
$privateKey = $certInfo["privateKey"];
$x509CertificateContent = base64_decode($certInfo["pcsid_binarySecurityToken"]);
$baseDocument = new DOMDocument();
$baseDocument->preserveWhiteSpace = true;
$baseDocument->load($xmlTemplatePath);
$documentTypes = [
["STDSI", "388", "Standard Invoice",""],
["STDCN", "383", "Standard CreditNote","InstructionNotes for Standard CreditNote"],
["STDDN", "381", "Standard DebitNote" , "InstructionNotes for Standard DebitNote"],
["SIMSI", "388", "Simplified Invoice",""],
["SIMCN", "383", "Simplified CreditNote", "InstructionNotes for Simplified CreditNote"],
["SIMDN", "381", "Simplified DebitNote", "InstructionNotes for Simplified DebitNote"]
];
$icv = $certInfo["lastICV"];
$pih = $certInfo["lastInvoiceHash"];
foreach ($documentTypes as $docType) {
list($prefix, $typeCode, $description, $instructionNote) = $docType;
$icv++;
$isSimplified = strpos($prefix, "SIM") === 0;
echo "\nProcessing {$description}...";
$newDoc = InvoiceHelper::ModifyXml($baseDocument, "{$prefix}-0001", $isSimplified ? "0200000" : "0100000", $typeCode, $icv, $pih, $instructionNote);
$jsonPayload = EInvoiceSigner::GetRequestApi($newDoc, $x509CertificateContent, $privateKey, true);
if($isSimplified){
//echo stripslashes($jsonPayload);
$requestType = "Invoice Reporting";
$apiUrl = $certInfo["reportingUrl"];
$response = ApiHelper::invoiceReporting($certInfo, $jsonPayload);
}else{
$requestType = "Invoice Clearance";
$apiUrl = $certInfo["clearanceUrl"];
$response = ApiHelper::invoiceClearance($certInfo, $jsonPayload);
}
//echo $response;
$cleanResponse = ApiHelper::cleanUpJson($response, $requestType, $apiUrl);
$jsonDecodedResponse = json_decode($response, true);
if ($jsonDecodedResponse) {
echo "\n\ncomplianceChecks Server Response: \n" . $cleanResponse;
} else {
echo "\n\nInvalid JSON Response: \n" . $response;
return false;
}
if ($response === null) {
echo "Failed to process {$description}: serverResult is null.\n";
return false;
}
$status = $isSimplified ? $jsonDecodedResponse["reportingStatus"] : $jsonDecodedResponse["clearanceStatus"];
if (strpos($status, "REPORTED") !== false || strpos($status, "CLEARED") !== false) {
$jsonPayload = json_decode($jsonPayload, true);
$certInfo["lastICV"] = $icv;
if($isSimplified){
$pih = $jsonPayload["invoiceHash"];
$certInfo["lastInvoiceHash"]=$pih;
}else{
list($invoiceHash, $base64QRCode) = InvoiceHelper::ExtractInvoiceHashAndBase64QrCode($jsonDecodedResponse["clearedInvoice"]);
$pih = $invoiceHash ;
}
ApiHelper::saveJsonToFile( "certificate/certificateInfo.json", $certInfo);
echo "\n{$description} processed successfully\n\n";
} else {
echo "Failed to process {$description}: status is {$status}\n";
return false;
}
usleep(200 * 1000); // 200 ms delay
}
?>