java实现支付宝h5手机支付

早过忘川 提交于 2019-12-06 11:26:38

一.首先去支付宝申请好应用,以及开通手机快捷支付。

二.支付宝开放平台的API文档

https://docs.open.alipay.com/203/107090/

在application.yml封装属性

@Value("${app.ali-pay-appId}")
private String APP_ID;
/**
 * 应用私钥
 */
@Value("${app.ali-pay-merchantPrivateSign}")
private String APP_PRIVATE_KEY;
/**
 * 支付宝公钥
 */
@Value("${app.ali-pay-aliPublicSign}")
private String ALIPAY_PUBLIC_KEY;
/**
 * 回调地址
 */
@Value("${app.ali-pay-notifyUrl}")
private String notify_url;
/**
 * 签名类型
 */
@Value("${app.ali-pay-signType}")
private String SIGN_TYPE;
/**
 * 编码集
 */
@Value("${app.ali-pay-charset}")
private String CHARSET;
/**
 * 支付宝网关
 */
@Value("${app.ali-pay-payGateWay}")
private String URL;

@Value("${app.order-pay-switch}")
private String flag;

 统一下单接口

//实例化阿里支付
            AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
//创建api对应的request
            AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();

            alipayRequest.setNotifyUrl(notify_url);
            //订单号,后续修改为正式的
            String outTradeNo = "" + System.currentTimeMillis();

            Map<String, Object> maps = new HashMap<>();
            maps.put("out_trade_no", outTradeNo);
          
            maps.put("total_amount", "0.01");
          
            //产品名后续修改
            maps.put("subject", "测试")
            maps.put("timeout_express", "3m");
            String postData = JSONObject.toJSONString(maps);
            alipayRequest.setBizContent(postData);

            logger.info("requestId:{},function:{},request:{}",requestId,"支付宝支付请求参数",postData);
            String form = alipayClient.pageExecute(alipayRequest).getBody();
            logger.info("requestId:{},function:{},response:{}" , requestId , "aliH5Pay" , form);
            logger.info("requestId:{},userId:{},function:{}",requestId,userIds,"发起支付宝支付请求");

 

//支付宝回调
    @PostMapping(value = "/ali")
    @SkipUserAuth
    public String notify(HttpServletRequest request){
        Map requestParams = request.getParameterMap();
        String q = "";
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            for (int i = 0; i < values.length; i++) {
                q+=name+"="+values[i]+"&";
            }
        }

        System.out.println("支付宝支付结果通知"+requestParams.toString());
        logger.info("requestId:{},function:{},response:{}",requestId,"支付宝支付结果通知",q);
        //获取支付宝POST过来反馈信息
        Map<String,String> params = new HashMap<String,String>();

        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);
        }
        logger.info("requestId:{},function:{},response:{}", requestId , "支付宝回调" , params.toString());

        //boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
        try {
            //验证签名
            boolean flag = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET,SIGN_TYPE);
            logger.info("requestId:{},function:{},response:{}", requestId, "支付宝回调验证签名", flag);
            if(flag){
                if("TRADE_SUCCESS".equals(params.get("trade_status"))){

                    //商户订单号
                    String out_trade_no = params.get("out_trade_no");
                    logger.info("打印支付宝回调数据" + out_trade_no);
                    //下面省略修改订单逻辑
                }
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            logger.info("requestId:{} err",request ,e);
            logger.error("requestId:{} err",request ,e);
        }
        return "success";
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!