java对接支付宝支付
@RequestMapping("/test")
public void doPost(HttpServletRequest httpRequest,
HttpServletResponse httpResponse) throws ServletException, IOException {
//商户号
String APP_ID="";
//私钥
String APP_PRIVATE_KEY="";
//编码
String CHARSET="UTF-8";
//公钥
String ALIPAY_PUBLIC_KEY="";
//我是沙箱环境 所有请求地址是沙箱版
AlipayClient alipayClient = new DefaultAlipayClient("/", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//这里主要是设置回调 也就是支付成功之后你要做什么操作的地址
alipayRequest.setReturnUrl("/CallBack/return_url.jsp");
alipayRequest.setNotifyUrl("/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
/**
* @description:
* @author: hwk
* @date: 2022/6/14 16:34
* @param: out_trade_no 订单编号
* total_amount 付款金额
* subject 商品名称
* product_code 支付方式 如手机网站 pc网站 APP支付等
**/
JSONObject jsonObject = new JSONObject();
jsonObject.put("out_trade_no", "264484811515156");
jsonObject.put("total_amount", "66");
jsonObject.put("subject", "测试支付");
jsonObject.put("product_code", "QUICK_WAP_WAY");
alipayRequest.setBizContent(jsonObject.toJSONString());
String form="";
try {
form = alipayClient.pageExecute(alipayRequest).getBody();
} catch (AlipayApiException e) {
e.printStackTrace();
}
//因为我不会写前端 所以直接输出测试 如果在正式环境中 建议直接return 返回的from表单
httpResponse.setContentType("text/html;charset=" + CHARSET);
httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
httpResponse.getWriter().flush();
httpResponse.getWriter().close();
}
//支付回调
@RequestMapping("/test1")
public void aliPayNotify() {
Map<String, String> params = getAllRequestParam(request);
String tradeStatus = params.get("trade_status");
if ("TRADE_SUCCESS".equals(tradeStatus)) {
System.out.println("支付成功");
}else {
System.out.println("支付失败");
}
}
private Map<String, String> getAllRequestParam(final HttpServletRequest request) {
Map<String, String> res = new HashMap<>(8);
Enumeration<?> temp = request.getParameterNames();
while (temp.hasMoreElements()) {
String en = (String) temp.nextElement();
String value = request.getParameter(en);
res.put(en, value);
}
return res;
}