springboot实现⽀付宝⽀付(沙箱环境,⽰例demo)
⼀:⾸先搭建springboot项⽬
⼆:由上⼀篇博⽂,⽰例代码可知: 1 先由⽹关,密钥等参数获取对象AlipayClient
AlipayClient alipayClient =new DefaultAlipayClient("openapi.alipay/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","R SA2");
中国十大名校大学为了⽅便,我们可以先将这些参数进⾏配置⽂件配置,在resouce⽬录下建⽴
alipay.properties
内容如下
# 应⽤ID,您的APPID,收款账号既是您的APPID对应⽀付宝账号
app_id = 2088888888888(填⽀付宝开放平台⾃⼰的appid)
# 商户私钥,您的PKCS8格式RSA2私钥
merchant_private_key = (上篇博⽂在线⽣成的私钥)
梦见宴席# ⽀付宝公钥,查看地址:(上篇博⽂在线⽣成的公钥
alipay_public_key =
# 页⾯跳转页⾯路径义参数
notify_url = localhost:8080/order/notify
# 页⾯跳转页⾯路径义参数
return_url = localhost:8080/order/return
# 签名⽅式
sign_type = RSA2
# 字符编码格式
charset = utf-8
lol怎么玩# ⽀付宝⽹关
gatewayUrl = openapi.alipaydev/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){
(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 = Resource("classpath:config/alipay.properties");
PropertiesFactoryBean config =new PropertiesFactoryBean();
config.setLocation(resources);
config.afterPropertiesSet();
Properties prop = Object();
//循环遍历所有得键值对并且存⼊集合
for(String key : prop.stringPropertyNames()){
map.put(key,(String) (key));
}
}catch(Exception e){
new Exception("配置⽂件加载失败");
}
}
}
在template下建⽴html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<H1>⽀付宝demo测试</H1>
<hr>
<form action="order/alipay"method="post">
*商户订单 :<br>qqmima
<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>
package;
import AlipayApiException;
import OrderVo;
import PayService;
import PostMapping;
import RequestMapping;
import RestController;
import 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接⼝和实现类
/*⽀付服务*/
江苏卫视天猫618超级晚直播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 {
t(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(
"json",
);
//2、设置请求参数
AlipayTradePagePayRequest alipayRequest =new AlipayTradePagePayRequest();
//页⾯跳转同步通知页⾯路径
alipayRequest.Key("return_url"));
/
/ 服务器异步通知页⾯路径
alipayRequest.Key("notify_url"));
//封装参数
alipayRequest.JSONString(orderVo));
//3、请求⽀付宝进⾏付款,并获取⽀付结果
String result = alipayClient.pageExecute(alipayRequest).getBody();
//返回付款信息
return result;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论