Java手把手教你完成支付宝H5支付

1,阿里的H5支付相对来微信来说很简单,第一步我们需要获取以下几个参数

    // 商户appid-----h5支付的ID
    public static String        APPID             = "APPID";
    // 私钥 pkcs8格式的
    public static String        RSA_PRIVATE_KEY   =""
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    public static String        notify_url        = "https://此处域名/faint-service/f/ali/alipayRechargeGoldNotify";
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问       商户可以自定义同步跳转地址
    public static String        return_url        = "https://此处域名/faint-service/static/h5/app/successh5.html";
    // 请求网关地址
    public static String        URL               = "https://openapi.alipay.com/gateway.do";
    // 编码
    public static String        CHARSET           = "UTF-8";
    // 返回格式
    public static String        FORMAT            = "json";
    // 支付宝公钥
    public static String        ALIPAY_PUBLIC_KEY = ""
    // RSA2
    public static String        SIGNTYPE          = "RSA2";

APPID:怎样申请支付宝支付的整个流程我就不一一细说了,APPID在你申请APP支付的时候就可以得到此数据

RSA_PRIVATE_KEY 和ALIPAY_PUBLIC_KEY   :公钥私钥也是你申请APP支付会设置的数据

notify_url        :支付宝支付成功回调到服务器的地址,获得成功数据

return_url        如果是H5支付的话,需要填写,APP支付不需要,支付成功返回的页面.

URL              :此URL是把数据打包到支付宝  唤醒支付宝支付的地址

CHARSET            FORMAT            SIGNTYPE          :这三个是固定死的,填就完事

2,开始写代码了,前端请求支付,后端处理数据,打包到支付宝服务器

@RequestMapping("aliPayRechargeGold")
    public void CreatPayOrderForH5(HttpServletRequest request, HttpServletResponse response, Integer rechargeNumber, String rechargeId) {
        try {
            //如果是模拟请求则返回null
            boolean bool = rechargeList.contains(rechargeId);
            
                //保存充值记录到数据库
                int aisle = 2; //支付宝充值
                AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.URL, AliPayConfig.APPID, AliPayConfig.RSA_PRIVATE_KEY, "json", "UTF-8", AliPayConfig.ALIPAY_PUBLIC_KEY, "RSA2");
                AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
                JSONObject order = new JSONObject();
                Integer money = mdRecharge.getRmb();//获取金额,单位元
                String reaMoney = money.toString() + ".00";
                order.put("out_trade_no", mdRecharge.getNumber()); //订单号
                order.put("subject", ""); //商品标题
                order.put("product_code", "QUICK_WAP_WAY");
                order.put("body", "");//商品名称
                order.put("total_amount", reaMoney); //金额
                alipayRequest.setBizContent(order.toString());
                //在公共参数中设置回跳和通知地址
                alipayRequest.setNotifyUrl(AliPayConfig.notify_url);
                alipayRequest.setReturnUrl(AliPayConfig.return_url);
                String form = alipayClient.pageExecute(alipayRequest).getBody();
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write(form);
                response.getWriter().flush();
                response.getWriter().close();
            
        } catch (AlipayApiException e) {
            logger.error("CreatPayOrderForH5", e);
        } catch (IOException e) {
            logger.error("CreatPayOrderForH5", e);
        }
    }

3,服务器收到支付宝支付成功的返回值

@ResponseBody
    @RequestMapping(value = "alipayRechargeGoldNotify", method = RequestMethod.POST)
    public String getNotify(HttpServletRequest request, HttpServletResponse response) {
        logger.info("AliController.getNotify=======start");
        Map requestParams = request.getParameterMap();
        //获取支付宝POST过来反馈信息
        Map params = new HashMap();
        for (Iterator 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);
            }
        }
        ///////////////////处理自己的逻辑//////////////////////
        try {
            boolean flag = AlipaySignature.rsaCheckV1(params, AliPayConfig.ALIPAY_PUBLIC_KEY, "UTF-8", "RSA2");
            if (flag) {
                if ("TRADE_SUCCESS".equals(params.get("trade_status"))) {
                    String orderSn = params.get("out_trade_no"); //商家订单号
                    String openid = params.get("buyer_id"); //买手ID
                    String moneyNumber = params.get("total_amount"); //实际支付金额
                    BigDecimal amountPay = new BigDecimal(moneyNumber);//将分转化为元的对象
                    //写自己的业务逻辑
                    logger.info("mdRechargeService.rechargeGoldTwo业务操作成功");
                }
                //签名验证成功
                response.getWriter().print("success");
                logger.info("flag签名验证成功");
            } else {
                // 验证失败
                response.getWriter().print("failure");
                logger.info("flag签名验证失败");
            }
        } catch (Exception e) {
            logger.info("flag签名获取订单信息异常");
        }
        logger.info("AliController.getNotify=======end");
        return null;
    }

 

4,关于前端H5页面的代码




   
    充值
   
   
   
   
   
   
 
   
   
   


   


       
       

       
       
       

           
           

微信支付


       

   

   
   


到这里呢,是不是觉得支付宝所有的支付都很简单

你可能感兴趣的:(Java手把手教你完成支付宝H5支付)