java开发记录-微信二维码支付

关于微信二维码支付的一点点总结.如上一个博客所说,开始开发前需要前往官网进行一系列的接入,从而得到相关的appid,密钥.

本次的开发中,使用谷歌zxing实现将支付链接字符串转为二维码.附上相关依赖:

<!-- 谷歌生成二维码 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.0</version>
</dependency>

1.PayUtils工具类

    和之前微信H5支付类似,只是将其中的支付类型trade_type修改为NATIVE,代码如下:

public class PayUtils {
	/**
	 * 公众号AppId
	 */
	public static final String APP_ID = "xxx";
	/**
	 * 公众号AppSecret
	 */
	public static final String APP_SECRET = "xxx";
	/**
	 * 微信支付商户号
	 */
	public static final String MCH_ID = "xxx";
	/**
	 * 微信支付API秘钥
	 */
	public static final String KEY = "xxx";
	/**
	 * 回调域名
	 */
	public static final String REDIRECT_DOMAIN = "http://xxx.com";

	private static final WxMpService wxMpService = new WxMpServiceImpl();
	private static final WxPayService wxPayService = new WxPayServiceImpl();

	static {
		WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
		config.setAppId(APP_ID);
		config.setSecret(APP_SECRET);
		wxMpService.setWxMpConfigStorage(config);

		WxPayConfig payConfig = new WxPayConfig();
		payConfig.setAppId(APP_ID);
		payConfig.setMchId(MCH_ID);
		payConfig.setMchKey(KEY);
		payConfig.setSignType(WxPayConstants.SignType.MD5);
		//二维码支付
		payConfig.setTradeType("NATIVE");
		payConfig.setNotifyUrl(REDIRECT_DOMAIN + "/pay/success");
		wxPayService.setConfig(payConfig);
	}

	public static WxMpService wxMpService() {
		return wxMpService;
	}

	public static WxPayService wxPayService() {
		return wxPayService;
	}

}

需要注意的是,在接下来的controller设计中,tradeType值为NATIVE时,微信统一下单接口的prodectId是必填的.

2.后台controller

	/**
	 * 首页
	 */
	@RequestMapping(value = "/index")
	public String index(Model model) {
		String qrcodePayUrl = PayUtils.REDIRECT_DOMAIN+"/pay/qrcodePay";
		model.addAttribute("qrcodePay",qrcodePayUrl);

		String downloadQRCode = PayUtils.REDIRECT_DOMAIN+"/pay/downloadQRCode";
		model.addAttribute("downloadQRCode",downloadQRCode);
		return "index";
	}


	/**
	 * 点击链接跳转显示支付二维码
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "/qrcodePay")
	public void qrcodePay(HttpServletRequest request,HttpServletResponse response,Model model){
		try {
			//统一订单必填参数
			WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
			orderRequest.setBody("微信二维码支付测试");
			orderRequest.setOutTradeNo(UUID.randomUUID().toString().replace("-", ""));
			orderRequest.setTotalFee(1);
			//用户ip
			orderRequest.setSpbillCreateIp(getRealIp(request));
			//商品ID 二维码支付时必填
			orderRequest.setProductId(UUID.randomUUID().toString().replace("-", ""));
			//签名创建
			Map<String,String> packageParams = new HashMap<>();
			packageParams.put("appid", PayUtils.APP_ID);
			packageParams.put("mch_id", PayUtils.MCH_ID);
			packageParams.put("nonce_str", UUID.randomUUID().toString().replace("-", ""));
			packageParams.put("signType",orderRequest.getSignType());
			String sign = SignUtils.createSign(packageParams,"MD5", PayUtils.KEY,false);
			orderRequest.setSign(sign);

			WxPayNativeOrderResult orderResult =  PayUtils.wxPayService().createOrder(orderRequest);
			String codeUrl = orderResult.getCodeUrl();

			QRcodeImageUtil.encode(codeUrl,300,300,getIconUrl(),null,false,response);
		} catch (WxPayException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 直接在页面上显示支付二维码
	 * @param request
	 * @return
	 */
	@RequestMapping("/downloadQRCode")
	public ResponseEntity<byte[]> downloadQRCode(HttpServletRequest request){
		try{
			//统一订单必填参数
			WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
			orderRequest.setBody("微信二维码支付测试");
			orderRequest.setOutTradeNo(UUID.randomUUID().toString().replace("-", ""));
			orderRequest.setTotalFee(1);
			//用户ip
			orderRequest.setSpbillCreateIp(getRealIp(request));
			//商品ID 二维码支付时必填
			orderRequest.setProductId(UUID.randomUUID().toString().replace("-", ""));
			//签名创建
			Map<String,String> packageParams = new HashMap<>();
			packageParams.put("appid", PayUtils.APP_ID);
			packageParams.put("mch_id", PayUtils.MCH_ID);
			packageParams.put("nonce_str", UUID.randomUUID().toString().replace("-", ""));
			packageParams.put("signType",orderRequest.getSignType());
			String sign = SignUtils.createSign(packageParams,"MD5", PayUtils.KEY,false);
			orderRequest.setSign(sign);

			WxPayNativeOrderResult orderResult =  PayUtils.wxPayService().createOrder(orderRequest);
			String codeUrl = orderResult.getCodeUrl();

			return QRcodeImageUtil.getResponseEntity(codeUrl,300,300,"png",getIconUrl());
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	//获取真实ip地址 通过阿帕奇代理的也能获取到真实ip
	private static String getRealIp(HttpServletRequest request){
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 获取二维码中心图标再项目中的地址
	 * @return
	 */
	private static String getIconUrl(){
		ClassLoader classLoader = PayUtils.class.getClassLoader();
		URL resource = classLoader.getResource("");
		String icon = resource.getPath();
		//target/classes/下的图片无法使用,所以将其截取掉,并自主拼接好src下的图片位置
		icon = icon.substring(0,icon.length()-15);
		icon = icon +"/src/main/resources/public/image/bg.jpg";
		return icon;
	}

这里的getIconUrl()是获取项目中图片的方法,之前通过获取classes下的图片,发现不能嵌套到二维码中,具体原因还不清楚,若有大神知道,望指点.

3.zxing工具类

关于zxing的使用方法,我也是从别的博主那找来的方法,对其中的方法进行了一点点修改,还没有进行总结归纳,不过忘记那位博主的地址了=_=...直接附代码吧.

package com.novice.project.wxpay.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class QRcodeImageUtil {
	/**
	 * 图片格式定义
	 * @value Array
	 */
	private static String[] IMAGE_TYPE = {"BMP", "bmp", "jpg", "JPG", "wbmp", "jpeg", "png", "PNG", "JPEG", "WBMP", "GIF", "gif","ICON","icon"};

	/**
	 * 二维码宽度
	 */
	public static final int WIDTH = 260;

	/**
	 * 二维码高度
	 */
	public static final int HEIGHT = 260;

	/**
	 * 图标宽度
	 */
	private static final int IMAGE_WIDTH = 68;
	/**
	 * 图标高度
	 */
	private static final int IMAGE_HEIGHT = 68;
	/**
	 * 底图大小【正方形】
	 */
	private static final int IMAGE_HALF_WIDTH = IMAGE_WIDTH / 2;
	/**
	 * 底图边框
	 */
	private static final int FRAME_WIDTH = 5;

	/**
	 * 二维码写码器
	 */
	private static MultiFormatWriter mutiWriter = new MultiFormatWriter();

	/**
	 * 二维码生成-方法入口
	 * @param content 内容
	 * @param width 宽度
	 * @param height 高度
	 * @param iconImagePath 图标原路径
	 * @param qrcodeImagePath 二维码存放路径
	 * @param hasFiller
	 *             比例不对时是否需要补白:true为补白; false为不补白
	 * @return
	 *         成功:返回输出图片绝对路径;失败:返回null
	 */
	public static String encode(String content, int width, int height,
	                            String iconImagePath, String qrcodeImagePath, boolean hasFiller, HttpServletResponse response) {
		try {
			/**
			 * 图标格式校验
			 */
			File icon = new File(iconImagePath);
			if(!icon.exists()){
				System.err.println("系统找不到图标所在文件 !");
				return null;
			}
			String iconFileName = icon.getName();
			// 得到上传文件的扩展名
			String fileExtName = iconFileName.substring(iconFileName.lastIndexOf(".") + 1);
			if(!checkIMGType(fileExtName)){
				System.err.println("图标格式有误 !");
				return null;
			}

			if(width<260||height<260){
				width = QRcodeImageUtil.WIDTH;
				height = QRcodeImageUtil.HEIGHT;
			}
			if(qrcodeImagePath!=null){
				ImageIO.write(genBarcode(content, width, height, iconImagePath,hasFiller),
						"png", new File(qrcodeImagePath));
				System.err.println("二维码已生成  "+qrcodeImagePath);
			}else{
				ImageIO.write(genBarcode(content, width, height, iconImagePath, hasFiller),
						"png", response.getOutputStream());
			}
			return qrcodeImagePath;
		} catch (IOException e) {
			System.err.println("图片读取异常 : "+e.getMessage());
		} catch (WriterException e) {
			System.err.println("图片输出异常 :"+e.getMessage());
		}
		return null;
	}

	/**
	 * 图片处理方法
	 * @param content
	 * @param width
	 * @param height
	 * @param iconImagePath
	 * @param hasFiller
	 *             比例不对时是否需要补白:true为补白; false为不补白;
	 * @return
	 * @throws WriterException
	 * @throws IOException
	 */
	private static BufferedImage genBarcode(String content, int width,
	                                        int height, String iconImagePath,boolean hasFiller) throws WriterException,
			IOException {
		// 读取源图像
		BufferedImage scaleImage = scale(iconImagePath, IMAGE_WIDTH,
				IMAGE_HEIGHT, hasFiller);
		int[][] srcPixels = new int[IMAGE_WIDTH][IMAGE_HEIGHT];
		for (int i = 0; i < scaleImage.getWidth(); i++) {
			for (int j = 0; j < scaleImage.getHeight(); j++) {
				srcPixels[i][j] = scaleImage.getRGB(i, j);
			}
		}

		Map<EncodeHintType, Object> hint = new HashMap<EncodeHintType, Object>();
		hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
		hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
		// 生成二维码
		BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE,
				width, height, hint);

		// 二维矩阵转为一维像素数组
		int halfW = matrix.getWidth() / 2;
		int halfH = matrix.getHeight() / 2;
		int[] pixels = new int[width * height];

		for (int y = 0; y < matrix.getHeight(); y++) {
			for (int x = 0; x < matrix.getWidth(); x++) {
				// 读取图片
				if (x > halfW - IMAGE_HALF_WIDTH
						&& x < halfW + IMAGE_HALF_WIDTH
						&& y > halfH - IMAGE_HALF_WIDTH
						&& y < halfH + IMAGE_HALF_WIDTH) {
					pixels[y * width + x] = srcPixels[x - halfW
							+ IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH];
				}
				// 在图片四周形成边框
				else if ((x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH
						&& x < halfW - IMAGE_HALF_WIDTH + FRAME_WIDTH
						&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH
						+ IMAGE_HALF_WIDTH + FRAME_WIDTH)
						|| (x > halfW + IMAGE_HALF_WIDTH - FRAME_WIDTH
						&& x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
						&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH
						+ IMAGE_HALF_WIDTH + FRAME_WIDTH)
						|| (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH
						&& x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
						&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH
						- IMAGE_HALF_WIDTH + FRAME_WIDTH)
						|| (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH
						&& x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
						&& y > halfH + IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH
						+ IMAGE_HALF_WIDTH + FRAME_WIDTH)) {
					pixels[y * width + x] = 0xfffffff;
				} else {
					// 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色;
					pixels[y * width + x] = matrix.get(x, y) ? 0xff000000
							: 0xfffffff;
				}
			}
		}

		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		image.getRaster().setDataElements(0, 0, width, height, pixels);

		return image;
	}

	/**
	 * 把传入的原始图像按高度和宽度进行缩放,生成符合要求的图标
	 *
	 * @param iconImagePath
	 *            小图标源文件地址
	 * @param height
	 *            目标高度
	 * @param width
	 *            目标宽度
	 * @param hasFiller
	 *            比例不对时是否需要补白:true为补白; false为不补白;
	 * @throws IOException
	 */
	private static BufferedImage scale(String iconImagePath, int height,
	                                   int width, boolean hasFiller) throws IOException {
		double ratio = 0.0; // 缩放比例
		File file = new File(iconImagePath);
		BufferedImage srcImage = ImageIO.read(file);
		Image destImage = srcImage.getScaledInstance(width, height,
				BufferedImage.SCALE_SMOOTH);
		// 计算比例
		if ((srcImage.getHeight() > height) || (srcImage.getWidth() > width)) {
			if (srcImage.getHeight() > srcImage.getWidth()) {
				ratio = (new Integer(height)).doubleValue()
						/ srcImage.getHeight();
			} else {
				ratio = (new Integer(width)).doubleValue()
						/ srcImage.getWidth();
			}
			AffineTransformOp op = new AffineTransformOp(
					AffineTransform.getScaleInstance(ratio, ratio), null);
			destImage = op.filter(srcImage, null);
		}
		if (hasFiller) {// 补白
			BufferedImage image = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D graphic = image.createGraphics();
			graphic.setColor(Color.white);
			graphic.fillRect(0, 0, width, height);
			if (width == destImage.getWidth(null))
				graphic.drawImage(destImage, 0,
						(height - destImage.getHeight(null)) / 2,
						destImage.getWidth(null), destImage.getHeight(null),
						Color.white, null);
			else
				graphic.drawImage(destImage,
						(width - destImage.getWidth(null)) / 2, 0,
						destImage.getWidth(null), destImage.getHeight(null),
						Color.white, null);
			graphic.dispose();
			destImage = image;
			System.err.println("INFO 图标补白已完成 ");
		}
		return (BufferedImage) destImage;
	}

	/**
	 * 图片格式校验
	 * @param fileExtName
	 * @return
	 */
	private static boolean checkIMGType(String fileExtName){
		boolean flag = false;
		for (String type : IMAGE_TYPE) {
			//-- 图片格式正确
			if(type.toLowerCase().equals(fileExtName.toLowerCase())){
				flag = true;
				break;
			}
		}
		//------------图片格式校验结束
		return flag;
	}

	/**
	 * 生成并下载二维码
	 * @param content
	 * @param width
	 * @param height
	 * @param format
	 * @param imgPath
	 * @return
	 */
	public static ResponseEntity<byte[]> getResponseEntity(String content,int width,int height,String format,String imgPath){
		try {
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			ImageIO.write(genBarcode(content,width,height,imgPath,false),format,outputStream);
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
			return new ResponseEntity<byte[]>(outputStream.toByteArray(),headers, HttpStatus.CREATED);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}

4.前台显示

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="x5-orientation" content="portrait">
    <title>微信二维码支付测试</title>
</head>
<body>
<div id="container">
    <a th:href="${qrcodePay}" style="font-size:40px;">
        二维码支付
    </a><br>
    <img th:src="${downloadQRCode}">
</div>
</body>
</html>

前台使用thymeleaf进行前后数据绑定,你也可以使用自己的方式.这里仅仅是一个参考.微信的公众号/H5/二维码支付demo也会提供.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值