需要接入支付寶支付了
支付寶支付相對于微信支付接入更麻煩一些,要配置密鑰啥的
需要支付寶開放平台賬号,并開通網站支付相關權限,具體檢視官方網站
上代碼:
1 - 引入依賴
com.alipay.sdk
alipay-sdk-java
4.10.0.ALL
2 - 配置,具體參數自行設定
importcom.alipay.api.AlipayApiException;importcom.alipay.api.AlipayClient;importcom.alipay.api.DefaultAlipayClient;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;
@Configurationpublic classAlipayConfig {
@Value("${***}")privateString appId;
@Value("${***}")privateString gateway;
@Value("${***}")privateString privateKey;
@Value("${***}")privateString alipayPublicKey;
@Value("${***}")privateString charset;
@Value("${***}")privateString signType;
@Beanpublic AlipayClient alipayClient() throwsAlipayApiException {return new DefaultAlipayClient(gateway, appId, privateKey, "json", charset, alipayPublicKey, signType);
}
}
3 - 支付,參數自行寫入,傳回的是一個連結,前端直接打開就可以了,接口就不寫了
public String aliPagePay(AlipayPara para) throwsAlipayApiException {
AlipayTradePagePayRequest alipayRequest= newAlipayTradePagePayRequest();
alipayRequest.setNotifyUrl(aliNotifyUrl);
JSONObject job= newJSONObject();job.put("out_trade_no", uuid());
job.put("product_code", para.getProductCode());
job.put("total_amount", para.getTotalAmount().toString());
job.put("subject", para.getSubject());
alipayRequest.setBizContent(job.toJSONString());
// 記錄支付單相關操作,代碼省略AlipayResponse response= alipayClient.pageExecute(alipayRequest, "GET");returnresponse.getBody();
}
4 - 異步通知,驗簽,同步沒用到,就不寫了
public void aliPagePayNotifyMethod(HttpServletRequest request) throwsAlipayApiException, SQLException {
// 驗簽if (!aliPagePayRsaCheck(request)) {throw new AlipayApiException("AliPagePay notify Rsa Check Failed");
}
String outTradeNo= getParameter(request, "out_trade_no");
// 由于我支付時建立了支付單,這裡查詢支付單if (order == null) {throw newSQLException();
}
String tradeStatus= getParameter(request, "trade_status");
// 支付成功,可能回調多次if ("TRADE_FINISHED".equals(tradeStatus) || "TRADE_SUCCESS".equals(tradeStatus)) {//查訂單,已支付就不處理了
if (order.isPay()) {return;
}
String totalAmount= getParameter(request, "total_amount");
// 判斷金額是否一緻if (order.getAmount().compareTo(new BigDecimal(totalAmount)) != 0) {throw newAlipayApiException();
}
String tradeNo= getParameter(request, "trade_no");
// 記錄交易号
}else{throw newAlipayApiException();
}
}
5 - 驗簽方法
private boolean aliPagePayRsaCheck(HttpServletRequest request) throwsAlipayApiException {
Map maps = new HashMap<>();
Map requestParams =request.getParameterMap();for(String name : requestParams.keySet()) {
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] + ",";
}
maps.put(name, valueStr);
}returnAlipaySignature.rsaCheckV1(maps, alipayPublicKey, charset, signType);
}
6 - 擷取參數方法 - 處理亂碼問題 - 有可能 - 沒具體測試,參考網上代碼加的
privateString getParameter(HttpServletRequest request, String paramName) {returnhandleEncodeingError(request.getParameter(paramName));
}privateString handleEncodeingError(String str) {return newString(str.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}
7 - 異步接口
// 注入
@RequestMapping(value = "***")public void aliPayNotify(HttpServletRequest request, HttpServletResponse response) throwsIOException {try{
payService.aliPagePayNotifyMethod(request);
response.getWriter().println("success");
}catch(Exception e) {
// 記錄下日志
response.getWriter().println("fail");
}
}
至此支付寶支付接入完成,經過測試,注意參數配置.
原文:https://www.cnblogs.com/SamNicole1809/p/13564101.html