java支付宝接口成功跳转到了支付宝页面,支付成功以后异步通知和同步返回不执行
网页集成支付宝接口,支付成功以后,异步通知和同步返回不执行
配置文件部分代码:
public static String log_path = "D:\aliPayLog\";
/**
* 字符编码格式 目前支持 gbk 或 utf-8
*/
public static String input_charset = "utf-8";
/**
* 签名方式 不需修改
*/
public static String sign_type = "MD5";
/**
* 服务器异步通知页面路径
*/
public static final String notifyUrl = "localhost:8087/AliPay/Notify";
/**
* 同步通知页面路径
*/
public static final String returnUrl = "localhost:8087/AliPay/Return";
/**
* 支付类型
*/
public static final String paymentType = "1";
controller部分代码:
@RequestMapping(value = "/Notify")
public void NotifyUrl(HttpServletRequest request, HttpServletResponse response) throws ParseException
{
PrintWriter out = null;
try
{
out = response.getWriter();
boolean isTrue = GetAliPayReturn(request);
if (isTrue)
out.write("success");
else
out.write("fail");
}
catch (IOException e)
{
e.printStackTrace();
}
}
@RequestMapping(value = "/Return")
public String ReturnUrl(HttpServletRequest request) throws UnsupportedEncodingException, ParseException
{
boolean isTrue = GetAliPayReturn(request);
if (isTrue)
return "redirect:/center/myOrder"; // 我的订单
else
return "redirect: /alipay/alipayErr";
}
private boolean GetAliPayReturn(HttpServletRequest request) throws UnsupportedEncodingException, ParseException
{
Map params = new HashMap();
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();)
{
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++)
{
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
// 乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
// valueStr = new String(valueStr.getBytes("ISO-8859-1"), "UTF-8");
if (valueStr != null && !"".equals(valueStr))
{
if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(valueStr)))
{
try
{
valueStr = new String(valueStr.getBytes("ISO-8859-1"), "UTF-8");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
params.put(name, valueStr);
}
boolean verify_result = AlipayNotify.verify(params);
if (verify_result)
{
// 订单id
String ID = params.get("extra_common_param");
// 订单编号
String order_id = params.get("out_trade_no");
// 支付宝交易号
String tradeNo = params.get("trade_no");
// 交易状态
String payStatus = params.get("trade_status");
StringBuilder strSql = new StringBuilder();
strSql.append("UPDATE t_order ");
// 获得订单id
if (payStatus.equals("TRADE_FINISHED"))
{
strSql.append("set order_status = 3 ");
}
else if (payStatus.equals("TRADE_SUCCESS"))
{
strSql.append("set order_status = 3 ");
}
else
{
strSql.append("set order_status = 4 ");
}
strSql.append(" where order_number = '" + order_id + "' ");
Statement stmt = null;
Connection conn = null;
try
{
String driver = "com.mysql.jdbc.Driver"; // 驱动程序名
String url = "jdbc:mysql://192.168.1.252:3306/travel?characterEncoding=UTF-8"; // URL指向要访问的数据库名scutcs
String user = "root"; // MySQL配置时的用户名
String password = "mysql"; // MySQL配置时的密码
Class.forName(driver); // 加载驱动程序
conn = DriverManager.getConnection(url, user, password); // 连续数据库
stmt = conn.createStatement();
stmt.execute(strSql.toString());
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
conn.close();
stmt.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return false;
}