一、前言
本文将基于 springboot+沙箱环境 来简单实现支付宝的电脑网站支付 功能
二、获取所需配置参数信息
- APPID
- 支付宝网关
- 支付宝公钥
- RSA2私钥
登录支付宝 -> 研发服务 -> 沙箱 https://open.alipay.com/platform/appDaily.htm?tab=info 设置RSA2
在线生成密钥工具:https://miniu.alipay.com/keytool/create
沙箱钱包自己下载使用官网提供的沙箱账号登录即可.
三、官网提供的电脑网站支付 Demo
https://opendocs.alipay.com/open/270/106291 注:这个demo就自己tomcat跑一下看看吧,比较简单,记得修改一下AlipayConfig 配置文件参数即可~
四、SpringBoot版Demo
api文档: https://opendocs.alipay.com/apis
1、pom.xml 中新增依赖
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.15.6.ALL</version>
</dependency>
2、application.yml 中添加配置
根据自己的配置修改即可
alipay:
app_id: xxx
merchant_private_key: xxx
alipay_public_key: xxx
notify_url: http://tw6zc5.natappfree.cc/error.html
return_url: http://tw6zc5.natappfree.cc/success
sign_type: RSA2
charset: utf-8
gatewayUrl: https://openapi.alipaydev.com/gateway.do
log_path: "log/"
3、配置类
@Data
@Configuration
@ConfigurationProperties(prefix = "alipay", ignoreUnknownFields = false)
public class AlipayConfigProperty {
public String app_id;
public String merchant_private_key;
public String alipay_public_key;
public String notify_url;
public String return_url;
public String sign_type;
public String charset;
public String gatewayUrl;
public String log_path;
}
4、测试支付接口
@Slf4j
@Controller
@RequestMapping("")
@Api(tags = {"测试支付接口"})
public class TestController {
@Autowired
private AlipayConfigProperty alipayConfigProperty;
@GetMapping("/index")
public String index() {
return "index.html";
}
@ApiOperation("支付")
@GetMapping(value = "alipay")
@ResponseBody
@SneakyThrows(Exception.class)
public String alipay() {
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfigProperty.gatewayUrl, alipayConfigProperty.app_id, alipayConfigProperty.merchant_private_key, "json", alipayConfigProperty.charset, alipayConfigProperty.alipay_public_key, alipayConfigProperty.sign_type);
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(alipayConfigProperty.return_url);
alipayRequest.setNotifyUrl(alipayConfigProperty.notify_url);
String out_trade_no = "008";
String total_amount = "1";
String subject = "测试-商品名称";
String body = "测试-商品描述";
alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
+ "\"total_amount\":\"" + total_amount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"body\":\"" + body + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
String result = alipayClient.pageExecute(alipayRequest).getBody();
log.info("[支付] 请求响应结果:{}", result);
return result;
}
@ApiOperation("支付成功回调")
@GetMapping(value = "success")
@SneakyThrows(Exception.class)
public String alipaySuccess(HttpServletRequest request) {
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] + ",";
}
valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayConfigProperty.alipay_public_key, alipayConfigProperty.charset, alipayConfigProperty.sign_type);
if (signVerified) {
String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
log.info("trade_no:" + trade_no + "<br/>out_trade_no:" + out_trade_no + "<br/>total_amount:" + total_amount);
return "success.html";
} else {
log.error("验签失败");
return "error.html";
}
}
}
5、回调成功/失败 html页面
success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>SUCCESS</h1>
</body>
</html>
error.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>ERROR</h1>
</body>
</html>
6、运行
调用支付接口 http://127.0.0.1:8080/alipay
温馨小提示:如果出现提示支付存在钓鱼风险! ,解决:换个浏览器清除所有缓存再次尝试即可!
支付成功后,会回调我们配置的return_url 地址,然后处理自己的业务即可…
五、本文案例demo源码
https://gitee.com/zhengqingya/java-workspace
今日分享语句: 学会自在,自在就是真正的自由,真正从许多虚妄中解脱出来,真正让一颗心获得喜悦。 自在就是从有心到无心,从有我到无我,从有生到无生。
|