微信支付Java实现 微信支付 NATIVE 方式 网页二维码的形式显示
立即下载
资源介绍:
微信支付Java实现 微信支付 NATIVE 方式 网页二维码的形式显示
package com.mqd.weChatPay.service.impl;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.http.HttpUtil;
import com.mqd.weChatPay.config.WeChatConfig;
import com.mqd.weChatPay.service.WeChatPayService;
import com.mqd.weChatPay.util.PayForUtil;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
@Service
public class WeChatPayServiceImpl implements WeChatPayService {
@Override
public void codePay(HttpServletRequest request, HttpServletResponse response) {
// 支付的商品名称
String body="嘴巴香大辣条";
//支付金额(单位:分)
String totalFee="200";
//获取二维码内容urlCode
String resXml = null;
try {
resXml = getNative( body, totalFee);
Map data= XmlUtil.xmlToMap(resXml);
String urlCode = data.get("code_url").toString();
//生成二维码到输出流
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
System.out.println(out);
QrCodeUtil.generate(urlCode, 300, 300,"jpg" ,out);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description 支付回调
*/
@Override
public void wxnotify(HttpServletRequest request, HttpServletResponse response) {
// 读取回调数据
InputStream inputStream;
StringBuffer sb = new StringBuffer();
try {
inputStream = request.getInputStream();
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((s = in.readLine()) != null) {
sb.append(s);
}
in.close();
inputStream.close();
// 解析xml成map
Map m=XmlUtil.xmlToMap(sb.toString());
// 过滤空 设置 TreeMap
SortedMap