支付宝境外支付沙箱调试 Third-party Merchant QR Code payment

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

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代码段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.支付宝会根据上一步你重定向的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

支付宝sdk下载地址工具

https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1google

相关文章
相关标签/搜索