IT人生
专注于java相关技术.
posts - 53,comments - 87,trackbacks - 0

index.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body >

<br>
<a href="<%=gateway.payment.payment.CreateUrl()%>"><img src="images/alipay_bwrx.gif" border="0"></a>

</body>
</html>

alipay_notify.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>

<%request.setCharacterEncoding("gb2312");
   String partner = ""; //partner合作伙伴id(必须填写)
   String privateKey = ""; //partner 的对应交易安全校验码(必须填写)
   String alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?service=notify_verify"
     + "&partner="
     + partner
     + "&notify_id="
     + request.getParameter("notify_id");

   //获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的
   //如果你的服务器不支持https访问的话,需要用老的接口查询地址了,论坛上会公布
   String responseTxt = gateway.checkURL.checkURL
     .check(alipayNotifyURL);

   Map params = new HashMap();
   //获得POST 过来参数设置到新的params中
   Map requestParams = request.getParameterMap();
   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 = valueStr + values[i];
    }
    params.put(name, valueStr);
   }
   
   String mysign = com.alipay.util.SignatureHelper.sign(params, privateKey);
   
   //out.println(mysign+"-----"+request.getParameter("sign"));
   if (mysign.equals(request.getParameter("sign")) || responseTxt.equals("true")  ){
    
    out.println("success");
  }
  %>

retrun_url.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>

<%request.setCharacterEncoding("gb2312");
   //String partner = ""; //partner合作伙伴id(必须填写)
   String privateKey = ""; //partner 的对应交易安全校验码(必须填写)
  // String alipayNotifyURL = "http://www.alipay.com/cooperate/gateway.do?service=notify_verify"
   //  + "&partner="
   //  + partner
   //  + "&notify_id="
   //  + request.getParameter("notify_id");

   //获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的
   //String responseTxt = gateway.checkURL.checkURL
   //  .check(alipayNotifyURL);
            //由于return url的返回通知,notifyid为空,无法到服务器查询真假,所以只要比对签名就好
   
   
   Map params = new HashMap();
   //获得POST 过来参数设置到新的params中
   Map requestParams = request.getParameterMap();
   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 = valueStr + values[i];
    }
    params.put(name, valueStr);
   }
   
   String mysign = com.alipay.util.SignatureHelper_return.sign(params, privateKey);
   
   //out.println(mysign+"-----"+request.getParameter("sign"));
   if (mysign.equals(request.getParameter("sign"))  ){
    
    
    if (request.getParameter("trade_status").equalsIgnoreCase("TRADE_FINISHED")){
            // 可以做重定向,也可以用来虚拟物品发货
    }
    
  }

  %>


SignatureHelper.java
package com.alipay.util;

import gateway.md5.Md5Encrypt;

import java.util.ArrayList;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class SignatureHelper {

 public static String sign(Map params, String privateKey) {
  Properties properties = new Properties();

  for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
   String name = (String) iter.next();
   Object value = params.get(name);
   if (value == null) {
    continue;
   }

   if (name == null || name.equalsIgnoreCase("sign")
     || name.equalsIgnoreCase("sign_type")) {
    continue;
   }

   properties.setProperty(name, value.toString());
  }

  String content = getSignatureContent(properties);
  return sign(content, privateKey);
 }

 @SuppressWarnings("unchecked")
 public static String getSignatureContent(Properties properties) {
  StringBuffer content = new StringBuffer();
  List keys = new ArrayList(properties.keySet());
  Collections.sort(keys);

  for (int i = 0; i < keys.size(); i++) {
   String key = (String) keys.get(i);
   String value = properties.getProperty(key);
   content.append((i == 0 ? "" : "&") + key + "=" + value);
  }

  return content.toString();
 }

 public static String sign(String content, String privateKey) {
  if (privateKey == null) {
   return null;
  }
  String signBefore = content + privateKey;
  // System.out.print(Md5Encrypt.md5(signBefore));
  return Md5Encrypt.md5(signBefore);

 }
}


SignatureHelper_return.java
package com.alipay.util;

import gateway.md5.Md5Encrypt;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class SignatureHelper_return {

 public static String sign(Map params, String privateKey) {
  Properties properties = new Properties();

  for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
   String name = (String) iter.next();
   Object value = params.get(name);
   if (value == null) {
    continue;
   }

   if ( name.equalsIgnoreCase("sign")
     || name.equalsIgnoreCase("sign_type")) {
    continue;
   }

   properties.setProperty(name, value.toString());
  }

  String content = getSignatureContent(properties);
  return sign(content, privateKey);
 }

 @SuppressWarnings("unchecked")
 public static String getSignatureContent(Properties properties) {
  StringBuffer content = new StringBuffer();
  List keys = new ArrayList(properties.keySet());
  Collections.sort(keys);

  for (int i = 0; i < keys.size(); i++) {
   String key = (String) keys.get(i);
   String value = properties.getProperty(key);
   content.append((i == 0 ? "" : "&") + key + "=" + value);
  }

  return content.toString();
 }

 public static String sign(String content, String privateKey) {
  if (privateKey == null) {
   return null;
  }
  String signBefore = content + privateKey;
  // System.out.print(Md5Encrypt.md5(signBefore));
  return Md5Encrypt.md5(signBefore);

 }
 
 
 
}


checkURL.java
 
package gateway.checkURL;
import java.net.*;
import java.io.*;


public class checkURL {
    /**
     * 对字符串进行MD5加密
  * @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();
   
   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;
 }


  }

Md5Encrypt.java
/**
 * Alipay.com Inc. Copyright (c) 2004-2005 All Rights Reserved.
 *
 * <p>
 * Created on 2005-7-9
 * </p>
 */
 package gateway.md5;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密算法
 */
public class Md5Encrypt {
    /**
     * 对字符串进行MD5加密
     *
     * @param text 明文
     *
     * @return 密文
     */
    public static String md5(String text) {
        MessageDigest msgDigest = null;

        try {
            msgDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("System doesn't support MD5 algorithm.");
        }

        msgDigest.update(text.getBytes());

        byte[] bytes = msgDigest.digest();

        byte   tb;
        char   low;
        char   high;
        char   tmpChar;

        String md5Str = new String();

        for (int i = 0; i < bytes.length; i++) {
            tb = bytes[i];

            tmpChar = (char) ((tb >>> 4) & 0x000f);

            if (tmpChar >= 10) {
                high = (char) (('a' + tmpChar) - 10);
            } else {
                high = (char) ('0' + tmpChar);
            }

            md5Str += high;
            tmpChar = (char) (tb & 0x000f);

            if (tmpChar >= 10) {
                low = (char) (('a' + tmpChar) - 10);
            } else {
                low = (char) ('0' + tmpChar);
            }

            md5Str += low;
        }

        return md5Str;
    }
}


payment.java
package gateway.payment;

import java.util.*;


public class payment {
 
 
  

 public static String CreateUrl()
      {  
 
    Date out_trade_no=new Date();
    String paygateway    = "https://www.alipay.com/cooperate/gateway.do?"; //'支付接口
    String service = "create_direct_pay_by_user";
    String partner   = "";  //partner合作伙伴ID(必填)
    String sign_type       =   "MD5";
    String subject = "订单号:" + out_trade_no;  //subject  商品名称
    String body = "交易地点总价包含邮费";  //body   商品描述
    String price      = "100";    //price   商品单价   0.01~50000.00
    String show_url        =   "www.alipay.com";
    String quantity        =   "1";
    String payment_type    =   "1";
    String logistics_type  =   "EXPRESS";//logistic为物流信息
    String logistics_fee   =   "2";
    String logistics_payment  =   "SELLER_PAY";
    String logistics_type_1  =   "EMS";
    String logistics_fee_1   =   "4";
    String logistics_payment_1  =   "SELLER_PAY";
    String seller_email    =    "";             //卖家账号(必填)
    String key             =    "";              //partner账户的支付宝安全校验码(必填)
    String notify_url      =    "http://10.2.17.136:8081/new_jsp/alipay_notify.jsp";//"alipay_notify.jsp"文件的地址

   
  
    String[] Oristr ={ "service="+service, "partner=" + partner, "subject=" + subject, "body=" + body, "out_trade_no=" + out_trade_no, "price=" + price, "show_url=" + show_url, "quantity=" + quantity, "payment_type=" + payment_type, "logistics_type=" + logistics_type, "logistics_fee=" + logistics_fee, "logistics_payment=" + logistics_payment, "logistics_type_1=" + logistics_type_1, "logistics_fee_1=" + logistics_fee_1, "logistics_payment_1=" + logistics_payment_1, "seller_email=" + seller_email, "notify_url=" + notify_url };
               
        Arrays.sort(Oristr);//Sorts the specified array of objects into ascending order
                     //according to the natural ordering of its elements.
      
          String prestr="";
         
          for (int i = 0; i < Oristr.length; i++)
             {
                 if (i==Oristr.length-1)
                 {
                     prestr = prestr + Oristr[i] ;
                 }
                 else
                 {
                     prestr = prestr + Oristr[i] + "&";
                 }
                 
             }

           prestr = prestr + key;

             //生成Md5摘要;
             String sign = gateway.md5.Md5Encrypt.md5(prestr);
            
             //构造支付Url;
             String parameter = "";
             parameter = parameter + paygateway;
             for (int i = 0; i < Oristr.length; i++)
             {
                 parameter = parameter + Oristr[i] + "&";              
             }

             parameter = parameter + "sign=" + sign + "&sign_type=" + sign_type;

             //返回支付Url;
             return parameter;

   
 
      }
}

注意:
包的位置,
参数排序是以字母的排序排的

转自:http://www.blogjava.net/etlan/archive/2006/06/30/55990.html



减肥瘦身品总汇     值得信赖*脉脉美妆*正品现货谢绝讲价     〓深港商盟〓名品欧衣坊(美国休闲品牌AF系列)     QQ三国游戏币及道具专卖店     小脸红红的瘦身旗舰店
posted on 2009-05-02 23:53 龙华城 阅读(777) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: