支付宝支付常量配置 AlipayConfig
public class AlipayConfig {
public static final String APPID = "";
public static final String notify_url = "";
public static final String return_url = "";
public static final String URL = "https://openapi.alipaydev.com/gateway.do";
public static final String CHARSET = "UTF-8";
public static final String FORMAT = "json";
public static final String ALIPAY_PUBLIC_KEY = "";
public static final String APP_PRIVATE_KEY = "";
public static final String SIGNTYPE = "RSA2";
}
支付接口 AliPayAppService
public interface AliPayAppService {
public String getAliPayOrderStr(T_order t_order);
public String notify(Map<String, String> conversionParams);
public int checkAlipay(String order_no);
public String alipayTradeRefund(String order_no, Double refund_fee);
支付实现 AliPayAppServiceImpl
@Service
public class AliPayAppServiceImpl implements AliPayAppService {
private static final Logger logger = Logger.getLogger(AliPayAppServiceImpl.class);
@Autowired
private IT_alipayment_orderService alipayment_orderService;
@Override
@Transactional(propagation = Propagation.REQUIRED)
public String getAliPayOrderStr(T_order t_order) {
String orderString = "";
logger.info("支付宝下单,商户订单号为:" + t_order.getOrder_no());
T_alipayment_order alipaymentOrder = new T_alipayment_order();
if (t_order.getOrder_id() != null) {
alipaymentOrder.setOrder_id(t_order.getOrder_id().longValue());
}
alipaymentOrder.setOut_trade_no(t_order.getOrder_no().toString());
alipaymentOrder.setTrade_status(0);
alipaymentOrder.setTotal_amount(t_order.getOrder_money());
alipaymentOrder.setReceipt_amount(0.00);
alipaymentOrder.setBuyer_pay_amount(0.00);
alipaymentOrder.setRefund_fee(0.00);
try {
AlipayClient alipayClient = new DefaultAlipayClient(AlipayAppConfig.URL, AlipayAppConfig.APPID,
AlipayAppConfig.APP_PRIVATE_KEY, AlipayAppConfig.FORMAT, AlipayAppConfig.CHARSET,
AlipayAppConfig.ALIPAY_PUBLIC_KEY, AlipayAppConfig.SIGNTYPE);
AlipayTradeAppPayRequest ali_request = new AlipayTradeAppPayRequest();
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setSubject("该订单");
model.setOutTradeNo(t_order.getOrder_no());
model.setTotalAmount(String.valueOf(t_order.getOrder_money()));
model.setProductCode("QUICK_MSECURITY_PAY");
ali_request.setBizModel(model);
ali_request.setNotifyUrl(AlipayAppConfig.notify_url);
AlipayTradeAppPayResponse alipayTradeAppPayResponse = alipayClient.sdkExecute(ali_request);
orderString = alipayTradeAppPayResponse.getBody();
if ( alipayment_orderService.selectCount(new EntityWrapper<T_alipayment_order>()
.eq("out_trade_no", t_order.getOrder_no().toString())) == 0 ) {
alipayment_orderService.createAlipayMentOrder(alipaymentOrder);
}
} catch (AlipayApiException e) {
e.printStackTrace();
logger.info("与支付宝交互出错,未能生成订单,请检查代码!");
JsonUtil.getResponseJson(ReturnCode.EXCEPTION_CODE, "返回数据失败", null, null);
}
return JsonUtil.getResponseJson(ReturnCode.SUCCSEE_CODE, "返回数据成功", null, orderString);
}
@Override
public int checkAlipay(String order_no) {
logger.info("向支付宝发起查询,查询商户订单号为:" + order_no);
try {
AlipayClient alipayClient = new DefaultAlipayClient(AlipayAppConfig.URL, AlipayAppConfig.APPID,
AlipayAppConfig.APP_PRIVATE_KEY, AlipayAppConfig.FORMAT, AlipayAppConfig.CHARSET,
AlipayAppConfig.ALIPAY_PUBLIC_KEY, AlipayAppConfig.SIGNTYPE);
AlipayTradeQueryRequest alipayTradeQueryRequest = new AlipayTradeQueryRequest();
alipayTradeQueryRequest.setBizContent("{" + "\"out_trade_no\" : \"" + order_no + "\"}");
AlipayTradeQueryResponse alipayTradeQueryResponse = alipayClient.execute(alipayTradeQueryRequest);
if (alipayTradeQueryResponse.isSuccess()) {
T_alipayment_order alipaymentOrder = alipayment_orderService.selectByOutTradeNo(order_no);
switch (alipayTradeQueryResponse.getTradeStatus())
{
case "TRADE_FINISHED":
alipaymentOrder.setTrade_status(3);
break;
case "TRADE_SUCCESS":
alipaymentOrder.setTrade_status(2);
break;
case "TRADE_CLOSED":
alipaymentOrder.setTrade_status(1);
break;
case "WAIT_BUYER_PAY":
alipaymentOrder.setTrade_status(0);
break;
default:
break;
}
alipayment_orderService.updateByPrimaryKey(alipaymentOrder);
return alipaymentOrder.getTrade_status();
} else {
logger.info("调用支付宝查询接口失败!");
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
return 0;
}
@Override
public String notify(Map<String, String> conversionParams) {
logger.info("支付宝异步请求逻辑处理");
boolean signVerified = false;
try {
signVerified = AlipaySignature.rsaCheckV1(conversionParams, AlipayAppConfig.ALIPAY_PUBLIC_KEY,
AlipayAppConfig.CHARSET, AlipayAppConfig.SIGNTYPE);
} catch (AlipayApiException e) {
logger.info("验签失败 !");
e.printStackTrace();
}
if (signVerified) {
System.out.println("验签通过");
String appId = conversionParams.get("app_id");
String notifyTime = conversionParams.get("notify_time");
String gmtCreate = conversionParams.get("gmt_create");
String gmtPayment = conversionParams.get("gmt_payment");
String tradeNo = conversionParams.get("trade_no");
String outTradeNo = conversionParams.get("out_trade_no");
String buyerLogonId = conversionParams.get("buyer_logon_id");
String buyerId = conversionParams.get("buyer_id");
String sellerId = conversionParams.get("seller_id");
String sellerEmail = conversionParams.get("seller_email");
String totalAmount = conversionParams.get("total_amount");
String receiptAmount = conversionParams.get("receipt_amount");
String buyerPayAmount = conversionParams.get("buyer_pay_amount");
String tradeStatus = conversionParams.get("trade_status");
T_alipayment_order alipaymentOrder = alipayment_orderService.selectByOutTradeNo(outTradeNo);
if (alipaymentOrder != null && totalAmount.equals(alipaymentOrder.getTotal_amount().toString())
&& AlipayAppConfig.APPID.equals(appId)) {
alipaymentOrder.setNotify_time(DateUtils.parse(notifyTime));
alipaymentOrder.setGmt_create(DateUtils.parse(gmtCreate));
alipaymentOrder.setGmt_payment(DateUtils.parse(gmtPayment));
alipaymentOrder.setTrade_no(tradeNo);
alipaymentOrder.setBuyer_logon_id(buyerLogonId);
alipaymentOrder.setBuyer_id(buyerId);
alipaymentOrder.setSeller_id(sellerId);
alipaymentOrder.setSeller_email(sellerEmail);
alipaymentOrder.setTotal_amount(Double.parseDouble(totalAmount));
alipaymentOrder.setReceipt_amount(Double.parseDouble(receiptAmount));
alipaymentOrder.setBuyer_pay_amount(Double.parseDouble(buyerPayAmount));
switch (tradeStatus)
{
case "TRADE_FINISHED":
alipaymentOrder.setTrade_status(3);
break;
case "TRADE_SUCCESS":
alipaymentOrder.setTrade_status(2);
break;
case "TRADE_CLOSED":
alipaymentOrder.setTrade_status(1);
break;
case "WAIT_BUYER_PAY":
alipaymentOrder.setTrade_status(0);
break;
default:
break;
}
int returnResult = alipayment_orderService.updateByPrimaryKey(alipaymentOrder);
if (tradeStatus.equals("TRADE_SUCCESS")) {
if (returnResult > 0) {
logger.info("======更新商户订单表成功======");
return "success";
} else {
return "fail";
}
} else {
return "fail";
}
} else {
logger.info("支付宝官方建议校验的值(out_trade_no、total_amount、sellerId、app_id),不一致!返回fail");
return "fail";
}
} else {
logger.info("验签不通过 !");
return "fail";
}
}
@Transactional(propagation = Propagation.REQUIRED)
@Override
public String alipayTradeRefund(String order_no, Double refund_fee) {
String returnStr = null;
int code = ReturnCode.SUCCSEE_CODE;
try {
AlipayClient alipayClient = new DefaultAlipayClient(AlipayAppConfig.URL, AlipayAppConfig.APPID,
AlipayAppConfig.APP_PRIVATE_KEY, AlipayAppConfig.FORMAT, AlipayAppConfig.CHARSET,
AlipayAppConfig.ALIPAY_PUBLIC_KEY, AlipayAppConfig.SIGNTYPE);
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
request.setBizContent("{" +
" \"out_trade_no\":\"" + order_no + "\"," +
" \"refund_amount\":\"" + refund_fee + "\"" +
" }");
AlipayTradeRefundResponse response = alipayClient.execute(request);
logger.info("退款返回参数===="+response);
if (response.isSuccess()) {
logger.info("====支付宝退款成功");
T_alipayment_order alipaymentOrder = alipayment_orderService.selectByOutTradeNo(order_no);
if (alipaymentOrder == null) {
logger.info("该订单不存在");
}
alipaymentOrder.setRefund_fee(Double.valueOf(response.getRefundFee()));
alipaymentOrder.setGmt_refund(response.getGmtRefundPay());
alipaymentOrder.setTrade_status(AlipayReturnEnum.TRADE_CLOSED.getIndex());
alipaymentOrder.setFund_change(response.getFundChange());
alipaymentOrder.setStore_name(response.getStoreName());
alipayment_orderService.updateByPrimaryKey(alipaymentOrder);
returnStr = response.getMsg();
} else {
logger.info("===退款失败===原因:"+response.getSubMsg());
returnStr = response.getSubMsg();
}
code = Integer.parseInt(response.getCode());
} catch (AlipayApiException e) {
e.printStackTrace();
JsonUtil.getResponseJson(ReturnCode.EXCEPTION_CODE, e.getMessage(), null, null);
}
return JsonUtil.getResponseJson(code, returnStr, null, null);
}
}
支付控制层 AliPayAppController
@Controller
@RequestMapping("/aliPayApp")
public class AliPayAppController {
private static final Logger logger = Logger.getLogger(AliPayAppController.class);
@Autowired
private AliPayAppService aliPayAppService;
@RequestMapping("/getAliPayOrderStr")
@ResponseBody
public String getAliPayOrderStr(T_order t_order) {
return aliPayAppService.getAliPayOrderStr(t_order);
}
@RequestMapping(value="/notify_url",method=RequestMethod.POST)
@ResponseBody
public String notify(HttpServletRequest request, HttpServletResponse response) throws IOException {
logger.info("支付宝异步返回支付结果开始");
Map<String, String[]> aliParams = request.getParameterMap();
Map<String, String> conversionParams = new HashMap<String, String>();
for (Iterator<String> iter = aliParams.keySet().iterator(); iter.hasNext();) {
String key = iter.next();
String[] values = aliParams.get(key);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
conversionParams.put(key, valueStr);
}
logger.info("返回参数集合:" + conversionParams);
String status = aliPayAppService.notify(conversionParams);
return JsonUtil.getResponseJson(1, status, null, null);
}
@RequestMapping("/checkAlipay")
@ResponseBody
public String checkAlipay(String order_no) {
int result = aliPayAppService.checkAlipay(order_no);
return JsonUtil.getResponseJson(ReturnCode.SUCCSEE_CODE, AlipayReturnEnum.getName(result), null, null);
}
@RequestMapping("/alipayTradeRefund")
@ResponseBody
public String alipayTradeRefund(String order_no, Double refund_fee) {
return aliPayAppService.alipayTradeRefund(order_no, refund_fee);
}
}