<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.11.54.ALL</version>
</dependency>
/**
* 电脑网站支付
*/
public static void pcWebPay(String amount,HttpServletResponse httpResponse) {
AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl("https://www.baidu.com");
alipayRequest.setNotifyUrl("https://xxx.xxx.com/abc/alipay/pcWebPayCallBack");
alipayRequest.setBizContent("{" +
" "out_trade_no":""+System.currentTimeMillis()+""," +
" "product_code":"FAST_INSTANT_TRADE_PAY"," +
" "total_amount":"+amount+"," +
" "subject":"Iphone6 16G"," +
" "body":"Iphone6 16G"," +
" "passback_params":"123&456&789"," +
" "extend_params":{" +
" "sys_service_provider_id":""+System.currentTimeMillis()+""" +
" }"+
" }");
try {
String form = alipayClient.pageExecute(alipayRequest).getBody();
httpResponse.setContentType("text/html;charset=" + CHARSET);
httpResponse.getWriter().write(form);
httpResponse.getWriter().flush();
httpResponse.getWriter().close();
} catch (AlipayApiException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 扫码支付回调
* @paramalipayPublicKey
* @paramrequest
* @return
*/
public static void qrcodePayCallBack(String alipayPublicKey, HttpServletRequest request, HttpServletResponse response) {
Map<String, String> result = 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] + ",";
}
//乱码解决,这段代码在出现乱码时使用。
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
result.put(name, valueStr);
}
//支付宝交易凭证号
String tradeNo = result.get("trade_no");
//原支付请求的商户订单号
String outTradeNo = result.get("out_trade_no");
String appId = result.get("app_id");
String sellerId = result.get("seller_id");
String totalAmount = result.get("total_amount");
String body = result.get("body");
logger.info("tradeNo=={},outTradeNo=={},appId=={},sellerId=={},totalAmount=={},body=={}",tradeNo,outTradeNo,appId,sellerId,totalAmount,body);
//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
try {
boolean flag = AlipaySignature.rsaCheckV1(result, alipayPublicKey, CHARSET, SIGN_TYPE);
logger.info("支付宝回调结果===========" + flag);
if (flag) {
logger.info("支付宝回调success===========");
toAliPayResult = "success";
} else {
logger.info("支付宝回调failure===========");
toAliPayResult = "failure";
}
//用字节流形式打印success,解决支付宝重复回调
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(toAliPayResult.getBytes());
out.flush();
out.close();
} catch (AlipayApiException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}