package com.example.util;
import com.example.util.WXPayConstants.SignType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.*;
public class WXPayUtil {
private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final Random RANDOM = new SecureRandom();
/**
* 元转换成分
* @Author: weichun
* @Date: 2019/12/19 18:20
*/
public static Long getMoney(String amount) {
if(StringUtils.isEmpty(amount)){
return 0l;
}
// 金额转化为分为单位
String currency = amount.replaceAll("\\$|\\¥|\\,", ""); //处理包含, ¥ 或者$的金额
int index = currency.indexOf(".");
int length = currency.length();
Long amLong = 0l;
if(index == -1){
amLong = Long.valueOf(currency+"00");
}else if(length - index >= 3){
amLong = Long.valueOf((currency.substring(0, index+3)).replace(".", ""));
}else if(length - index == 2){
amLong = Long.valueOf((currency.substring(0, index+2)).replace(".", "")+0);
}else{
amLong = Long.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");
}
return amLong;
}
/**
* XML格式字符串转换为Map
*
* @param strXML XML字符串
* @return XML数据转换后的Map
* @throws Exception
*/
public static Map<String, String> xmlToMap(String strXML) throws Exception {
try {
Map<String, String> data = new HashMap<String, String>();
DocumentBuilder documentBuilder = WXPayXmlUtil.newDocumentBuilder();
InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
org.w3c.dom.Document doc = documentBuilder.parse(stream);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getDocumentElement().getChildNodes();
for (int idx = 0; idx < nodeList.getLength(); ++idx) {
Node node = nodeList.item(idx);
if (node.getNodeType() == Node.ELEMENT_NODE) {
org.w3c.dom.Element element = (org.w3c.dom.Element) node;
data.put(element.getNodeName(), element.getTextContent());
}
}
try {
stream.close();
} catch (Exception ex) {
// do nothing
}
return data;
} catch (Exception ex) {
WXPayUtil.getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML);
throw ex;
}
}
/**
* 将Map转换为XML格式的字符串
*
* @param data Map类型数据
* @return XML格式的字符串
* @throws Exception
*/
public static String mapToXml(Map<String, String> data) throws Exception {
org.w3c.dom.Document document = WXPayXmlUtil.newDocument();
org.w3c.dom.Element root = document.createElement("xml");
document.appendChild(root);
for (String key: data.keySet()) {
String value = data.get(key);
if (value == null) {
value = "";
}
value = value.trim();
org.w3c.dom.Element filed = document.createElement(key);
filed.appendChild(document.createTextNode(value));
root.appendChild(filed);
}
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
try {
writer.close();
}
catch (Exception ex) {
}
return output;
}
/**
* 生成带有 sign 的 XML 格式字符串
*
* @param data Map类型数据
* @param key API密钥
* @return 含有sign字段的XML
*/
public static String generateSignedXml(final Map<String, String> data, String key) throws Exception {
return generateSignedXml(data, key, SignType.MD5);
}
/**
* 生成带有 sign 的 XML 格式字符串
*
* @param data Map类型数据
* @param key API密钥
* @param signType 签名类型
* @return 含有sign字段的XML
*/
public static String generateSignedXml(final Map<String, String> data, String key, SignType signType) throws Exception {
String sign = generateSignature(data, key, signType);
data.put(WXPayConstants.FIELD_SIGN, sign);
return mapToXml(data);
}
/**
* 判断签名是否正确
*
* @param xmlStr XML格式数据
* @param key API密钥
* @return 签名是否正确
* @throws Exception
*/
public static boolean isSignatureValid(String xmlStr, String key) throws Exception {
Map<String, String> data = xmlToMap(xmlStr);
if (!data.containsKey(WXPayConstants.FIELD_SIGN) ) {
return false;
}
String sign = data.get(WXPayConstants.FIELD_SIGN);
return generateSignature(data, key).equals(sign);
}
/**
* 判断签名是否正确,必须包含sign字段,否则返回false。使用MD5签名。
*
* @param data Map类型数据
* @param key API密钥
* @return 签名是否正确
* @throws Exception
*/
public static boolean isSignatureValid(Map<String, String> data, String key) throws Exception {
return isSignatureValid(data, key, SignType.MD5);
}
/**
* 判断签名是否正确,必须包含sign字段,否则返回false。
*
* @param data Map类型数据
* @param key API密钥
* @param signType 签名方式
* @return 签名是否正确
* @throws Exception
*/
public static boolean isSignatureValid(Map<String, String> data, String key, SignType signType) throws Exception {
if (!data.containsKey(WXPayConstants.FIELD_SIGN) ) {
return false;
}
String sign = data.get(WXPayConstants.FIELD_SIGN);
return generateSignature(data, key, signType).equals(sign);
}
/**
* 生成签名
*
* @param data 待签名数据
* @param key API密钥
* @return 签名
*/
public static String generateSignature(final Map<String, String> data, String key) throws Exception {
return generateSignature(data, key, SignType.MD5);
}
/**
* 生成签名. 注意,若含有sign_type字段,必须和signType参数保持一致。
*
* @param data 待签名数据
* @param key API密钥
* @param signType 签名方式
* @return 签名
*/
public static String generateSignature(final Map<String, String> data, String key,
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
支持H5、微信小程序支付SDK (130个子文件)
WXPayUtil.class 11KB
HttpClient.class 7KB
PayController.class 5KB
PayServiceImpl.class 4KB
WXPayConstants.class 3KB
AuthToken.class 2KB
WXAuthTokenUtil.class 2KB
SlingPayVo.class 1KB
WXPayXmlUtil.class 1KB
Order.class 1KB
WXPayConstants$SignType.class 1KB
HttpClient$1.class 907B
WxpayApplication.class 696B
PayService.class 584B
MyBatisConfig.class 564B
WxpayApplicationTests.class 548B
WXConfig.class 464B
OrderMapper.class 259B
mvnw.cmd 6KB
.gitignore 333B
wxpay.iml 10KB
maven-wrapper.jar 50KB
WXPayUtil.java 11KB
HttpClient.java 5KB
MavenWrapperDownloader.java 5KB
PayController.java 4KB
PayServiceImpl.java 3KB
WXPayConstants.java 3KB
AuthToken.java 2KB
WXAuthTokenUtil.java 1KB
WXPayXmlUtil.java 1KB
SlingPayVo.java 1KB
Order.java 783B
WXConfig.java 438B
PayService.java 398B
MyBatisConfig.java 390B
WxpayApplication.java 314B
WxpayApplicationTests.java 223B
OrderMapper.java 185B
HELP.md 310B
mvnw 10KB
maven-wrapper.properties 218B
workspace.xml 10KB
pom.xml 3KB
Maven__org_springframework_boot_spring_boot_starter_validation_2_2_2_RELEASE.xml 793B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_2_2_RELEASE.xml 793B
Maven__org_springframework_boot_spring_boot_starter_logging_2_2_2_RELEASE.xml 772B
Maven__org_springframework_boot_spring_boot_starter_tomcat_2_2_2_RELEASE.xml 765B
Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_10_1.xml 760B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_2_2_RELEASE.xml 758B
Maven__org_mybatis_spring_boot_mybatis_spring_boot_autoconfigure_1_2_0.xml 754B
Maven__org_springframework_boot_spring_boot_starter_json_2_2_2_RELEASE.xml 751B
Maven__org_springframework_boot_spring_boot_starter_test_2_2_2_RELEASE.xml 751B
Maven__org_springframework_boot_spring_boot_starter_jdbc_2_2_2_RELEASE.xml 751B
Maven__org_springframework_boot_spring_boot_starter_web_2_2_2_RELEASE.xml 744B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 724B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_10_1.xml 719B
Maven__org_springframework_boot_spring_boot_starter_2_2_2_RELEASE.xml 716B
Maven__org_mybatis_spring_boot_mybatis_spring_boot_starter_1_2_0.xml 712B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_10_1.xml 705B
Maven__org_hibernate_validator_hibernate_validator_6_0_18_Final.xml 705B
Maven__org_springframework_boot_spring_boot_test_2_2_2_RELEASE.xml 695B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_29.xml 684B
Maven__org_springframework_spring_expression_5_2_2_RELEASE.xml 682B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_10_1.xml 675B
Maven__org_springframework_spring_context_5_2_2_RELEASE.xml 661B
Maven__org_springframework_boot_spring_boot_2_2_2_RELEASE.xml 660B
Maven__jakarta_annotation_jakarta_annotation_api_1_3_5.xml 657B
Maven__org_junit_platform_junit_platform_commons_1_5_2.xml 657B
Maven__jakarta_activation_jakarta_activation_api_1_2_1.xml 657B
Maven__jakarta_validation_jakarta_validation_api_2_0_1.xml 657B
Maven__com_fasterxml_jackson_core_jackson_databind_2_10_1.xml 654B
Maven__org_springframework_spring_webmvc_5_2_2_RELEASE.xml 654B
Maven__org_junit_platform_junit_platform_engine_1_5_2.xml 650B
Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_29.xml 649B
Maven__org_springframework_spring_beans_5_2_2_RELEASE.xml 647B
Maven__org_springframework_spring_jdbc_5_2_2_RELEASE.xml 640B
Maven__org_springframework_spring_core_5_2_2_RELEASE.xml 640B
Maven__org_springframework_spring_test_5_2_2_RELEASE.xml 640B
Maven__org_junit_jupiter_junit_jupiter_params_5_5_2.xml 639B
Maven__org_junit_jupiter_junit_jupiter_engine_5_5_2.xml 639B
Maven__org_apache_tomcat_embed_tomcat_embed_el_9_0_29.xml 635B
Maven__jakarta_xml_bind_jakarta_xml_bind_api_2_3_2.xml 635B
Maven__org_springframework_spring_jcl_5_2_2_RELEASE.xml 633B
Maven__org_springframework_spring_aop_5_2_2_RELEASE.xml 633B
Maven__org_springframework_spring_web_5_2_2_RELEASE.xml 633B
Maven__org_jboss_logging_jboss_logging_3_4_1_Final.xml 632B
Maven__org_apache_logging_log4j_log4j_to_slf4j_2_12_1.xml 632B
Maven__com_fasterxml_jackson_core_jackson_core_2_10_1.xml 626B
Maven__org_springframework_spring_tx_5_2_2_RELEASE.xml 626B
Maven__org_mockito_mockito_junit_jupiter_3_1_0.xml 622B
compiler.xml 622B
Maven__org_junit_jupiter_junit_jupiter_api_5_5_2.xml 618B
Maven__net_bytebuddy_byte_buddy_agent_1_10_4.xml 602B
Maven__org_apache_httpcomponents_httpclient_4_5_6.xml 601B
Maven__mysql_mysql_connector_java_8_0_15.xml 598B
Maven__org_apache_logging_log4j_log4j_api_2_12_1.xml 597B
Maven__org_apiguardian_apiguardian_api_1_1_0.xml 596B
Maven__org_apache_httpcomponents_httpcore_4_4_12.xml 594B
Maven__ch_qos_logback_logback_classic_1_2_3.xml 592B
共 130 条
- 1
- 2
资源评论
qq_34721097
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功