配置pom.xml
com.alipay.sdk
alipay-sdk-java
//返回给前端
public String prepay(long orderId) {
Order order=orderService.queryOrder(orderId);
AlipayClient alipayClient = new DefaultAlipayClient(“https://openapi.alipay.com/gateway.do”, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, “json”, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody(“订单:”+order.getOrder_id());
model.setSubject(“订单:”+order.getOrder_id());
model.setOutTradeNo(orderId+"");
model.setTimeoutExpress(“60m”);
model.setTotalAmount(order.getPay_cash().toString());
model.setProductCode(“QUICK_MSECURITY_PAY”);
request.setBizModel(model);
request.setNotifyUrl(AlipayConfig.notify_url);
InfluxdbClient.getInstance().createMetric("rmby").attr("action","payAlipay").attr("order_id", String.valueOf(order.getOrder_id())).send(order.getPay_cash().doubleValue());
try {
//这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
return response.getBody();
} catch (AlipayApiException e){
throw new RuntimeException(e);
}
}
回调
@RequestMapping("/notify")
public void alipayNotify(HttpServletRequest request,HttpServletResponse response){
Map<String,String> params=new HashMap<String,String>();
Map<String,String[]> requestParams=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);
}
try {
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”);//交易状态
try{
boolean verify_result=alipayService.rsaCheckV1(params);
log.info(“out_trade_no:”+out_trade_no+",trade_no:"+trade_no+",trade_status:"+trade_status+",verify_result:"+verify_result);
if(verify_result){
if(“TRADE_SUCCESS”.equals(trade_status)||“TRADE_FINISHED”.equals(trade_status)){
String total_amount=new String(request.getParameter(“total_amount”).getBytes(“ISO-8859-1”),“UTF-8”);
log.info(“out_trade_no:”+out_trade_no+",total_amount:"+total_amount);
Order order=orderService.queryOrder(Long.parseLong(out_trade_no));
if(order!=null){
//执行业务代码
}else{
throw new RuntimeException("order_id:"+out_trade_no +"not exists!");
}
}
}
}catch (RuntimeException e) {
e.printStackTrace();
log.info("out_trade_no:"+out_trade_no+",trade_no:"+trade_no+",trade_status:"+trade_status+e.getMessage());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}