我要集成的是 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