1、添加依赖
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.20</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.41</version>
</dependency>
2、工具类代码
package com.shenma2009.utils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
/**
* @author 军哥
* @version 1.0
* @description: WeixinPayUtils
* @date 2023/4/28 14:34
*/
//需要添加的依赖
//<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
//<dependency>
//<groupId>org.apache.httpcomponents</groupId>
//<artifactId>httpcore</artifactId>
//<version>4.4.10</version>
//</dependency>
//
//<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
//<dependency>
//<groupId>org.apache.httpcomponents</groupId>
//<artifactId>httpclient</artifactId>
//<version>4.5.6</version>
//</dependency>
//
//<dependency>
//<groupId>com.alibaba.fastjson2</groupId>
//<artifactId>fastjson2</artifactId>
//<version>2.0.20</version>
//</dependency>
public class WeixinPayUtils {
public static String GET_ORDER_URL = "https://www.shenmazong.com/weixin/getOrderNo";
public static String GET_ORDER_STATE_URL = "https://www.shenmazong.com/weixin/getOrderInfo?orderNo=";
public static String POST_ORDER_PAY_URL = "https://www.shenmazong.com/weixin/postOrderObj";
/**
* @description 获取订单编号
* @author 军哥
* @date 2023/4/28 14:55
* @version 1.0
*/
public String getOrderNo() throws URISyntaxException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(GET_ORDER_URL);
HttpGet httpGet = new HttpGet(uriBuilder.build());
// httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
CloseableHttpResponse response = client.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity);
System.out.println(str);
response.close();
client.close();
// 处理字符串返回值
JSONObject jsonObject = JSON.parseObject(str);
String orderNo = jsonObject.getString("data");
return orderNo;
}
/**
* @description 获取订单状态
* @author 军哥
* @date 2023/4/28 14:56
* @version 1.0
*/
public Integer getOrderState(String orderNo) throws URISyntaxException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(GET_ORDER_STATE_URL + orderNo);
HttpGet httpGet = new HttpGet(uriBuilder.build());
// httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
CloseableHttpResponse response = client.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity);
System.out.println(str);
response.close();
client.close();
// 处理字符串返回值
JSONObject jsonObject = JSON.parseObject(str);
Integer state = jsonObject.getInteger("code");
return state;
}
/**
* @description 支付接口
* @author 军哥
* @date 2023/4/28 15:06
* @version 1.0
*/
public String postOrderPay(String orderId, Integer amount, String desc) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// 创建httpPost远程连接实例
HttpPost post = new HttpPost(POST_ORDER_PAY_URL);
String result = "";
try (CloseableHttpClient closeableHttpClient = httpClientBuilder.build()) {
// HttpEntity entity = new StringEntity(jsonDataStr);
// 修复 POST json 导致中文乱码
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("outTradeNo", orderId);
paramMap.put("description", desc);
paramMap.put("amount", amount);
paramMap.put("attach", "test");
HttpEntity entity = new StringEntity(JSON.toJSONString(paramMap), "UTF-8");
post.setEntity(entity);
post.setHeader("Content-type", "application/json");
HttpResponse resp = closeableHttpClient.execute(post);
try {
if(resp.getStatusLine().getStatusCode() == 200) {
HttpEntity respEntity = resp.getEntity();
String str = EntityUtils.toString(respEntity);
result = str;
// 解析返回接口
JSONObject jsonObject = JSON.parseObject(str);
JSONObject data = jsonObject.getJSONObject("data");
String codeUrl = data.getString("codeUrl");
result = codeUrl;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
3、支付接口实体类
package com.shenma2009.vo;
import lombok.Data;
import java.io.Serializable;
/**
* @author 军哥
* @version 1.0
* @description: WeixinPayVo
* @date 2023/4/28 15:09
*/
@Data
public class WeixinPayVo implements Serializable {
private String orderId;
private Integer amount;
private String desc;
}
4、后端生成二维码工具类
package com.shenma2009.utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
/**
* @author 军哥
* @version 1.0
* @description: QRCodeUtil
* @date 2023/4/29 9:00
*/
// <dependency>
// <groupId>com.google.zxing</groupId>
// <artifactId>javase</artifactId>
// <version>3.3.3</version>
// </dependency>
public class QRCodeUtil {
public static void createQRCode(HttpServletResponse response, String codeURL) {
// 生成二维码
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 1);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(codeURL, BarcodeFormat.QR_CODE, 300, 300, hints);
OutputStream out = response.getOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "png", out);// 输出二维码
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
5、支付接口
package com.shenma2009.controller;
import com.shenma2009.domain.ResultResponse;
import com.shenma2009.utils.QRCodeUtil;
import com.shenma2009.utils.WeixinPayUtils;
import com.shenma2009.vo.WeixinPayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URISyntaxException;
/**
* @author 军哥
* @version 1.0
* @description: WeixinPayController
* @date 2023/4/28 14:42
*/
@RestController
@Slf4j
@RequestMapping(value = "/weixin")
public class WeixinPayController {
@PostMapping(value = "/getOrderNo")
public ResultResponse getOrderNo() throws IOException, URISyntaxException {
WeixinPayUtils weixinPayUtils = new WeixinPayUtils();
String orderNo = weixinPayUtils.getOrderNo();
return ResultResponse.SUCCESS(orderNo);
}
@PostMapping(value = "/getOrderState")
public ResultResponse getOrderState(String orderId) throws IOException, URISyntaxException {
WeixinPayUtils weixinPayUtils = new WeixinPayUtils();
Integer orderState = weixinPayUtils.getOrderState(orderId);
if(orderState.equals(0)) {
return ResultResponse.SUCCESS();
}
return ResultResponse.FAILED(500, "支付失败");
}
@PostMapping(value = "/postOrderPay")
public ResultResponse postOrderPay(@RequestBody WeixinPayVo weixinPayVo) {
WeixinPayUtils weixinPayUtils = new WeixinPayUtils();
String url_code = weixinPayUtils.postOrderPay(weixinPayVo.getOrderId(), weixinPayVo.getAmount(), weixinPayVo.getDesc());
return ResultResponse.SUCCESS(url_code);
}
@GetMapping(value = "/getOrderQRCode/{orderId}/{amount}")
public void getOrderQRCode(HttpServletResponse response, @PathVariable("orderId") String orderId, @PathVariable("amount") Integer amount) {
WeixinPayUtils weixinPayUtils = new WeixinPayUtils();
String codeUrl = weixinPayUtils.postOrderPay(orderId, amount, "支付测试");
if(codeUrl != null) {
QRCodeUtil.createQRCode(response, codeUrl);
}
}
}