功能展示
选择支付宝支付会在页面弹出由支付宝当面付接口提供的二维码,该二维码是支付宝API返回的二维码链接,前端接收转换为图片。
需要的参数
alipay.alipayPublicKey= //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥
alipay.gatewayHost= //支付宝网关
alipay.appId= //应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
alipay.appPrivateKey= //商户私钥,您的PKCS8格式RSA2私钥
alipay.notifyUrl= //服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问。注意:每次重新启动ngrok,都需要根据实际情况修改这个配置
Contoller层
@Autowired
private AliPayService aliPayService;
@Autowired
private GdsRealoutService gdsRealoutService;
@RequestMapping("/getAliPayQrPay")
@ResponseBody
public DataResult getAliPayQrPay(String subject,String outTradeNo,String totalAmount) {
return aliPayService.getAliPayQrPay(subject,outTradeNo,totalAmount);
}
@RequestMapping(value = "/alipayNotifyUrl",method = RequestMethod.POST)
@ResponseBody
public String alipayNotifyUrl(HttpServletRequest request){
if (aliPayService.notifyUrl(request).equals("success")){
GdsRealout gdsRealout = gdsRealoutService.selectByPrimaryKey(request.getParameter("out_trade_no").substring(0, request.getParameter("out_trade_no").length() - 13));
String userName = gdsRealout.getUpdatename();
WebSocketServerEndpoint ws = endpointMap.get(userName);
if (ws == null) {
System.out.println("not connected to " + userName);
return "fail to connect";
}
// 支付状态:0为默认值 未支付,1为已支付
gdsRealout.setTrade_status(1);
gdsRealoutService.updateByPrimaryKey(gdsRealout);
ws.sendMsg("支付成功",userName);
}
return "success";
}
ServiceImpl层
@Autowired
private GdsRealoutService gdsRealoutService;
@Override
public DataResult getAliPayQrPay(String subject, String outTradeNo, String totalAmount) {
// 1. 设置参数(全局只需设置一次) 设置支付参数
Factory.setOptions(getOptions());
try {
// 2. 发起API调用(以创建当面付收款二维码为例)
AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace()
.preCreate(subject, outTradeNo+ new Date().getTime(), totalAmount);
// 3. 处理响应或异常
if (ResponseChecker.success(response)) {
return new DataResult(0,"调用成功",response);
} else {
return new DataResult(1,"调用失败",response.msg);
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
@Override
public String notifyUrl(HttpServletRequest request) {
Map<String,String> map= new HashMap();
Enumeration<String> parameterNames = request.getParameterNames();
while(parameterNames.hasMoreElements()){
String key = parameterNames.nextElement();
String value = request.getParameter(key);
map.put(key, value);
}
//验签
try {
if(Factory.Payment.Common().verifyNotify(map)){
System.out.println("异步通知验签通过");
//验证用户的支付结果
String trade_status = request.getParameter("trade_status");
if(trade_status.equals("TRADE_SUCCESS")){
System.out.println("支付交易成功");
//这里根据业务更新订单支付状态.....
//数据库中 发货单 添加remark: out_trade_no:success;
String outTradeNo = map.get("out_trade_no");
GdsRealout gdsRealout = gdsRealoutService.selectByPrimaryKey(outTradeNo.substring(0,outTradeNo.length()-13));
gdsRealout.setTrade_status(1);
gdsRealoutService.updateByPrimaryKey(gdsRealout);
}
}else {
System.out.println("异步通知验签失败");
return "fail";
}
} catch (Exception e) {
System.err.println("异步发生异常{}"+e.getMessage());
return "fail";
}
return "success";
}
public static Config getOptions() {
Resource resource = new ClassPathResource("/config/DEV/properties/hzsr_payment.properties");
Properties props = null;
try {
props = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
e.printStackTrace();
}
Config config = new Config();
config.protocol = "https";
config.gatewayHost = props.getProperty("alipay.gatewayHost");
config.signType = "RSA2";
config.appId = props.getProperty("alipay.appId");
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
config.merchantPrivateKey = props.getProperty("alipay.appPrivateKey");
config.alipayPublicKey = props.getProperty("alipay.alipayPublicKey");
//可设置异步通知接收服务地址(可选)
config.notifyUrl = props.getProperty("alipay.notifyUrl");
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
config.encryptKey = "wvGnt8bUcfVuIiiLygqwGA==";
return config;
}