对接诺诺电子发票(Java)
沙箱环境:
url: https://sandbox.nuonuocs.cn/open/v1/services
appKey: SD63236305
appSecret: SDDED2523BED4643
下载诺诺的SDK:SDK下载
引入项目:
服务实现层代码:
// 创建发票信息表
@Override
public InvoiceDto create(Invoice resources){
resources.setId(snowflake.nextIdStr());
if (InvoiceConstants.InvoiceType.ELECTRONIC.getCode().equals(resources.getInvoiceType())){
// 电子发票
JSONObject result = JSONObject.parseObject(makeInvoice());
String newResult = result.getString("result");
JSONObject invoiceResult = JSONObject.parseObject(newResult);
// 获取发票流水号
String serialNos = invoiceResult.getString("invoiceSerialNum");
resources.setSerialNo(serialNos);
Map<String, String> invoiceMap = getInvoiceUrlByInvoiceSerialNum(serialNos);
System.out.println("Map::::::" + invoiceMap);
resources.setStatus(invoiceMap.get("status"));
if (invoiceMap.get("status").equals(SUCCESS_CODE)){
resources.setInvoiceNo(invoiceMap.get("invoiceNo"));
resources.setInvoiceCode(invoiceMap.get("invoiceCode"));
// 将日期格式转化
String invoiceTime = invoiceMap.get("invoiceTime");
LocalDateTime time = LocalDateTimeUtil.of(Long.valueOf(invoiceTime));
String format = LocalDateTimeUtil.format(time, DatePattern.NORM_DATETIME_PATTERN);
resources.setInvoiceTime(format);
resources.setImgUrl(invoiceMap.get("imgUrl"));
resources.setPdfUrl(invoiceMap.get("pdfUrl"));
}
}else if (InvoiceConstants.InvoiceType.PAPERY.getCode().equals(resources.getInvoiceType())){
// 纸质发票
resources.setStatus(SUCCESS_CODE);
}
return invoiceMapper.toDto(invoiceRepository.save(resources));
}
// 对接诺诺的开票接口
public String makeInvoice(){
String taxNum = "339901999999142"; //企业税号
String method = "nuonuo.ElectronInvoice.requestBillingNew";
// 沙箱环境下token可以为空
String token = "";
String content = "{\"order\":"
// 发票明细
+ "{\"invoiceDetail\":[" + "{"
// 税率
+ "\"taxRate\":\"" + 0.13 + "\","
// 发票行性质:0,正常行;1,折扣行;2,被折扣行
+ "\"invoiceLineProperty\":\"" + 0 + "\","
// 商品名
+ "\"goodsName\":\"" + "物业费"+ "\","
+ "\"goodsCode\":\"" + "3040801"+ "\","
// 不含税金额
+ "\"taxExcludedAmount\":\"" + 1 + "\","
// 单价含税标志 0 不含税 1 含税
+ "\"withTaxFlag\":\"" + 0 + "\","
// 数量
+ "\"num\":\"" + 1 + "\","
// 税额 [不含税金额] * [税率] = [税额]
+ "\"tax\":\"" + 0.13 + "\","
// 单位
+ "\"unit\":\"" + "" + "\","
// 扣除额,差额征税时填写
+ "\"deduction\":\"" + 0 + "\","
// 单价(精确到小数点后8位)
+ "\"price\":\"" + 1 + "\"}],"
// 购方名称 Y
+ "\"buyerName\":\"" + "测试名称" + "\","
// 购方地址 N
+ "\"buyerAddress\":\"" + "测试地址" + "\","
// 购方税号(企业要填,个人可为空) N
+ "\"buyerTaxNum\":\"" + "123456789000000000" + "\","
// 开票类型:1:蓝票;2:红票
+ "\"invoiceType\":\"" + 1 + "\","
// 发票种类
+ "\"invoiceLine\":\"" + "p" + "\","
// 清单标志:非清单:0;清单:1,默认:0,电票固定为0
+ "\"listFlag\":\"" + 0 + "\","
// 推送方式:-1,不推送;0,邮箱;1,手机(默认);2,邮箱、手机
+ "\"pushMode\":\"" + 2 + "\","
// 购方手机(开票成功会短信提醒购方,不受推送方式影响) Y
+ "\"buyerPhone\":\"" + "****" + "\","
// 邮箱地址,沙箱环境也可以发送
+ "\"email\":\"" + "1234@qq.com" + "\","
// 开票员id(诺诺系统中的id) N
+ "\"clerkId\":\"" + "ceshi" + "\","
+ "\"payer\":\"" + "nickname" + "\","
// 销方银行账号和开户行地址
+ "\"salerAccountr\":\"" + "测试银行测试支行120200590990432278" + "\","
+ "\"salerAddressr\":\"" + "测试地址" + "\","
// 订单号(每个企业唯一) Y
+ "\"orderNo\":\"" + "20230205112" + "\","
// 销方电话
+ "\"salerTelr\":\"" + "0731-168168168" + "\","
// 订单时间 Y
+ "\"invoiceDate\":\"" + "2016-01-13 12:30:00" + "\","
// 开票员 Y
+ "\"clerk\":\"" + "ceshi" + "\","
// 分机号(只能为空或者数字)
+ "\"extensionNumber\":\"" + "0" + "\","
// 销方税号
+ "\"salerTaxNum\":\"" + "339901999999142" + "\""+
"}}";
String senid = UUID.randomUUID().toString().replace("-", ""); // 唯一标识,由企业自己生成32位随机码
// 接口调用
NNOpenSDK sdk = NNOpenSDK.getIntance();
String json = sdk.sendPostSyncRequest(url, senid, appKey, appSecret, token,taxNum, method, content);
// 响应报文解析
System.out.println("\n\n\n【API请求】");
System.out.println(json);
JSONObject newJson = JSONObject.parseObject(json);
if (!newJson.getString("code").equals(APPLY_SUCCESS_CODE)){
// 开票申请失败
throw new BadRequestException(newJson.getString("describe"));
}
return json;
}
// 发票开完后的查询接口
@Override
public Map<String, String> getInvoiceUrlByInvoiceSerialNum(String invoiceSerialNum) {
String taxnum = "339901999999142";//授权企业税号, 商户填""
String method = "nuonuo.ElectronInvoice.queryInvoiceResult";
String token = "";
Map<String, String> map = new HashMap<>();
NNOpenSDK sdk = NNOpenSDK.getIntance();
String content = "{" + "\"serialNos\":[" + invoiceSerialNum + "]" + "}";
String senid = UUID.randomUUID().toString().replace("-", "");
String result = sdk.sendPostSyncRequest(url, senid, appKey, appSecret, token, taxnum, method, content);
System.out.println(result);
JSONObject newresult = JSONObject.parseObject(result);
String code = newresult.getString("code");
String describe = newresult.getString("describe");
map.put("code", code);
if (code.equals(APPLY_SUCCESS_CODE)) {
JSONArray eventInfoData = (JSONArray) newresult.get("result");
JSONObject jsonArray = eventInfoData.getJSONObject(0);
String status = jsonArray.getString("status");
String statusMsg = jsonArray.getString("statusMsg");
map.put("status", status);
map.put("statusMsg", statusMsg);
if (status.equals(SUCCESS_CODE)) {
map.put("imgUrl", jsonArray.getString("imgUrls"));
map.put("pdfUrl", jsonArray.getString("pdfUrl"));
map.put("invoiceCode",jsonArray.getString("invoiceCode"));
map.put("invoiceNo",jsonArray.getString("invoiceNo"));
map.put("invoiceTime",jsonArray.getString("invoiceTime"));
}
return map;
} else {
map.put("statusMsg", describe);
}
return map;
}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_52217182/article/details/129012900