运行官方微信支付demo(超详细)
运行官方微信支付demo(超详细)
首先要说说写这篇文章的初衷:集成支付宝支付运行demo都是可以正常运行的,但是我下载下来微信支付的demo,却发现一大堆报错,而且相关文章几乎没有,可能大家觉得没必要,也许你觉得很简单;但是技术大牛都是从小白开始的,所以本文将详细描述如何运行官方微信支付demo。
环境准备
1. IDE选择: 本文使用IntelliJ IDEA2022.2作为开发工具。
2. Java版本: 使用Java11(LTS)作为开发语言。
3. 微信支付SDK: 下载最新的微信支付SDK,目前为v3.7.0。
步骤一:创建微信支付项目
1. 在IntelliJ IDEA中新建一个Maven项目,选择Java11作为开发语言。
2. 将下载好的微信支付SDK添加到项目中。
3. 创建一个新的类文件,例如`WeChatPayDemo.java`。
步骤二:配置微信支付环境
1. 在`WeChatPayDemo.java`中创建一个`Config`类来存储微信支付的API密钥和商户ID等信息。
```javapublic class Config {
public static final String APP_ID = "your_app_id";
public static final String MCH_ID = "your_mch_id";
public static final String API_KEY = "your_api_key";
}
```
2. 在`WeChatPayDemo.java`中创建一个`WXPayConfig`类来存储微信支付的配置信息。
```javapublic class WXPayConfig {
private String appId;
private String mchId;
private String apiKey;
public WXPayConfig(String appId, String mchId, String apiKey) {
this.appId = appId;
this.mchId = mchId;
this.apiKey = apiKey;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getMchId() {
return mchId;
}
public void setMchId(String mchId) {
this.mchId = mchId;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
}
```
3. 在`WeChatPayDemo.java`中创建一个`WXPayUtil`类来处理微信支付的业务逻辑。
```javapublic class WXPayUtil {
private static final String CHARSET = "UTF-8";
public static String getNonceStr() {
return UUID.randomUUID().toString();
}
public static String getTimestamp() {
return System.currentTimeMillis() /1000 + "";
}
public static String getSignType() {
return "MD5";
}
public static String getSign(String apiKey, String nonceStr, String timestamp) {
String sign = apiKey + "=" + apiKey + "&nonce_str=" + nonceStr + "×tamp=" + timestamp;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(sign.getBytes(CHARSET));
return bytesToHex(bytes);
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
private static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() ==1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
public static boolean isSignValid(String apiKey, String nonceStr, String timestamp, String sign) {
String expectedSign = getSign(apiKey, nonceStr, timestamp);
return expectedSign.equals(sign);
}
}
```
4. 在`WeChatPayDemo.java`中创建一个`WXPayRequest`类来封装微信支付的请求信息。
```javapublic class WXPayRequest {
private String appId;
private String mchId;
private String apiKey;
private String nonceStr;
private String timestamp;
private String sign;
public WXPayRequest(String appId, String mchId, String apiKey) {
this.appId = appId;
this.mchId = mchId;
this.apiKey = apiKey;
this.nonceStr = WXPayUtil.getNonceStr();
this.timestamp = WXPayUtil.getTimestamp();
this.sign = WXPayUtil.getSign(apiKey, nonceStr, timestamp);
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getMchId() {
return mchId;
}
public void setMchId(String mchId) {
this.mchId = mchId;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getNonceStr() {
return nonceStr;
}
public void setNonceStr(String nonceStr) {
this.nonceStr = nonceStr;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
}
```
5. 在`WeChatPayDemo.java`中创建一个`WXPayService`类来处理微信支付的业务逻辑。
```javapublic class WXPayService {
private static final String API_URL = " public boolean pay(WXPayRequest request) {
try {
URL url = new URL(API_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
String json = "{"appid":"" + request.getAppId() + "","mch_id":"" + request.getMchId() + "","nonce_str":"" + request.getNonceStr() + "","body":"" + "测试支付" + "","total_fee":1,"spbill_create_ip":"192.168.1.1","notify_url":" byte[] bytes = json.getBytes("UTF-8");
connection.getOutputStream().write(bytes);
int responseCode = connection.getResponseCode();
if (responseCode ==200) {
String result = readResponse(connection);
JSONObject jsonObject = new JSONObject(result);
return jsonObject.getString("return_code").equals("SUCCESS") && jsonObject.getString("return_msg").equals("OK");
} else {
throw new RuntimeException("Failed to pay, response code: " + responseCode);
}
} catch (IOException | JSONException e) {
throw new RuntimeException(e);
}
}
private String readResponse(HttpURLConnection connection) throws IOException {
StringBuilder builder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
int character;
while ((character = reader.read()) != -1) {
builder.append((char) character);
}
}
return builder.toString();
}
}
```
6. 在`WeChatPayDemo.java`中创建一个`Main`类来测试微信支付的业务逻辑。
```javapublic class Main {
public static void main(String[] args) {
WXPayService service = new WXPayService();
WXPayRequest request = new WXPayRequest(Config.APP_ID, Config.MCH_ID, Config.API_KEY);
boolean result = service.pay(request);
System.out.println("是否支付成功:" + result);
}
}
```
7. 运行`Main`类,测试微信支付的业务逻辑。
步骤三:配置微信支付环境
1. 在微信支付平台中创建一个商户账号。
2. 在微信支付平台中创建一个API密钥。
3. 将API密钥添加到`Config`类中。
4. 将商户ID添加到`WXPayRequest`类中。
步骤四:测试微信支付
1. 运行`Main`类,测试微信支付的业务逻辑。
2. 在微信支付平台中查看支付结果。
以上就是如何运行官方微信支付demo的详细步骤。