支付宝APP支付生成签名 - JAVA

/ 编程技术 / 没有评论 / 2210浏览

作者:玉米


首先第一步

下载阿里支付宝SDK(JAVA)版: 点我进入阿里巴巴支付官网下载

下载地址:https://docs.open.alipay.com/54/106370

创建一个类:AlipayConfig 以下是源代码:

import java.util.HashMap;
import java.util.Map;

public class AlipayConfig {

/** 签名编码 */

public static final String SIGN_CHARSET = "utf-8";

/** 字符编码 */
public static final String CHARSET = "utf-8";

/** 签名类型  RSA 或者RSA2*/
public static final String SIGN_TYPE = "RSA2";

public static final String APPID = "你的APPID";

/** 应用私钥 */
public static final String PRIVATE_KEY = "你的应用公钥";	
/** 支付宝公钥 */
public static final String ALIPAY_PUBLIC_KEY = "你的支付宝公钥";

/** 支付宝网关 (固定值)*/
public static final String ALIPAY_GATEWAY = "https://openapi.alipay.com/gateway.do";

/** 支付宝回调地址 */
public static final String NOTIFY_URL = "你的服务器回调地址";

public static final Map<String, String> errMap = new HashMap<String, String>();
static {
	errMap.put("ACQ.SYSTEM_ERROR", "系统错误");
	errMap.put("ACQ.INVALID_PARAMETER", "参数无效");
	errMap.put("ACQ.SELLER_BALANCE_NOT_ENOUGH", "卖家余额不足");
	errMap.put("ACQ.REFUND_AMT_NOT_EQUAL_TOTAL", "退款金额超限");
	errMap.put("ACQ.REASON_TRADE_BEEN_FREEZEN", "请求退款的交易被冻结");
	errMap.put("ACQ.TRADE_NOT_EXIST", "交易不存在");
	errMap.put("ACQ.TRADE_HAS_FINISHED", "交易已完结");
	errMap.put("ACQ.TRADE_STATUS_ERROR", "交易状态非法");
	errMap.put("ACQ.DISCORDANT_REPEAT_REQUEST", "不一致的请求");
	errMap.put("ACQ.REASON_TRADE_REFUND_FEE_ERR", "退款金额无效");
	errMap.put("ACQ.TRADE_NOT_ALLOW_REFUND", "当前交易不允许退款");
   }

}

然后建立一个SignUtils

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConstants;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.internal.util.json.JSONWriter;

public class SignUtils {

	private static final String DEFAULT_CHARSET = "utf-8";

	private static String getBizContent(String orderNo, String subject, String merchName, BigDecimal amount) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("timeout_express", "12h");

		map.put("product_code", "QUICK_MSECURITY_PAY");

		map.put("total_amount", amount.toString());

		map.put("subject", subject);

		map.put("body", merchName);

		map.put("out_trade_no", orderNo);

		JSONWriter json = new JSONWriter();
		String bizJson = json.write(map, true);
		System.out.println(bizJson);
		return bizJson.toString();
	}

	public static Map<String, String> getAliyPayParam(String payno, String subject, String merchName, BigDecimal amount)
			throws AlipayApiException {
		Map<String, String> params = new HashMap<>();
		params.put("app_id", AlipayConfig.APPID);
		String biz_content = getBizContent(payno, subject, merchName, amount);
		params.put("biz_content", biz_content);
		params.put("charset", DEFAULT_CHARSET);
		params.put("format", "json");
		params.put("method", "alipay.trade.app.pay");

		params.put("notify_url", AlipayConfig.NOTIFY_URL);
		params.put("sign_type", AlipayConstants.SIGN_TYPE_RSA2);

		params.put("timestamp", "2018-03-15 10:00:00");
		params.put("version", "1.0");
		return params;
	}

	/**
	 * 构造支付订单参数信息
	 * 
	 * @param map
	 *            支付订单参数
	 * @return
	 */
	public static String buildOrderParam(Map<String, String> map) {
		List<String> keys = new ArrayList<String>(map.keySet());

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < keys.size() - 1; i++) {
			String key = keys.get(i);
			String value = map.get(key);
			sb.append(buildKeyValue(key, value, true));
			sb.append("&");
		}

		String tailKey = keys.get(keys.size() - 1);
		String tailValue = map.get(tailKey);
		sb.append(buildKeyValue(tailKey, tailValue, true));

		return sb.toString();
	}

	/**
	 * 拼接键值对
	 * 
	 * @param key
	 * @param value
	 * @param isEncode
	 * @return
	 */
	private static String buildKeyValue(String key, String value, boolean isEncode) {
		StringBuilder sb = new StringBuilder();
		sb.append(key);
		sb.append("=");
		if (isEncode) {
			try {
				sb.append(URLEncoder.encode(value, "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				sb.append(value);
			}
		} else {
			sb.append(value);
		}
		return sb.toString();
	}

	/**
	 * 获得签名
	 * @param map 待签名内容
	 * @param rsaKey 应用私钥
	 * @param signType 加密类型
	 * @return 签名
	 */
	public static String getSign(Map<String, String> map, String rsaKey, String signType) throws AlipayApiException {
		List<String> keys = new ArrayList<String>(map.keySet());
		// key排序
		Collections.sort(keys);

		StringBuilder authInfo = new StringBuilder();
		for (int i = 0; i < keys.size() - 1; i++) {
			String key = keys.get(i);
			String value = map.get(key);
			authInfo.append(buildKeyValue(key, value, false));
			authInfo.append("&");
		}

		String tailKey = keys.get(keys.size() - 1);
		String tailValue = map.get(tailKey);
		authInfo.append(buildKeyValue(tailKey, tailValue, false));

		String oriSign = AlipaySignature.rsaSign(AlipaySignature.getSignContent(map), AlipayConfig.PRIVATE_KEY,
				AlipayConfig.CHARSET, signType);
		String encodedSign = "";

		try {
			encodedSign = URLEncoder.encode(oriSign, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "sign=" + encodedSign;
	}

	/**
	 * 拼装最后的订单签名
	 * 
	 * @param payno
	 *            支付订单号
	 * @param subject
	 *            购买描述
	 * @param merchName
	 *            商品名称
	 * @param amount
	 *            金额
	 * @return
	 */
	public static String getAliySignOrderStr(String payno, String subject, String merchName, BigDecimal amount)
			throws UnsupportedEncodingException, AlipayApiException {
		// 构造请求参数
		Map<String, String> params = getAliyPayParam(payno, subject, merchName, amount);
		// 拼接签名字符串
		String parmStr = buildOrderParam(params);

		String orderInfo = parmStr + "&" + getSign(params, AlipayConfig.PRIVATE_KEY, AlipayConstants.SIGN_TYPE_RSA2);

		return orderInfo;
	}

}

以上就是生成APP支付签名的全部内容。