Java实现 支付宝的 app吊起支付和 网页扫码

2022-04-01 08:09:46 浏览数 (1)

引入相关依赖

代码语言:javascript复制
 <dependency>
            <groupId>com.alipay</groupId>
            <artifactId>sdk.java</artifactId>
            <version>3.3.4</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/alipay-sdk-java-3.3.4.ALL.jar</systemPath>
 </dependency>
代码语言:javascript复制
@Controller
@RequestMapping(value = "ali")
public class Alipay {

    @RequestMapping("alipay")
    public  String aliPay() {
        return "WEB/alipay";

    }

    @RequestMapping("alipay1")
    @ResponseBody
    public JSONObject pay1(ServletRequest request, ServletResponse response) throws AlipayApiException, IOException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号,商户网站订单系统中唯一订单号,必填
        //String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        String out_trade_no = request.getParameter("WIDout_trade_no");
        //付款金额,必填
        //String total_amount = new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8");
        String total_amount = request.getParameter("WIDtotal_amount");
        //订单名称,必填
        //String subject = new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8");
        String subject = request.getParameter("WIDsubject");
        //商品描述,可空
        //String body = new String(request.getParameter("WIDbody").getBytes("ISO-8859-1"),"UTF-8");
        String body = request.getParameter("WIDbody");
        alipayRequest.setBizContent("{"out_trade_no":""  out_trade_no  "","
                  ""total_amount":""  total_amount  "","
                  ""subject":""  subject  "","
                  ""body":""  body  "","
                  ""product_code":"FAST_INSTANT_TRADE_PAY"}");

        //若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{"out_trade_no":""  out_trade_no  "","
        //        ""total_amount":""  total_amount  "","
        //        ""subject":""  subject  "","
        //        ""body":""  body  "","
        //        ""timeout_express":"10m","
        //        ""product_code":"FAST_INSTANT_TRADE_PAY"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
//        PrintWriter out = response.getWriter();
//        //输出
//        out.println(result);
//        out.flush();
//        out.close();
        JSONObject  model = new JSONObject();
        model.put("remark", result);
        return model;
    }



    /***
     * 回调函数
     * @return
     */
    @RequestMapping("getUrl")
    @ResponseBody
    public String   getUrl(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        return null;
    }
代码语言:javascript复制
public class AlipayConfig {
    //↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    // 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
    public static String app_id = "2018080960988601";

    // 商户私钥,您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "";

    // 【支付宝公钥】【不是应用公匙】,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "******************";

    // 服务器异步通知页面路径  需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    public static String notify_url = "http://alipay.lyytxx.com/***************";

    // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    public static String return_url = "http://alipay.lyytxx.com/*************";

    // 签名方式
    public static String sign_type = "RSA2";

    // 字符编码格式
    public static String charset = "utf-8";

    // 支付宝网关
    public static String gatewayUrl = "https://openapi.alipay.com/gateway.do";

    // 支付宝网关
    public static String log_path = "C:\";


//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

    /**
     * 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
     * @param sWord 要写入日志里的文本内容
     */
    public static void logResult(String sWord) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(log_path   "alipay_log_"   System.currentTimeMillis() ".txt");
            writer.write(sWord);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

@WebFilter( urlPatterns = {"*"},asyncSupported = false )
public class GloblaFilter {
    public void destroy() {
    }
    public void doFilter(ServletRequest request, ServletResponse response, javax.servlet.FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }
    public void init(FilterConfig filterConfig) throws ServletException {
    }
}

0 人点赞