-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRedInvoiceExample.java
More file actions
118 lines (101 loc) · 4.86 KB
/
RedInvoiceExample.java
File metadata and controls
118 lines (101 loc) · 4.86 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
package tax.invoice.example;
import tax.invoice.InvoiceClient;
import tax.invoice.model.ApiResponse;
import tax.invoice.model.AuthorizationResponse;
import tax.invoice.util.OtherUtil;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import static java.lang.Thread.sleep;
/**
* 数电发票SDK使用示例
*/
public class RedInvoiceExample {
public static void main(String[] args) {
try {
// 显式设置 System.out 的编码为 UTF-8
System.setOut(new PrintStream(System.out, true, "UTF-8"));
String appKey = "";
String appSecret = "";
String nsrsbh = "";//统一社会信用代码
String username = "";//手机号码(电子税务局)
String fphm = "";
String kprq = "";
String token = "";
// 创建客户端
InvoiceClient client = new InvoiceClient(appKey, appSecret);
if (token != null && !token.isEmpty()) {
client.setAuthorization(token);
} else {
/*
* 获取授权Token文档
* @see https://fa-piao.com/doc.html#api1?source=github
*/
ApiResponse<AuthorizationResponse> authResponse = client.getAuthorization(nsrsbh);
if (authResponse.isSuccess()) {
System.out.println("授权成功,Token: " + authResponse.getData().getToken());
}
}
/*
* 1. 数电申请红字前查蓝票信息接口
* @link https://fa-piao.com/doc.html#api8?source=github
*/
Map<String, Object> queryInvoiceParams = new HashMap<>();
queryInvoiceParams.put("nsrsbh", nsrsbh);
queryInvoiceParams.put("fphm", fphm);
queryInvoiceParams.put("username", username);
// queryInvoiceParams.put("kprq", kprq);
queryInvoiceParams.put("sqyy", "2");
ApiResponse<Map<String, Object>> queryInvoiceResponse = client.queryBlueTicketInfo(queryInvoiceParams);
if (queryInvoiceResponse.isSuccess()) {
System.out.println("1 可以申请红字");
sleep(2000);
/*
* 2. 申请红字信息表
* @link https://fa-piao.com/doc.html#api9?source=github
*/
Map<String, Object> applyRedParams = new HashMap<>();
applyRedParams.put("xhdwsbh", nsrsbh);
applyRedParams.put("yfphm", fphm);
applyRedParams.put("username", username);
applyRedParams.put("sqyy", "2");
applyRedParams.put("chyydm", "01");
ApiResponse<Map<String, Object>> applyRedResponse = client.applyRedInfo(applyRedParams);
if (applyRedResponse.isSuccess()) {
System.out.println("2 申请红字信息表" );
sleep(2000);
/*
* 3. 开具红字发票
* @link https://fa-piao.com/doc.html#api10?source=github
*/
Map<String, Object> redInvoiceParams = new HashMap<>();
redInvoiceParams.put("fpqqlsh", "red" + fphm);
redInvoiceParams.put("username", username);
redInvoiceParams.put("xhdwsbh", nsrsbh);
Map<String, Object> applyRedData = applyRedResponse.getData();
if (applyRedData == null || applyRedData.get("xxbbh") == null) {
System.out.println("红字信息表返回字段缺失: " + applyRedResponse.getData());
return;
}
redInvoiceParams.put("tzdbh", applyRedData.get("xxbbh").toString());
redInvoiceParams.put("yfphm", fphm);
ApiResponse<Map<String, Object>> redInvoiceResponse = client.redTicket(redInvoiceParams);
if (redInvoiceResponse.isSuccess()) {
System.out.println("3 负数开具成功");
}else {
System.out.println(redInvoiceResponse.getCode()+"数电票负数开具失败:"+redInvoiceResponse.getMsg());
System.out.println(redInvoiceResponse.getData());
}
}else {
System.out.println(applyRedResponse.getCode()+"申请红字信息表失败:"+applyRedResponse.getMsg());
System.out.println(applyRedResponse.getData());
}
}else {
System.out.println(queryInvoiceResponse.getCode()+"查询发票信息失败:"+queryInvoiceResponse.getMsg());
System.out.println(queryInvoiceResponse.getData());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}