支付宝沙箱支付
开发前准备
- Easy支付官方文档:https://opendocs.alipay.com/open/009ys9
- api文档:https://opendocs.alipay.com/apis
- 支付宝开发网址
支付宝开放平台: https://open.alipay.com/platform/home.htm
支付宝沙箱环境: https://openhome.alipay.com/platform/appDaily.htm?tab=info
支付宝开发者文档:https://openhome.alipay.com/developmentDocument.htm
电脑网站支付流程:https://docs.open.alipay.com/270
生成签名:https://docs.open.alipay.com/291/106103/
支付宝沙箱配置
-
打开支付宝开放平台,进入支付宝开发者账户,进入开发者控制台
https://open.alipay.com/dev/workspace
-
进入沙箱应用配置
-
在沙箱应用 -> 开发信息中
Java集成沙箱支付
Maven依赖
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk -->
<!-- Alipay Easy SDK -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-easysdk</artifactId>
<version>2.2.0</version>
</dependency>
支付配置
- 在application.yml文件中配置
# 支付宝配置
alipay:
# 请填写您的AppId,例如:2019091767145019
appId:
# 请填写您的应用私钥
merchantPrivateKey:
# 请填写您的支付宝公钥
alipayPublicKey:
# 请填写您的支付类接口异步通知接收服务地址
notifyUrl:
- alipay的java配置
在config目录中创建 AliPayConfig.java配置类
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* 支付宝配置类
* @author fangqi174956
*/
@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class AliPayConfig {
// AppId,例如:2019091767145019
private String appId;
// 应用私钥
private String merchantPrivateKey;
// 支付宝公钥
private String alipayPublicKey;
// 支付类接口异步通知接收服务地址
private String notifyUrl;
private Logger logger = LoggerFactory.getLogger(AliPayConfig.class);
@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.merchantPrivateKey;
config.alipayPublicKey = this.alipayPublicKey;
config.notifyUrl = this.notifyUrl;
Factory.setOptions(config);
logger.info("====== 支付宝SDK初始化成功! ======");
}
}
- 创建alipay的支付接口
在AlipayController中,新建pay()接口和支付成功异步回调notify接口
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.qingge.springboot.common.Result;
import com.qingge.springboot.entity.AliPay;
import com.qingge.springboot.entity.Orders;
import com.qingge.springboot.service.IOrdersService;
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;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* alipay支付控制器
* @author fangqi174956
*/
@RestController
@RequestMapping("/alipay")
public class AliPayController {
@Resource
private IOrdersService ordersService;
@GetMapping("/pay")
public String pay(AliPay aliPay){
System.out.println("alipay===" + aliPay);
AlipayTradePagePayResponse response;
try {
// 1. 发起API调用(以创建当面付收款二维码为例)
response = Factory.Payment.Page()
.pay(aliPay.getSubject(),aliPay.getTradeNo(),aliPay.getTotalAmount(),"");
} catch (Exception e) {
System.err.println("调用遭遇异常,原因:" + e.getMessage());
throw new RuntimeException(e.getMessage(), e);
}
return response.getBody();
}
/**
* 支付宝回调
* 注意: 这里必须是POST 请求
*/
@PostMapping("/notify")
public Result payNotify(HttpServletRequest request) throws Exception {
if("TRADE_SUCCESS".equals(request.getParameter("trade_status"))){
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("map参数--->" + name + "=" + request.getParameter(name));
}
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"));
String orderNo = params.get("out_trade_no");
String payTime = params.get("gmt_payment");
String alipayNo = params.get("trade_no");
// 根据订单编号查询订单信息
Orders orders = ordersService.getOrderByNo(orderNo);
orders.setState(true);
orders.setPayTime(payTime);
orders.setAlipayNo(alipayNo);
System.out.println("更新后的订单信息" + orders);
// 支付宝验签
if(Factory.Payment.Common().verifyNotify(params)){
// 验签通过
// 更新订单表订单状态
ordersService.updateById(orders);
}
}
return Result.success();
}
}
- 参数实体类
import lombok.Data;
/**
* 支付实体类
* @author fangqi174956
*/
@Data
public class AliPay {
private String subject;
private String traceNo;
private String totalAmount;
}
注意:在拦截器里加上 忽略alipay的接口设置,这个很重要!
- 在config/InterceptorConfig.java 拦截器配置中,忽略alipay的接口的拦截
订单表基本设计
支付流程
支付宝支付流程交互关系
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/4929.html