SpringBoot整合支付宝沙箱完成支付功能

1 支付宝开发平台

1.1 百度搜索“支付宝开发平台”

1.2 访问“支付宝开发平台”

1.3 进行注册

1.4 注册完毕之后,支付宝扫描登录

1.4.1 登录之后,选择“控制台”

1.4.2 进入“控制台”之后,下来,找到“沙箱”,点击即可

1.5 沙箱应用,里面会有我们需要的内容

1.5.1 沙箱应用

1.5.2 沙箱账号

2 SpringBoot 整合 沙箱

2.1 创建SpringBoot项目

2.2 添加 支付宝SDK 依赖

        <!--支付宝SDK-->
        <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>4.22.110.ALL</version>
        </dependency>


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

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.24</version>
        </dependency>

2.3 application.yml 配置内容

alipay:
  #APPID
  appId: XXXXXX
  #应用私钥
  appPrivateKey: XXXXXX
  #支付宝公钥
  alipayPublicKey: XXXXXX
  #回调接口 (使用内网穿透:https://natapp.cn/)
  # notifyUrl 内容为:回调地址+controller中自定义的回调请求
  notifyUrl:  http://bxbunc.natappfree.cc/alipay/notify
server:
  port: 8888

2.3.1 appId、appPrivateKey、alipayPublicKey 都是从 “支付宝开放平台”=》控制台,查找的

2.3.2 notifyUrl 回调接口设置

2.3.2.1 当前项目的地址为本地地址,无法实现支付宝沙箱的调用,因此使用内网穿透(如:https://natapp.cn)将当前项目的本地地址转为网络的地址,这样支付宝沙箱就能对本地项目进行回调 

        备注:内网穿透具体使用,查看“4 补充:Natapp 使用”

2.4 自定义配置类(与yml文件结合)

@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class MyAliPayConfig {
    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初始化成功=======");
    }

}

2.5 支付类

package com.laoma.springbootalypay.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.easysdk.factory.Factory;
import com.laoma.springbootalypay.config.MyAliPayConfig;
import com.laoma.springbootalypay.pojo.AliPay;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/alipay")
public class AliPayController {
    //-支付宝网关地址
    private static final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private static final String FORMAT_JSON = "JSON";
    private static final String CHARSET_UTF8 = "UTF-8";
    private static final String SIGN_TYPE_RSA2 = "RSA2";
    @Autowired
    private MyAliPayConfig myAliPayConfig;

    @GetMapping("/pay")
    public void pay(AliPay aliPay, HttpServletResponse httpResponse) throws Exception{
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, myAliPayConfig.getAppId(),
                myAliPayConfig.getAppPrivateKey(), FORMAT_JSON, CHARSET_UTF8, myAliPayConfig.getAlipayPublicKey(), SIGN_TYPE_RSA2);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl(myAliPayConfig.getNotifyUrl());
        //-商品的内容可以从系统中传递过来,目前这里是手写的
        aliPay.setTraceNo(UUID.randomUUID().toString().replaceAll("-",""));
        aliPay.setTotalAmount("100");
        aliPay.setSubject("笔记本华硕");
        //=====
        request.setBizContent("{\"out_trade_no\":\"" + aliPay.getTraceNo() + "\","
                + "\"total_amount\":\"" + aliPay.getTotalAmount() + "\","
                + "\"subject\":\"" + aliPay.getSubject() + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET_UTF8);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
    @PostMapping("/notify")  // 注意这里必须是POST接口
    public String payNotify(HttpServletRequest request) throws Exception {
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
            System.out.println("=========支付宝异步回调========");

            Map<String, String> params = new HashMap<>();
            Map<String, String[]> 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";
    }
}

3 项目测试

注意:一定先启动 natapp,在启动 Springboot项目,因为启动natapp之后,才能获取到natapp生成的网络地址,然后将地址复制到项目的yml文件中

3.1 启动 Natapp

3.1.1 命令启动

在natapp的跟目录下执行:natapp -authtoken=xxxx

将生成的Forwarding后面的地址,复制到 springboot项目的yml文件中

3.2 启动springboot项目

3.3 访问 http://localhost:8888/alipay/pay 即可,效果如下:

 输入沙箱提供的买家账号与密码,在输入支付密码

支付成功之后的效果

通过回调函数的调用,在控制台输出的支付信息,如下

4 补充:Natapp 使用

4.1 访问 Natapp 官网

4.2 注册/登录 并 下载客户端

4.3 登录之后首页面

4.4 选择购买隧道=》免费隧道

可以创建自己的免费隧道

4.5 我的隧道进行配置,配置结束即可

配置本地项目的路径

4.6 下载的客户端配置(window版)

进入 natapp根目录执行:natapp -authtoken=xxxx

注意:authtoken 是 natapp中生成的,如 4.5 中显示的 authtoken中的内容

红色框标注的内容为springboot项目中yml设置的notifyUrl中回调地址。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴能为您提供这篇文章,下面是springboot整合支付宝沙箱支付的案例。 1. 首先,您需要注册一个支付宝账号,并申请沙箱环境的权限。 2. 在支付宝开放平台申请应用,并获取应用的AppID、应用私钥和支付宝公钥。 3. 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>3.3.0.ALL</version> </dependency> ``` 4. 创建一个AliPayConfig类,用于配置支付宝相关参数。 ``` @Configuration public class AliPayConfig { //支付宝网关 private String gatewayUrl; //商户应用id private String appid; //RSA私钥 private String privateKey; //字符编码格式 private String charset; //支付宝公钥 private String alipayPublicKey; //签名类型 private String signType; @Value("${ali.pay.gatewayUrl}") private String aliPayGatewayUrl; @Value("${ali.pay.appid}") private String aliPayAppid; @Value("${ali.pay.privateKey}") private String aliPayPrivateKey; @Value("${ali.pay.charset}") private String aliPayCharset; @Value("${ali.pay.alipayPublicKey}") private String aliPayAlipayPublicKey; @Value("${ali.pay.signType}") private String aliPaySignType; @PostConstruct public void init() { this.gatewayUrl = aliPayGatewayUrl; this.appid = aliPayAppid; this.privateKey = aliPayPrivateKey; this.charset = aliPayCharset; this.alipayPublicKey = aliPayAlipayPublicKey; this.signType = aliPaySignType; } @Bean public AlipayClient alipayClient() { return new DefaultAlipayClient(gatewayUrl, appid, privateKey, "json", charset, alipayPublicKey, signType); } } ``` 5. 创建一个AliPayService类,用于处理支付宝相关业务逻辑。 ``` @Service public class AliPayService { @Autowired private AlipayClient alipayClient; @Value("${ali.pay.notifyUrl}") private String aliPayNotifyUrl; public String aliPay(String orderId, String totalAmount, String subject) { String result = ""; AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); alipayRequest.setReturnUrl("http://localhost:8080/returnUrl"); alipayRequest.setNotifyUrl(aliPayNotifyUrl); alipayRequest.setBizContent("{\"out_trade_no\":\"" + orderId + "\"," + "\"total_amount\":\"" + totalAmount + "\"," + "\"subject\":\"" + subject + "\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); try { result = alipayClient.pageExecute(alipayRequest).getBody(); } catch (AlipayApiException e) { e.printStackTrace(); } return result; } public void aliPayNotify(Map<String, String> params) { boolean verifyResult = false; try { verifyResult = AlipaySignature.rsaCheckV1(params, alipayClient.getAlipayPublicKey(), "UTF-8", "RSA2"); } catch (AlipayApiException e) { e.printStackTrace(); } if (verifyResult) { String outTradeNo = params.get("out_trade_no"); String tradeNo = params.get("trade_no"); String tradeStatus = params.get("trade_status"); if ("TRADE_SUCCESS".equals(tradeStatus)) { //支付成功 } } } } ``` 6. 在controller中调用AliPayService类进行支付宝支付。 ``` @Controller public class AliPayController { @Autowired private AliPayService aliPayService; @RequestMapping("/aliPay") @ResponseBody public String aliPay(HttpServletRequest request, HttpServletResponse response) throws IOException { String orderId = "201908140020000001"; String totalAmount = "0.01"; String subject = "测试支付"; String result = aliPayService.aliPay(orderId, totalAmount, subject); return result; } @RequestMapping("/returnUrl") public String returnUrl(HttpServletRequest request, HttpServletResponse response) throws IOException { Map<String, String> params = new HashMap<>(); Map<String, String[]> requestParams = request.getParameterMap(); for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) { String name = iter.next(); String[] values = requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } params.put(name, valueStr); } aliPayService.aliPayNotify(params); return "success"; } } ``` 以上就是springboot整合支付宝沙箱支付的案例,希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值