银联开票接口

直接上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;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值