java后台支付宝app支付调用sdk进行支付
支付宝sdk
创建一个支付宝的util类
public class AlipayUtil {
public static final String APP_ID = "2016091100486489";//这个支付id为我的沙箱的app_id
private static final String APP_PRIVATE_KEY = "************";//支付宝的私钥
public static final String ALIPAY_PUBLIC_KEY = "***********";//这个为你在支付宝上的商户的公钥,而不是支付宝公钥
public static final String ALIPAY_ZHI_KEY ="**********";//这个是支付宝的公钥
public static final String SIGNTYPE = "RSA2";//算法
public static final String NOTIFYURL = "http://trivel.free.ngrok.cc/***";//支付宝回调的借口
private static AlipayClient alipayClient = null;
因为支付宝alipayClient本身是线程安全的,因此只用创建一个,创建成单例的模式
public static AlipayClient getAlipayClient() {
if (alipayClient == null) {
synchronized (AlipayUtil.class) {//AlipayConstants.CHARSET_UTF8
if (null == alipayClient) {//https://openapi.alipay.com/gateway.do https://openapi.alipaydev.com/gateway.do
alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID,
APP_PRIVATE_KEY, AlipayConstants.FORMAT_JSON,AlipayConstants.CHARSET_UTF8,
ALIPAY_ZHI_KEY,SIGNTYPE);
}
}
}
return alipayClient;
}
}
使用支付宝的sdk,给app端发送一个签名
public Map<String,String> alipayTradeAppPay() throws AlipayApiException, UnsupportedEncodingException{
AlipayTradeAppPayRequest aRequest = new AlipayTradeAppPayRequest();HashMapresult = new HashMap();
Map<String,String> result = new HashMap<String,String>();
AlipayTradeAppPayModel amodel = new AlipayTradeAppPayModel();
amodel.setBody("测试数据");
amodel.setSubject("名称");
amodel.setGoodsType("1");//0虚拟产品1实物
amodel.setOutTradeNo("产品的订单号");
amodel.setProductCode("QUICK_MSECURITY_PAY");
amodel.setTotalAmount("总钱数,注意应为double类型");
amodel.setTimeoutExpress("30m");//支付宝支付的时间设置
aRequest.setBizModel(amodel);
aRequest.setNotifyUrl(AlipayUtil.NOTIFYURL);//设置回调的接口
AlipayTradeAppPayResponse aResponse = AlipayUtil.getAlipayClient().sdkExecute(aRequest);
if(aResponse.isSuccess()){
result.put("result", aResponse.getBody());
result.put("status", "0");
result.put("msg","订单生成成功");
} else {
result.put("status", "1");
result.put("msg", "订单生成失败");
}
return result;
}
给APP端返回的result,app端不用再去编码,因为其已经是编码过得,否则会起吊客户端支付宝失败
支付宝回调的接口,当支付完成时,支付宝会回调这个接口,并进行签名的认证
public String alipayTradeNotifyUrl(HttpServletRequest request,HttpServletResponse response){
Mapparams = new HashMap();
//1.从支付宝回调的request域中取值
MaprequestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = iter.next();
String[] values = requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
// 乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
// valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
params.put(name, valueStr);
}
//2.封装必须参数
String tradeStatus = request.getParameter("trade_status"); //交易状态
//3.签名验证(对支付宝返回的数据验证,确定是支付宝返回的)
boolean signVerified = false;
try {
//3.1调用SDK验证签名
signVerified = AlipaySignature.rsaCheckV1(params,AlipayUtil.ALIPAY_ZHI_KEY, AlipayConstants.CHARSET_UTF8, AlipayUtil.SIGNTYPE);
} catch (AlipayApiException e) {
e.printStackTrace();
}
//4.对验签进行处理
if (signVerified) { //验签通过
if(tradeStatus.equals("TRADE_SUCCESS")) {
System.err.println("验签失败1");
//只处理支付成功的订单: 修改交易表状态,支付成功
return "success";
}else{
return "fail";
}
} else { //验签不通过
return "fail";
}
}