🎙告诉你:Java是世界上最美好的语言
💎比较擅长的领域:前端开发
是的,我需要您的:
🧡点赞❤️关注💙收藏💛
是我持续下去的动力!
目录
一.了解各种地址
1.1 沙箱地址
https://open.alipay.com/develop/sandbox/app
1.2 官方文档
https://opendocs.alipay.com/open/270/01didh
1.3 gitee项目学习
alipay-demo: 使用支付宝沙箱实现支付功能 - Gitee.com
二 . 支付宝沙箱信息
编辑
编辑 natapp地址
三. 配置内网步骤
3.1 购买隧道
编辑
3.2 配置隧道
编辑
3.3 复制authtoken
编辑3.4 配置config.ini
编辑
3.5 配置config.ini修改authtoken
编辑
3.6 拷贝到authtoken
编辑
3.7 运行
编辑
3.8 结果
编辑
四. 完整版sdk 依赖
<dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.22.110.ALL</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.20</version> </dependency>
五 . 在application.yml 里面进行配置
alipay: appId: 9021000142654289 appPrivateKey: MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCBXHmjB4B6iUR+sQ9i7U1G17HByH1wmmwFKqCw3hajIKubVJwj6i5aaRzcjoeChJiMHqcCNs+2i9ZcLfMEcC2/my29VA/9XnGjpB21ZzdRMTxRKc95bL3nUpbR+ROej+cUUQcr1NV4dNm1giIFippi4TpCJtHYBztyBXZno4HJe/dq6POdGFTgzXeVxExAm8cL4t1GRVSzi7hRV70Xq14BKffRJErpzSg+x4l4qAnc2CcinP9H7aUxPdfMxVXku1wQjm1ExSbg4YNK0FoSqsR+NO2v97TwoBNKUXAw+Z/OyRXvn9pmB+0KysCSfZDJY+2TM41MdNzbDI+xWwmI7fVRAgMBAAECggEAYuH9WNFzNDeZU/6kdEpQt8ZI6ogG2vwvdgOPsaMSSr++bY3PjOSFajbPCQkqLGqO8Qy9PMQJItDhmrTE/q+BSOWloQOQ9WwsEZpx2yh68GViZeg/3+FviFNpVje+94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lw== alipayPublicKey: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAialzjLbre28XpAcAlAhbq3dKvK02gnuHqK1uKXtqh/RuJgoO1rZ8gcw5fUD0qgBba4kWO6y/6or8Xsie1OLc5b2AiBK9LTbn3LRSaCwSPPoQOXq1fDOfbWeD8bFDlNDLLTV+LVRnwMuBJE0vPJGU1jCWy5G0YoIvoGaQcSHlFMq0bD3QBf7qi/OSXwbfpu40jKclvnaSOnn1ylHVFJhHgyzZNOHwaDvAk0F6RwIwakQdRQqfMLeoeQKHIUQcQvKtQkXohbY8AUb1lKWllYy6ge5AKlCMq9diMeftCDLvsubq4Aug1/hhgcyK5El3ERaFnADgUKCqyd3sHPp4IL+x8QIDAQAB notifyUrl: http://qmqvmt.natappfree.cc/alipay/notify returnUrl: http://localhost:8888/return
编辑
六. 配置文件AlipayConfig.java
只需要获取配置alipay的参数即可
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Data @Component @ConfigurationProperties(prefix = "alipay") //读取 配置值 public class AliPayConfig { private String appId; // appID private String appPrivateKey; // 应用私钥 private String alipayPublicKey; // 支付宝公钥 private String notifyUrl; // 支付成功后支付宝异步通知的接口地址 private String returnUrl; // 支付成功之后返回的路径 public String getReturnUrl() { return returnUrl; } public void setReturnUrl(String returnUrl) { this.returnUrl = returnUrl; } public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getAppPrivateKey() { return appPrivateKey; } public void setAppPrivateKey(String appPrivateKey) { this.appPrivateKey = appPrivateKey; } public String getAlipayPublicKey() { return alipayPublicKey; } public void setAlipayPublicKey(String alipayPublicKey) { this.alipayPublicKey = alipayPublicKey; } public String getNotifyUrl() { return notifyUrl; } public void setNotifyUrl(String notifyUrl) { this.notifyUrl = notifyUrl; } }
七. 参数AliPay.java
import lombok.Data; @Data public class AliPay { private String traceNo; // 我们自己生成的订单编号 private double totalAmount;// 订单的总金额 private String subject; // 支付的名称 private String alipayTraceNo; public String getTraceNo() { return traceNo; } public void setTraceNo(String traceNo) { this.traceNo = traceNo; } public double getTotalAmount() { return totalAmount; } public void setTotalAmount(double totalAmount) { this.totalAmount = totalAmount; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getAlipayTraceNo() { return alipayTraceNo; } public void setAlipayTraceNo(String alipayTraceNo) { this.alipayTraceNo = alipayTraceNo; } }
八. AliPayController.java
注意看注释!
package com.kyh.controller; import cn.hutool.json.JSONObject; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.internal.util.AlipaySignature; import com.alipay.api.request.AlipayTradePagePayRequest; import com.kyh.common.AliPayConfig; 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 javax.annotation.Resource; //jdk17不能用 //import javax.servlet.http.HttpServletRequest; //jdk17不能用 //import javax.servlet.http.HttpServletResponse; //jdk17不能用 import jakarta.annotation.Resource; // 17用 jakarta import jakarta.servlet.http.HttpServletRequest; //用jakarta import jakarta.servlet.http.HttpServletResponse; import java.util.Date; import java.util.HashMap; import java.util.Map; // xjlugv6874@sandbox.com // 9428521.24 - 30 = 9428491.24 + 30 = 9428521.24 @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"; // 返回格式 private static final String CHARSET = "UTF-8"; // 编码 //签名方式 private static final String SIGN_TYPE = "RSA2"; // 签名方式 密钥 @Resource private AliPayConfig aliPayConfig; @GetMapping("/pay") // &subject=xxx&traceNo=xxx&totalAmount=xxx public void pay(AliPay aliPay, HttpServletResponse httpResponse) throws Exception { // alipay 接受传来的数据 System.out.println(aliPayConfig.getAppId()+" "+ aliPayConfig.getAppPrivateKey()); System.out.println(aliPayConfig.getAlipayPublicKey()); // 测试输出 // 1. 创建Client,通用SDK提供的Client,负责调用支付宝的API AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, aliPayConfig.getAppId(), aliPayConfig.getAppPrivateKey(), FORMAT, CHARSET, aliPayConfig.getAlipayPublicKey(), SIGN_TYPE); // 2. 创建 Request并设置Request参数 AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); // 发送请求的 Request类 request.setNotifyUrl(aliPayConfig.getNotifyUrl()); // 支付成功后支付宝异步通知的接口地址 request.setReturnUrl(aliPayConfig.getReturnUrl()); // 支付成功后跳转的页面 AliPayController.java的pay接口设置回调的url:request.setReturnUrl(aliPayConfig.getReturnUrl()); JSONObject bizContent = new JSONObject(); System.out.println(aliPay.getSubject()+" "+aliPay.getTraceNo()+" "+aliPay.getTotalAmount()); bizContent.set("out_trade_no", aliPay.getTraceNo()); // 我们自己生成的订单编号 bizContent.set("total_amount", aliPay.getTotalAmount()); // 订单的总金额 bizContent.set("subject", aliPay.getSubject()); // 支付的名称 bizContent.set("product_code", "FAST_INSTANT_TRADE_PAY"); // 固定配置 request.setBizContent(bizContent.toString()); // 执行请求,拿到响应的结果,返回给浏览器 String form = ""; try { form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单 } catch (AlipayApiException e) { e.printStackTrace(); } System.out.println("form="+form); httpResponse.setContentType("text/html;charset=" + CHARSET); httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面 httpResponse.getWriter().flush(); httpResponse.getWriter().close(); } @PostMapping("/notify") // 注意这里必须是POST接口 public String payNotify(HttpServletRequest request) throws Exception { System.out.println("支付宝发出异步"); 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 outTradeNo = params.get("out_trade_no"); String gmtPayment = params.get("gmt_payment"); String alipayTradeNo = params.get("trade_no"); String sign = params.get("sign"); String content = AlipaySignature.getSignCheckContentV1(params); boolean checkSignature = AlipaySignature.rsa256CheckContent(content, sign, aliPayConfig.getAlipayPublicKey(), "UTF-8"); // 验证签名 // 支付宝验签 if (checkSignature) { // 验签通过 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")); // 查询订单 // update 表 修改DAO的数据 } } return "success"; } }
九. 实现回调功能
- 加配置(前端支付页面的地址):alipay.returnUrl=http://localhost:8080/orders
- AlipayConfig.java 加一个属性:returnUrl
- AliPayController.java的pay接口设置回调的url:request.setReturnUrl(aliPayConfig.getReturnUrl());
十.页面调用
window.open('http://localhost:9090/alipay/pay?traceNo=' + row.orderNo + "&totalAmount=" + row.price + '&subject=' + row.name)
以上即全部的内容,配置完之后就可以实现了!!!
十一. 效果图
编辑
🎙告诉你:Java是世界上最美好的语言
💎比较擅长的领域:前端开发
是的,我需要您的:
🧡点赞❤️关注💙收藏💛
是我持续下去的动力!