今天進入開立發票環節
完成昨天的前置作業後
就可以開始進行發票開立作業
由於發票開立規則複雜
對應的情境也很多
有許多參數在某些情況下是必填,有時又是選填
我會建議先自行閱讀綠界的官方技術文件
並確認公司開立發票的種類後
再進行串接才是正確的步驟喔
那我們開始吧!
電子發票是採用json格式
千萬不要用錯
Content Type :application/json
HTTP Method :POST
首先,先帶大家看呼叫電子發票的payload架構
{
"MerchantID": "2000132",
"RqHeader": {
"Timestamp": 帶入當下的時間戳
},
"Data": "加密資料(開立發票的參數)"
}
這裡要注意的是Timestamp必須為(GMT+8)的時間
且驗證時間區間只有10分鐘
如果超過這個期限
Timestamp就會失效
Request也會失敗哦
所以自己的主機必須進行時間校正
避免產生時差導致呼叫API失敗
那Data應該放甚麼呢?
這裡舉個例子:
消費者到我的網站購買以下的東西:
5個單價10元的「玩具1」(含稅)
10個單價為60元的「玩具2」(含稅)
發票總金額為650元
且消費者選擇拿到紙本發票
原始的Data大概是長這樣
{
"MerchantID": "2000132",
"RelateNumber": "20240918093209",
"CustomerID": "",
"CustomerIdentifier": "",
"CustomerName": "tester",
"CustomerAddr": " ",
"CustomerPhone": "0911111111",
"CustomerEmail": "aaa@test.com",
"Print": "1",
"Donation": "0",
"LoveCode": "",
"CarrierType": "",
"CarrierNum": "",
"TaxType": "1",
"SalesAmount": 650,
"Items": [
{
"ItemName": "toy1",
"ItemCount": 5,
"ItemWord": "pcs",
"ItemPrice": 10,
"ItemAmount": 50
},
{
"ItemName": "toy2",
"ItemCount": 10,
"ItemWord": "pcs",
"ItemPrice": 60,
"ItemAmount": 600
}
],
"InvType": "07",
"vat": "1"
}
確認好發票開立資訊後
再將上面的data進行AES加密
XeEOdHpTRvxKEqs/JD9RSd16s7VtpyWVCN6AV44pKTV7XoPByaStato0iqOI39rIIaMsANswngbBd822nVES1Vk01+vtRiP0gl220hMB0R/mrJubd78rx7qX//RN+pWl1C0CxhnMv5s3KIBAQMb2BP/kuqK9KMyZ7IDAKY5pRz7NiS8jetf285YScxIDeBl0EtbUXkLliDBWGGXycU9dU6FMO1Wn6PHsf1BRT2qtoP0t/Nj7JqiYPSrIyngwUEecIizFbiHnMk7eY6hNeBDF4oOlveqncWtmSiCXPPC/WCCl5Dt0DXZjenDTqlDUDtk3X3WOD5JTrms7fMNkePNGUh9qNb6V5lyqDuyvT3DmVNbTmp8R68r6Te9W4+P+qZbvUuIm3TUbnlkHs77fvrbyHXLn1EJzWnc4VjXF1tIQpykbbSot6Jn8O19+v0k1NJQpJAmdEy1Fm15z/U7n8B8PgvHaaz38bs1Gg8gb9bslGQfOQl6e25g7pB581QWiiemiUHNXSEpnc5DsDb9wr9e/cY6FWI8AXd5Fhjkidd8YoW8tJ6EBHIO4oqi9QLbxDJGZ73IEBiX9kJ2RUyhT7T0vK81NWy5CSme3wpcAKLr+8bMMIwqPimXCElxKIjaqXkxK96zYmhI8xvNsZqBGJZA/uZYo7SiZmC1pgzV42f194EXXV5y2bamGdpen54C7GHxC9n7ISoWBYJMq3GixoVZcZu7wWzWaAOgRAk8p+XK6zyWkBS9ipCfxsIuKGthTL8h9/wh/BrVh8LsJeX1oksJkV4ZZurVA3Z5kw2Lq37F/07J1veVQguslBATjEXBRADWJLxKYLXCBr0puN2ufyBTVVkNKspR33A9wnNOPKjIwul8B7+0t1YV4wyEogZv8KVJOE9pxLz/p6JO1wrCAtwfUDlGAdSpNhC3a7q9c9FMB4BL6dnWT6dNybg38rtuu3+cLGuoQRt31eTDqqwdTcGfnaLzR6rAF224KT0ScUVytcJGH+6CK9GotPe4IZXsdFKyXnn3fETM1QVZBjGkKlk5y5b2CfbZJouPHYIozLxFkpeaMjDl6yuIXWfDfdaELq0ZOAPQc3DJ7wsO51XOI8w8mkw==
實際呼叫「一般開立發票」API的Payload就會長這樣:
{
"MerchantID": "2000132",
"RqHeader": {
"Timestamp": 1726628024
},
"Data": "XeEOdHpTRvxKEqs/JD9RSd16s7VtpyWVCN6AV44pKTV7XoPByaStato0iqOI39rIIaMsANswngbBd822nVES1Vk01+vtRiP0gl220hMB0R/mrJubd78rx7qX//RN+pWl1C0CxhnMv5s3KIBAQMb2BP/kuqK9KMyZ7IDAKY5pRz7NiS8jetf285YScxIDeBl0EtbUXkLliDBWGGXycU9dU6FMO1Wn6PHsf1BRT2qtoP0t/Nj7JqiYPSrIyngwUEecIizFbiHnMk7eY6hNeBDF4oOlveqncWtmSiCXPPC/WCCl5Dt0DXZjenDTqlDUDtk3X3WOD5JTrms7fMNkePNGUh9qNb6V5lyqDuyvT3DmVNbTmp8R68r6Te9W4+P+qZbvUuIm3TUbnlkHs77fvrbyHXLn1EJzWnc4VjXF1tIQpykbbSot6Jn8O19+v0k1NJQpJAmdEy1Fm15z/U7n8B8PgvHaaz38bs1Gg8gb9bslGQfOQl6e25g7pB581QWiiemiUHNXSEpnc5DsDb9wr9e/cY6FWI8AXd5Fhjkidd8YoW8tJ6EBHIO4oqi9QLbxDJGZ73IEBiX9kJ2RUyhT7T0vK81NWy5CSme3wpcAKLr+8bMMIwqPimXCElxKIjaqXkxK96zYmhI8xvNsZqBGJZA/uZYo7SiZmC1pgzV42f194EXXV5y2bamGdpen54C7GHxC9n7ISoWBYJMq3GixoVZcZu7wWzWaAOgRAk8p+XK6zyWkBS9ipCfxsIuKGthTL8h9/wh/BrVh8LsJeX1oksJkV4ZZurVA3Z5kw2Lq37F/07J1veVQguslBATjEXBRADWJLxKYLXCBr0puN2ufyBTVVkNKspR33A9wnNOPKjIwul8B7+0t1YV4wyEogZv8KVJOE9pxLz/p6JO1wrCAtwfUDlGAdSpNhC3a7q9c9FMB4BL6dnWT6dNybg38rtuu3+cLGuoQRt31eTDqqwdTcGfnaLzR6rAF224KT0ScUVytcJGH+6CK9GotPe4IZXsdFKyXnn3fETM1QVZBjGkKlk5y5b2CfbZJouPHYIozLxFkpeaMjDl6yuIXWfDfdaELq0ZOAPQc3DJ7wsO51XOI8w8mkw=="
}
若前置作業有做
參數也都正確並成功呼叫API後
可以收到綠界回傳:
{
"MerchantID": 2000132,
"RpHeader": {
"Timestamp": 1726628025,
},
"TransCode": 1,
"TransMsg": "Success",
"Data": "6xafbXXjyQnBuHWKr5nDl3elIhyu1OUPiZsXnreThwQMIYbPFyWZHL9ALxDT9maQm1zzov+o2Suh56+NpeqrtK7A6EwTVdAJGbl/4ATxEScKukTLEjpSflzkNBeN513sqfGsjM8REOlO29AJFOeRnq77T3j3hw/J1MGWYX5fxwmghnRt1w6ucRVmmy2URzGVqYrLsLe/3e729ziChmhFHOilY2s/jLkfoHEGuW7rTulxO6SVKnCFV0/775JfVT4uF63fZDnFFj7A4J/2gmtLmSU2MfYqpNZyuGuVqMG5bTmNgkF92VhZAHCaIiL/sqY+"
}
很多人看到TransCode:1,就認為是發票開立成功
其實TransCode:1的意思是代表API傳輸資料成功
實際開立發票的結果在data裡面
只要將取回的payload內的data進行反向解密就可以囉
{
"RtnCode": 1,
"RtnMsg": "開立發票成功",
"InvoiceNo": "SK10023612",
"InvoiceDate": "2024-09-18 10:53:44",
"RandomNumber": "8416"
}
只有收到「RtnCode: 1」才算是成功哦!
完成後可以登入綠界後台查詢是不是真的有成功開立發票
實際的發票會長這樣
以上,就是開立發票的完整過程
明天開始會說一些細項的部分
有問題都可以留言
我們明天見