第一步:基于沙箱环境 配置参数,正式环境的 需要配置正式环境:https://openhome.alipay.com/platform/appDaily.htm?tab=info
第二步,引入jar包:
<dependency>
<groupId>com.egzosn</groupId>
<artifactId>pay-java-common</artifactId>
<version>2.10.2</version>
</dependency>
<dependency>
<groupId>com.egzosn</groupId>
<artifactId>pay-java-ali</artifactId>
<version>2.10.2</version>
</dependency>
jar包来源于git:地址:https://github.com/egzosn/pay-java-parent
第三步: 基于沙箱环境 配置参数,正式环境的 需要配置正式环境:
private PayService service = null;
@PostConstruct
public void init() {
AliPayConfigStorage aliPayConfigStorage = new AliPayConfigStorage();
aliPayConfigStorage.setPid("合作者id");
aliPayConfigStorage.setAppId("应用id");
aliPayConfigStorage.setKeyPublic("支付宝公钥");
aliPayConfigStorage.setKeyPrivate("应用私钥");
aliPayConfigStorage.setNotifyUrl("异步回调地址");
aliPayConfigStorage.setSignType(SignUtils.RSA.name());
aliPayConfigStorage.setSeller(“上家收款账号”);
aliPayConfigStorage.setInputCharset("utf-8");
//是否为测试账号,沙箱环境
aliPayConfigStorage.setTest(true);
//请求连接池配置
HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
//最大连接数
httpConfigStorage.setMaxTotal(20);
//默认的每个路由的最大连接数
httpConfigStorage.setDefaultMaxPerRoute(10);
service = new AliPayService(aliPayConfigStorage, httpConfigStorage);
}
第四步:供app提供创建订单信息接口:
@ApiOperation("返回订单信息")
@RequestMapping(value = "app", method = RequestMethod.POST)
public Map payBack(@RequestBody PayOrder payOrder) throws IOException{
init();
payOrder.setTransactionType(AliTransactionType.APP);
Map<String, Object> data = new HashMap<>();
data.put("state", 1);
//App支付
payOrder.setTransactionType(AliTransactionType.APP);
data.put("orderInfo", service.orderInfo(payOrder));
//获取APP支付所需的信息组,直接给app端就可使用
return data;
}
第五步:校验回调信息,设置业务逻辑:注意:此api,不需要设置api方法。否则,可能会回调不成功。
@ApiOperation("回调地址")
@RequestMapping(value = "payBack")
public String payBack(HttpServletRequest request) throws IOException{
init();
//获取支付宝POST过来反馈信息
Map params=service.getParameter2Map(request.getParameterMap(), request.getInputStream());
if(null ==params) return service.getPayOutMessage("fail", "失败").toMessage();
System.out.println("支付宝支付结果通知"+params.toString());
//校验
if (service.verify(params)) {
//这里处理业务逻辑
String orderNo=(String)params.get("out_trade_no");
if(StringUtils.isNotBlank(orderNo)){
scmSalesOrderService.updateStatus(orderNo);
}
//......业务逻辑处理块........
return service.getPayOutMessage("success", "成功").toMessage();
}
return service.getPayOutMessage("fail", "失败").toMessage();
}