Controller*
@ResponseBody
@PostMapping("/pay11")
public Map<String, Object> doPostForm(@RequestBody Rechange params) {
Map<String, Object> map = new HashMap<>();
// params傳遞的參數
String formResult = sendxwwwform( params);//調用from表單法法
map.put("data", formResult);
return map;
}
/**
* form表單提交
* @param url
* @param paramMap
* @return
*/
public String sendxwwwform(Rechange params) {
//生成訂單號的工具
DateFormat order_d = new DateFormat();
String out_trade_no = order_d.dateFormat().get("random").toString();
//生成簽名的Util 我的文章中有生成簽名的方法 需要的請自行查看
BaseBean maps = new BaseBean();
//map集合需要存儲的參數
Map<String, Object> map = new HashMap<>();
map.put("_input_charset","utf-8");//參數編碼字符集
map.put("service","create_direct_pay_by_user");
map.put("partner","");//合作者身份ID
map.put("seller_id", "");//賣家支付寶用戶號
map.put("payment_type", "1");//只支持取值爲1(商品購買)。
map.put("notify_url", "http://***/notify_url");//服務器異步通知頁面路徑
map.put("return_url", "http://***/frame");//頁面跳轉同步通知頁面路徑
map.put("out_trade_no", out_trade_no);
map.put("subject", "");//商品名稱
map.put("total_fee", params.getMoney());//交易金額
//調用方法 生成簽名
map = maps.toMap(map);
String ress = "<form id='alipaysubmit' name='alipaysubmit' action='https://mapi.alipay.com/gateway.do?_input_charset=utf-8' method='POST'>"
+ "<input type='hidden' name='service' value='create_direct_pay_by_user'>"
+ "<input type='hidden' name='partner' value=''>"//合作者身份ID
+ "<input type='hidden' name='seller_id' value=''>"//賣家支付寶用戶號
+ "<input type='hidden' name='payment_type' value='1'>"//只支持取值爲1(商品購買)。
+ "<input type='hidden' name='notify_url' value='http://***/notify_url'>"
+ "<input type='hidden' name='return_url' value='http://***/frame'>"
+ "<input type='hidden' name='out_trade_no' value='"+out_trade_no+"'>"
+ "<input type='hidden' name='subject' value=''>"//商品名稱
+ "<input type='hidden' name='total_fee' value='"+params.getMoney()+"'>"//交易金額
+ "<input type='hidden' name='sign' value='"+map.get("sign")+"'>"//傳入生成的簽名
+ "<input type='hidden' name='sign_type' value='MD5'>"
+ "<input type='submit' value='正在跳轉'></form>";
//from表單傳給前端 拉取支付頁面
return ress;
}
@RequestMapping(value="/notify_url")
@ResponseBody
public void asy(HttpServletRequest request) throws UnsupportedEncodingException {
//存儲回調接口獲取的參數
Map<String,String> params = new HashMap<String,String>();
//獲取回調需要驗籤的參數
Map<String, Object> params2 = new HashMap<String,Object>();
Map requestParams = request.getParameterMap();
Map<Object, Object> resultMap = new HashMap<>();
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] + ",";
}
//亂碼解決,這段代碼在出現亂碼時使用。
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
if(name.equals("sign_type") || name.equals("sign") || valueStr.equals("")){
}else{
params2.put(name, valueStr);
}
}
//調用加簽接口
BaseBean maps = new BaseBean();
String ss = (String)maps.toMap(params2).get("sign");
try {
// 商戶訂單號
// String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
//
// String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
String sign = new String(request.getParameter("sign").getBytes("ISO-8859-1"), "UTF-8");
// 驗證簽名 是否匹配
if(sign.equals(ss)){
System.out.println("驗證簽名通過1");
//獲取 通知校驗ID
String notify_id = (String)params2.get("notify_id");
//判斷校驗ID是否爲空
if(!params2.get("notify_id").equals("")){
//調用 下文 notify_id驗籤方法
String boole = sendGet("合作者身份ID",(String)params2.get("notify_id"));
//判斷是否成功
if(boole.equals("true")){
System.out.println("驗證簽名通過2");
// 商戶訂單號
String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
String total_fee = new String(request.getParameter("total_fee").getBytes("ISO-8859-1"), "UTF-8");
//判斷是否支付成功
if ("TRADE_SUCCESS".equals(trade_status)) {
}else{
System.out.println("支付失敗");
}
}else{
System.out.println("notify_id驗籤失敗!");
}
}else{
System.out.println("notify_id爲空!");
}
}else{
System.out.println("sign驗籤失敗!");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("SUCCESS");
}
public String sendGet( String partner,String notify_id) {
String result = "";
BufferedReader in = null;
try {
String urlNameString ="http://notify.alipay.com/trade/notify_query.do?partner=" +partner+"¬ify_id="+notify_id;
System.out.println(urlNameString);
URL realUrl = new URL(urlNameString);
// 打開和URL之間的連接
URLConnection connection = realUrl.openConnection();
// 設置通用的請求屬性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立實際的連接
connection.connect();
// 獲取所有響應頭字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍歷所有的響應頭字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定義 BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
System.out.println(line);
}
} catch (Exception e) {
System.out.println("發送GET請求出現異常!" + e);
e.printStackTrace();
}
// 使用finally塊來關閉輸入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}