欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

支付宝支付Java

程序员文章站 2022-07-13 16:54:08
...
@PostMapping("pay")
@ResponseBody
public Result pay(String taskName, String orderNum, Double amount, Integer payType, HttpServletRequest request) throws IOException, AlipayApiException {
    if (1 == payType) {
        String sign = getAlipaySign(taskName, orderNum, amount);//支付宝支付
        return ResultUtil.success(sign);
    } else {
        Map<String, String> resultMap = getWxPay(orderNum, amount, request);
        return ResultUtil.success(resultMap);
    }
}
  /**
     * 获取支付宝签名串
     *
     * @return
     * @throws IOException
     * @throws AlipayApiException
     */
    public String getAlipaySign(String title, String out_trade_no, Double total_amount) throws IOException, AlipayApiException {
        //实例化客户端
        AlipayClient alipayClient = new
                DefaultAlipayClient(AlipayConfig.URL,
                AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGN_TYPE);
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody("支付描述");//获取支付类型名称
        model.setSubject("支付标题");//支付标题
        model.setOutTradeNo(out_trade_no);//支付订单号
        model.setTotalAmount(total_amount.toString());
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl(AlipayConfig.notifyUrl);
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        return response.getBody();
    }

支付回掉方法

  /**
     * 支付成功回调
     *
     * @return
     * @throws IOException
     * @throws AlipayApiException
     */
    @PostMapping("reciveNotify")
    @ResponseBody
    public String reciveNotify(HttpServletRequest request) throws IOException, AlipayApiException {
        System.out.println("支付回调----------------------->");
        //获取支付宝POST过来反馈信息
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        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);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("orderNum", params.get("out_trade_no"));
        Order order = orderService.getOne(queryWrapper);
        if (order != null) {
            System.out.println("支付回调----------------------->"+order.toString());
            boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGN_TYPE); //调用SDK验证签名
            System.out.println("支付回调----------------------->"+signVerified);
            if (signVerified) {
                //更新订单信息
                order.setAlipayNum(params.get("out_trade_no"));
                order.setYwid(params.get("out_biz_no"));
                order.setState(2);
                orderService.updateById(order);
               
                return "success";
            }
        }
        return "error";
    }