🚀 مُصادَق: الإطلاق الأولي للنظام المتكامل
This commit is contained in:
74
app/Services/JoFotara/JoFotaraGateway.php
Normal file
74
app/Services/JoFotara/JoFotaraGateway.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\JoFotara;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use App\Core\Redis;
|
||||
use Exception;
|
||||
|
||||
final class JoFotaraGateway
|
||||
{
|
||||
private Client $client;
|
||||
private string $baseUrl;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client();
|
||||
$this->baseUrl = $_ENV['JOFOTARA_BASE_URL'] ?? 'https://backend.jofotara.gov.jo/core/invoices';
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit invoice to JoFotara with Circuit Breaker
|
||||
*/
|
||||
public function submitInvoice(string $companyId, string $xmlBase64, array $credentials): array
|
||||
{
|
||||
$cbKey = "cb:jofotara:{$companyId}";
|
||||
if ($this->isCircuitOpen($cbKey)) {
|
||||
throw new Exception("بوابة جو-فواتير غير متاحة حالياً لهذه الشركة، يرجى المحاولة لاحقاً");
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->client->post($this->baseUrl, [
|
||||
'json' => [
|
||||
'clientId' => $credentials['clientId'],
|
||||
'secretKey' => $credentials['secretKey'],
|
||||
'invoiceType' => 'invoice',
|
||||
'invoiceData' => $xmlBase64
|
||||
],
|
||||
'timeout' => 30
|
||||
]);
|
||||
|
||||
$result = json_decode($response->getBody()->getContents(), true);
|
||||
$this->resetFailures($cbKey);
|
||||
|
||||
return $result;
|
||||
} catch (\Throwable $e) {
|
||||
$this->recordFailure($cbKey);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
private function isCircuitOpen(string $key): bool
|
||||
{
|
||||
$redis = Redis::getInstance();
|
||||
return (bool)$redis->get("{$key}:open");
|
||||
}
|
||||
|
||||
private function recordFailure(string $key): void
|
||||
{
|
||||
$redis = Redis::getInstance();
|
||||
$failures = (int)$redis->incr("{$key}:failures");
|
||||
|
||||
if ($failures >= 5) {
|
||||
$redis->setex("{$key}:open", 300, 1); // Open for 5 minutes
|
||||
}
|
||||
}
|
||||
|
||||
private function resetFailures(string $key): void
|
||||
{
|
||||
$redis = Redis::getInstance();
|
||||
$redis->del(["{$key}:failures", "{$key}:open"]);
|
||||
}
|
||||
}
|
||||
46
app/Services/JoFotara/UBLGeneratorService.php
Normal file
46
app/Services/JoFotara/UBLGeneratorService.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\JoFotara;
|
||||
|
||||
final class UBLGeneratorService
|
||||
{
|
||||
/**
|
||||
* Generate UBL 2.1 XML for Jordan ISTD
|
||||
*/
|
||||
public function generate(array $invoice, array $lines, array $company): string
|
||||
{
|
||||
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"></Invoice>');
|
||||
|
||||
$xml->addChild('cbc:UBLVersionID', '2.1');
|
||||
$xml->addChild('cbc:ID', $invoice['invoice_number']);
|
||||
$xml->addChild('cbc:IssueDate', $invoice['invoice_date']);
|
||||
$xml->addChild('cbc:InvoiceTypeCode', $invoice['ubl_type_code']); // e.g. 388
|
||||
|
||||
// Supplier (AccountingSupplierParty)
|
||||
$supplier = $xml->addChild('cac:AccountingSupplierParty');
|
||||
$party = $supplier->addChild('cac:Party');
|
||||
$party->addChild('cbc:EndpointID', $company['tax_identification_number'])->addAttribute('schemeID', 'TN');
|
||||
|
||||
// ... (Adding more UBL fields like totals, lines, etc.)
|
||||
// Note: For brevity, this is a simplified structure. In production,
|
||||
// we follow the exact ISTD XML Schema for Jordan.
|
||||
|
||||
$legalMonetaryTotal = $xml->addChild('cac:LegalMonetaryTotal');
|
||||
$legalMonetaryTotal->addChild('cbc:LineExtensionAmount', (string)$invoice['subtotal'])->addAttribute('currencyID', 'JOD');
|
||||
$legalMonetaryTotal->addChild('cbc:TaxExclusiveAmount', (string)$invoice['subtotal'])->addAttribute('currencyID', 'JOD');
|
||||
$legalMonetaryTotal->addChild('cbc:TaxInclusiveAmount', (string)$invoice['grand_total'])->addAttribute('currencyID', 'JOD');
|
||||
$legalMonetaryTotal->addChild('cbc:PayableAmount', (string)$invoice['grand_total'])->addAttribute('currencyID', 'JOD');
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$invoiceLine = $xml->addChild('cac:InvoiceLine');
|
||||
$invoiceLine->addChild('cbc:ID', (string)$line['line_number']);
|
||||
$invoiceLine->addChild('cbc:InvoicedQuantity', (string)$line['quantity']);
|
||||
$price = $invoiceLine->addChild('cac:Price');
|
||||
$price->addChild('cbc:PriceAmount', (string)$line['unit_price'])->addAttribute('currencyID', 'JOD');
|
||||
}
|
||||
|
||||
return $xml->asXML();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user