当前位置:   article > 正文

对接诺诺电子发票(Java)_诺诺java

诺诺java

沙箱环境:

url: https://sandbox.nuonuocs.cn/open/v1/services
appKey: SD63236305
appSecret: SDDED2523BED4643

下载诺诺的SDK:SDK下载

引入项目:

服务实现层代码:

  1. // 创建发票信息表
  2. @Override
  3. public InvoiceDto create(Invoice resources){
  4. resources.setId(snowflake.nextIdStr());
  5. if (InvoiceConstants.InvoiceType.ELECTRONIC.getCode().equals(resources.getInvoiceType())){
  6. // 电子发票
  7. JSONObject result = JSONObject.parseObject(makeInvoice());
  8. String newResult = result.getString("result");
  9. JSONObject invoiceResult = JSONObject.parseObject(newResult);
  10. // 获取发票流水号
  11. String serialNos = invoiceResult.getString("invoiceSerialNum");
  12. resources.setSerialNo(serialNos);
  13. Map<String, String> invoiceMap = getInvoiceUrlByInvoiceSerialNum(serialNos);
  14. System.out.println("Map::::::" + invoiceMap);
  15. resources.setStatus(invoiceMap.get("status"));
  16. if (invoiceMap.get("status").equals(SUCCESS_CODE)){
  17. resources.setInvoiceNo(invoiceMap.get("invoiceNo"));
  18. resources.setInvoiceCode(invoiceMap.get("invoiceCode"));
  19. // 将日期格式转化
  20. String invoiceTime = invoiceMap.get("invoiceTime");
  21. LocalDateTime time = LocalDateTimeUtil.of(Long.valueOf(invoiceTime));
  22. String format = LocalDateTimeUtil.format(time, DatePattern.NORM_DATETIME_PATTERN);
  23. resources.setInvoiceTime(format);
  24. resources.setImgUrl(invoiceMap.get("imgUrl"));
  25. resources.setPdfUrl(invoiceMap.get("pdfUrl"));
  26. }
  27. }else if (InvoiceConstants.InvoiceType.PAPERY.getCode().equals(resources.getInvoiceType())){
  28. // 纸质发票
  29. resources.setStatus(SUCCESS_CODE);
  30. }
  31. return invoiceMapper.toDto(invoiceRepository.save(resources));
  32. }
  1. // 对接诺诺的开票接口
  2. public String makeInvoice(){
  3. String taxNum = "339901999999142"; //企业税号
  4. String method = "nuonuo.ElectronInvoice.requestBillingNew";
  5. // 沙箱环境下token可以为空
  6. String token = "";
  7. String content = "{\"order\":"
  8. // 发票明细
  9. + "{\"invoiceDetail\":[" + "{"
  10. // 税率
  11. + "\"taxRate\":\"" + 0.13 + "\","
  12. // 发票行性质:0,正常行;1,折扣行;2,被折扣行
  13. + "\"invoiceLineProperty\":\"" + 0 + "\","
  14. // 商品名
  15. + "\"goodsName\":\"" + "物业费"+ "\","
  16. + "\"goodsCode\":\"" + "3040801"+ "\","
  17. // 不含税金额
  18. + "\"taxExcludedAmount\":\"" + 1 + "\","
  19. // 单价含税标志 0 不含税 1 含税
  20. + "\"withTaxFlag\":\"" + 0 + "\","
  21. // 数量
  22. + "\"num\":\"" + 1 + "\","
  23. // 税额 [不含税金额] * [税率] = [税额]
  24. + "\"tax\":\"" + 0.13 + "\","
  25. // 单位
  26. + "\"unit\":\"" + "" + "\","
  27. // 扣除额,差额征税时填写
  28. + "\"deduction\":\"" + 0 + "\","
  29. // 单价(精确到小数点后8位)
  30. + "\"price\":\"" + 1 + "\"}],"
  31. // 购方名称 Y
  32. + "\"buyerName\":\"" + "测试名称" + "\","
  33. // 购方地址 N
  34. + "\"buyerAddress\":\"" + "测试地址" + "\","
  35. // 购方税号(企业要填,个人可为空) N
  36. + "\"buyerTaxNum\":\"" + "123456789000000000" + "\","
  37. // 开票类型:1:蓝票;2:红票
  38. + "\"invoiceType\":\"" + 1 + "\","
  39. // 发票种类
  40. + "\"invoiceLine\":\"" + "p" + "\","
  41. // 清单标志:非清单:0;清单:1,默认:0,电票固定为0
  42. + "\"listFlag\":\"" + 0 + "\","
  43. // 推送方式:-1,不推送;0,邮箱;1,手机(默认);2,邮箱、手机
  44. + "\"pushMode\":\"" + 2 + "\","
  45. // 购方手机(开票成功会短信提醒购方,不受推送方式影响) Y
  46. + "\"buyerPhone\":\"" + "****" + "\","
  47. // 邮箱地址,沙箱环境也可以发送
  48. + "\"email\":\"" + "1234@qq.com" + "\","
  49. // 开票员id(诺诺系统中的id) N
  50. + "\"clerkId\":\"" + "ceshi" + "\","
  51. + "\"payer\":\"" + "nickname" + "\","
  52. // 销方银行账号和开户行地址
  53. + "\"salerAccountr\":\"" + "测试银行测试支行120200590990432278" + "\","
  54. + "\"salerAddressr\":\"" + "测试地址" + "\","
  55. // 订单号(每个企业唯一) Y
  56. + "\"orderNo\":\"" + "20230205112" + "\","
  57. // 销方电话
  58. + "\"salerTelr\":\"" + "0731-168168168" + "\","
  59. // 订单时间 Y
  60. + "\"invoiceDate\":\"" + "2016-01-13 12:30:00" + "\","
  61. // 开票员 Y
  62. + "\"clerk\":\"" + "ceshi" + "\","
  63. // 分机号(只能为空或者数字)
  64. + "\"extensionNumber\":\"" + "0" + "\","
  65. // 销方税号
  66. + "\"salerTaxNum\":\"" + "339901999999142" + "\""+
  67. "}}";
  68. String senid = UUID.randomUUID().toString().replace("-", ""); // 唯一标识,由企业自己生成32位随机码
  69. // 接口调用
  70. NNOpenSDK sdk = NNOpenSDK.getIntance();
  71. String json = sdk.sendPostSyncRequest(url, senid, appKey, appSecret, token,taxNum, method, content);
  72. // 响应报文解析
  73. System.out.println("\n\n\n【API请求】");
  74. System.out.println(json);
  75. JSONObject newJson = JSONObject.parseObject(json);
  76. if (!newJson.getString("code").equals(APPLY_SUCCESS_CODE)){
  77. // 开票申请失败
  78. throw new BadRequestException(newJson.getString("describe"));
  79. }
  80. return json;
  81. }
  1. // 发票开完后的查询接口
  2. @Override
  3. public Map<String, String> getInvoiceUrlByInvoiceSerialNum(String invoiceSerialNum) {
  4. String taxnum = "339901999999142";//授权企业税号, 商户填""
  5. String method = "nuonuo.ElectronInvoice.queryInvoiceResult";
  6. String token = "";
  7. Map<String, String> map = new HashMap<>();
  8. NNOpenSDK sdk = NNOpenSDK.getIntance();
  9. String content = "{" + "\"serialNos\":[" + invoiceSerialNum + "]" + "}";
  10. String senid = UUID.randomUUID().toString().replace("-", "");
  11. String result = sdk.sendPostSyncRequest(url, senid, appKey, appSecret, token, taxnum, method, content);
  12. System.out.println(result);
  13. JSONObject newresult = JSONObject.parseObject(result);
  14. String code = newresult.getString("code");
  15. String describe = newresult.getString("describe");
  16. map.put("code", code);
  17. if (code.equals(APPLY_SUCCESS_CODE)) {
  18. JSONArray eventInfoData = (JSONArray) newresult.get("result");
  19. JSONObject jsonArray = eventInfoData.getJSONObject(0);
  20. String status = jsonArray.getString("status");
  21. String statusMsg = jsonArray.getString("statusMsg");
  22. map.put("status", status);
  23. map.put("statusMsg", statusMsg);
  24. if (status.equals(SUCCESS_CODE)) {
  25. map.put("imgUrl", jsonArray.getString("imgUrls"));
  26. map.put("pdfUrl", jsonArray.getString("pdfUrl"));
  27. map.put("invoiceCode",jsonArray.getString("invoiceCode"));
  28. map.put("invoiceNo",jsonArray.getString("invoiceNo"));
  29. map.put("invoiceTime",jsonArray.getString("invoiceTime"));
  30. }
  31. return map;
  32. } else {
  33. map.put("statusMsg", describe);
  34. }
  35. return map;
  36. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/298021
推荐阅读
相关标签
  

闽ICP备14008679号