springboot集成支付宝的支付(easy版)
阅读原文时间:2023年07月08日阅读:1

SpringBoot对接支付宝

  • 需要先注册账号

    到支付宝开发者平台创建网页支付应用

  • 启用公钥模式

    需要使用到appId和下面的两个秘钥


  • 写配置信息的代码

    1.引入依赖

            <dependency>
                <groupId>com.alipay.sdk</groupId>
                <artifactId>alipay-easysdk</artifactId>
                <version>2.2.0</version>
            </dependency>

    2.写配置

    alipay:
      appId: appid
      appPrivateKey: 应用私钥
      alipayPublicKey: 支付宝的公钥
      notifyUrl: 回调地址

    3.新建一个alipayConfig类

    import com.alipay.easysdk.factory.Factory;
    import com.alipay.easysdk.kernel.Config;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    import javax.annotation.PostConstruct;
    
    @Data
    @Component
    //拿配置文件里面alipay开头的配置信息
    @ConfigurationProperties(prefix = "alipay")
    public class AliPayConfig {
        private String appId;
        private String appPrivateKey;
        private String alipayPublicKey;
        private String notifyUrl;
    @PostConstruct
    public void init() {
        // 设置参数(全局只需设置一次)
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipaydev.com";
        config.signType = "RSA2";
        config.appId = this.appId;
        config.merchantPrivateKey = this.appPrivateKey;
        config.alipayPublicKey = this.alipayPublicKey;
        config.notifyUrl = this.notifyUrl;
        Factory.setOptions(config);
        System.out.println("=======支付宝SDK初始化成功=======");
    }
    }

    4.写接口

        @GetMapping("/pay")
        public String pay(AliPay aliPay) {
            AlipayTradePagePayResponse response;
            try {
                //  发起API调用(以创建当面付收款二维码为例)
                response = Factory.Payment.Page()
                        .pay(URLEncoder.encode(aliPay.getSubject(), "UTF-8"), aliPay.getTraceNo(),String.valueOf(aliPay.getTotalAmount()), "");
            } catch (Exception e) {
                System.err.println("调用遭遇异常,原因:" + e.getMessage());
                throw new RuntimeException(e.getMessage(), e);
            }
            return response.getBody();
        }
    
    //如果设置了拦截器 需要放行该接口

    5.设置回调接口

     @PostMapping("/notify")  // 注意这里必须是POST接口
        public String payNotify(HttpServletRequest request) throws Exception {
            if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
                System.out.println("=========支付宝异步回调========");
            Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
            Map&lt;String, String[]&gt; requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
                // System.out.println(name + " = " + request.getParameter(name));
            }
            String tradeNo = params.get("out_trade_no");
            String gmtPayment = params.get("gmt_payment");
            String alipayTradeNo = params.get("trade_no");
            // 支付宝验签
            if (Factory.Payment.Common().verifyNotify(params)) {
                // 验签通过
                System.out.println("交易名称: " + params.get("subject"));
                System.out.println("交易状态: " + params.get("trade_status"));
                System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
                System.out.println("商户订单号: " + params.get("out_trade_no"));
                System.out.println("交易金额: " + params.get("total_amount"));
                System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
                System.out.println("买家付款时间: " + params.get("gmt_payment"));
                System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
    
                // 更新订单未已支付
                //ordersMapper.updateState(tradeNo, "已支付", gmtPayment, alipayTradeNo);
            }
        }
        return "success";
    }</code></pre></li>

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器