支付宝扫码支付

支付宝扫码支付的流程和微信扫码支付的留言相同。
前提条件:需要有支付宝开放平台,有上线的应用,有面对面支付服务;
支付宝扫码支付_第1张图片
1.通过预支付返回qr_code,开发者根据qr_code生成二维码;
2.用户扫码支付,完成后,支付宝调用开发者的配置的回调url;

主要的代码如下:
1.生成qr_code:

[servlet]
@SuppressWarnings("all")
@WebServlet(urlPatterns={"/getQRCode"}, description = "生成预支付交易并返回交易链接(qr_code),扫码支付")
public class GetQRCodeServlet extends BaseReturnJsonServlet<Map<String, Object> > {
    private static final Logger logger = LogManager.getLogger(GetQRCodeServlet.class);

    private static final long serialVersionUID = 1L;

    protected Map  processPost(HttpServletRequest request, HttpServletResponse response) throws ParameterConvertException {
        String attach = ConvertUtil.getNonEmptyStringFromRequestParam(request, "attach");//交易记录中的自定义参数
        String subject = ConvertUtil.getNonEmptyStringFromRequestParam(request, "subject");//订单标题
        String total_fee = ConvertUtil.getNonEmptyStringFromRequestParam(request, "total_fee");//单位为元,小数点后最多为两位
        String order_id = ConvertUtil.getNonEmptyStringFromRequestParam(request, "order_id");//订单id

        Map parametersMap = CommonUtils.createMap("out_trade_no", order_id);//商户订单号,64个字符以内、只能包含字母、数字、下划线
        CommonUtils.generateMap(parametersMap, "total_amount", total_fee);//订单总金额,单位为元,精确到小数点后两位
        CommonUtils.generateMap(parametersMap, "subject", subject);//订单标题
        CommonUtils.generateMap(parametersMap, "timeout_express", ALIPayConstants.TIMEOUT_EXPRESS);
        CommonUtils.generateMap(parametersMap, "body", attach);//商品描述
        Map result = AliPayService.precreate(parametersMap);
        return result;
    }

}

[service]
public static Map precreate(Map data){
        AlipayClient alipayClient = new DefaultAlipayClient(ALIPayConstants.URL, ALIPayConstants.APP_ID, ALIPayConstants.APP_PRIVATE_KEY, ALIPayConstants.FORMAT, ALIPayConstants.CHARSET, ALIPayConstants.APP_PUBLIC_KEY, ALIPayConstants.SIGN_TYPE); //获得初始化的AlipayClient
        AlipayTradePrecreateRequest requestAliPay = new AlipayTradePrecreateRequest();//创建API对应的request类
        requestAliPay.setNotifyUrl(ALIPayConstants.NOTIFY_URL);
        requestAliPay.setBizContent(JSON.toJSONString(data));//设置业务参数
        AlipayTradePrecreateResponse responseAliPay = null;
        try {
            responseAliPay = alipayClient.execute(requestAliPay);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        String result = responseAliPay.getBody();
        return JSON.parseObject(result, Map.class);
    }

2.支付宝的回调
这里一定要注意,最后一定要返回给支付宝success7个字符;
支付宝服务器会不断重发通知,直到超过24小时22分钟。一般情况下,25小时以内完成8次通知(通知的间隔频率一般是:4m,10m,10m,1h,2h,6h,15h);

@SuppressWarnings("all")
@WebServlet(urlPatterns={"/notify"}, description = "支付宝异步回调通知")
public class NotifyServlet extends HttpServlet {
    private static final Logger logger = LogManager.getLogger(NotifyServlet.class);
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response){
        Writer writer = null;
        String result = null;
        Map notifyMap = null;
        //返回结果
        try {
            CommonUtils.getRequestInfo(request, logger);
            notifyMap =  CommonUtils.resquestParameterToMap(request);
            //TODO 进行支付宝验签
            //如果验签失败说明该请求不是来自支付宝,返回404
            if(AlipaySignature.rsaCheckV1(CommonUtils.resquestParameterToMap2(request), ALIPayConstants.APP_PUBLIC_KEY, ALIPayConstants.CHARSET, ALIPayConstants.SIGN_TYPE)){
                logger.trace("签名验证成功");
                if(!"TRADE_SUCCESS".equals(notifyMap.get("trade_status")) && !"TRADE_FINISHED".equals(notifyMap.get("trade_status"))){
                    response.setStatus(404);
                }
            }
            writer = response.getWriter();
            request.setCharacterEncoding("UTF-8");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(notifyMap != null){
                //回掉通知(仅在成功的时候才会调用)
                AliPayService.callback(notifyMap);
                //向数据库插入日志数据
                AliPayService.insertLog(notifyMap);
            }
            //TODO 回调通知
            try {
                writer.write("success");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

代码中还包含有与数据库的交互,这里只是服务端,所以不和订单系集成
代码地址:https://github.com/lichenyigit/java.ali.pay.service

如果觉得文章真心好, 请打赏下我吧http://blog.csdn.net/assassinsshadow/article/details/76086401

你可能感兴趣的:(支付宝,扫码支付)