iT邦幫忙

10

快速使用財政部電子發票API 使用 C#

石頭 2017-07-10 18:36:3632348 瀏覽

小弟最近在串接使用財政部的API

看完文件後發現他的API其實蠻單純,每個API的簽章和參數排序方式都一樣,唯一不同地方是呼叫API所帶的參數

所以我做一個小框架可方便日後擴展 "電子發票相關的API"

原始碼放到 Github

財政部電子發票API文件連結

電子發票API申請連結

使用這個API框架很簡單 只須完成以下幾個步驟

  1. Config輸入 財政部申請的appId和appKey
  2. 創建兩個Model
    • 一個是財政部所需參數的Model
    • 一個是財政部回應參數的Model
  3. 創建一個要實作的Api物件並繼承ApiBase抽象類,並在泛型那邊指定所需 Model的類型
  4. 在外部使用 MoblieInvoiceApiFactroy.GetInstace 方法將 傳入所需Model,工廠會反射回應相對應的Api物件
  5. 使用Api物件.ExcuteApi方法 傳入所需Model
    下面詳細說明使用方法已 "QryWinningList" 查詢中獎發票號碼清單 為例子

打開webConfig輸入你的appKey和appId

<!--在這裡填入跟財政部申請的AppId-->
<add key="GovAppId" value="" />
<!--在這裡填入跟財政部申請的AppKey-->
<add key="GovAPIKey" value=""/>

在Model資料夾中創建2個Class

public class QryWinningListModel
{
    public string invTerm { get; set; }
}

public class QryWinningListViewModel
{
    public string code { get; set; }
    public string msg { get; set; }
    public string invoYm { get; set; }
    public string superPrizeNo { get; set; }
    public string spcPrizeNo { get; set; }
    public string spcPrizeNo2 { get; set; }
    public string spcPrizeNo3 { get; set; }
    public string firstPrizeNo1 { get; set; }
    public string firstPrizeNo2 { get; set; }
    public string firstPrizeNo3 { get; set; }
    public string firstPrizeNo4 { get; set; }
    public string firstPrizeNo5 { get; set; }
    public string firstPrizeNo6 { get; set; }
    public string firstPrizeNo7 { get; set; }
    public string firstPrizeNo8 { get; set; }
    public string firstPrizeNo9 { get; set; }
    public string firstPrizeNo10 { get; set; }
    public string sixthPrizeNo1 { get; set; }
    public string sixthPrizeNo2 { get; set; }
    public string sixthPrizeNo3 { get; set; }
    public string superPrizeAmt { get; set; }
    public string spcPrizeAmt { get; set; }
    public string firstPrizeAmt { get; set; }
    public string secondPrizeAmt { get; set; }
    public string thirdPrizeAmt { get; set; }
    public string fourthPrizeAmt { get; set; }
    public string fifthPrizeAmt { get; set; }
    public string sixthPrizeAmt { get; set; }
    public string sixthPrizeNo4 { get; set; }
    public string sixthPrizeNo5 { get; set; }
    public string sixthPrizeNo6 { get; set; }
}

Service資料夾創建 qryInvDetailApi 類別 並繼承 ApiBase 在泛型上提供 財政部所需參數Model的型別

public class qryInvDetailApi : ApiBase<qryInvDetailModel>
{
    protected override string SetParamter(qryInvDetailModel model)
    {
        SortedDictionary<string, string> paramter = new SortedDictionary<string, string>();
        paramter["version"] = "0.3";
        paramter["action"] = "qryInvDetail";
        paramter["invTerm"] = model.invTerm;
        paramter["UUID"] = UUID;
        paramter["type"] = model.type;
        paramter["invNum"] = model.invNum;
        paramter["generation"] = model.generation;
        paramter["invTerm"] = model.invTerm;
        paramter["invDate"] = model.invDate;
        paramter["encrypt"] = model.encrypt;
        paramter["sellerID"] = model.sellerID;
        paramter["randomNumber"] = model.randomNumber;
        paramter["appID"] = GovAppId;
        return PraramterHelper.DictionaryToParamter(paramter);
    }
}

在上面實現父類的 SetParamter 方法提供Paramter參數,而可以使用 SortedDictionary 因為參數要按升冪排列

在InvoiceController 使用工廠創建Api物件將所需參數Model傳入

使用回傳物件的ExcuteApi方法將所需參數Model傳入

最後財政部就會回傳執行結果Json


2018-12-16 更新

將此框架發布到Nuget上

https://www.nuget.org/packages/ElectronicInvoice_TW/


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
海綿寶寶
iT邦大神 1 級 ‧ 2017-07-11 08:53:57

我看不懂
不過我覺得這是個很有用的API
感謝您把他分享出來
/images/emoticon/emoticon41.gif/images/emoticon/emoticon41.gif/images/emoticon/emoticon41.gif

石頭 iT邦高手 1 級 ‧ 2017-07-11 17:59:51 檢舉

謝謝海綿大大
小弟第一次發文有些地方寫的不好再請見諒
剛剛才發現我忘記附上api文件的連結了XD
現在已經補上了

0
chommy
iT邦新手 1 級 ‧ 2017-07-11 10:53:59

感謝D大無私的分享

石頭 iT邦高手 1 級 ‧ 2017-07-11 18:00:26 檢舉

不會^^
大家可以互相交流想法!!

2
yuanshang
iT邦新手 4 級 ‧ 2017-07-11 12:19:35

非常有用的示範程式,尤其是 ApiBase、reflection and factory,易於使用與擴展,感謝!

石頭 iT邦高手 1 級 ‧ 2017-07-11 18:03:24 檢舉

您真內行!!
我使用工廠來製造一系列的API
而後面的人只需繼承ApiBase 只需填寫參數即可撰寫完成api

0
小魚
iT邦大師 1 級 ‧ 2017-07-11 12:38:28

感謝大大的分享
/images/emoticon/emoticon41.gif

石頭 iT邦高手 1 級 ‧ 2017-07-11 18:01:10 檢舉

不會^^
大家可以互相交流想法!!

0
mamalovei
iT邦新手 5 級 ‧ 2017-07-15 21:18:45

謝謝分享!

石頭 iT邦高手 1 級 ‧ 2018-02-21 13:22:14 檢舉

/images/emoticon/emoticon12.gif

0
shaojim12
iT邦見習生 ‧ 2018-02-10 05:06:54

你好:
我最近自己用c#寫了一個pos機,我接下來想讓他能夠開電子發票。
但是我發現申請電子發票必須要有樣本,但我又還沒有開過電子發票,這樣怎麼會有樣本,還是我有那邊搞錯了嗎?
小弟是新手,還不太懂這些,希望能夠給一些指點,感謝您

石頭 iT邦高手 1 級 ‧ 2018-02-21 13:22:03 檢舉

我目前只碰查詢端API對於開立那邊不熟悉><,請問您有開立發票相關文件嗎?我所知道大平台API文件大部分文件都在這邊,希望對你有幫助

0
tsengs
iT邦新手 4 級 ‧ 2018-12-17 09:43:29

感謝分享

石頭 iT邦高手 1 級 ‧ 2018-12-17 16:34:51 檢舉

不客氣^^

1
blantt
iT邦新手 5 級 ‧ 2020-08-25 13:44:44

請教一下,我run專案時,出現一個錯誤在
public class MyApi : ApiBase{
....
}
它說: CS7036 未提供任何可對應到 'ApiBase.ApiBase(IConfig, ISysLog)' 之必要型式參數 'config' 的引數
,
請問為何呢 ? 謝謝!

石頭 iT邦高手 1 級 ‧ 2020-08-25 14:14:41 檢舉

如果你要擴充使用ApiBase需要指定泛型,目前Base Class需要傳入 (IConfig config,ISysLog log) 物件當作建構子參數

blantt iT邦新手 5 級 ‧ 2020-09-03 10:43:20 檢舉

感謝,可以正常執行了^^

0
codecetfp
iT邦新手 5 級 ‧ 2020-11-28 18:00:48

目前正在開發電子發票APP,想請教一下,
在電子發票API文件中,查詢發票明細時,需要"encrypt"發票檢驗碼參數,
請問這個參數是甚麼呢?
之前看到一篇"電子發票二維碼規格"文件當中提到掃描電子發票左邊的QRCode可獲得的資訊當中,含有24碼的加密驗證資訊,是指這個嗎?

石頭 iT邦高手 1 級 ‧ 2020-11-30 13:59:40 檢舉

對 需要從QRCode中取得.

codecetfp iT邦新手 5 級 ‧ 2020-12-11 22:09:20 檢舉

可以執行了!感謝!

0
阿JOE
iT邦新手 5 級 ‧ 2021-01-08 04:40:14

請教一下...

用了此API,是否就不用再透過...TURNKEY或加值中心的平台來上傳發票了?

石頭 iT邦高手 1 級 ‧ 2021-01-12 14:12:46 檢舉

您好 這個是查詢發票API框架 尚未支援 上傳發票

阿JOE iT邦新手 5 級 ‧ 2021-01-20 00:47:07 檢舉

了解,謝謝您的回覆

我要留言

立即登入留言