1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java实现支付宝二维码支付(Spring Boot)

java实现支付宝二维码支付(Spring Boot)

时间:2021-09-18 16:10:34

相关推荐

java实现支付宝二维码支付(Spring Boot)

本文章主要介绍H5使用的支付宝二维码是如果通过java生成的,方便大家更好的对接。首先集成alipay的sdk,然后要到支付宝商户平台创建应用,生成私钥 公钥,话不多说直接上代码块

@ApiOperation(value = "支付宝生成二维码", httpMethod = "POST", response = ResultJsonBean.class, notes = "")@RequestMapping(value = "/alipayQrcode", method = {RequestMethod.POST,RequestMethod.GET})@ResponseBody@ParentPCUserpublic 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 {//获得初始化的AlipayClientAlipayClient 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.mon.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@ParentPCUserpublic 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){//获得初始化的AlipayClientAlipayClient 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;}}

这种方式是跳转到支付宝的公共扫码页面做支付

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。