银联开票接口

直接上service

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.framework.util.DateUtil;
import com.framework.util.PriceChangeUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;
import java.io.UnsupportedEncodingException;
@Service
public class FaPiaoService {@Value("${fapiao.msgSrc}")private String msgSrc;@Value("${fapiao.key}")private String key;@Value("${fapiao.url}")private String url;@Value("${fapiao.notifyUrl}")private String notifyUrl;public Map<String,String> setFaPiao(FaPiaoDto faPiaoDtoP) throws NoSuchAlgorithmException, UnsupportedEncodingException {long dayTime = DateUtil.getDayTime();//当天0点的数据String workDay = DateUtil.getTimestampToDate(dayTime, "yyyyMMdd");String requestTimestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());String msgType =  "issue";String msgId =  UUID.randomUUID().toString();FaPiaoDto faPiaoDto = new FaPiaoDto();if(null!=faPiaoDtoP.getBuyerAddress() && !faPiaoDtoP.getBuyerAddress().isEmpty()){faPiaoDto.setBuyerAddress(faPiaoDtoP.getBuyerAddress());}if(null!=faPiaoDtoP.getBuyerTelephone() && !faPiaoDtoP.getBuyerTelephone().isEmpty()){faPiaoDto.setBuyerTelephone(faPiaoDtoP.getBuyerTelephone());}if(null!=faPiaoDtoP.getBuyerBank() && !faPiaoDtoP.getBuyerBank().isEmpty()){faPiaoDto.setBuyerBank(faPiaoDtoP.getBuyerBank());}if(null!=faPiaoDtoP.getBuyerAccount() && !faPiaoDtoP.getBuyerAccount().isEmpty()){faPiaoDto.setBuyerAccount(faPiaoDtoP.getBuyerAccount());}String invoiceMaterial = "ELECTRONIC";String invoiceType = "PLAIN";String sign = "";faPiaoDto.setBuyerTaxCode(faPiaoDtoP.getBuyerTaxCode());faPiaoDto.setAmount(faPiaoDtoP.getAmount());faPiaoDto.setBuyerName(faPiaoDtoP.getBuyerName());faPiaoDto.setMsgType(msgType);faPiaoDto.setMerOrderDate(workDay);faPiaoDto.setMsgId(msgId);faPiaoDto.setTerminalId(faPiaoDtoP.getTerminalId()+key);faPiaoDto.setMerchantId(faPiaoDtoP.getMerchantId());faPiaoDto.setInvoiceType(invoiceType);faPiaoDto.setMerOrderId(faPiaoDtoP.getMerOrderId());faPiaoDto.setRequestTimestamp(requestTimestamp);faPiaoDto.setMsgSrc(msgSrc);faPiaoDto.setInvoiceMaterial(invoiceMaterial);List<FaPiaoGoodsDetailDto> faPiaoGoodsDetailDtos =faPiaoDtoP.getGoodsDetail();for(int i=0;i<faPiaoGoodsDetailDtos.size();i++){faPiaoGoodsDetailDtos.get(i).setIndex(i+1);faPiaoGoodsDetailDtos.get(i).setAttribute(0);}faPiaoDto.setGoodsDetail(faPiaoGoodsDetailDtos);faPiaoDto.setNotifyUrl(notifyUrl);String faPiaoDtoStr = JSONObject.toJSONString(faPiaoDto);JSONObject signParams = JSONObject.parseObject(faPiaoDtoStr, Feature.OrderedField);String signParamsStr = this.toUrlParams(signParams);sign = this.getSign(signParamsStr);faPiaoDto.setSign(sign);faPiaoDto.setTerminalId(faPiaoDtoP.getTerminalId());JSONObject faPiaoDtoParamsNew = JSONObject.parseObject(JSONObject.toJSONString(faPiaoDto), Feature.OrderedField);Map<String, String> stringObjectMap = this.requestFaPiaoData(faPiaoDtoParamsNew);try {Thread.sleep(3000);//延迟三秒等待出票} catch (InterruptedException e) {e.printStackTrace();}return stringObjectMap;}public static String toUrlParams(JSONObject json) {StringBuilder sb = new StringBuilder();for (String key : json.keySet()) {Object value = json.get(key);if(value!=null && !value.toString().isEmpty()){//过滤空值sb.append("&");sb.append(key);sb.append("=");sb.append(value.toString());}}String str =  sb.toString();if (str == null || str.length() == 0) {return str;}else{return str.substring(1);}}private String getSign(String inputString) throws NoSuchAlgorithmException {MessageDigest md = MessageDigest.getInstance("SHA-256");byte[] hashBytes = md.digest(inputString.getBytes(StandardCharsets.UTF_8));StringBuilder sb = new StringBuilder();for (byte b : hashBytes) {sb.append(String.format("%02x", b));}return sb.toString().toUpperCase();}/*** @return* @Author xx* @Description //开票接口* @Date 16:46 2023/5/18* @Param**/private Map<String, String> requestFaPiaoData(JSONObject params) {String fapiaoData = HttpUtils.post(params, url);Map map = JSONObject.parseObject(fapiaoData, Map.class);return map;}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部