SpringBoot整合支付宝APP支付
作者:Snow、杨 时间:2021-10-12 02:57:24
前言
现在是移动端产品疯狂的年代,随之,移动端支付也是热门小技能,最近本公司在做一个移动端,要接入微信支付和支付宝支付, * 惯,功能做完之后做个复盘记录,这边主要讲解支付宝APP支付
所需条件
1、创建蚂蚁金服开放平台公司账号
2、选择开发中心---->移动应用
3、选择支付接入
4、创建应用
5、查看应用appId
6、添加功能
两个功能:支付宝授权功能和APP支付功能
7、功能签约
开发流程
引入支付宝支付SDK
<!-- 支付宝支付 SDK -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.7.4.ALL</version>
</dependency>
1、支付宝参数配置类
/**
* AlipayConfig.java
* com.prereadweb.order.config
* Copyright (c) 2019, 北京聚智未来科技有限公司版权所有.
*/
package com.prereadweb.order.config;
/**
* @Description: 支付宝支付配置文件
* @author: Administrator
* @date: 2019/6/11 17:01
*/
public class AlipayConfig {
// APPID
public static String app_id = "你应用的APPID";
// 生成公钥时对应的私钥(填自己的)
public static String private_key = "你的秘钥";
//异步回调接口:得放到服务器上,且使用域名解析 IP
public static String notify_url = "回调函数接口";
//支付宝网关(注意沙箱alipaydev,正式则为 alipay)不需要修改
public static String url = "https://openapi.alipay.com/gateway.do";
//编码类型
public static String charset = "UTF-8";
//数据类型
public static String format = "json";
// 公钥
public static String public_key = "你的公钥";
//签名类型
public static String signtype = "RSA2";
}
2、下单接口
controller层
/**
* @Function: 去支付
* @author: YangXueFeng
* @Date: 2019/6/11 16:10
*/
@RequestMapping("/gotopay")
public Object goToPay(@Param("orderId") Long orderId){
return alipayViewService.setGotoPayInfos(orderId);
}
service层
/**
* @Function: 去支付
* @author: YangXueFeng
* @Date: 2019/6/11 16:11
*/
@Override
public Map<String, Object> setGotoPayInfos(Long orderId) {
Map<String, Object> map = new HashMap<>();
if(Util.isEmpty(orderId)){
map.put("code", UserStatusEnum.EMPTY.intKey());
map.put("msg", UserStatusEnum.EMPTY.value());
return map;
}
/* 查询订单信息 */
PayParameterForm payParameter = orderMapper.getPayParameter(orderId);
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.url, AlipayConfig.app_id, AlipayConfig.private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.public_key, AlipayConfig.signtype);//支付宝需要的参数serverUrl、appId、private_key、format、charset、public_key、signType
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody(payParameter.getTitle());//商品信息
model.setSubject(payParameter.getTitle());//商品名称
model.setOutTradeNo(String.valueOf(payParameter.getOrderId()));//订单号
model.setTimeoutExpress("30m");//支付超时时间
/*
model.setTotalAmount(String.valueOf(payParameter.getActualPrice()));// 支付金额
*/
model.setTotalAmount(String.valueOf(0.01));// 支付金额
request.setBizModel(model);
// 回调地址(充值订单)
request.setNotifyUrl(AlipayConfig.notify_url);// 回调地址
//这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = null;
try {
response = alipayClient.sdkExecute(request);
map.put("code", UserStatusEnum.SUCCESS.intKey());
map.put("msg", UserStatusEnum.SUCCESS.value());
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("payPath", response.getBody());
map.put("data", dataMap);
return map;
} catch (AlipayApiException e) {
e.printStackTrace();
}
map.put("code", UserStatusEnum.ERROR.intKey());
map.put("msg", UserStatusEnum.ERROR.value());
return map;
}
3、回调接口
controller层
/**
* @Function: 支付宝异步通知回调
* @author: YangXueFeng
* @Date: 2019/6/11 20:02
*/
@ResponseBody
@RequestMapping("/notify")
public String notify(HttpServletRequest request, HttpServletResponse response) {
return alipayViewService.notify(request, response);
}
service层
/**
* @Function: 支付宝异步回调接口
* @author: YangXueFeng
* @Date: 2019/6/11 20:03
*/
@Override
public String notify(HttpServletRequest request, HttpServletResponse response) {
Map<String, String> params = new HashMap<String, String>();
//从支付宝回调的request域中取值
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] + ",";
}
// 乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
// valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
params.put(name, valueStr);
}
//商品订单号
String out_trade_no = request.getParameter("out_trade_no"); // 商户订单号
// 当前交易状态
String tradeStatus = request.getParameter("trade_status"); //交易状态
// 支付金额
String totalAmount = request.getParameter("total_amount"); //交易状态
// 支付时间
String payDate = request.getParameter("gmt_payment"); //交易状态
//3.签名验证(对支付宝返回的数据验证,确定是支付宝返回的)
boolean signVerified = false;
try {
//3.1调用SDK验证签名
signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.public_key, AlipayConfig.charset, AlipayConfig.signtype);
} catch (AlipayApiException e) {
e.printStackTrace();
}
//返回状态存入redis中
//对验签进行处理
if (signVerified) {
//验签通过
if(tradeStatus.equals("TRADE_SUCCESS")) {
//支付成功后的业务处理
OrderEntity order = orderMapper.getOrderInfo(Long.valueOf(out_trade_no));
if(!Util.isEmpty(order)){
order.setStatus(CalculatStaticConstant.CHECK_ONE);
order.setCompleteTime(DateUtil.currentDate());
orderMapper.updateOrder(order);
}
/* 添加支付信息 */
OrderPayEntity orderPay = new OrderPayEntity();
orderPay.setId(Long.valueOf(IdUtils.getPrimaryKey()));
orderPay.setOrderId(order.getId());
orderPay.setUserId(order.getUserId());
orderPay.setPayPrice(Double.valueOf(totalAmount));
orderPay.setPayType(PayTypeEnum.ALI_PAY.intKey());
orderPay.setStatus(CalculatStaticConstant.CHECK_ONE);
orderPay.setPayTime(payDate);
orderMapper.saveOrderPay(orderPay);
RedisUtil.set("ali"+out_trade_no, tradeStatus,300);
return "success";
}
} else { //验签不通过
System.err.println("验签失败");
return "failure";
}
return "failure";
}
此处回调函数接口要有返回值,成功后返回success(success是小写,别大写),此处如果没有返回值的话,支付宝会一直进行回调通知
至此,支付宝APP支付OK
来源:https://blog.csdn.net/qq_37345604/article/details/93855402
标签:SpringBoot,支付宝
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用maven运行Java Main的三种方法解析
2021-09-24 10:09:28
Java Socket通信之聊天室功能
2022-02-04 11:47:35
![](https://img.aspxhome.com/file/2023/3/61283_0s.jpg)
Android Studio打包jar及aar包的方法
2023-06-18 15:59:53
![](https://img.aspxhome.com/file/2023/2/82392_0s.jpg)
IDEA解决src和resource下创建多级目录的操作
2023-03-24 07:05:37
![](https://img.aspxhome.com/file/2023/0/63690_0s.jpg)
浅谈java中静态方法的重写问题详解
2022-12-24 10:13:04
Android 沉浸式状态栏及悬浮效果
2023-07-29 23:03:15
![](https://img.aspxhome.com/file/2023/4/86014_0s.gif)
springboot 文件上传大小配置的方法
2023-08-02 11:49:16
Spring boot如何集成kaptcha并生成验证码
2023-09-13 04:00:24
![](https://img.aspxhome.com/file/2023/8/64918_0s.png)
java使用influxDB数据库的详细代码
2023-03-04 05:22:27
IDEA新建springboot项目时未生成pom.xml文件的解决操作
2022-08-22 03:16:31
![](https://img.aspxhome.com/file/2023/8/62518_0s.png)
SpringMVC的执行过程浅析
2021-05-31 20:51:11
![](https://img.aspxhome.com/file/2023/4/62074_0s.png)
Java调用wsdl接口的两种方法(axis和wsimport)
2023-06-23 14:41:22
![](https://img.aspxhome.com/file/2023/8/65338_0s.png)
spring boot学习笔记之操作ActiveMQ指南
2023-09-12 20:11:52
SpringCloud基本Rest微服务工程搭建过程
2023-08-28 16:23:29
![](https://img.aspxhome.com/file/2023/0/63090_0s.png)
Java虚拟机使用jvisualvm工具远程监控tomcat内存
2023-11-28 22:15:49
![](https://img.aspxhome.com/file/2023/2/60462_0s.png)
解决javaWEB中前后台中文乱码问题的3种方法
2023-03-22 22:39:26
![](https://img.aspxhome.com/file/2023/7/64807_0s.jpg)
Spring Boot实现分布式系统中的服务发现和注册(最新推荐)
2022-07-10 03:50:23
Spring Boot 配置和使用多线程池的实现
2022-09-04 19:53:02
详解java倒计时三种简单实现方式
2023-11-11 16:47:38
![](https://img.aspxhome.com/file/2023/8/62508_0s.png)
通过FeignClient调用微服务提供的分页对象IPage报错的解决
2022-01-27 20:19:23
![](https://img.aspxhome.com/file/2023/7/63057_0s.png)