step-by-step集成阿里巴巴支付寶接口

關於支付寶的一些信息可以直接訪問支付寶網站https://www.alipay.com/。

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

其實這個流程與以前講paypal接口的基本類似,都是爲了實現訂單的自動付款確認。

paypal與支付寶在使用時候有一點區別:paypal接口是直接通過一個form提交給paypal網站;而支付寶是通過一個url跳轉到支付寶網站的。

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

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

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

Java代碼
  1. /**  
  2.      * 根據訂單生成支付寶接口URL.  
  3.      * @param httpRequest  
  4.      * @param order 訂單實例  
  5.      * @return  
  6.      * @throws Exception  
  7.      */  
  8.     public static String makeOrderAlipayUrl(HttpServletRequest httpRequest,Order order) throws Exception {   
  9.         HashMap hm = new HashMap();   
  10.         hm.put("_input_charset",httpRequest.getCharacterEncoding());//採用相同的編碼方式   
  11.         hm.put("body","您在www.xxx.com上的訂單");//填寫在跳到支付寶頁面上顯示的付款內容信息   
  12.         hm.put("discount","-5");//填寫折扣信息 -5表示抵扣5元   
  13.         hm.put("logistics_fee","10");//物流費用   
  14.         hm.put("logistics_payment","BUYER_PAY");//物流費用支付人 BUYER_PAY=買家支付物流費用   
  15.         hm.put("logistics_type","EXPRESS");//物流方式   
  16.         hm.put("notify_url","http://www.xxx.com/notifyurl.jsp");//客戶付款後,支付寶調用的頁面   
  17.         hm.put("out_trade_no",order.getId());//外部交易號,最好具有唯一性,在獲取支付寶發來的付款信息時使用.   
  18.         hm.put("partner",partnerId);//partnerId(合作伙伴ID)   
  19.         hm.put("agent",partnerId);//partnerId(合作伙伴ID)   
  20.         hm.put("payment_type","1");//支付類型 1=商品購買,2=服務購買,...   
  21.         hm.put("price","105.30");//訂單金額信息   
  22.         hm.put("quantity","1");//訂單商品數量,一般都是寫1,它是按照整個訂單包來計算   
  23.         hm.put("return_url","http://www.xxx.com/ReturnUrl.jsp");//客戶付款成功後,顯示給客戶的頁面   
  24.         hm.put("seller_email","[email protected]");//你的支付寶賬戶email   
  25.         hm.put("service","create_direct_pay_by_user");//create_direct_pay_by_user=直接付款,trade_create_by_buyer 擔保付款    
  26.         hm.put("subject","www.xxx.com的訂單");//填寫在跳到支付寶頁面上顯示的付款標題信息   
  27.         String payGateway = "https://www.alipay.com/cooperate/gateway.do?";//跳轉到支付寶的url頭   
  28.         return makeUrl(hm,securityCode,httpRequest.getCharacterEncoding(),payGateway);//securityCode(安全碼)    
  29.     }   
  30.        
  31.        
  32.     /**  
  33.      * 根據傳入的參數生成alipay的支付URL  
  34.      * @param hm 參數值  
  35.      * @param securityCode 安全碼  
  36.      * @param charset 編碼  
  37.      * @param payGateway 支付寶gateway  
  38.      * @return  
  39.      */  
  40.     public static String makeUrl(HashMap hm,String securityCode,String charset,String payGateway) throws Exception{   
  41.         List keys = new ArrayList(hm.keySet());   
  42.         Collections.sort(keys);//支付寶要求參數必須按字母排序   
  43.         StringBuffer content = new StringBuffer();   
  44.         for (int i = 0; i < keys.size(); i++) {   
  45.             content.append((String) keys.get(i));   
  46.             content.append("=");   
  47.             content.append((String) hm.get((String) keys.get(i)));   
  48.             if (i != keys.size() - 1) {   
  49.                 content.append("&");   
  50.             }   
  51.         }   
  52.         content.append(securityCode);   
  53.         String sign = md5(content.toString(),charset);   
  54.         content.delete(0,content.length());   
  55.         content.append(payGateway);   
  56.         for (int i = 0; i < keys.size(); i++) {   
  57.             content.append(keys.get(i));   
  58.             content.append("=");   
  59.             content.append(URLEncoder.encode((String) hm.get(keys.get(i)), charset));   
  60.             content.append("&");   
  61.         }   
  62.         content.append("sign=");   
  63.         content.append(sign);   
  64.         content.append("&sign_type=MD5");   
  65.         keys.clear();   
  66.         keys = null;   
  67.         return content.toString();   
  68.     }   
  69.        
  70.     /**  
  71.      * 生成md5編碼字符串.  
  72.      * @param str 源字符串  
  73.      * @param charset 編碼方式  
  74.      * @return  
  75.      *  
  76.      */  
  77.     public static String md5(String str,String charset) {   
  78.         if (str == null)   
  79.             return null;   
  80.         char hexDigits[] = { '0''1''2''3''4''5''6''7''8''9',   
  81.                 'a''b''c''d''e''f' };   
  82.            
  83.         MessageDigest md5MessageDigest = null;   
  84.         byte[] md5Bytes = null;   
  85.         char md5Chars[] = null;   
  86.         byte[] strBytes = null;   
  87.         try {   
  88.             strBytes = str.getBytes(charset);   
  89.             md5MessageDigest = MessageDigest.getInstance("MD5");   
  90.             md5MessageDigest.update(strBytes);   
  91.             md5Bytes = md5MessageDigest.digest();   
  92.             int j = md5Bytes.length;   
  93.             md5Chars = new char[j * 2];   
  94.             int k = 0;   
  95.             for (int i = 0; i < j; i++) {   
  96.                 byte md5Byte = md5Bytes[i];   
  97.                 md5Chars[k++] = hexDigits[md5Byte >>> 4 & 0xf];   
  98.                 md5Chars[k++] = hexDigits[md5Byte & 0xf];   
  99.             }   
  100.             return new String(md5Chars);   
  101.         } catch (NoSuchAlgorithmException e) {   
  102.             //Log.output(e.toString(), Log.STD_ERR);   
  103.             return null;   
  104.         } catch (UnsupportedEncodingException e) {   
  105.             //Log.output(e.toString(), Log.STD_ERR);   
  106.             return null;   
  107.         } finally {   
  108.             md5MessageDigest = null;   
  109.             strBytes = null;   
  110.             md5Bytes = null;   
  111.         }   
  112.     }  
/**
     * 根據訂單生成支付寶接口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","[email protected]");//你的支付寶賬戶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]的一個例子:

Html代碼
  1. <%@ page contentType="text/html;charset=UTF-8"%><%@ page import="com.soft4j.AlipayMgr"%><%   
  2.     String ret = AlipayMgr.insert(request);   
  3.     if(ret==null){   
  4.         out.print("success");//成功接收支付寶發來的付款信息   
  5.     }else{   
  6.         out.print("fail");//出錯   
  7.     }   
  8. %>  
<%@ 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代碼
  1. /*  
  2.  * Created on 2005-6-12  
  3.  * Author stephen  
  4.  * Email zhoujianqiang AT gmail DOT com  
  5.  * CopyRight(C)2005-2008 , All rights reserved.  
  6.  */  
  7. package com.soft4j;   
  8.   
  9. import java.sql.Connection;   
  10. import java.sql.SQLException;   
  11. import java.util.Enumeration;   
  12. import java.util.Vector;   
  13. import javax.servlet.http.HttpServletRequest;   
  14.   
  15. /**  
  16.  * 支付寶付款通知接口.  
  17.  *   
  18.  * @author stephen  
  19.  * @version 1.0.0  
  20.  */  
  21. public final class NotifyUrlMgr {   
  22.        
  23.        
  24.     public static String insert(HttpServletRequest httpRequest) {   
  25.            
  26.         //定義變量和進行必要的初始化工作   
  27.         Enumeration parameterNames = null;   
  28.         String parameterName = null;   
  29.         String parameterValue = null;   
  30.         int count = 0;   
  31.         Vector[] params = null;   
  32.         Vector vParameterName = new Vector();   
  33.         Vector vParameterValue = new Vector();   
  34.            
  35.         try {   
  36.             String orderId = httpRequest.getParameter("out_trade_no");//訂單號   
  37.             if(orderId==null||"".equals(orderId)) orderId="-1";   
  38.             parameterNames = httpRequest.getParameterNames();   
  39.             boolean isPrint = false;   
  40.             while (parameterNames.hasMoreElements()) {//循環收取支付寶發來的所有參數信息   
  41.                 parameterName = (String) parameterNames.nextElement();   
  42.                 parameterValue = httpRequest.getParameter(parameterName);   
  43.                 if(parameterValue==null) parameterValue="";   
  44.                 vParameterName.add(parameterName);   
  45.                 vParameterValue.add(parameterValue);   
  46.                 count++;   
  47.             }   
  48.                
  49.             //這裏添加對收到信息的處理:一般是將這些信息存入數據庫,然後對客戶的訂單進行處理.   
  50.                
  51.             return null;   
  52.         } catch (Exception e) {   
  53.             return e.toString();   
  54.         } finally {   
  55.             //   
  56.         }   
  57.     }   
  58.   
  59. }  
/*
 * 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 {
            //
        }
    }

}

http://stephen830.javaeye.com/blog/254827

這樣系統可以在客戶使用支付寶付款後,自動的根據支付寶發來的付款信息確認客戶的付款情況,並進行相應的後續操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章