快速使用财政部电子发票API 使用 C#

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

看完文件后发现他的API其实蛮单纯,每个API的签章和参数排序方式都一样,唯一不同地方是呼叫API所带的参数

所以我做一个小框架可方便日后扩展 "电子发票相关的API"

原始码放到 Github

财政部电子发票API文件连结

电子发票API申请连结

使用这个API框架很简单 只须完成以下几个步骤

Config输入 财政部申请的appId和appKey创建两个Model一个是财政部所需参数的Model一个是财政部回应参数的Model创建一个要实作的Api物件并继承ApiBase抽象类,并在泛型那边指定所需 Model的类型在外部使用 MoblieInvoiceApiFactroy.GetInstace 方法将 传入所需Model,工厂会反射回应相对应的Api物件使用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/


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章