本文章主要介绍H5使用的支付宝二维码是如果通过java生成的,方便大家更好的对接。
首先集成alipay的sdk,然后要到支付宝商户平台创建应用,生成私钥 公钥,
话不多说直接上代码块
@ApiOperation(value = "支付宝生成二维码", httpMethod = "POST", response = ResultJsonBean.class, notes = "")
@RequestMapping(value = "/alipayQrcode", method = {RequestMethod.POST,RequestMethod.GET})
@ResponseBody
@ParentPCUser
public void alipayQrcode(
@ApiParam(value="token")@RequestParam(required = false) String token,
@ApiParam(value="支付金额",required = true)@RequestParam(required = true) Long totalprice,
@ApiParam(value="支付订单号",required = true)@RequestParam(required = true) String orderNo,
HttpServletRequest requests,HttpServletResponse responses) throws AlipayApiException {
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(OrderInfoUtil.requestUrl, OrderInfoUtil.APP_ID, OrderInfoUtil.APP_PRIVATE_KEY, "json", AlipayUtil.charset, OrderInfoUtil.ALIPAY_PUBLIC_KEY, AlipayUtil.sign_type);
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();//创建API对应的request类
Map<String,Object> map=new HashMap<>();
//设置回调地址
request.setNotifyUrl(AlipayUtil.NOTIFY_URL);
//根据订单号查询订单信息
Map<String,Object> maps=new HashMap<>();
maps.put("out_trade_no",orderNo);
maps.put("total_amount",totalprice+"");
maps.put("subject","科谊达微课");
maps.put("store_id","NJ_001");
maps.put("timeout_express","90m");
//把订单信息转换为json对象的字符串
String postdata = JSONObject.fromObject(maps).toString();
request.setBizContent(postdata);
AlipayTradePrecreateResponse response = alipayClient.execute(request);
String body = response.getBody();
JSONObject jsonObject = JSONObject.fromObject(body);
String qr_code = jsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code");
//流输出
ServletOutputStream sos = null;
try {
sos = responses.getOutputStream();
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 指定纠错级别(L--7%,M--15%,Q--25%,H--30%)
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 编码内容,编码类型(这里指定为二维码),生成图片宽度,生成图片高度,设置参数
BitMatrix bitMatrix = new MultiFormatWriter().encode(qr_code, BarcodeFormat.QR_CODE, 200, 200, hints);
//生成二维码
MatrixToImageWriter.writeToStream(bitMatrix, "png", sos);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
这里要强调的是APP_PRIVATE_KEY是应用私钥,而ALIPAY_PUBLIC_KEY是支付宝公钥,sign_type用RSA还是RSA2取决你在商户平台使用的哪一种,否则会报签名错误,二维码返回其实是有两种方式一种是我写的这种,自己生产一个适用于任何地方的二维码,生成二维码代码
package com.kyd.util.pay;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import com.google.zxing.common.BitMatrix;
public class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {
}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format "
+ format + " to " + file);
}
}
public static void writeToStream(BitMatrix matrix, String format,
OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format "
+ format);
}
}
}
还有一种是生成form表单的形式
@ApiOperation(value = "支付宝生成二维码", httpMethod = "POST", response = ResultJsonBean.class, notes = "")
@RequestMapping(value = "/alipayQrcode", method = {RequestMethod.POST,RequestMethod.GET})
@ResponseBody
@ParentPCUser
public String alipayQrcode(
@ApiParam(value="token")@RequestParam(required = false) String token,
@ApiParam(value="支付金额",required = true)@RequestParam(required = true) Long totalprice,
@ApiParam(value="支付订单号",required = true)@RequestParam(required = true) String orderNo,
HttpServletRequest request,HttpServletResponse response){
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayUtil.gatewayUrl, OrderInfoUtil.APP_ID, OrderInfoUtil.APP_PRIVATE_KEY, "json", AlipayUtil.charset, OrderInfoUtil.ALIPAY_PUBLIC_KEY, AlipayUtil.sign_type);
//创建PC场景下单并支付请求对象
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
//设置同步返回地址,HTTP/HTTPS开头字符串
alipayRequest.setReturnUrl(AlipayUtil.RETURN_URL);
//支付宝服务器主动通知商户服务器里指定的页面http/https路径。
alipayRequest.setNotifyUrl(AlipayUtil.NOTIFY_URL);//在公共参数中设置回跳和通知地址
Map<String,String> requestMap = new HashMap<>();
requestMap.put("out_trade_no",orderNo);//订单编号
requestMap.put("product_code","FAST_INSTANT_TRADE_PAY");//产品交易码
requestMap.put("total_amount",totalprice+"");//实际付款金额
requestMap.put("subject","微课");//商品名称
//填充业务参数
alipayRequest.setBizContent(JSON.toJSONString(requestMap));
String form="";
try {
//调用SDK生成表单
form = alipayClient.pageExecute(alipayRequest).getBody();
} catch (AlipayApiException e) {
e.printStackTrace();
}
return form;
}
}
这种方式是跳转到支付宝的公共扫码页面做支付