網(wǎng)站如何集成支付寶
國內(nèi)電子商務(wù)系統(tǒng)實(shí)現(xiàn)的基本流程如下:
客戶在系統(tǒng)內(nèi)下訂單 -> 系統(tǒng)根據(jù)訂單生成支付寶接口url -> 客戶通過url使用支付寶(網(wǎng)上銀行)付款 -> 支付寶將客戶的付款完成信息發(fā)送給電子商務(wù)系統(tǒng) -> 系統(tǒng)收到支付寶信息后確定客戶訂單已經(jīng)付款 -> 進(jìn)行發(fā)貨等后續(xù)流程。
在開始下面的內(nèi)容之前,你要先有一個(gè)支付寶賬戶,如果要集成支付寶接口,你還必須申請(qǐng)開通服務(wù)(關(guān)于如何開通,可以直接到支付寶網(wǎng)站上申請(qǐng)).在服務(wù)開通后,支付寶會(huì)給你2個(gè)字符串編號(hào):1個(gè)partnerId(合作伙伴ID),還有1個(gè)securityCode(安全碼).當(dāng)你拿到這2個(gè)碼的時(shí)候就可以開始下面的內(nèi)容了.
(1)如何調(diào)用支付寶接口?(將客戶的訂單信息按照既定的規(guī)則生成一個(gè)url跳轉(zhuǎn)到支付寶網(wǎng)站)
通過下面方法[makeOrderAlipayUrl(HttpServletRequest httpRequest,Order order)]的調(diào)用得到支付寶的url,然后進(jìn)行跳轉(zhuǎn)(response.sendRedirect(url);).
-
-
-
-
-
-
-
- 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");
- hm.put("logistics_fee","10");
- hm.put("logistics_payment","BUYER_PAY");
- hm.put("logistics_type","EXPRESS");
- hm.put("notify_url","http://www.xxx.com/notifyurl.jsp");//客戶付款后,支付寶調(diào)用的頁面
- hm.put("out_trade_no",order.getId());
- hm.put("partner",partnerId);
- hm.put("agent",partnerId);
- hm.put("payment_type","1");
- hm.put("price","105.30");
- hm.put("quantity","1");
- hm.put("return_url","http://www.xxx.com/ReturnUrl.jsp");//客戶付款成功后,顯示給客戶的頁面
- hm.put("seller_email","alipay@xxx.com");
- hm.put("service","create_direct_pay_by_user");
- hm.put("subject","www.xxx.com的訂單");
- String payGateway = "https://www.alipay.com/cooperate/gateway.do?";//跳轉(zhuǎn)到支付寶的url頭
- return makeUrl(hm,securityCode,httpRequest.getCharacterEncoding(),payGateway);
- }
-
-
-
-
-
-
-
-
-
-
- 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();
- }
-
-
-
-
-
-
-
-
- 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) {
-
- return null;
- } catch (UnsupportedEncodingException e) {
-
- return null;
- } finally {
- md5MessageDigest = null;
- strBytes = null;
- md5Bytes = null;
- }
- }
當(dāng)客戶通過接口url付款后,支付寶會(huì)自動(dòng)的去調(diào)用前面提供的[notify_url]參數(shù)中的url.
(2)支付寶將付款信息返回給系統(tǒng)
當(dāng)客戶付款后,支付寶就會(huì)自動(dòng)調(diào)用上面表單提供的[notify_url],下面是一個(gè)[notifyurl.jsp]的一個(gè)例子:
- <%@ 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");
- }
- %>
如果確認(rèn)收到支付寶發(fā)來的客戶付款信息,則返回"success",這樣子支付寶就知道系統(tǒng)已經(jīng)收到信息了;否則返回"fail",這樣支付寶會(huì)過一段時(shí)間后再次發(fā)來。其實(shí),只有當(dāng)支付寶收到"success"的返回信息后才會(huì)停止發(fā)送付款信息,否則會(huì)自動(dòng)的每隔一段時(shí)間就調(diào)用上面
的[notify_url]通信接口。
(3)系統(tǒng)處理支付寶發(fā)來的付款信息
import java.sql.Connection; import java.sql.SQLException; import java.util.Enumeration; import java.util.Vector; import javax.servlet.http.HttpServletRequest; 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 { } } }
這樣系統(tǒng)可以在客戶使用支付寶付款后,自動(dòng)的根據(jù)支付寶發(fā)來的付款信息確認(rèn)客戶的付款情況,并進(jìn)行相應(yīng)的后續(xù)操作.
如果你的電子商務(wù)系統(tǒng)不是java環(huán)境的,也可以參考上面的內(nèi)容。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。