付款到零钱代码实现如下:
1、支付方法
@Service
@AllArgsConstructor
public class PayService {
/**
* 企业付款到零钱
* @param openid
* @param no
* @param userName
* @param amount
* @throws WxPayException
*/
public Map<String,String> entPay(String openid,String no,String userName,Integer amount) throws Exception{
String mchId = "xxx";//商户号ID
String appid = "xxx";//appid
String key = "xxx";//商户号Key
String certFile = "C:\\Users\\Administrator\\Desktop\\apiclient_cert.p12";//api证书文件存放路径
Map<String,String> parameterMap = new TreeMap<>();
parameterMap.put("amount", amount.toString());
parameterMap.put("check_name", "NO_CHECK");//不验证用户真实姓名
parameterMap.put("mch_appid", appid);
parameterMap.put("mchid", mchId);
parameterMap.put("partner_trade_no", no);
parameterMap.put("nonce_str", WxPayUtils.generateNonceStr());
parameterMap.put("openid", openid);
// parameterMap.put("re_user_name", userName);
parameterMap.put("spbill_create_ip", "127.0.0.1");
parameterMap.put("desc","提现");
parameterMap.put("sign", WxPayUtils.generateSignature(parameterMap,key,"MD5"));
String reuqestXml = WxPayUtils.mapToXml(parameterMap);
System.out.println("微信企业付款到零钱请求集合"+reuqestXml);
//发送请求到企业付款到零钱的Api。发送请求是一个方法来的POST
String wxUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; //获取付款到零钱的api接口
//调用方法发送了
String weixinPost = HttpUtil.HttpClientSSL(reuqestXml,mchId,wxUrl,certFile);
//解析返回的xml数据
System.out.println("微信企业付款到零钱返回结果"+weixinPost);
Map<String, String> resultMap = WxPayUtils.xmlToMap(weixinPost);
return resultMap;
}
}
2、工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
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 WxPayUtils {
private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final Random RANDOM = new SecureRandom();
/**
* 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) {
WxPayUtils.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;
}
/**
* 获取随机字符串 Nonce Str
*
* @return String 随机字符串
*/
public static String generateNonceStr() {
char[] nonceChars = new char[32];
for (int index = 0; index < nonceChars.length; ++index) {
nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
}
return new String(nonceChars);
}
public static String generateSignature(Map<String, String> data, String key, String signType) throws Exception {
Set<String> keySet = data.keySet();
String[] keyArray = keySet.toArray(new String[keySet.size()]);
Arrays.sort(keyArray);
StringBuilder sb = new StringBuilder();
for (String k : keyArray) {
if (k.equals("sign")) {
continue;
}
if (data.get(k).trim().length() > 0) // 参数值为空,则不参与签名
sb.append(k).append("=").append(data.get(k).trim()).append("&");
}
sb.append("key=").append(key);
if ("MD5".equals(signType)) {
return MD5(sb.toString()).toUpperCase();
}
else {
throw new Exception(String.format("Invalid sign_type: %s", signType));
}
}
/**
* 生成 MD5
*
* @param data 待处理数据
* @return MD5结果
*/
public static String MD5(String data) throws Exception {
java.security.MessageDigest md = MessageDigest.getInstance("MD5");
byte[] array = md.digest(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
}
/**
* 日志
* @return
*/
public static Logger getLogger() {
Logger logger = LoggerFactory.getLogger("wxpay java sdk");
return logger;
}
}
3、xml工具类
import org.w3c.dom.Document;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
/**
* 2018/7/3
*/
public final class WXPayXmlUtil {
public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
documentBuilderFactory.setXIncludeAware(false);
documentBuilderFactory.setExpandEntityReferences(false);
return documentBuilderFactory.newDocumentBuilder();
}
public static Document newDocument() throws ParserConfigurationException {
return newDocumentBuilder().newDocument();
}
}
4、携带证书的http请求工具类型
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyStore;
/**
* 网络请求工具类
*/
@Slf4j
public class HttpUtil {
/**
* 携带证书的httpPOST请求
* @param postDataXML
* @param certPassword
* @param requestUrl
* @param certFilePath
* @return
* @throws Exception
*/
public static String HttpClientSSL(String postDataXML, String certPassword, String requestUrl,String certFilePath) throws Exception{
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try{
KeyStore keyStore = getCertificate(certPassword,certFilePath);
SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, certPassword.toCharArray()).build();
SSLConnectionSocketFactory sslf = new SSLConnectionSocketFactory(sslContext);
httpClient = HttpClients.custom().setSSLSocketFactory(sslf).build();
HttpPost httpPost = new HttpPost(requestUrl);//退款接口
httpPost.addHeader("Content-Type", "text/xml");
StringEntity reqEntity = new StringEntity(postDataXML,"UTF-8");
httpPost.setEntity(reqEntity);
String result = null;
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity, "UTF8");
EntityUtils.consume(httpEntity);
return result;
}finally {//关流
httpClient.close();
httpResponse.close();
}
}
/**
* 获取证书文件
* @certPassword 证书密码
* @fileUrl 文件路径
*/
private static KeyStore getCertificate(String certPassword,String fileUrl){
//try-with-resources 关流
try (FileInputStream inputStream = new FileInputStream(new File(fileUrl))) {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(inputStream, certPassword.toCharArray());
return keyStore;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
文章浏览阅读1.3k次。#安装paddlehub!pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simpleLooking in indexes: https://pypi.tuna.tsinghua.edu.cn/simpleCollecting paddlehub==1.6.0[?25l Downloading htt..._python3: symbol lookup error: /opt/conda/envs/python35-paddle120-env/lib/pyt
文章浏览阅读197次。前言前面我们已经搭建了基础环境,现在将开发环境更完善一些。devtool在开发的过程,我们会经常调试,so,为了方便我们在chrome中调试源代码,需要更改webpack.config.js,然后启动webpack-dev-server。完成之后在chrome浏览器中打开debug,点击Sources选项,即可看见提示,继而输入你想查看的源文件名即可显示该文件源代码,如果你觉得某处代码有问题,对应..._react 项目 报错 parsing error:the keyword 'import' is reserved
文章浏览阅读296次。ArrayMap在内存使用上较HashMap更有优势,在Android开发中广为使用的基础API,也是大家所推荐的方法,但你是否想过Google如此重要的基础类存在缺陷?..._arraymap 的 坑
文章浏览阅读1w次。网上有很多关于svchost.exe是什么进程,svchost.exe是什么病毒,svchost.exe占用cpu100%或占用大量内存,svchost.exe有十几个,svchost.exe偷偷连网很象木马的问题,本文提供关于svchost.exe进程的所有信息和用各种专业软件进行查杀的方法。 1)svchost.exe是什么进程 Svchost.exe是微软视窗操作系统里的一个
文章浏览阅读7.1k次。 什么叫社区?社区就是团体的意思,具体表现为某个论坛(Forum)参与者群体。当今,在互联网时代,论坛多半是指“网络论坛”。我们问,Ubuntu社区及其论坛是怎么发展起来的?它有什么特点?根据Ubuntu论坛的统计(Ubuntu Linux Forums),8月20日,该论坛涉及236,650个主题,1,400,948篇文章(多半是帖子)。Ubuntu论坛参与人数为150,703,其中积极_ub社区服务器论坛
文章浏览阅读2.8w次,点赞19次,收藏161次。无线通信中 RSRP RSRQ RSSI SINR的定义和区别参考链接:https://blog.csdn.net/shij19/article/details/52946435/RSRP(Reference Signal ReceivedPower参考信号接收功率):小区下行公共导频在测量带宽内功率的线性值(每个RE上的功率),当存在多根接收天线时,需要对多根天线上的测量结果进行比较,上报值不低于任何一个分支对应的RSRP值,max(RSRP00,RSRP01)。即为信号功率S。反映当前信道的路径_rsrq
文章浏览阅读287次。Atitit。監聽鍵盤上下左右方向鍵事件java js jquery c#.net1.Keyword鍵盤事件 方向鍵 上下左右 按鍵監聽2.通用的實現流程Bind (control,key_eventHandel)key_eventHandel獲得當前控件var now=$(".selected");3.Js的實現3.1.Bind control ,event handelerkey_...
文章浏览阅读1.1k次。redis集群从bind127.0.0.1 修改为本地IP需要的相关配置之前配置集群的时候都是默认配置,后来发现在用Jedis连接集群测试的时候无法连接。后来查到是因为配置问题(配置成了127.0.0.1),所以在外面连接的时候无法连接到,需要指定本地ip才可以。于是将其重新配置了一次,终于成功!下面整理了一下重新配置的步骤(以6个redis为例):1、关闭redis集群查找所有redis进程[hadoop@master redis-cluster]$ ps -ef | grep redis_bind 127.0.0.1
文章浏览阅读3.2w次,点赞10次,收藏60次。ModScan32和ModSim32调试工具使用说明_modscan
文章浏览阅读1.8w次,点赞30次,收藏102次。超详细(免安装版)MySQL安装配置教程_mysql免安装配置教程
文章浏览阅读3.2k次。算法分析:先对木棍结构体数组按照l排序,消除一个变量的干扰,然后再找w连续上升的子序列。用临时变量temp,遇到更大的作更新,且标记为0,采用贪心策略去找有几个连续上升的子序列#include #include using namespace std;typedef struct stick{ int l; int w;}stick;int cmp(const void *a,const void *b){ _(1)第一根木棒需要1分钟的准备时间。 (2)在加工完一根长为length,重为weight
文章浏览阅读1.1w次,点赞2次,收藏8次。第十期安防弱电资料包内容后台有朋友问到计算机的拓扑结构,今天我们就来看下几种常见的计算机网络结构。拓扑结构一般指点和线的几何排列或组成的几何图形。计算机网络的拓扑结构是指一个网络的通信链路和结点的几何排列或物理布局图形。链路是网络中相邻两个结点之间的物理通路,结点指计算机和有关的网络设备,甚至指一个网络。按拓扑结构,计算机网络可分为以下五类。一、总线型这种网络拓扑结构中所有设备都直接与总线相连,它..._五种拓扑结构图照片