java支付宝H5支付及回调

一、支付

当前台点击支付按钮,开始调用支付接口

 public void CreatPayOrderForH5(HttpSession session, HttpServletRequest request,  HttpServletResponse response)
{
    AlipayClient alipayClient = new  DefaultAlipayClient(AliPayConfig.URL, AliPayConfig.APPID, AliPayConfig.RSA_PRIVATE_KEY, AliPayConfig.FORMAT, AliPayConfig.input_charset, AliPayConfig.ALIPAY_PUBLIC_KEY, AliPayConfig.sign_type); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        //发起支付宝支付
        //设置支付完成后的返回地址
        alipayRequest.setReturnUrl("www.baidu.com");
        //设置回跳和通知地址
        alipayRequest.setNotifyUrl("www.google.com");
        alipayRequest.setBizContent("{" +
            "    \"out_trade_no\":\""+订单号+"\"," +
            "    \"total_amount\":\""+订单金额+"\"," +
            "    \"subject\":\""+商品标题+"\"," +
            "    \"product_code\":\"QUICK_WAP_PAY\"" +
            "  }");
        String form="";
        try
        {
            // 调用SDK生成表单
            form = alipayClient.pageExecute(alipayRequest).getBody();
        }
        catch (AlipayApiException e)
        {
            form = "err";
            e.printStackTrace();
        } 

        response.setContentType("text/html;charset=" + AliPayConfig.input_charset);
                    //直接将完整的表单html输出到页面
                    response.getWriter().write(form);
                    response.getWriter().flush();
                    response.getWriter().close();
}

    /*说明:
    1、AliPayConfig为常量定义文件,里面包括支付宝支付的一些常用值,如AppId,公钥、私钥等等;
    2、支付接口不需要返回值,直接把支付宝生成的form输出到页面即可,from正确的话前台会开始跳转支付宝
    */

二、回调

支付完成后,支付宝开始调用回调接口

public String AlipayTradePayNotify(Map<String, String[]> requestParams) throws AlipayApiException
    {
        Map<String, String> params = new HashMap<String, String>();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();)
        {
            String name = (String)iter.next();
            String[] values = (String[])requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++)
            {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决,这段代码在出现乱码时使用。
            // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        boolean signVerified = AlipaySignature.rsaCheckV1(params, AliPayConfig.ALIPAY_PUBLIC_KEY, AliPayConfig.input_charset, AliPayConfig.sign_type); 
        // 调用SDK验证签名
        if (signVerified)
        {
            //签名验证成功
           response.getWriter().print("success");
           //处理自己的逻辑
        }
        else
        {
            // 验证失败
            response.getWriter().print("failure");
            // 调试用,写文本函数记录程序运行情况是否正常
            String sWord = AlipaySignature.getSignCheckContentV1(params);
            AliPayConfig.logResult(sWord);
        }
    }

/*
    说明:
    1、回调接口一定要写response.getWriter().print("状态"),否则支付宝收不到返回信息会一直回调该接口
*/

三、问题记录

若前台返回:错误代码 invalid-signature 错误原因: 无效签名,请检查AlipayClient是否被正确初始化

详细文档请参见官方文档:https://docs.open.alipay.com/203/105285/

你可能感兴趣的:(java,H5,支付宝,H5支付)