支付宝 初步学习

String paygateway  // 支付接口
			String service  // //快速付款交易服务
			String sign_type = "MD5"; // 签名方式
			String out_trade_no // 商户网站订单
			String input_charset   =  "GBK";  
			String partner         // 支付宝合作伙伴id// (账户内提取)
			String key    // 支付宝安全校验码(账户内提取)
			String seller_email   // 卖家支付宝帐户
			String extra_common_param="COD2MOTOPAY"; //货单付款返红包字段
			
			以上是账户信息,以下是商品信息
			String payment_type = "1";//支付宝类型.1代表商品购买
			String subject //商品名称
			String body  // 商品描述,推荐格式:商品名称(订单编号:订单编号)
			
			String total_fee  // 订单总价			
			String show_url = "";

			String return_url  	// 支付完成后跳转返回的网址URL
			String notify_url // 支付完成后异步通知的网址URL	
			
			String paymethod = "";//赋值:bankPay(网银);cartoon(卡通); directPay(余额)//三种付款方式都要,参数为空
			String defaultbank = "";
//paygateway 的值为: https://www.alipay.com/cooperate/gateway.do?
                       

public static String CreateUrl(String paygateway, String service,
			String sign_type, String out_trade_no, String input_charset,
			String partner, String key, String show_url, String body,
			String total_fee, String payment_type, String seller_email,
			String subject, String notify_url, String return_url,
			String paymethod, String defaultbank,String token) {

		Map<String, String> params = new HashMap<String, String>();
		params.put("service", service);
		params.put("partner", partner);
		params.put("subject", subject);
		params.put("body", body);
		params.put("out_trade_no", out_trade_no);
		params.put("total_fee", total_fee);
		params.put("show_url", show_url);
		params.put("payment_type", payment_type);
		params.put("seller_email", seller_email);
		params.put("return_url", return_url);
		params.put("notify_url", notify_url);
		params.put("_input_charset", input_charset);
		params.put("paymethod", paymethod);
		params.put("defaultbank", defaultbank);
		
		if(token!=null){
			params.put("token", token);
		}
		String prestr = "";

		prestr = prestr + key;
		//System.out.println("prestr=" + prestr);

		String sign = com.homevv.cps.alipay.util.Md5Encrypt.md5(getContent(params, key));

		String parameter = "";
		parameter = parameter + paygateway;
		//System.out.println("prestr="  + parameter);
		List<String> keys = new ArrayList<String>(params.keySet());
		for (int i = 0; i < keys.size(); i++) {
			String value = params.get(keys.get(i));
			if (value == null || value.trim().length() == 0) {
				continue;
			}
			try {
				parameter = parameter + keys.get(i) + "="
						+ URLEncoder.encode(value, input_charset) + "&";
			} catch (UnsupportedEncodingException e) {

				e.printStackTrace();
			}
		}

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

		return parameter;

	}






private static String getContent(Map<String, String> params, String privateKey) {
		List<String> keys = new ArrayList<String>(params.keySet());
		Collections.sort(keys);
		String prestr = "";
		boolean first = true;
		for (int i = 0; i < keys.size(); i++) {
			String key = keys.get(i);
			String value = params.get(key);
			if (value == null || value.trim().length() == 0) {
				continue;
			}
			if (first) {
				prestr = prestr + key + "=" + value;
				first = false;
			} else {
				prestr = prestr + "&" + key + "=" + value;
			}
		}
		return prestr + privateKey;
	}

完整的:

package two;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class a1 {
	// paygateway 的值为: https://www.alipay.com/cooperate/gateway.do?

	private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
	
	public static void main(String[] args) {
		String s = CreateUrl("https://www.alipay.com/cooperate/gateway.do?", 
				"create_direct_pay_by_user", "MD5", "D130118960597", "GBK", "2088301328367423", 
				"3lemaoraz4f8brjeaifdonhok7kwm3da", "", "订单编号", "0.1", "1", "[email protected]", 
				"测试。。。等等", "", "", "directPay", "BOCB2C", null);
		
		System.out.println(s);
	}

	public static String CreateUrl(String paygateway, String service,
			String sign_type, String out_trade_no, String input_charset,
			String partner, String key, String show_url, String body,
			String total_fee, String payment_type, String seller_email,
			String subject, String notify_url, String return_url,
			String paymethod, String defaultbank, String token) {

		Map<String, String> params = new HashMap<String, String>();
		params.put("service", service);
		params.put("partner", partner);
		params.put("subject", subject);
		params.put("body", body);
		params.put("out_trade_no", out_trade_no);
		params.put("total_fee", total_fee);
		params.put("show_url", show_url);
		params.put("payment_type", payment_type);
		params.put("seller_email", seller_email);
		params.put("return_url", return_url);
		params.put("notify_url", notify_url);
		params.put("_input_charset", input_charset);
		params.put("paymethod", paymethod);
		params.put("defaultbank", defaultbank);

		if (token != null) {
			params.put("token", token);
		}
		String prestr = "";

		prestr = prestr + key;
		// System.out.println("prestr=" + prestr);

		String sign = md5(getContent(params, key));

		String parameter = "";
		parameter = parameter + paygateway;
		// System.out.println("prestr=" + parameter);
		List<String> keys = new ArrayList<String>(params.keySet());
		for (int i = 0; i < keys.size(); i++) {
			String value = params.get(keys.get(i));
			if (value == null || value.trim().length() == 0) {
				continue;
			}
			try {
				parameter = parameter + keys.get(i) + "="
						+ URLEncoder.encode(value, input_charset) + "&";
			} catch (UnsupportedEncodingException e) {

				e.printStackTrace();
			}
		}

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

		return parameter;

	}

	private static String getContent(Map<String, String> params,
			String privateKey) {
		List<String> keys = new ArrayList<String>(params.keySet());
		Collections.sort(keys);
		String prestr = "";
		boolean first = true;
		for (int i = 0; i < keys.size(); i++) {
			String key = keys.get(i);
			String value = params.get(key);
			if (value == null || value.trim().length() == 0) {
				continue;
			}
			if (first) {
				prestr = prestr + key + "=" + value;
				first = false;
			} else {
				prestr = prestr + "&" + key + "=" + value;
			}
		}
		return prestr + privateKey;
	}

	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.");
		}

		try {
			msgDigest.update(text.getBytes("GBK"));

		} catch (UnsupportedEncodingException e) {

			throw new IllegalStateException(
					"System doesn't support your  EncodingException.");

		}

		byte[] bytes = msgDigest.digest();

		String md5Str = new String(encodeHex(bytes));

		return md5Str;
	}

	public static char[] encodeHex(byte[] data) {

		int l = data.length;

		char[] out = new char[l << 1];

		// two characters form the hex value.
		for (int i = 0, j = 0; i < l; i++) {
			out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
			out[j++] = DIGITS[0x0F & data[i]];
		}

		return out;
	}
}