我要集成的是 Third-party Merchant QR Code payment 的 System Integration 业务流程。java
业务流程图https://global.alipay.com/service/external_QR_Code/41spring
先讲 第一步 (Configure the authorization callback page URL)和 第二步 (Obtain the Alipay user ID)apache
package com.test.core.alipay.utils;; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Hashtable; import java.util.Map; import javax.imageio.ImageIO; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.superjh.core.alipay.utils.ZxingUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //生成二维码工具类 public class ZxingUtils { private static Log log = LogFactory.getLog(ZxingUtils.class); private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private 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; } private 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); } } /** 将内容contents生成长宽均为width的图片,图片路径由imgPath指定 */ public static File getQRCodeImge(String contents, int width, String imgPath) { return getQRCodeImge(contents, width, width, imgPath); } /** 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定 */ public static File getQRCodeImge(String contents, int width, int height, String imgPath) { try { Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.CHARACTER_SET, "UTF8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); File imageFile = new File(imgPath); writeToFile(bitMatrix, "png", imageFile); return imageFile; } catch (Exception e) { log.error("create QR code error!", e); return null; } } }
springMVC Controller代码段json
//支付宝用户受权 @RequestMapping(value="/aliPayOauth") public void aliPayOauth( HttpServletRequest request,HttpServletResponse response, @RequestHeader ("User-Agent") String userAgent) throws IOException{ logger.info(userAgent); //用userAgent 判断来自支付宝 if(AlipayCore.fromAlipay(userAgent)){ //这里是沙箱环境 response.sendRedirect("https://openauth.alipaydev.com/oauth2/publicAppAuthorize.htm?app_id=你的app_id&state=换成你要带的参数&scope=auth_base&redirect_uri&redirect_uri=沙箱里配置的受权回调地址"); } }
3.1支付宝回调回来以后 (支付宝会带来auth_code参数) 你能够用auth_code调用支付宝的 api
https://openauth.alipay.com/oauth2/publicAppAuthorize.htm 接口来获取用户ID服务器
springMVC Controller代码段 (这里我调用的是支付宝SDK来完成此接口的调用)session
@RequestMapping(value="/oauthRedirect") public String oauthRedirect(HttpSession session, Model model,HttpServletResponse response, String app_id,String source,String scope,String auth_code,String state, @RequestHeader ("User-Agent") String userAgent)throws Exception { AlipayClient alipayClient = new DefaultAlipayClient( "https://openapi.alipaydev.com/gateway.do", app_id, APP_PRIVATE_KEY "json","utf-8", ALIPAY_PUBLIC_KEY, "RSA"); AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest(); request.setGrantType("authorization_code"); request.setCode(auth_code); AlipaySystemOauthTokenResponse oauthResponse = alipayClient.execute(request); logger.info("支付宝用户受权信息:"+oauthResponse.getBody()); if(oauthResponse.isSuccess()){ model.addAttribute("userId", oauthResponse.getUserId()); model.addAttribute("state", state); //上一步的自定义参数 } return "/dashboard"; }
到此就能够在支付宝中呈现出你须要显示的支付界面了app
https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1google