Java实现小程序调起微信支付
时间: 2023-06-12 18:06:16 浏览: 195
要在Java中调用微信支付,需要使用微信开放平台提供的统一下单API。下面是一个简单的示例代码,展示了如何使用Java调用微信支付API:
```java
public class WechatPayUtil {
private static final String URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; // 统一下单API地址
public static String createOrder(String appid, String mch_id, String out_trade_no, String body, int total_fee, String notify_url, String trade_type, String spbill_create_ip, String key) throws Exception {
// 构造请求参数
SortedMap<String, String> paramMap = new TreeMap<String, String>();
paramMap.put("appid", appid);
paramMap.put("mch_id", mch_id);
paramMap.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
paramMap.put("body", body);
paramMap.put("out_trade_no", out_trade_no);
paramMap.put("total_fee", String.valueOf(total_fee));
paramMap.put("spbill_create_ip", spbill_create_ip);
paramMap.put("notify_url", notify_url);
paramMap.put("trade_type", trade_type);
// 生成签名
String sign = createSign(paramMap, key);
paramMap.put("sign", sign);
// 将请求参数转换为XML格式
String xml = mapToXml(paramMap);
// 发起HTTP请求
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
httpPost.setEntity(new StringEntity(xml, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// 处理响应结果
String responseXml = EntityUtils.toString(httpEntity, "UTF-8");
SortedMap<String, String> responseMap = xmlToMap(responseXml);
String prepayId = responseMap.get("prepay_id");
return prepayId;
}
private static String createSign(SortedMap<String, String> paramMap, String key) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
if (entry.getValue() != null && !"".equals(entry.getValue()) && !"sign".equals(entry.getKey())) {
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
}
sb.append("key=").append(key);
String str = sb.toString();
String sign = MD5Util.MD5Encode(str, "UTF-8").toUpperCase();
return sign;
}
private static String mapToXml(SortedMap<String, String> map) throws Exception {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("xml");
for (Map.Entry<String, String> entry : map.entrySet()) {
if (entry.getValue() != null) {
Element element = root.addElement(entry.getKey());
element.setText(entry.getValue());
}
}
return document.asXML();
}
private static SortedMap<String, String> xmlToMap(String xml) throws Exception {
SortedMap<String, String> map = new TreeMap<String, String>();
Document document = DocumentHelper.parseText(xml);
Element root = document.getRootElement();
List<Element> elements = root.elements();
for (Element element : elements) {
map.put(element.getName(), element.getText());
}
return map;
}
}
```
其中,createOrder方法用于调用统一下单API,传入的参数包括:
- appid:公众账号ID或应用ID
- mch_id:商户号
- out_trade_no:商户订单号
- body:商品描述
- total_fee:总金额,单位为分
- notify_url:接收微信支付异步通知回调地址
- trade_type:交易类型,JSAPI代表公众号支付
- spbill_create_ip:终端IP
- key:商户密钥,用于生成签名
调用成功后,会返回预支付ID(prepay_id),用于生成支付请求。
阅读全文