直接上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();
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();
}
private Map<String, String> requestFaPiaoData(JSONObject params) {
String fapiaoData = HttpUtils.post(params, url);
Map map = JSONObject.parseObject(fapiaoData, Map.class);
return map;
}
}