1. 配置
AlipayConfig.java
// 请求网关地址
public static String PRO_URL = "https://openapi.alipay.com/gateway.do";
// 编码
public static String CHARSET = "UTF-8";
// 返回格式
public static String FORMAT = "json";
// RSA2
public static String SIGNTYPE = "RSA2";
// 风韵乘客 appid
public static String APP_ID = "2018011901980871";
// 风韵乘客 私钥 pkcs8格式的
public static String PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCaKk6MN+SgbUzW7nxaK25Oe/oM4qBHTtfHCM65EVjT8xJjcGzw9x5p7K7rA1NpklLZFg8LMBunmJZW+0sfs1HX70SUS6s3CTwD2JPtXA1B0vRR0NTXVzsYTStMierEjMRthqSN2ae/7xQJq+DtO1QnycPCjihKCXyujnkw4wO4d/7HDbsR5U3tHVMRV7VcJK5zI0z+mqgT2zyJiATT8RCGnCG/8EEULFp68S2NBduCYHzA7DL/mX2X0xne70c9qFo4oyFXttfOxJZ1v41GOJX8XIBuDNwS7T+PYYoyylGiKDAugz6aykJ0OwiCDrGCSCcG0Jp3qfeQJciHZ25LNlVjAgMBAAECggEAY7TNDF7x3NgUBEZF4sOIGfaQ6FiCNNxHLhECMiuH+byRvgJuUwJghz1ue8yw2+yo7IIZxLCekixbv3HTSnNdkXzMK+UsnPG6APrH4ZHReSsInQmlPojKTZNLV7szPV+Vgwi70IpetgHYQdEKLiRa70jM9T2fzRKJsxOuSfHo7BqdAla8bN09YlJY+W/skpQswunu91PBQgBSGllKXNHydc/6x/Ts7kGEP86OyIrKR4DEpiNQ0S1wdFfMEl6Yrn9CAuzn4S+D4V0gRj94aXzqBIXUNpqPh/sXylkI/TDbLKDUPHHGW23ZIvzMF3BmVdEnyI8d81ewQw32VcUMDVZswQKBgQDdzgXGCRq+oJXE9GAJLyl5jZyAWi/LXQ5b6eubbyJWLYJhQopW36IZN4zz3jpMFPT5b+HFStTsLLkInT2qPpEWpID7E+vunHwG8RPaab8YyKksoSUF2ESET5580e8M5bGxyFVCBrcm5KEzA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BEHprlDv88arWf/ZtJ3gAVlIwyHYuCuSZqMZP62D3xM9U9uJp7fc5BxerC5IS7EipGWXWmAarkfsffK/t460TvGh1sLTCe1wwIDAQAB";
application.yml
alipay:
passenger:
pay:
return_url: http://fy-share.natapp1.cc/#/paysuccess/
notify_url: http://fy-gateway.natapp4.cc/driver-service/notify
2. 工具类
AlipayUtils.java
@Slf4j
public class AlipayUtils {
@Value("${alipay.passenger.pay.return_url}")
private String return_url;
@Value("${alipay.passenger.pay.notify_url}")
private String notify_url;
public static AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.PRO_URL, AlipayConfig.APP_ID, AlipayConfig.PRIVATE_KEY, AlipayConfig.FORMAT,
AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
/**
* 支付宝支付
*/
public static void ali_pay(Integer orderId, HttpServletResponse httpResponse) throws Exception{
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
alipayRequest.setReturnUrl(return_url+orderId);//支付成功同步通知页面
alipayRequest.setNotifyUrl(notify_url);//异步回调
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddmmssSSS");
String date = sdf.format(new Date());
alipayRequest.setBizContent("{" +" \"out_trade_no\":\""+date+"\"," +
" \"total_amount\":\"0.1\"," +
" \"subject\":\"张学亮测试支付宝支付\"," +
" \"product_code\":\"QUICK_WAP_PAY\"" +
" }");//填充业务参数
String form="";
try {
form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
} catch (AlipayApiException e) {
e.printStackTrace();
}
httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
httpResponse.getWriter().flush();
httpResponse.getWriter().close();
}
}
3. Controller
前端调用此接口,唤起支付宝客户端App
@RequestMapping("gotoPay")
public void gotoPay(Integer orderId,HttpServletResponse response){
try {
AlipayUtils.ali_pay(orderId,response);
} catch (Exception e) {
e.printStackTrace();
}
}
支付宝异步回调接口
@PostMapping("notify")
public String notify(HttpServletRequest request,HttpServletResponse response)throws Exception{
//获取支付宝POST过来反馈信息
Map<String,String> params = new HashMap<String,String>();
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] + ",";
}
//乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
params.put(name, valueStr);
}
//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)//
//商户订单号
String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//支付宝交易号
String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
//交易状态
String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");
//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//
//计算得出通知验证结果
//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
boolean verify_result = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, "RSA2");
if(verify_result){//验证成功
//
//请在这里加上商户的业务逻辑程序代码
//——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
if(trade_status.equals("TRADE_FINISHED")){//签约的不是可退款协议
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//如果有做过处理,不执行商户的业务程序
//注意:
//如果签约的是可退款协议,退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
//如果没有签约可退款协议,那么付款完成后,支付宝系统发送该交易状态通知。
} else if (trade_status.equals("TRADE_SUCCESS")){//签约的是可退款协议
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//如果有做过处理,不执行商户的业务程序
System.out.println("=====异步通知TRADE_SUCCESS:支付成功=====");
// response.sendRedirect("http://fy-share.natapp1.cc/#/paysuccess/500079723");
//注意:
//如果签约的是可退款协议,那么付款完成后,支付宝系统发送该交易状态通知。
}
return "success";
// out.clear();
// out.println("success"); //请不要修改或删除
// System.out.print("异步通知验证成功!!!!!!!!!!!!!!!!!===");
//
}else{//验证失败
// out.println("fail");
System.out.print("异步通知验证失败!!!!!!!!!!!!!!!!!");
return "fail";
}
}