Home  >  Article  >  Java  >  How to implement Java docking with Alipay payment project

How to implement Java docking with Alipay payment project

WBOY
WBOYforward
2023-04-19 21:52:051546browse

java docking Alipay payment demonstration

Now there are many projects that require docking payment. Here we mainly explain the docking Alipay H5 payment

Nonsense Not much to say about the code

Introducing Alipay’s official sdk

 <!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk -->
        <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-easysdk</artifactId>
            <version>2.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
        <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>4.10.124.ALL</version>
        </dependency>

Writing a method to call payment

 @RequestMapping("/test")
    public void doPost(HttpServletRequest httpRequest,
                       HttpServletResponse httpResponse) throws ServletException, IOException {

        //商户号
        String APP_ID="";
        //私钥
        String APP_PRIVATE_KEY="";
        //编码
        String CHARSET="UTF-8";
        //公钥
        String ALIPAY_PUBLIC_KEY="";
        //我是沙箱环境 所有请求地址是沙箱版
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
        //这里主要是设置回调 也就是支付成功之后你要做什么操作的地址
        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
        alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        /**
         * @description:
         * @author: hwk
         * @date: 2022/6/14 16:34
         * @param: out_trade_no 订单编号
         * total_amount 付款金额
         * subject 商品名称
         * product_code 支付方式 如手机网站 pc网站 APP支付等
         **/
       JSONObject jsonObject = new JSONObject();
        jsonObject.put("out_trade_no", "264484811515156");
        jsonObject.put("total_amount", "66");
        jsonObject.put("subject", "测试支付");
        jsonObject.put("product_code", "QUICK_WAP_WAY");
        alipayRequest.setBizContent(jsonObject.toJSONString());
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //因为我不会写前端 所以直接输出测试 如果在正式环境中 建议直接return 返回的from表单
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
    //支付回调
    @RequestMapping("/test1")
    public void aliPayNotify() {
            Map<String, String> params = getAllRequestParam(request);
            String tradeStatus = params.get("trade_status");
            if ("TRADE_SUCCESS".equals(tradeStatus)) {
                System.out.println("支付成功");
            }else {
                System.out.println("支付失败");
            }
    }


    private Map<String, String> getAllRequestParam(final HttpServletRequest request) {
        Map<String, String> res = new HashMap<>(8);
        Enumeration<?> temp = request.getParameterNames();
        while (temp.hasMoreElements()) {
            String en = (String) temp.nextElement();
            String value = request.getParameter(en);
            res.put(en, value);
        }
        return res;
    }

Enter the formal participation When paying, you can use string json to receive it. Here is just a demonstration

Successful demonstration picture

How to implement Java docking with Alipay payment project

How to implement Java docking with Alipay payment project

How to implement Java docking with Alipay payment project

Explain the parameters required for actual payment

Except the appid public key and private key

You are normal The parameters required for payment are nothing more than the id of the product (mainly used to query the name and price of the product based on the product id and other subsequent calls for payment), the purchaser
and then query the details of the product and set them to the corresponding parameters. Call Alipay sdk to generate the from form

You can perform some operations after successful payment in the callback

For example: the purchase is a membership Then after the payment is successful, go to open or renew the membership for him

One thing to remind you that the order created before initiating payment must be unpaid. In the callback, determine whether the user is successful

If a user actively checks whether the payment is successful, it is recommended to first go to the database to check whether the payment is successful according to the order number. If it is not equal to go to the official interface to check whether the payment is successful. The advantage of this is Sometimes the callback is a little slow and the user takes the initiative to query. If the payment is successful, it will not cause the return of payment failure

The above is the detailed content of How to implement Java docking with Alipay payment project. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete