java程序支付寶接口付費功能的實現

之前作過c#應用程序支付寶api接口功能,如今轉移到Java程序上,代碼如何實現呢? 一、從你的網站提交到支付寶:c#

/** * 將訂單提交支付寶進行網上支付 */ public ActionForward submitAlipayUrl(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String orderNo = request.getParameter("orderNo");//訂單編號 HashMap hm = new HashMap(); hm.put("_input_charset", "utf-8");// 採用相同的編碼方式 hm.put("body", "企鵝個性化圖書訂單,訂單號:" + orderNo);// 填寫在跳到支付寶頁面上顯示的付款內容信息 hm.put("notify_url", "http://127.0.0.1:8888/mypenguin/orders.do?method=AlipayNotify");// 客戶付款後,支付寶調用的頁面 hm.put("out_trade_no", orderNo);// 外部交易號,最好具備惟一性,在獲取支付寶發來的付款信息時使用. hm.put("partner", "2088002302055380");// partnerId(合做夥伴ID) //hm.put("agent", "2088002302055380");// partnerId(合做夥伴ID) hm.put("payment_type", "1");// 支付類型 1=商品購買,2=服務購買,... //hm.put("price", "178.00");// 訂單金額信息 hm.put("total_fee", "178.00");// 訂單金額和信息 //hm.put("quantity", "1");// 訂單商品數量,通常都是寫1,它是按照整個訂單包來計算 hm.put("return_url", "http://127.0.0.1:8888/mypenguin/orders.do?method=AlipayReturn");// 客戶付款成功後,顯示給客戶的頁面 hm.put("show_url", "http://127.0.0.1:8888/mypenguin/books.do?method=list&kindId=1");//展現地址,即在支付頁面時,商品名稱旁邊的「詳情」的連接地址。 hm.put("seller_email", "monicali_7766@sina.com");// 你的支付寶帳戶email hm.put("service", "create_direct_pay_by_user");// create_direct_pay_by_user=直接付款,trade_create_by_buyer=擔保付款 hm.put("subject", "企鵝個性化圖書訂單號:" + orderNo);// 填寫在跳到支付寶頁面上顯示的付款標題信息 String payGateway = "https://www.alipay.com/cooperate/gateway.do";// 跳轉到支付寶的url頭 String securityCode="7nj9sczcnfc4n8366j46mrzsbqjksmzn";//securityCode(安全碼); String sign = makeUrl(hm,securityCode,"utf-8",payGateway);//securityCode(安全碼); hm.put("sign", sign); hm.put("sign_type", "MD5"); PrintWriter out = response.getWriter();api

out.println("<form name='alipaysubmit' method='post'  action='https://www.alipay.com/cooperate/gateway.do?_input_charset=utf-8' >");
    out.println("<input type='hidden' name='service' value='" + hm.get("service") + "'>");
    out.println("<input type='hidden' name='partner' value='" + hm.get("partner") + "'>");
    out.println("<input type='hidden' name='seller_email' value='" + hm.get("seller_email") + "'>");

out.println("<input type='hidden' name='out_trade_no' value='" + hm.get("out_trade_no") + "'>"); out.println("<input type='hidden' name='subject' value='" + hm.get("subject") + "'>"); out.println("<input type='hidden' name='body' value='" + hm.get("body") + "'>"); out.println("<input type='hidden' name='total_fee' value='" + hm.get("total_fee") + "'>"); out.println("<input type='hidden' name='show_url' value='" + hm.get("show_url") + "'>"); out.println("<input type='hidden' name='return_url' value='" + hm.get("return_url") + "'>"); out.println("<input type='hidden' name='notify_url' value='" + hm.get("notify_url") + "'>"); out.println("<input type='hidden' name='payment_type' value='1'>"); out.println("<input type='hidden' name='sign' value='" + hm.get("sign") + "'>"); out.println("<input type='hidden' name='sign_type' value='" + hm.get("sign_type") + "'>"); out.println("</form>"); out.println("<script>"); out.println(" document.alipaysubmit.submit()"); out.println("</script>"); return null; }安全

/** * 根據傳入的參數生成Alipay的支付URL * @param hm 參數值 * @param securityCode 安全碼 * @param charset 編碼 * @param payGateway 支付寶gateway服務器

  • @return */ public static String makeUrl(HashMap hm,String securityCode,String charSet,String payGateway) throws Exception{ List keys = new ArrayList(hm.keySet()); Collections.sort(keys);//支付寶要求參數必須按字母排序 StringBuffer content = new StringBuffer(); for (int i = 0; i < keys.size(); i++) { content.append((String) keys.get(i)); content.append("="); content.append((String) hm.get((String) keys.get(i))); if (i != keys.size() - 1) { content.append("&"); } } content.append(securityCode);
    String sign = DataUtil.MD5Encode(content.toString(), charSet); //MD5加密 return sign; }

二、return_url的內容: /** * 支付寶支付完成之後,當前窗口會從支付寶的頁面跳轉回這個頁面。該頁面稱做「返回頁」, * 是同步被支付寶服務器所調用,可看成是支付完成後的提示信息頁,如「您的某某某訂單,多少金額已支付成功」。 */ public ActionForward AlipayReturn(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?service=notify_verify"; //String alipayNotifyURL = "http://notify.alipay.com/trade/notify_query.do?";//此路徑是在上面連接地址沒法起做用時替換使用。 alipayNotifyURL = alipayNotifyURL+ "&partner=2088002302055380&notify_id="+ request.getParameter("notify_id"); // 獲取支付寶ATN返回結果,true是正確的訂單信息,false 是無效的 String responseTxt = check(alipayNotifyURL); // 得到POST 過來參數設置到新的requestParams中 Map requestParams = request.getParameterMap(); List keys = new ArrayList(requestParams.keySet()); Collections.sort(keys); StringBuffer content = new StringBuffer(); for (int i = 0; i < keys.size(); i++) { String key=(String) keys.get(i); if(!key.equals("")&&!key.equals("sign")&&!key.equals("sign_type")){ content.append((String) keys.get(i)); content.append("="); content.append((String)requestParams.get((String) keys.get(i))); if (i != keys.size() - 1) { content.append("&");
} } } String securityCode="7nj9sczcnfc4n8366j46mrzsbqjksmzn";//securityCode(安全碼); content.append(securityCode); String mysign = DataUtil.MD5Encode(content.toString(), "utf-8"); System.out.println("--------------------------content="+content); System.out.println("--------------------------mysign="+mysign); System.out.println("--------------------------sign="+request.getParameter("sign")); if (mysign.equals(request.getParameter("sign")) && responseTxt.equals("true") ){ if (request.getParameter("trade_status").equalsIgnoreCase("TRADE_FINISHED")){ // 能夠作重定向,也能夠用來虛擬物品發貨 } return mapping.findForward("payOK");//交易成功 }else{ return mapping.findForward("payFail");//交易失敗 } }app

三、notify_url的內容: /** * 通知返回URL,僅適用於異步返回處理結果的接口。有些服務是沒法當即返回處理結果的,那麼須要經過這個URL將處理結果異步返回給合做夥伴 */ public ActionForward AlipayNotify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?service=notify_verify"; //String alipayNotifyURL = "http://notify.alipay.com/trade/notify_query.do?";//此路徑是在上面連接地址沒法起做用時替換使用。 alipayNotifyURL = alipayNotifyURL+ "&partner=2088002302055380&notify_id="+ request.getParameter("notify_id"); // 獲取支付寶ATN返回結果,true是正確的訂單信息,false 是無效的 String responseTxt = check(alipayNotifyURL); // 得到POST 過來參數設置到新的requestParams中 Map requestParams = request.getParameterMap(); List keys = new ArrayList(requestParams.keySet()); Collections.sort(keys); StringBuffer content = new StringBuffer(); for (int i = 0; i < keys.size(); i++) { String key=(String) keys.get(i); if(!key.equals("")&&!key.equals("sign")&&!key.equals("sign_type")){ content.append((String) keys.get(i)); content.append("=");
content.append((String)requestParams.get((String) keys.get(i))); if (i != keys.size() - 1) { content.append("&"); } } } String securityCode="7nj9sczcnfc4n8366j46mrzsbqjksmzn";//securityCode(安全碼); content.append(securityCode); String mysign = DataUtil.MD5Encode(content.toString(), "utf-8"); System.out.println("--------------------------content="+content); System.out.println("--------------------------mysign="+mysign); System.out.println("--------------------------sign="+request.getParameter("sign")); if (mysign.equals(request.getParameter("sign")) && responseTxt.equals("true") ){ if (request.getParameter("trade_status").equalsIgnoreCase("TRADE_FINISHED")){ // 能夠作重定向,也能夠用來虛擬物品發貨 } return mapping.findForward("payOK"); }else{ return mapping.findForward("payFail"); } }異步

/**oop

  • @param myUrl *
    • @param url
    • @return 獲取url內容 */ public static String check(String urlvalue ) {

String inputLine = ""; try { URL url = new URL(urlvalue); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();post

BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));網站

String stTemp = ""; while((stTemp = in.readLine()) != null) { System.out.println(stTemp); inputLine = inputLine + stTemp; } } catch(Exception e) { e.printStackTrace(); } return inputLine; } 代碼編寫完畢,能夠試着debug調試一下ui

相關文章
相關標籤/搜索