我要集成的是 Third-party Merchant QR Code payment 的 System Integration 业务流程。
业务流程图https://global.alipay.com/service/external_QR_Code/41
先讲 第一步 (Configure the authorization callback page URL)和 第二步 (Obtain the Alipay user ID)
1.生成二维码 (内容为 一个支付宝可访问到的url ) 下面是工具类 其中url可以带上你需要的参数
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;
}
}
}
2.支付宝用户扫码后 根据你的url(也就是上一步的url)跳转到你的服务器地址
springMVC Controller代码段
//支付宝用户授权
@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.支付宝会根据上一步你重定向的redirect_uri访问的的授权回调页面 (其中redirect_uri 需要与你沙箱的配置的授权回调地址一致)
3.1支付宝回调回来之后 (支付宝会带来auth_code参数) 你可以用auth_code调用支付宝的
https://openauth.alipay.com/oauth2/publicAppAuthorize.htm 接口来获取用户ID
springMVC Controller代码段 (这里我调用的是支付宝SDK来完成此接口的调用)
@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";
}
到此就可以在支付宝中呈现出你需要显示的支付界面了
https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1