对接支付开发
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
package com.ruoyi.ali;
|
||||
|
||||
|
||||
import com.alipay.api.internal.util.AlipaySignature;
|
||||
import com.ruoyi.annotation.TradePlatform;
|
||||
import com.ruoyi.constant.Platform;
|
||||
import com.ruoyi.core.ElegentValid;
|
||||
import com.ruoyi.dto.ValidResponse;
|
||||
import com.ruoyi.exceptions.TradeException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@TradePlatform(Platform.ALI)
|
||||
@Slf4j
|
||||
public class AlipayElegentValid implements ElegentValid {
|
||||
|
||||
|
||||
@Autowired
|
||||
private AlipayConfig alipayConfig;
|
||||
|
||||
|
||||
/**
|
||||
* 订单回调结果通知验签
|
||||
* 参考代码: https://opendocs.alipay.com/open/194/103296?ref=api 异步返回结果的验签
|
||||
* @param httpEntity
|
||||
* @param httpRequest
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public ValidResponse validPay(HttpEntity<String> httpEntity, HttpServletRequest httpRequest) throws TradeException {
|
||||
ValidResponse validResponse=new ValidResponse();
|
||||
try {
|
||||
Map<String, String> params = getParams(httpRequest);
|
||||
//获取支付宝POST过来反馈信息,将异步通知中收到的待验证所有参数都存放到map中
|
||||
//String body = httpEntity.getBody();
|
||||
//调用SDK验证签名
|
||||
//公钥验签示例代码
|
||||
boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayConfig.getPublicKey(), "utf-8", alipayConfig.getSignType());
|
||||
if (signVerified) {
|
||||
validResponse.setValid(true);
|
||||
validResponse.setOrderSn((String) params.get("out_trade_no"));
|
||||
return validResponse;
|
||||
} else {
|
||||
validResponse.setValid(false);
|
||||
validResponse.setOrderSn( (String) params.get("out_trade_no") );
|
||||
return validResponse;
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
throw new TradeException("验签异常");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款结果通知验签
|
||||
* 参考官网 https://opendocs.alipay.com/support/01ravh
|
||||
* @param httpEntity
|
||||
* @param httpRequest
|
||||
* @return
|
||||
* @throws TradeException
|
||||
*/
|
||||
@Override
|
||||
public ValidResponse validRefund(HttpEntity<String> httpEntity, HttpServletRequest httpRequest) throws TradeException {
|
||||
ValidResponse validResponse=new ValidResponse();
|
||||
|
||||
try {
|
||||
//获取支付宝POST过来反馈信息,将异步通知中收到的待验证所有参数都存放到map中
|
||||
Map<String, String> params = getParams(httpRequest);
|
||||
//调用SDK验证签名
|
||||
//公钥验签示例代码
|
||||
boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayConfig.getPublicKey(), "utf-8", alipayConfig.getSignType());
|
||||
if (signVerified) {
|
||||
validResponse.setValid(true);
|
||||
validResponse.setOrderSn( (String) params.get("out_trade_no") );
|
||||
return validResponse;
|
||||
} else {
|
||||
validResponse.setValid(false);
|
||||
validResponse.setOrderSn( (String) params.get("out_trade_no") );
|
||||
return validResponse;
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
validResponse.setValid(false);
|
||||
return validResponse;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String,String> getParams(HttpServletRequest httpServletRequest){
|
||||
Map<String,String> params = new HashMap< String , String >();
|
||||
Map requestParams = httpServletRequest.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");
|
||||
params.put (name,valueStr);
|
||||
}
|
||||
log.info("params:{}",params);
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String successResult() {
|
||||
return AlipayConstant.SUCCESS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String failResult() {
|
||||
return AlipayConstant.FAIL;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user