diff --git a/Altob.NtuInvoiceGateway/Pages/Invoice.cshtml b/Altob.NtuInvoiceGateway/Pages/Invoice.cshtml index f8235bb..04c6fb7 100644 --- a/Altob.NtuInvoiceGateway/Pages/Invoice.cshtml +++ b/Altob.NtuInvoiceGateway/Pages/Invoice.cshtml @@ -54,7 +54,6 @@
- @@ -63,6 +62,21 @@ + @{ + if (string.IsNullOrWhiteSpace(Model.InvoiceData.Identifier)) + { + Model.InvoiceData.Identifier = Model.CompanyTaxId; + } + } + +
+ + + +
+
注意:以下三個選項只能填寫其中一個
diff --git a/Altob.NtuInvoiceGateway/Pages/Invoice.cshtml.cs b/Altob.NtuInvoiceGateway/Pages/Invoice.cshtml.cs index da79cbb..d0da787 100644 --- a/Altob.NtuInvoiceGateway/Pages/Invoice.cshtml.cs +++ b/Altob.NtuInvoiceGateway/Pages/Invoice.cshtml.cs @@ -7,20 +7,24 @@ using System.Text.Json; namespace Altob.NtuInvoiceGateway.Pages; +[IgnoreAntiforgeryToken] // 允許外部系統 POST JSON 請求 public class InvoiceModel : PageModel { private readonly ILogger _logger; private readonly IHttpClientFactory _httpClientFactory; private readonly CompanyInfo _companyInfo; + private readonly InvoiceApiOptions _invoiceApiOptions; public InvoiceModel( ILogger logger, IHttpClientFactory httpClientFactory, - IOptions companyInfo) + IOptions companyInfo, + IOptions invoiceApiOptions) { _logger = logger; _httpClientFactory = httpClientFactory; _companyInfo = companyInfo.Value; + _invoiceApiOptions = invoiceApiOptions.Value; } [BindProperty] @@ -35,55 +39,49 @@ public class InvoiceModel : PageModel public string DisplayTransDateTime { get; set; } = string.Empty; public string DisplayTransAmount { get; set; } = string.Empty; - public void OnGet( - string? identifier = null, - string? transDateTime = null, - string? transAmount = null, - string? deviceID = null, - string? email = null, - string? carrierID = null, - string? locationID = null, - string? carPlateNum = null, - string? orderID = null, - string? buyerIdentifier = null, - // string? loveCode = null, - string? taxType = null) + public void OnGet() { - // 從查詢字串接收系統轉頁傳來的資料 - if (!string.IsNullOrEmpty(identifier)) - InvoiceData.Identifier = identifier; - if (!string.IsNullOrEmpty(transDateTime)) - { - InvoiceData.TransDateTime = transDateTime; - DisplayTransDateTime = transDateTime; - } - if (!string.IsNullOrEmpty(transAmount)) - { - InvoiceData.TransAmount = transAmount; - DisplayTransAmount = transAmount; - } - if (!string.IsNullOrEmpty(deviceID)) - InvoiceData.DeviceID = deviceID; - if (!string.IsNullOrEmpty(email)) - InvoiceData.Email = email; - if (!string.IsNullOrEmpty(carrierID)) - InvoiceData.CarrierID = carrierID; - if (!string.IsNullOrEmpty(locationID)) - InvoiceData.LocationID = locationID; - if (!string.IsNullOrEmpty(carPlateNum)) - InvoiceData.CarPlateNum = carPlateNum; - if (!string.IsNullOrEmpty(orderID)) - InvoiceData.OrderID = orderID; - if (!string.IsNullOrEmpty(buyerIdentifier)) - InvoiceData.BuyerIdentifier = buyerIdentifier; - // if (!string.IsNullOrEmpty(loveCode)) - // InvoiceData.LoveCode = loveCode; - if (!string.IsNullOrEmpty(taxType)) - InvoiceData.TaxType = taxType; + // GET 方法保留為空,主要接收方式改為 POST JSON } public async Task OnPostAsync() { + // 檢查是否為 JSON 請求(外部系統轉頁) + if (Request.ContentType?.Contains("application/json") == true) + { + try + { + // 從 Request Body 讀取 JSON 資料 + using var reader = new StreamReader(Request.Body); + var jsonContent = await reader.ReadToEndAsync(); + + var jsonData = JsonSerializer.Deserialize(jsonContent, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true + }); + + if (jsonData != null) + { + InvoiceData = jsonData; + DisplayTransDateTime = InvoiceData.TransDateTime; + DisplayTransAmount = InvoiceData.TransAmount; + ModelState.Clear(); // ensure Razor uses the JSON payload values + + _logger.LogInformation("Received JSON redirect for OrderID: {OrderID}", InvoiceData.OrderID); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Error parsing JSON request"); + ErrorMessage = "接收轉頁資料時發生錯誤"; + } + + // 顯示表單讓用戶填寫 + return Page(); + } + + // 處理表單提交 // 保留發票資訊顯示 DisplayTransDateTime = InvoiceData.TransDateTime; DisplayTransAmount = InvoiceData.TransAmount; @@ -117,26 +115,54 @@ public class InvoiceModel : PageModel return Page(); } + if (string.IsNullOrWhiteSpace(_invoiceApiOptions.Endpoint)) + { + ErrorMessage = "未設定外部發票 API 的位址"; + _logger.LogError("Invoice API endpoint is not configured"); + return Page(); + } + try { // 呼叫 TODO API(這裡使用規格中的範例 API) + // http://192.168.110.72:22055/api/Intella/invoiceInfo + // { + // "identifier": "12345678", + // "transDateTime": "2026/01/08 12:00:00", + // "transAmount": "30", + // "deviceID": "test", + // "locationID": "1", + // "carPlateNum": "ABC1235", + // "orderID": "260109_1", + // "email": "", + // "carrierID": "/ab12345", + // "buyerIdentifier": "", + // "taxType": "1", + // "loveCode": "" + // } var httpClient = _httpClientFactory.CreateClient(); - var jsonContent = JsonSerializer.Serialize(InvoiceData, new JsonSerializerOptions + var requestBody = JsonSerializer.Serialize(InvoiceData, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); - var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); + var content = new StringContent(requestBody, Encoding.UTF8, "application/json"); - // TODO: 替換為實際的 API 端點 - var response = await httpClient.PostAsync("YOUR_API_ENDPOINT_HERE", content); + var response = await httpClient.PostAsync(_invoiceApiOptions.Endpoint, content); var responseContent = await response.Content.ReadAsStringAsync(); var apiResponse = JsonSerializer.Deserialize(responseContent, new JsonSerializerOptions { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase + PropertyNameCaseInsensitive = true }); + if (!response.IsSuccessStatusCode) + { + ErrorMessage = $"提交失敗:{apiResponse?.msg ?? response.StatusCode.ToString()}"; + _logger.LogWarning("Invoice submission failed with HTTP {StatusCode} for OrderID: {OrderID}", response.StatusCode, InvoiceData.OrderID); + return Page(); + } + if (apiResponse?.msgCode == "0000") { SuccessMessage = "發票資訊提交成功!"; diff --git a/Altob.NtuInvoiceGateway/Pages/TestPostInvoice.cshtml b/Altob.NtuInvoiceGateway/Pages/TestPostInvoice.cshtml index 22c7fa9..dabe75c 100644 --- a/Altob.NtuInvoiceGateway/Pages/TestPostInvoice.cshtml +++ b/Altob.NtuInvoiceGateway/Pages/TestPostInvoice.cshtml @@ -1,4 +1,5 @@ @page +@model TestPostInvoiceModel @{ ViewData["Title"] = "POST 轉頁測試"; Layout = null; @@ -25,12 +26,20 @@
- + 8位數字
- +
@@ -42,26 +51,26 @@
- +
- + 1個字元
- +
- +
@@ -114,6 +123,21 @@
+
+ +
+
+
JSON POST 測試(符合規格要求)
+
+
+

✓ 此方式符合英特拉規格:Method: POST, 資料格式: JSON

+ +
+
+
+
@@ -127,5 +151,55 @@
+ diff --git a/Altob.NtuInvoiceGateway/Pages/TestPostInvoice.cshtml.cs b/Altob.NtuInvoiceGateway/Pages/TestPostInvoice.cshtml.cs index c58b5d0..c7ee67e 100644 --- a/Altob.NtuInvoiceGateway/Pages/TestPostInvoice.cshtml.cs +++ b/Altob.NtuInvoiceGateway/Pages/TestPostInvoice.cshtml.cs @@ -1,9 +1,23 @@ +using System; +using Altob.NtuInvoiceGateway.Models; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.Extensions.Options; namespace Altob.NtuInvoiceGateway.Pages; public class TestPostInvoiceModel : PageModel { + public string CompanyTaxId { get; } + public string CurrentTransDateTime { get; } + public string CurrentOrderNo { get; } + + public TestPostInvoiceModel(IOptions companyInfo) + { + CompanyTaxId = companyInfo.Value.TaxId; + CurrentTransDateTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); + CurrentOrderNo = DateTime.Now.ToString("ORDERyyyyMMddHHmmssfff"); + } + public void OnGet() { } diff --git a/Altob.NtuInvoiceGateway/Program.cs b/Altob.NtuInvoiceGateway/Program.cs index cd178e2..efa201d 100644 --- a/Altob.NtuInvoiceGateway/Program.cs +++ b/Altob.NtuInvoiceGateway/Program.cs @@ -7,6 +7,8 @@ builder.Services.AddHttpClient(); // Configure company info builder.Services.Configure( builder.Configuration.GetSection("CompanyInfo")); +builder.Services.Configure( + builder.Configuration.GetSection("InvoiceApi")); var app = builder.Build(); @@ -28,4 +30,4 @@ app.MapStaticAssets(); app.MapRazorPages() .WithStaticAssets(); -app.Run(); \ No newline at end of file +app.Run(); diff --git a/Altob.NtuInvoiceGateway/appsettings.Development.json b/Altob.NtuInvoiceGateway/appsettings.Development.json index 770d3e9..8a08e7c 100644 --- a/Altob.NtuInvoiceGateway/appsettings.Development.json +++ b/Altob.NtuInvoiceGateway/appsettings.Development.json @@ -5,5 +5,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "InvoiceApi": { + "Endpoint": "http://192.168.110.72:22055/api/Intella/invoiceInfo" } } diff --git a/Altob.NtuInvoiceGateway/appsettings.json b/Altob.NtuInvoiceGateway/appsettings.json index 39cdfe6..6dd2c9f 100644 --- a/Altob.NtuInvoiceGateway/appsettings.json +++ b/Altob.NtuInvoiceGateway/appsettings.json @@ -9,5 +9,8 @@ "CompanyInfo": { "Name": "國立台灣大學臨時停車場", "TaxId": "18384226" + }, + "InvoiceApi": { + "Endpoint": "http://192.168.110.72:22055/api/Intella/invoiceInfo" } }