Skip to content

Instantly share code, notes, and snippets.

@NestorHuang
Created September 15, 2017 03:24
Show Gist options
  • Save NestorHuang/3a5141210c4d787d2715c48adb712644 to your computer and use it in GitHub Desktop.
Save NestorHuang/3a5141210c4d787d2715c48adb712644 to your computer and use it in GitHub Desktop.
e首發票C#呼叫新增訂單Sample Code
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace POC.HttpClientSample
{
class Program
{
static void Main(string[] args)
{
AppendOrder();
}
private static void AppendOrder()
{
RequestObject _requestObjects = getRequestData();
string _serviceUrl = "https://teinv.systemlead.com/";
string _getListUrl = string.Format("{0}api/Append/Order", _serviceUrl);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(_getListUrl);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
HttpResponseMessage response = client.PostAsync(_getListUrl,
new StringContent(JsonConvert.SerializeObject(_requestObjects).ToString(),
Encoding.UTF8, "application/json")).Result;
if (response.IsSuccessStatusCode)
{
var jsonString = response.Content.ReadAsStringAsync();
jsonString.Wait();
var _result = JsonConvert.DeserializeObject<ResponseObject>(jsonString.Result);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static RequestObject getRequestData()
{
RequestObject _request = new RequestObject();
_request.CompanyID = "12345678";
_request.Timestamp = "1477585655";
_request.Signature = "4754A5FE41749DE99E82FC5502348C86912E3DCF562D769781E1026064ADF7AC";
_request.Data = getOrder();
return _request;
}
private static Order getOrder()
{
Order _order = new Order();
_order.CompanyID = "12345678";
_order.InvoiceFor = "B";
_order.BillingNo = "OrderID0001";
_order.BuyerID = "23456789";
_order.BuyerInvoiceTitle = "範例測試有限公司";
_order.BuyerName = "購買人姓名";
_order.BuyerTelNo = "077190888";
_order.BuyerAddress = "高雄市鳳山區光遠路226號B1";
_order.BuyerEmail = "[email protected]";
_order.RelateNumber = "PO-12345678";
_order.PrintMark = "N";
_order.TaxType = "1";
_order.SalesAmount = 5238;
_order.TaxAmount = 262;
_order.TotalAmount = 5500;
_order.Details = new List<Detail>();
Detail _detail = new Detail();
_detail.DetailID = "001";
_detail.ProductID = "P1234";
_detail.ProductName = "產品名稱";
_detail.Quantity = 5.0M;
_detail.UnitPrice = 95.238M;
_detail.SubTotal = 476.19M;
_order.Details.Add(_detail);
_detail = new Detail();
_detail.DetailID = "003";
_detail.ProductID = "P3456";
_detail.ProductName = "明細編號3碼";
_detail.Quantity = 4.0M;
_detail.UnitPrice = 952.3809M;
_detail.SubTotal = 3809.5236M;
_detail.Remark = "貨品缺1件候補";
_order.Details.Add(_detail);
return _order;
}
}
public class RequestObject
{
public string CompanyID { get; set; }
public string Timestamp { get; set; }
public string Signature { get; set; }
public Order Data { get; set; }
}
public class Order
{
public string CompanyID { get; set; }
public string InvoiceFor { get; set; }
public string BillingNo { get; set; }
public string BuyerID { get; set; }
public string BuyerInvoiceTitle { get; set; }
public string BuyerName { get; set; }
public string BuyerTelNo { get; set; }
public string BuyerEmail { get; set; }
public string BuyerAddress { get; set; }
public string BuyerCustomerNumber { get; set; }
public string RelateNumber { get; set; }
public string PrintMark { get; set; }
public string TaxType { get; set; }
public long SalesAmount { get; set; }
public long TaxAmount { get; set; }
public long TotalAmount { get; set; }
public List<Detail> Details { get; set; }
}
public class Detail
{
public string DetailID { get; set; }
public string ProductID { get; set; }
public string ProductName { get; set; }
public decimal Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal SubTotal { get; set; }
public string Remark { get; set; }
}
public class ResponseObject
{
public string InvoiceID { get; set; }
public DateTime? InvoiceDateTime { get; set; }
public string AuthCode { get; set; }
public string Message { get; set; }
public List<string> CheckedErrorResult { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment