Files
sam-sales/barobill/etax/api/status.php

169 lines
5.2 KiB
PHP

<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
// 바로빌 API 설정 로드
require_once(__DIR__ . '/barobill_config.php');
// POST 데이터 읽기
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || !isset($input['invoiceId'])) {
http_response_code(400);
echo json_encode([
"success" => false,
"error" => "Invoice ID is required"
], JSON_UNESCAPED_UNICODE);
exit;
}
$invoiceId = $input['invoiceId'];
// SOAP 클라이언트가 초기화되었고 CERTKEY가 있으면 실제 API 호출
$useRealAPI = !empty($barobillSoapClient) && !empty($barobillCertKey);
// 파일에서 데이터 읽기
$dataFile = __DIR__ . '/invoices_data.json';
$existingData = [];
if (file_exists($dataFile)) {
$existingData = json_decode(file_get_contents($dataFile), true);
if (!is_array($existingData)) {
$existingData = [];
}
}
// 기본 데이터에서도 찾기
$defaultInvoices = [
[
"id" => "inv_001",
"issueKey" => "BARO-2024-001",
"status" => "sent"
],
[
"id" => "inv_002",
"issueKey" => "BARO-2024-002",
"status" => "issued"
],
[
"id" => "inv_003",
"issueKey" => "BARO-2024-003",
"status" => "sent"
]
];
$allInvoices = array_merge($defaultInvoices, $existingData['invoices'] ?? []);
// 해당 세금계산서 찾기
$invoice = null;
foreach ($allInvoices as $inv) {
if ($inv['id'] === $invoiceId) {
$invoice = $inv;
break;
}
}
if (!$invoice) {
http_response_code(404);
echo json_encode([
"success" => false,
"error" => "Invoice not found"
], JSON_UNESCAPED_UNICODE);
exit;
}
if ($useRealAPI && isset($invoice['issueKey'])) {
// 실제 바로빌 SOAP API 호출 - 국세청 전송
// issueKey를 MgtKey로 사용
$mgtKey = $invoice['mgtKey'] ?? $invoice['issueKey'];
$apiResult = sendToNTS($mgtKey);
if ($apiResult['success']) {
$apiData = $apiResult['data'];
// SOAP 응답에서 NTS 접수번호 추출
$ntsReceiptNo = '';
if (is_object($apiData) && isset($apiData->NTSConfirmNum)) {
$ntsReceiptNo = $apiData->NTSConfirmNum;
} elseif (is_array($apiData) && isset($apiData['NTSConfirmNum'])) {
$ntsReceiptNo = $apiData['NTSConfirmNum'];
}
// 상태 업데이트
$invoice['status'] = 'sent';
$invoice['ntsReceiptNo'] = $ntsReceiptNo;
$invoice['sentAt'] = date('Y-m-d\TH:i:s');
// 파일에 저장된 데이터 업데이트
if (isset($existingData['invoices'])) {
foreach ($existingData['invoices'] as &$inv) {
if ($inv['id'] === $invoiceId) {
$inv = $invoice;
break;
}
}
file_put_contents($dataFile, json_encode($existingData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}
$response = [
"success" => true,
"message" => "국세청 전송이 완료되었습니다.",
"data" => [
"issueKey" => $invoice['issueKey'] ?? '',
"mgtKey" => $mgtKey,
"ntsReceiptNo" => $invoice['ntsReceiptNo'],
"status" => "sent",
"sentAt" => $invoice['sentAt']
],
"invoice" => $invoice,
"api_response" => $apiData,
"soap_request" => $apiResult['soap_request'] ?? null,
"soap_response" => $apiResult['soap_response'] ?? null
];
} else {
$response = [
"success" => false,
"error" => $apiResult['error'] ?? "API 호출 실패",
"error_code" => $apiResult['error_code'] ?? null,
"error_detail" => $apiResult['error_detail'] ?? null,
"soap_request" => $apiResult['soap_request'] ?? null,
"soap_response" => $apiResult['soap_response'] ?? null
];
}
} else {
// 시뮬레이션 모드
$ntsReceiptNo = "NTS-" . date('Y') . "-" . str_pad(rand(100000, 999999), 6, '0', STR_PAD_LEFT);
$invoice['status'] = 'sent';
$invoice['ntsReceiptNo'] = $ntsReceiptNo;
$invoice['sentAt'] = date('Y-m-d\TH:i:s');
// 파일에 저장된 데이터 업데이트
if (isset($existingData['invoices'])) {
foreach ($existingData['invoices'] as &$inv) {
if ($inv['id'] === $invoiceId) {
$inv = $invoice;
break;
}
}
file_put_contents($dataFile, json_encode($existingData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}
usleep(800000); // 0.8초 지연 시뮬레이션
$response = [
"success" => true,
"message" => "국세청 전송이 완료되었습니다. (시뮬레이션 모드)",
"data" => [
"issueKey" => $invoice['issueKey'] ?? '',
"ntsReceiptNo" => $ntsReceiptNo,
"status" => "sent",
"sentAt" => $invoice['sentAt']
],
"invoice" => $invoice,
"simulation" => true
];
}
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
?>