微信支付二维码Java代码
时间: 2023-10-19 22:08:52 浏览: 99
以下是微信支付二维码的Java代码示例:
```java
import java.net.URLEncoder;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class WeChatPayQRCode {
private static final String CHARSET_UTF8 = "UTF-8";
private static final String SIGN_TYPE_HMAC_SHA256 = "HMAC-SHA256";
private static final String SIGN_TYPE_MD5 = "MD5";
private static String createSign(SortedMap<String, String> parameters, String key, String signType)
throws Exception {
StringBuilder sb = new StringBuilder();
for (String k : parameters.keySet()) {
String v = parameters.get(k);
if (v != null && !"".equals(v.trim()) && !"sign".equals(k) && !"key".equals(k)) {
sb.append(k).append("=").append(v).append("&");
}
}
sb.append("key=").append(key);
if (SIGN_TYPE_MD5.equals(signType)) {
return MD5(sb.toString()).toUpperCase();
} else if (SIGN_TYPE_HMAC_SHA256.equals(signType)) {
return HMACSHA256(sb.toString(), key);
} else {
throw new Exception(String.format("Invalid sign_type: %s", signType));
}
}
private static String MD5(String data) throws Exception {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(data.getBytes(CHARSET_UTF8));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
}
private static String HMACSHA256(String data, String key) throws Exception {
Mac sha256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(CHARSET_UTF8), "HmacSHA256");
sha256.init(secretKeySpec);
byte[] array = sha256.doFinal(data.getBytes(CHARSET_UTF8));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
}
private static String buildQuery(SortedMap<String, String> parameters) throws Exception {
StringBuilder sb = new StringBuilder();
for (String k : parameters.keySet()) {
String v = parameters.get(k);
if (v != null && !"".equals(v.trim())) {
sb.append(k).append("=").append(URLEncoder.encode(v, CHARSET_UTF8)).append("&");
}
}
return sb.toString().substring(0, sb.length() - 1);
}
public static String generateQRCode(String appId, String mchId, String key, String body, String outTradeNo,
int totalFee, String notifyUrl, String tradeType) throws Exception {
SortedMap<String, String> parameters = new TreeMap<>();
parameters.put("appid", appId);
parameters.put("mch_id", mchId);
parameters.put("nonce_str", java.util.UUID.randomUUID().toString().replace("-", ""));
parameters.put("body", body);
parameters.put("out_trade_no", outTradeNo);
parameters.put("total_fee", String.valueOf(totalFee));
parameters.put("spbill_create_ip", "127.0.0.1");
parameters.put("notify_url", notifyUrl);
parameters.put("trade_type", tradeType);
parameters.put("product_id", outTradeNo); // NATIVE trade_type必填,商品ID
String sign = createSign(parameters, key, SIGN_TYPE_MD5);
parameters.put("sign", sign);
String query = buildQuery(parameters);
String url = String.format("weixin://wxpay/bizpayurl?%s", query);
return url;
}
}
```
使用示例:
```java
String appId = "your_app_id";
String mchId = "your_mch_id";
String key = "your_key";
String body = "test";
String outTradeNo = "123456789";
int totalFee = 100;
String notifyUrl = "http://your.notify.url";
String tradeType = "NATIVE";
String qrCodeUrl = WeChatPayQRCode.generateQRCode(appId, mchId, key, body, outTradeNo, totalFee, notifyUrl, tradeType);
```
注意,为了使用该代码,你需要引入 Apache Commons Codec 依赖包。可以在 Maven 中添加以下代码:
```xml
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
```
阅读全文