AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
为了方便,我们可以先将这些参数进行配置文件配置,在resouce目录下建立
alipay.properties
内容如下
# 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
app_id = 2088888888888(填支付宝开放平台自己的appid)
# 商户私钥,您的PKCS8格式RSA2私钥
merchant_private_key = (上篇博文在线生成的私钥)
# 支付宝公钥,查看地址:(上篇博文在线生成的公钥
alipay_public_key =
# 页面跳转页面路径义参数
notify_url = http://localhost:8080/order/notify
# 页面跳转页面路径义参数
return_url = http://localhost:8080/order/return
# 签名方式
sign_type = RSA2
# 字符编码格式
charset = utf-8
# 支付宝网关
gatewayUrl = https://openapi.alipaydev.com/gateway.do
# 支付宝网关
log_path = "C:\\"
建立类PropertiesConfig
/* 应用启动加载文件*/
@Component
public class PropertiesConfig implements ApplicationListener {
//保存加载配置参数
private static Map<String, String> aliPropertiesMap = new HashMap<String, String>();
/*获取配置参数值*/
public static String getKey(String key) {
return aliPropertiesMap.get(key);
}
/*监听启动完成,执行配置加载到aliPropertiesMap*/
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationReadyEvent) {
this.init(aliPropertiesMap);//应用启动加载
}
}
/*初始化加载aliPropertiesMap*/
public void init(Map<String, String> map) {
// 获得PathMatchingResourcePatternResolver对象
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
//加载resource文件(也可以加载resources)
Resource resources = resolver.getResource("classpath:config/alipay.properties");
PropertiesFactoryBean config = new PropertiesFactoryBean();
config.setLocation(resources);
config.afterPropertiesSet();
Properties prop = config.getObject();
//循环遍历所有得键值对并且存入集合
for (String key : prop.stringPropertyNames()) {
map.put(key, (String) prop.get(key));
}
} catch (Exception e) {
new Exception("配置文件加载失败");
}
}
}
在template下建立html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<H1>支付宝demo测试H1>
<hr>
<form action="order/alipay" method="post">
*商户订单 :<br>
<input type="text" name="out_trade_no"><br>
*订单名称 :<br>
<input type="text" name="subject"><br>
*付款金额 :<br>
<input type="text" name="total_amount"><br>
商品描述 :<br>
<input type="text" name="body"><br>
<input type="submit" value="支付宝支付">
form>
body>
html>
controller类
package com.gzh.paytest.controller;
import com.alipay.api.AlipayApiException;
import com.gzh.paytest.entity.OrderVo;
import com.gzh.paytest.service.PayService;
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;
/* 订单接口 */
@RestController()
@RequestMapping("order")
public class OrderController {
@Resource
private PayService payService;//调用支付服务
@RequestMapping("return")
public String PayReturn(){
return "支付成功";
}
@RequestMapping("notify")
public String PayNotify(){
return "支付失败";
}
/*阿里支付*/
@PostMapping(value = "alipay")
public String alipay(String out_trade_no,String subject,String total_amount,String body) throws AlipayApiException {
return payService.aliPay(new OrderVo()
.setBody(body)
.setOut_trade_no(out_trade_no)
.setTotal_amount(new StringBuffer().append(total_amount))
.setSubject(subject));
}
}
service接口和实现类
/*支付服务*/
public interface PayService {
/*支付宝*/
String aliPay(OrderVo orderVo) throws AlipayApiException;
}
/* 支付服务 */
@Service(value = "alipayOrderService")
public class PayServiceImpl implements PayService {
@Override
public String aliPay(OrderVo orderVo) throws AlipayApiException {
return AlipayUtil.connect(orderVo);
}
}
订单实体类。注意,我们调用支付宝的接口,所以参数名必须和接口文档中一致(上一篇有文档,详细自己查看)
/*订单对象*/
@Data
@Accessors(chain = true)
public class OrderVo {
/*商户订单号,必填*/
private String out_trade_no;
/*订单名称,必填*/
private String subject;
/*付款金额,必填*/
private StringBuffer total_amount;
/*商品描述,可空*/
private String body;
/*超时时间参数*/
private String timeout_express="10m";
private String product_code="FAST_INSTANT_TRADE_PAY";
}
调用支付宝接口类
/* 支付宝 */
public class AlipayUtil {
public static String connect(OrderVo orderVo) throws AlipayApiException {
//1、获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(
PropertiesConfig.getKey("gatewayUrl"),//支付宝网关
PropertiesConfig.getKey("app_id"),//appid
PropertiesConfig.getKey("merchant_private_key"),//商户私钥
"json",
PropertiesConfig.getKey("charset"),//字符编码格式
PropertiesConfig.getKey("alipay_public_key"),//支付宝公钥
PropertiesConfig.getKey("sign_type")//签名方式
);
//2、设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
//页面跳转同步通知页面路径
alipayRequest.setReturnUrl(PropertiesConfig.getKey("return_url"));
// 服务器异步通知页面路径
alipayRequest.setNotifyUrl(PropertiesConfig.getKey("notify_url"));
//封装参数
alipayRequest.setBizContent(JSON.toJSONString(orderVo));
//3、请求支付宝进行付款,并获取支付结果
String result = alipayClient.pageExecute(alipayRequest).getBody();
//返回付款信息
return result;
}