SpringBoot实现支付宝支付功能【沙箱】

1、引入所需依赖

        
        
            com.alipay.sdk
            alipay-sdk-java
            4.22.113.ALL
        

2、登录支付宝开放平台,进入个人沙箱环境,这些信息需要填写在下面的配置类中。

SpringBoot实现支付宝支付功能【沙箱】_第1张图片

 3、查看密钥信息

SpringBoot实现支付宝支付功能【沙箱】_第2张图片

 4、编写一个工具类,相关信息在个人沙箱环境里面都有,自行修改(相关密钥可以点击系统默认密钥进行查看)

public class AlipayConfig {
    /**
     * 支付宝网关地址
     */
    public static String serverUrl = "https://openapi.alipaydev.com/gateway.do";
    /**
     * appId
     */
    public static String appId = "2021000119697439";
    /**
     * 应用私钥 后期替换成自己的私钥
     */
    public static String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC3PgI3lAW40dRwevBzwxdUkKGhjVIX9K4zuUmCeF5PhWo11LGTmhjrTpBEq/BiBa/v1yZl+3/3/eU14aGxwE86qMnUEiweNVIclp9Gh2o85RiB2EGXyAi5wvTdwziZ50P2s/5VFzyhjjU/Pj6XKg+SShc1gyFsEfttGnMsvP3tm44XsxtRFvTZBqGV90sZ61H9DzRVxHf4dfhnRLJvRFVYC+2XdGFwP+1+UOhCdjOJla6wkrz9B0kvXB1XdAdJHuXTvisTRgBDnc8lY2tyA9RIfAceqHFHub1x0Q0V2HDgYcd2EllGCDP/MLeLAKbGC4H+bh6w57h3NkhbhA/ER8adAgMBAAECggEAGpWgdLfA7MNkcr0jTWgsZlRlXq7GDjutWkuzYn5cEGVmt1CYQrXEj77iJ1Ral54DnGQ2ae1G74+iRWLq+4qpXV8Vj3xk4Ujo6q6Styr1l0DsozyeJ3UlWVcNSybGMXCDoYyxey7KgtZg9kVf5PVi0JypqKgjNfz0YLU6kdIUouYbmz7HPBRQSV3mLclAUiJmbiVa7g6luV/PyNJFh3c1PITvsXah6gvOmOuARn3ZadHvS546AbLYmerVI0c3/Y7HOW5XyLnR+EzzqA+PXtXAV/RV67ywhb9KSEJBevsbPe83ZQkzkoXhKZFNhQb5QdmDXmNA6cRZ1hW6ohuhcQKBgQDlwzuAA3MHpQ3MLfyuUUD1o2fvQ9hymURm4BGzHrPlSz+3yWIuNyUKCfbtslqdGeHPXTlOaKtBiS2wrVbv/JaNA/Elzl+BZgkNCcb3tnsha7e522syHdgapdPrXyGSDN69mIZsWoRJQlGpRISJnd75XClZf3nGlr4g4hV9+dS3rwKBgQDMKtJUSxlb8rf9NZ/xOmklcLxBgFGo39noVS+FDz5O1P1o3I1LSHF+vOTkkqLTO4TrEtr2q0Ijb/xj65wX42i0Av2t63fbMEAkYTatih8Pve9LtX1G6nv2Fw9U/ZDOUKRdhPO2GDG59SifXEgDI4eV29AZhh4oqwRH7wz5neitcwKBgQCg2SR2YfPNjkcygZ87ibaoQFUqs5x73cB9JLA/bWcTRrK8v0b0g7ZyaLdtgEbfZJcozGKp+ayqnJZX8YHDUFk1L2VKykciF4SOlCGumRlqNDbS7kEFPM9lGGJbjKW70Ta+FGFGybeCceMpVMZUcSj/+P0Zopxl4nIIIz5YiuXYEQKBgDmC8n6WLQ2OoK9uFKDiIE5QpGcbG2WjsQFWRa2IrreMHdoVDqQfQwE9eoi8matA/98sD4wej+HloMYQz4aXI55ZnVYJl1iiDd9xlvJpjvhjO24rMJ/yW3rl4gbB6rGDW9mQIwqvZAqK+adyWkKwFnhrVAJsmkBAdFNDDneeOLXrAoGBAOEsOJ76yaxY3RGrvWnGm7aA/tyVLNtpz3Hj0T/zTN24bsKm5TwuobVq5jVvQ2UagBQRlGAl3mP6XiVv0NclDPRupSCOjhkbLlMw+B6rE5uV7HkxtrtHsSFaruehRI94K6tbGNQJdImqWjHKBIjqoHr8snLk41vXxxwyK2G38puD";
    /**
     * 发送数据的格式 目前只能为json
     */
    public static String format = "json";
    /**
     * 设置字符集编码 目前只能为utf-8
     */
    public static String charset = "utf-8";
    /**
     * 支付宝公钥 后期替换成自己的支付宝公钥
     */
    public static String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh2yYUlCpO6tNqNa+svqMJfnl1Ax9LrOAf8bNxexVPIJh6LF77ca4qVPpuYTbvQNuGEMDSP35uZ3g+Vch3WasIaYy/D36NfOVjPzrB2b3cXKbUSEjgRawnx15oyBj687tOKEJcPO9okGUgYr2/HkVQPVU0IpFZVRfcRk8BbadS7AOz38RtRYteY1bu+YVFWqf0O0WTILHAZeDpLqENhgHjmj1ATKdCPel0K2US/uCSe6rPJgclFHplEBKI9ysfTKfhEKNhj56w6CrG7XQ1MQHZI1V7rR2busYRntoHh/DB6CZSiv/X5L8+TxZvAtUCn+z6Ns+8e3LfG1/bq59OU0wIDAQAB";
    /**
     * 支付宝签名 目前是 RSA2
     */
    public static String signType = "RSA2";
    public static AlipayClient getAlipayClient() {
        // 获得初始化的AlipayClient
        return new DefaultAlipayClient(AlipayConfig.serverUrl, AlipayConfig.appId,
                AlipayConfig.privateKey, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.publicKey,
                AlipayConfig.signType);
    }
}

4、编写controller,模拟请求接口

package com.lili.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lili.util.AlipayConfig;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author QiJingJing
 * @since 2022/12/31
 */
@RestController
@RequestMapping("alipay")
public class PayController {
    /**
     * 支付请求接口
     */
    @RequestMapping("pay/{orderId}/{cost}") // 订单id(不能重复)和消费金额
    public void pay(@PathVariable String orderId,@PathVariable String cost, HttpServletResponse response){
        try {
            AlipayClient alipayClient = AlipayConfig.getAlipayClient();
            // 设置请求参数
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            AlipayTradePayModel model = new AlipayTradePayModel();
            // 设定订单号 必须要写,且订单号不能重复,目前已经只是做测试,已经写死
            model.setOutTradeNo(orderId);
            // 设置订单金额
            model.setTotalAmount(cost);
            // 订单名字
            model.setSubject("书籍订单");
            // 订单描述
            model.setBody("这是一本书籍的消费订单");
            // 产品码
            model.setProductCode("FAST_INSTANT_TRADE_PAY");
            // 设置参数
            alipayRequest.setBizModel(model);
            // 设置回调地址
            String url = "http://localhost:8080/alipay/success?orderId="+orderId+"";
            System.out.println(url);
            alipayRequest.setReturnUrl(url);
            String result = alipayClient.pageExecute(alipayRequest).getBody();
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 支付成功后,回调页面
     */
    @RequestMapping("success")
    public void success(@RequestParam("orderId") String orderId, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("订单Id为"+orderId+"的订单已经成功支付");
    }
}

5、浏览器访问http://localhost:8080/alipay/pay/23232/509

订单号为23232,金额为509元,回车后可以看到出现以下页面

SpringBoot实现支付宝支付功能【沙箱】_第3张图片

点击订单详情可以看到我们订单的信息:

 SpringBoot实现支付宝支付功能【沙箱】_第4张图片

 6、我们需要登录买家账号进行登录即可

SpringBoot实现支付宝支付功能【沙箱】_第5张图片

 7、登录完成后,会出现以下页面(余额不够的话,可以在上面页面进行充值即可)

SpringBoot实现支付宝支付功能【沙箱】_第6张图片

 8、支付完成后,即可成功跳入我们设置的回调地址

SpringBoot实现支付宝支付功能【沙箱】_第7张图片

 d409e734577f41c28ddab2023d194a1b.png

你可能感兴趣的:(java,数据库,mysql)