日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网站集成支付宝

發布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网站集成支付宝 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

 剛在Javaeye看到一篇文章,關于網站集成支付寶的,正是我之前想了解的,不過作者寫得有些零亂,解釋也不太清楚,代碼格式更是看著郁悶,待以后有空消化消化,再實踐下,現先貼出來保存著吧。

?

(本文轉載于http://stephen830.javaeye.com/blog/254827 )

?

國內電子商務系統實現的基本流程如下:
客戶在系統內下訂單 -> 系統根據訂單生成支付寶接口url -> 客戶通過url使用支付寶(網上銀行)付款 -> 支付寶將客戶的付款完成信息發送給電子商務系統 -> 系統收到支付寶信息后確定客戶訂單已經付款 -> 進行發貨等后續流程。

?

在開始下面的內容之前,你要先有一個支付寶賬戶,如果要集成支付寶接口,你還必須申請開通服務(關于如何開通,可以直接到支付寶網站上申請).在服 務開通后,支付寶會給你2個字符串編號:1個partnerId(合作伙伴ID),還有1個securityCode(安全碼).當你拿到這2個碼的時候 就可以開始下面的內容了.

(1)如何調用支付寶接口?(將客戶的訂單信息按照既定的規則生成一個url跳轉到支付寶網站)

通過下面方法[makeOrderAlipayUrl(HttpServletRequest httpRequest,Order order)]的調用得到支付寶的url,然后進行跳轉(response.sendRedirect(url);).

Java代碼 ?
  • /**?? ?
  • ?????*?根據訂單生成支付寶接口URL.?? ?
  • ?????*?@param?httpRequest?? ?
  • ?????*?@param?order?訂單實例?? ?
  • ?????*?@return?? ?
  • ?????*?@throws?Exception?? ?
  • ?????*/ ????
  • ????public ? static ?String?makeOrderAlipayUrl(HttpServletRequest?httpRequest,Order?order)? throws ?Exception?{?????
  • ????????HashMap?hm?=?new ?HashMap();?????
  • ????????hm.put("_input_charset" ,httpRequest.getCharacterEncoding()); //采用相同的編碼方式??? ??
  • ????????hm.put("body" , "您在www.xxx.com上的訂單" ); //填寫在跳到支付寶頁面上顯示的付款內容信息??? ??
  • ????????hm.put("discount" , "-5" ); //填寫折扣信息?-5表示抵扣5元??? ??
  • ????????hm.put("logistics_fee" , "10" ); //物流費用??? ??
  • ????????hm.put("logistics_payment" , "BUYER_PAY" ); //物流費用支付人?BUYER_PAY=買家支付物流費用??? ??
  • ????????hm.put("logistics_type" , "EXPRESS" ); //物流方式??? ??
  • ????????hm.put("notify_url" , "http://www.xxx.com/notifyurl.jsp" );//客戶付款后,支付寶調用的頁面?????
  • ????????hm.put("out_trade_no" ,order.getId()); //外部交易號,最好具有唯一性,在獲取支付寶發來的付款信息時使用.??? ??
  • ????????hm.put("partner" ,partnerId); //partnerId(合作伙伴ID)??? ??
  • ????????hm.put("agent" ,partnerId); //partnerId(合作伙伴ID)??? ??
  • ????????hm.put("payment_type" , "1" ); //支付類型?1=商品購買,2=服務購買,...??? ??
  • ????????hm.put("price" , "105.30" ); //訂單金額信息??? ??
  • ????????hm.put("quantity" , "1" ); //訂單商品數量,一般都是寫1,它是按照整個訂單包來計算??? ??
  • ????????hm.put("return_url" , "http://www.xxx.com/ReturnUrl.jsp" );//客戶付款成功后,顯示給客戶的頁面?????
  • ????????hm.put("seller_email" , "alipay@xxx.com" ); //你的支付寶賬戶email??? ??
  • ????????hm.put("service" , "create_direct_pay_by_user" ); //create_direct_pay_by_user=直接付款,trade_create_by_buyer?擔保付款???? ??
  • ????????hm.put("subject" , "www.xxx.com的訂單" ); //填寫在跳到支付寶頁面上顯示的付款標題信息??? ??
  • ????????String?payGateway?=?"https://www.alipay.com/cooperate/gateway.do?" ;//跳轉到支付寶的url頭?????
  • ????????return ?makeUrl(hm,securityCode,httpRequest.getCharacterEncoding(),payGateway); //securityCode(安全碼)???? ??
  • ????}?????
  • ?????????
  • ?????????
  • ????/**?? ?
  • ?????*?根據傳入的參數生成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?=?md5(content.toString(),charset);?????
  • ????????content.delete(0 ,content.length());?????
  • ????????content.append(payGateway);?????
  • ????????for ?( int ?i?=? 0 ;?i?<?keys.size();?i++)?{?????
  • ????????????content.append(keys.get(i));?????
  • ????????????content.append("=" );?????
  • ????????????content.append(URLEncoder.encode((String)?hm.get(keys.get(i)),?charset));?????
  • ????????????content.append("&" );?????
  • ????????}?????
  • ????????content.append("sign=" );?????
  • ????????content.append(sign);?????
  • ????????content.append("&sign_type=MD5" );?????
  • ????????keys.clear();?????
  • ????????keys?=?null ;?????
  • ????????return ?content.toString();?????
  • ????}?????
  • ?????????
  • ????/**?? ?
  • ?????*?生成md5編碼字符串.?? ?
  • ?????*?@param?str?源字符串?? ?
  • ?????*?@param?charset?編碼方式?? ?
  • ?????*?@return?? ?
  • ?????*?? ?
  • ?????*/ ????
  • ????public ? static ?String?md5(String?str,String?charset)?{?????
  • ????????if ?(str?==? null )?????
  • ????????????return ? null ;?????
  • ????????char ?hexDigits[]?=?{? '0' ,? '1' ,? '2' ,? '3' ,? '4' ,? '5' ,? '6' ,? '7' ,? '8' ,? '9' ,?????
  • ????????????????'a' ,? 'b' ,? 'c' ,? 'd' ,? 'e' ,? 'f' ?};?????
  • ?????????????
  • ????????MessageDigest?md5MessageDigest?=?null ;?????
  • ????????byte []?md5Bytes?=? null ;?????
  • ????????char ?md5Chars[]?=? null ;?????
  • ????????byte []?strBytes?=? null ;?????
  • ????????try ?{?????
  • ????????????strBytes?=?str.getBytes(charset);?????
  • ????????????md5MessageDigest?=?MessageDigest.getInstance("MD5" );?????
  • ????????????md5MessageDigest.update(strBytes);?????
  • ????????????md5Bytes?=?md5MessageDigest.digest();?????
  • ????????????int ?j?=?md5Bytes.length;?????
  • ????????????md5Chars?=?new ? char [j?*? 2 ];?????
  • ????????????int ?k?=? 0 ;?????
  • ????????????for ?( int ?i?=? 0 ;?i?<?j;?i++)?{?????
  • ????????????????byte ?md5Byte?=?md5Bytes[i];?????
  • ????????????????md5Chars[k++]?=?hexDigits[md5Byte?>>>?4 ?&? 0xf ];?????
  • ????????????????md5Chars[k++]?=?hexDigits[md5Byte?&?0xf ];?????
  • ????????????}?????
  • ????????????return ? new ?String(md5Chars);?????
  • ????????}?catch ?(NoSuchAlgorithmException?e)?{?????
  • ????????????//Log.output(e.toString(),?Log.STD_ERR);??? ??
  • ????????????return ? null ;?????
  • ????????}?catch ?(UnsupportedEncodingException?e)?{?????
  • ????????????//Log.output(e.toString(),?Log.STD_ERR);??? ??
  • ????????????return ? null ;?????
  • ????????}?finally ?{?????
  • ????????????md5MessageDigest?=?null ;?????
  • ????????????strBytes?=?null ;?????
  • ????????????md5Bytes?=?null ;?????
  • ????????}?????
  • ????}????
  • /** * 根據訂單生成支付寶接口URL. * @param httpRequest * @param order 訂單實例 * @return * @throws Exception */ public static String makeOrderAlipayUrl(HttpServletRequest httpRequest,Order order) throws Exception { HashMap hm = new HashMap(); hm.put("_input_charset",httpRequest.getCharacterEncoding());//采用相同的編碼方式 hm.put("body","您在www.xxx.com上的訂單");//填寫在跳到支付寶頁面上顯示的付款內容信息 hm.put("discount","-5");//填寫折扣信息 -5表示抵扣5元 hm.put("logistics_fee","10");//物流費用 hm.put("logistics_payment","BUYER_PAY");//物流費用支付人 BUYER_PAY=買家支付物流費用 hm.put("logistics_type","EXPRESS");//物流方式 hm.put("notify_url","http://www.xxx.com/notifyurl.jsp");//客戶付款后,支付寶調用的頁面 hm.put("out_trade_no",order.getId());//外部交易號,最好具有唯一性,在獲取支付寶發來的付款信息時使用. hm.put("partner",partnerId);//partnerId(合作伙伴ID) hm.put("agent",partnerId);//partnerId(合作伙伴ID) hm.put("payment_type","1");//支付類型 1=商品購買,2=服務購買,... hm.put("price","105.30");//訂單金額信息 hm.put("quantity","1");//訂單商品數量,一般都是寫1,它是按照整個訂單包來計算 hm.put("return_url","http://www.xxx.com/ReturnUrl.jsp");//客戶付款成功后,顯示給客戶的頁面 hm.put("seller_email","alipay@xxx.com");//你的支付寶賬戶email hm.put("service","create_direct_pay_by_user");//create_direct_pay_by_user=直接付款,trade_create_by_buyer 擔保付款 hm.put("subject","www.xxx.com的訂單");//填寫在跳到支付寶頁面上顯示的付款標題信息 String payGateway = "https://www.alipay.com/cooperate/gateway.do?";//跳轉到支付寶的url頭 return makeUrl(hm,securityCode,httpRequest.getCharacterEncoding(),payGateway);//securityCode(安全碼) } /** * 根據傳入的參數生成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 = md5(content.toString(),charset); content.delete(0,content.length()); content.append(payGateway); for (int i = 0; i < keys.size(); i++) { content.append(keys.get(i)); content.append("="); content.append(URLEncoder.encode((String) hm.get(keys.get(i)), charset)); content.append("&"); } content.append("sign="); content.append(sign); content.append("&sign_type=MD5"); keys.clear(); keys = null; return content.toString(); } /** * 生成md5編碼字符串. * @param str 源字符串 * @param charset 編碼方式 * @return * */ public static String md5(String str,String charset) { if (str == null) return null; char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; MessageDigest md5MessageDigest = null; byte[] md5Bytes = null; char md5Chars[] = null; byte[] strBytes = null; try { strBytes = str.getBytes(charset); md5MessageDigest = MessageDigest.getInstance("MD5"); md5MessageDigest.update(strBytes); md5Bytes = md5MessageDigest.digest(); int j = md5Bytes.length; md5Chars = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte md5Byte = md5Bytes[i]; md5Chars[k++] = hexDigits[md5Byte >>> 4 & 0xf]; md5Chars[k++] = hexDigits[md5Byte & 0xf]; } return new String(md5Chars); } catch (NoSuchAlgorithmException e) { //Log.output(e.toString(), Log.STD_ERR); return null; } catch (UnsupportedEncodingException e) { //Log.output(e.toString(), Log.STD_ERR); return null; } finally { md5MessageDigest = null; strBytes = null; md5Bytes = null; } }

    ?

    ?

    ?

    ?

    當客戶通過接口url付款后,支付寶會自動的去調用前面提供的[notify_url]參數中的url.

    (2)支付寶將付款信息返回給系統
    當客戶付款后,支付寶就會自動調用上面表單提供的[notify_url],下面是一個[notifyurl.jsp]的一個例子:

    Java代碼 ?
  • <%@?page?contentType= "text/html;charset=UTF-8" %><%@?page? import = "com.soft4j.AlipayMgr" %><%?????
  • ????String?ret?=?AlipayMgr.insert(request);?????
  • ????if (ret== null ){?????
  • ????????out.print("success" ); //成功接收支付寶發來的付款信息??? ??
  • ????}else {?????
  • ????????out.print("fail" ); //出錯??? ??
  • ????}?????
  • %>??
  • <%@ page contentType="text/html;charset=UTF-8"%><%@ page import="com.soft4j.AlipayMgr"%><% String ret = AlipayMgr.insert(request); if(ret==null){ out.print("success");//成功接收支付寶發來的付款信息 }else{ out.print("fail");//出錯 } %>

    ?

    ??

    如果確認收到支付寶發來的客戶付款信息,則返回"success",這樣子支付寶就知道系統已經收到信息了;否則返回"fail",這樣支付寶會過 一段時間后再次發來。其實,只有當支付寶收到"success"的返回信息后才會停止發送付款信息,否則會自動的每隔一段時間就調用上面
    的[notify_url]通信接口。

    (3)系統處理支付寶發來的付款信息

    Java代碼 ?
  • /*?? ?
  • ?*?Created?on?2005-6-12?? ?
  • ?*?Author?stephen?? ?
  • ?*?Email?zhoujianqiang?AT?gmail?DOT?com?? ?
  • ?*?CopyRight(C)2005-2008?,?All?rights?reserved.?? ?
  • ?*/ ????
  • package ?com.soft4j;?????
  • ????
  • import ?java.sql.Connection;?????
  • import ?java.sql.SQLException;?????
  • import ?java.util.Enumeration;?????
  • import ?java.util.Vector;?????
  • import ?javax.servlet.http.HttpServletRequest;?????
  • ????
  • /**?? ?
  • ?*?支付寶付款通知接口.?? ?
  • ?*??? ?
  • ?*?@author?stephen?? ?
  • ?*?@version?1.0.0?? ?
  • ?*/ ????
  • public ? final ? class ?NotifyUrlMgr?{?????
  • ?????????
  • ?????????
  • ????public ? static ?String?insert(HttpServletRequest?httpRequest)?{?????
  • ?????????????
  • ????????//定義變量和進行必要的初始化工作??? ??
  • ????????Enumeration?parameterNames?=?null ;?????
  • ????????String?parameterName?=?null ;?????
  • ????????String?parameterValue?=?null ;?????
  • ????????int ?count?=? 0 ;?????
  • ????????Vector[]?params?=?null ;?????
  • ????????Vector?vParameterName?=?new ?Vector();?????
  • ????????Vector?vParameterValue?=?new ?Vector();?????
  • ?????????????
  • ????????try ?{?????
  • ????????????String?orderId?=?httpRequest.getParameter("out_trade_no" ); //訂單號??? ??
  • ????????????if (orderId== null || "" .equals(orderId))?orderId= "-1" ;?????
  • ????????????parameterNames?=?httpRequest.getParameterNames();?????
  • ????????????boolean ?isPrint?=? false ;?????
  • ????????????while ?(parameterNames.hasMoreElements())?{ //循環收取支付寶發來的所有參數信息??? ??
  • ????????????????parameterName?=?(String)?parameterNames.nextElement();?????
  • ????????????????parameterValue?=?httpRequest.getParameter(parameterName);?????
  • ????????????????if (parameterValue== null )?parameterValue= "" ;?????
  • ????????????????vParameterName.add(parameterName);?????
  • ????????????????vParameterValue.add(parameterValue);?????
  • ????????????????count++;?????
  • ????????????}?????
  • ?????????????????
  • ????????????//這里添加對收到信息的處理:一般是將這些信息存入數據庫,然后對客戶的訂單進行處理.??? ??
  • ?????????????????
  • ????????????return ? null ;?????
  • ????????}?catch ?(Exception?e)?{?????
  • ????????????return ?e.toString();?????
  • ????????}?finally ?{?????
  • ????????????//??? ??
  • ????????}?????
  • ????}?????
  • ????
  • }?
  • 總結

    以上是生活随笔為你收集整理的网站集成支付宝的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。