-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedInvoiceExample.cs
More file actions
125 lines (117 loc) · 4.52 KB
/
RedInvoiceExample.cs
File metadata and controls
125 lines (117 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using Tax.Invoice;
using System.Text;
public class RedInvoiceExample
{
/**
* 示例入口,演示POST multipart/form-data请求
*/
public static void Main(string[] args)
{
try
{
var appKey = "";
var appSecret = "";
var nsrsbh = "";
var username = "";
// var password = "";
var type = "7";
// var title = "";
var fphm = "26502000000569538151";
// var kprq = "";
var token = "";
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("dotnet " + Environment.Version);
var client = new InvoiceClient(appKey, appSecret);
/*
* 获取授权Token文档
* @see https://fa-piao.com/doc.html#api1?source=github
*/
var authResponse = client.GetAuthorization(nsrsbh, type);
if (authResponse.IsSuccess)
{
token = authResponse.Data.Token;
client.SetAuthorization(token);
}
/*
* 1. 数电申请红字前查蓝票信息接口
* @link https://fa-piao.com/doc.html#api8?source=github
*/
var queryInvoiceParams = new Dictionary<string, object>
{
["nsrsbh"] = nsrsbh,
["fphm"] = fphm,
["username"] = username,
["sqyy"] = "2"
};
var queryInvoiceResponse = client.QueryBlueTicketInfo(queryInvoiceParams);
if (queryInvoiceResponse.IsSuccess)
{
Console.WriteLine("1 可以申请红字");
System.Threading.Thread.Sleep(2000);
/*
* 2. 申请红字信息表
* @link https://fa-piao.com/doc.html#api9?source=github
*/
var applyRedParams = new Dictionary<string, object>
{
["xhdwsbh"] = nsrsbh,
["yfphm"] = fphm,
["username"] = username,
["sqyy"] = "2",
["chyydm"] = "01"
};
var applyRedResponse = client.ApplyRedInfo(applyRedParams);
if (applyRedResponse.IsSuccess)
{
Console.WriteLine("2 申请红字信息表");
System.Threading.Thread.Sleep(2000);
/*
* 3. 开具红字发票
* @link https://fa-piao.com/doc.html#api10?source=github
*/
var redInvoiceParams = new Dictionary<string, object>
{
["fpqqlsh"] = "red" + fphm,
["username"] = username,
["xhdwsbh"] = nsrsbh
};
var applyRedData = applyRedResponse.Data;
if (applyRedData == null || applyRedData.GetValueOrDefault("xxbbh") == null)
{
Console.WriteLine("红字信息表返回字段缺失: " + applyRedResponse.Data);
return;
}
redInvoiceParams["tzdbh"] = applyRedData.GetValueOrDefault("xxbbh")?.ToString();
redInvoiceParams["yfphm"] = fphm;
var redInvoiceResponse = client.RedTicket(redInvoiceParams);
if (redInvoiceResponse.IsSuccess)
{
Console.WriteLine("3 负数开具成功");
}
else
{
Console.WriteLine(redInvoiceResponse.Code + "数电票负数开具失败:" + redInvoiceResponse.Msg);
Console.WriteLine(redInvoiceResponse.Data);
}
}
else
{
Console.WriteLine(applyRedResponse.Code + "申请红字信息表失败:" + applyRedResponse.Msg);
Console.WriteLine(applyRedResponse.Data);
}
}
else
{
Console.WriteLine(queryInvoiceResponse.Code + "查询发票信息失败:" + queryInvoiceResponse.Msg);
Console.WriteLine(queryInvoiceResponse.Data);
}
}
catch (Exception e)
{
Console.WriteLine("请求异常:" + e.Message);
Console.WriteLine(e);
}
}
}