支付宝支付——当面付功能实现

功能展示

在这里插入图片描述
选择支付宝支付会在页面弹出由支付宝当面付接口提供的二维码,该二维码是支付宝API返回的二维码链接,前端接收转换为图片。

需要的参数

alipay.alipayPublicKey= //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥
alipay.gatewayHost= //支付宝网关
alipay.appId= //应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
alipay.appPrivateKey= //商户私钥,您的PKCS8格式RSA2私钥
alipay.notifyUrl= //服务器异步通知页面路径  需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问。注意:每次重新启动ngrok,都需要根据实际情况修改这个配置

Contoller层

@Autowired
    private AliPayService aliPayService;
    @Autowired
    private GdsRealoutService gdsRealoutService;

    @RequestMapping("/getAliPayQrPay")
    @ResponseBody
    public DataResult getAliPayQrPay(String subject,String outTradeNo,String totalAmount) {
        return aliPayService.getAliPayQrPay(subject,outTradeNo,totalAmount);
    }
    @RequestMapping(value = "/alipayNotifyUrl",method = RequestMethod.POST)
    @ResponseBody
    public String alipayNotifyUrl(HttpServletRequest request){
        if (aliPayService.notifyUrl(request).equals("success")){
            GdsRealout gdsRealout = gdsRealoutService.selectByPrimaryKey(request.getParameter("out_trade_no").substring(0, request.getParameter("out_trade_no").length() - 13));
            String userName = gdsRealout.getUpdatename();
            WebSocketServerEndpoint ws = endpointMap.get(userName);
            if (ws == null) {
                System.out.println("not connected to " + userName);
                return "fail to connect";
            }
            // 支付状态:0为默认值 未支付,1为已支付
            gdsRealout.setTrade_status(1);
            gdsRealoutService.updateByPrimaryKey(gdsRealout);
            ws.sendMsg("支付成功",userName);
        }
        return "success";
    }

ServiceImpl层

@Autowired
    private GdsRealoutService gdsRealoutService;

    @Override
    public DataResult getAliPayQrPay(String subject, String outTradeNo, String totalAmount) {
        // 1. 设置参数(全局只需设置一次) 设置支付参数
        Factory.setOptions(getOptions());

        try {
            // 2. 发起API调用(以创建当面付收款二维码为例)
            AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace()
                    .preCreate(subject, outTradeNo+ new Date().getTime(), totalAmount);
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                return new DataResult(0,"调用成功",response);
            } else {
                return new DataResult(1,"调用失败",response.msg);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public String notifyUrl(HttpServletRequest request) {
        Map<String,String> map= new HashMap();
        Enumeration<String> parameterNames = request.getParameterNames();
        while(parameterNames.hasMoreElements()){
            String key = parameterNames.nextElement();
            String value = request.getParameter(key);
            map.put(key, value);
        }
        //验签
        try {
            if(Factory.Payment.Common().verifyNotify(map)){
                System.out.println("异步通知验签通过");
                //验证用户的支付结果
                String trade_status = request.getParameter("trade_status");
                if(trade_status.equals("TRADE_SUCCESS")){
                    System.out.println("支付交易成功");
                    //这里根据业务更新订单支付状态.....
                    //数据库中 发货单 添加remark: out_trade_no:success;
                    String outTradeNo = map.get("out_trade_no");
                    GdsRealout gdsRealout = gdsRealoutService.selectByPrimaryKey(outTradeNo.substring(0,outTradeNo.length()-13));
                    gdsRealout.setTrade_status(1);
                    gdsRealoutService.updateByPrimaryKey(gdsRealout);
                }

            }else {
                System.out.println("异步通知验签失败");
                return "fail";
            }
        } catch (Exception e) {
            System.err.println("异步发生异常{}"+e.getMessage());
            return "fail";
        }
        return "success";
    }


    public static Config getOptions() {
        Resource resource = new ClassPathResource("/config/DEV/properties/hzsr_payment.properties");
        Properties props = null;
        try {
            props = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = props.getProperty("alipay.gatewayHost");
        config.signType = "RSA2";
        config.appId = props.getProperty("alipay.appId");
        // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = props.getProperty("alipay.appPrivateKey");
        config.alipayPublicKey = props.getProperty("alipay.alipayPublicKey");
        //可设置异步通知接收服务地址(可选)
         config.notifyUrl = props.getProperty("alipay.notifyUrl");
        //可设置AES密钥,调用AES加解密相关接口时需要(可选)
        config.encryptKey = "wvGnt8bUcfVuIiiLygqwGA==";
        return config;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xj-_-xj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值