当前位置:   article > 正文

2023最新之 教你如何使用Springboot集成支付宝沙箱支付(支持回调- 亲测有效)_springboot支付宝支付后回调

springboot支付宝支付后回调

1、进入支付宝开放平台

https://openhome.alipay.comhttps://openhome.alipay.com

 2、进入沙箱

2.1 点击启用公钥

点击查看:

3、Java集成沙箱支付

3.1  在pom.xml文件中添加依赖

  1. <!--支付宝依赖 -->
  2. <dependency>
  3. <groupId>com.alipay.sdk</groupId>
  4. <artifactId>alipay-easysdk</artifactId>
  5. <version>2.2.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.alipay.sdk</groupId>
  9. <artifactId>alipay-sdk-java</artifactId>
  10. <version>4.22.113.ALL</version>
  11. </dependency>

3.2 在 application.yml 文件中配置

  1. alipay:
  2. appId:
  3. appPrivateKey:
  4. alipayPublicKey:
  5. notifyUrl:

3.3 alipay的java配置:AplipayConfig.java

  1. package com.shijiu.config;
  2. import com.alipay.easysdk.factory.Factory;
  3. import com.alipay.easysdk.kernel.Config;
  4. import lombok.Data;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.stereotype.Component;
  7. import javax.annotation.PostConstruct;
  8. /**
  9. * @Author
  10. * @Date Created in 2023/5/5 15:06
  11. * @DESCRIPTION:
  12. * @Version V1.0
  13. */
  14. @Data
  15. @Component
  16. //读取yml文件中alipay 开头的配置
  17. @ConfigurationProperties(prefix = "alipay")
  18. public class AliPayConfig {
  19. private String appId;
  20. private String appPrivateKey;
  21. private String alipayPublicKey;
  22. private String notifyUrl;
  23. @PostConstruct
  24. public void init() {
  25. // 设置参数(全局只需设置一次)
  26. Config config = new Config();
  27. config.protocol = "https";
  28. config.gatewayHost = "openapi.alipaydev.com";
  29. config.signType = "RSA2";
  30. config.appId = this.appId;
  31. config.merchantPrivateKey = this.appPrivateKey;
  32. config.alipayPublicKey = this.alipayPublicKey;
  33. config.notifyUrl = this.notifyUrl;
  34. Factory.setOptions(config);
  35. System.out.println("=======支付宝SDK初始化成功=======");
  36. }
  37. }

 3.4   **注意:在拦截器里面加上 忽略alipay接口的配置,这很重要!**

3.5 新建一个AliPayController.java

  1. package com.shijiu.controller;
  2. import cn.hutool.core.date.DatePattern;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.alipay.easysdk.factory.Factory;
  5. import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
  6. import com.shijiu.common.Result;
  7. import com.shijiu.config.AliPayConfig;
  8. import com.shijiu.dao.mapper.ShopOrderMapper;
  9. import com.shijiu.pojo.AliPay;
  10. import com.shijiu.pojo.ShopOrder;
  11. import org.joda.time.DateTime;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.PostMapping;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RestController;
  17. import java.net.URLEncoder;
  18. import com.alipay.api.AlipayApiException;
  19. import com.alipay.api.AlipayClient;
  20. import com.alipay.api.DefaultAlipayClient;
  21. import com.alipay.api.request.AlipayTradePagePayRequest;
  22. import com.alipay.easysdk.factory.Factory;
  23. import org.springframework.web.bind.annotation.GetMapping;
  24. import org.springframework.web.bind.annotation.PostMapping;
  25. import org.springframework.web.bind.annotation.RequestMapping;
  26. import org.springframework.web.bind.annotation.RestController;
  27. import javax.annotation.Resource;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;
  30. import java.time.LocalDateTime;
  31. import java.util.Date;
  32. import java.util.HashMap;
  33. import java.util.Map;
  34. /**
  35. * @Author
  36. * @Date Created in 2023/5/5 15:23
  37. * @DESCRIPTION:
  38. * @Version V1.0
  39. */
  40. @RestController
  41. @RequestMapping("alipay")
  42. @Transactional(rollbackFor = Exception.class)
  43. public class AliPayController {
  44. @Resource
  45. AliPayConfig aliPayConfig;
  46. @Resource
  47. private ShopOrderMapper shopOrderMapper;
  48. private static final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
  49. private static final String FORMAT ="JSON";
  50. private static final String CHARSET ="utf-8";
  51. private static final String SIGN_TYPE ="RSA2";
  52. @GetMapping("/pay") // &subject=xxx&traceNo=xxx&totalAmount=xxx
  53. public void pay(AliPay aliPay, HttpServletResponse httpResponse) throws Exception {
  54. AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, aliPayConfig.getAppId(),
  55. aliPayConfig.getAppPrivateKey(), FORMAT, CHARSET, aliPayConfig.getAlipayPublicKey(), SIGN_TYPE);
  56. AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
  57. request.setNotifyUrl(aliPayConfig.getNotifyUrl());
  58. request.setBizContent("{\"out_trade_no\":\"" + aliPay.getTraceNo() + "\","
  59. + "\"total_amount\":\"" + aliPay.getTotalAmount() + "\","
  60. + "\"subject\":\"" + aliPay.getSubject() + "\","
  61. + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
  62. String form = "";
  63. try {
  64. // 调用SDK生成表单
  65. form = alipayClient.pageExecute(request).getBody();
  66. } catch (AlipayApiException e) {
  67. e.printStackTrace();
  68. }
  69. httpResponse.setContentType("text/html;charset=" + CHARSET);
  70. // 直接将完整的表单html输出到页面
  71. httpResponse.getWriter().write(form);
  72. httpResponse.getWriter().flush();
  73. httpResponse.getWriter().close();
  74. }
  75. @PostMapping("/notify") // 注意这里必须是POST接口
  76. public String payNotify(HttpServletRequest request) throws Exception {
  77. if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
  78. System.out.println("=========支付宝异步回调========");
  79. Map<String, String> params = new HashMap<>();
  80. Map<String, String[]> requestParams = request.getParameterMap();
  81. for (String name : requestParams.keySet()) {
  82. params.put(name, request.getParameter(name));
  83. // System.out.println(name + " = " + request.getParameter(name));
  84. }
  85. String tradeNo = params.get("out_trade_no");
  86. String gmtPayment = params.get("gmt_payment");
  87. String alipayTradeNo = params.get("trade_no");
  88. // 支付宝验签
  89. if (Factory.Payment.Common().verifyNotify(params)) {
  90. // 验签通过
  91. System.out.println("交易名称: " + params.get("subject"));
  92. System.out.println("交易状态: " + params.get("trade_status"));
  93. System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
  94. System.out.println("商户订单号: " + params.get("out_trade_no"));
  95. System.out.println("交易金额: " + params.get("total_amount"));
  96. System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
  97. System.out.println("买家付款时间: " + params.get("gmt_payment"));
  98. System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
  99. // 更新订单未已支付
  100. ShopOrder order = new ShopOrder();
  101. order.setId(tradeNo);
  102. order.setStatus("1");
  103. Date payTime = DateUtil.parse(gmtPayment, "yyyy-MM-dd HH:mm:ss");
  104. order.setZhhifuTime(payTime);
  105. shopOrderMapper.updateById(order);
  106. }
  107. }
  108. return "success";
  109. }
  110. }

 3.6 新建  AliPay.java 参数

  1. package com.shijiu.pojo;
  2. import lombok.Data;
  3. /**
  4. * @Author
  5. * @Date Created in 2023/5/5 15:26
  6. * @DESCRIPTION:
  7. * @Version V1.0
  8. */
  9. @Data
  10. public class AliPay {
  11. private String traceNo;
  12. private double totalAmount;
  13. private String subject;
  14. private String alipayTraceNo;
  15. }

 前端vue 代码:(重点)

  1. handleEdit(row) {
  2. this.$confirm('确认支付?, 是否继续?', '删除打卡信息?', {
  3. confirmButtonText: '确定',
  4. cancelButtonText: '取消',
  5. type: 'warning'
  6. }).then(() => {
  7. window.open(`http://localhost:8888/alipay/pay?subject=${row.orderName}&traceNo=${row.id}&totalAmount=${row.prince}`)
  8. this.$message.success('付款成功')
  9. this.load()
  10. }).catch(() => {
  11. this.$message({
  12. type: 'info',
  13. message: '已取消付款'
  14. })
  15. this.load()
  16. })
  17. },

3.7 接口测试

然后我们访问URL进行测试,测试的url格式:

http://localhost:8888/alipay/pay?subject=测试商品&traceNo= 666&totalAmount=1000

 

 

3.8 内网穿透:https://natapp.cn/

配置免费的隧道,端口:8888:(你项目中的地址端口号

 

3.9 新建文本文档 (启动内网穿透工具):

内容为下列内容。修改格式为 .bat

 每次启动回调地址都不同!!!

 3.10  回调地址:(如果一次不行,就多重新启动start.bat ,并更新yml文件中的回调地址

触发回调函数:

看下方的:

3.11 支付结果:(示例:227)

已支付

 学习来源:

Springboot集成支付宝沙箱支付_哔哩哔哩_bilibili大坑!集成支付宝沙箱支付!文档:https://blog.csdn.net/xqnode/article/details/124457790, 视频播放量 16884、弹幕量 103、点赞数 390、投硬币枚数 400、收藏人数 517、转发人数 72, 视频作者 程序员青戈, 作者简介 毕设私聊Q:1938976892,相关视频:支付宝沙箱支付快速集成版,沙箱支付宝项目实战教学 SpringBoot + Vue前后端分离项目整合沙箱支付宝完成支付功能,黑马程序员Java在线支付教程,深度学习掌握Java(微信支付+支付宝支付等)常用支付方式,从0开始带你手撸一套SpringBoot+Vue后台管理系统(2022年最新版),计算机毕业设计轻松搞定支付宝沙箱支付配置,8分钟学完springboot+vue前后分离的增删改查,springboot实现支付宝沙箱付款和退款功能,【尚硅谷】微信支付&支付宝支付,一套搞定Java在线支付开发教程,【java毕业设计】springboot vue校园二手闲置物品交易平台源码和论文 含支付宝沙箱支付功能,支付宝在线支付接入视频教程-Java版https://www.bilibili.com/video/BV1KS4y1w7yE/?spm_id_from=333.999.0.0&vd_source=419fe38ebae639bb2494d02c5fe95313

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/186190
推荐阅读
相关标签
  

闽ICP备14008679号