赞
踩
支付宝APP支付,无论在文档上,还是在demo上,比微信支付高好几个level吧.使用起来非常方便,基本上不会有什么太大的坑,只要严格按照demo,和文档进行操作的话,基本上可以一把过的.在这里要提示下,加签和验签使用的公钥问题:加签是在开发商平台查看公钥的时候的:应用公钥,而回调验签使用的,在开发商平台查看公钥的时候的:支付公钥.一定要注意这个,要不回调会报错的.
Controller:
@PostMapping("/payRequest") @ApiOperation("统一下单alipay支付") public Result payRequest(@RequestBody MallOrderPayDto mallOrderPayDto) throws Exception { log.info("ali 支付:{}",mallOrderPayDto); try { Map<String,String> maps = aliPayService.payRequest(mallOrderPayDto); return super.success(maps); } catch (Exception e) { log.info("订单ali支付异常:{}",e); return super.error("订单数据异常!"); } } @PostMapping(value = "/aliPayOrderCallBack") @ApiOperation("alipay支付回调") public Map<String, String> aliPayOrderCallBack(HttpServletRequest request){ Map<String, String> map = new HashMap<String, String>(); try { AliPayVO aliPayVo = friendsMallAliPayService.aliPayCallBack(request); aliPayService.aliPayOrderCallBack(aliPayVo); map.put("msg", "SUCCESS"); } catch (Exception e) { e.printStackTrace(); log.error("aliPayOrderCallBack Error.",e); } return map; }
Service:
public interface AliPayService {
//支付宝支付
public Map<String, String> payRequest(OrderPayDto orderPayDto) throws Exception;
//支付宝回调
public AliPayVO aliPayCallBack(HttpServletRequest request) throws Exception;
//支付宝订单支付回调业务处理
public void aliPayOrderCallBack(AliPayVO aliPayVo) throws Exception;
}
ServiceImpl:
@Slf4j @Service @Transactional(rollbackFor = Exception.class) public class AliPayServiceImpl implements AliPayService { //订单支付支付宝回调地址 public static String ALIPAY_ORDER_CALLBACK = "http://127.0.0.1:8080/api/alipay/aliPayOrderCallBack"; @Override public Map<String, String> payRequest(OrderPayDto orderPayDto) throws Exception { Map<String, String> map = new HashMap<String, String>(); //TODO 要查库.获取配置 AlipayConfig alipayConfig = new AlipayConfig(); alipayConfig.setUrl("https://openapi.alipay.com/gateway.do"); alipayConfig.setAppId("2020920193812");//appId alipayConfig.setRsaPrivateKey("aaaa");//私钥 alipayConfig.setAlipayPublicKey("bbbbbbb");//公钥 alipayConfig.setCharset("utf-8");//编码格式 alipayConfig.setFormat("json");//返回格式 alipayConfig.setSignType("RSA2");//签名方式 //实例化客户端 log.info("实例化客户端开始====================="); AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getUrl(), alipayConfig.getAppId(), alipayConfig.getRsaPrivateKey(), alipayConfig.getFormat(), alipayConfig.getCharset(), alipayConfig.getAlipayPublicKey(), alipayConfig.getSignType()); AlipayTradeAppPayRequest aliPayRequest = new AlipayTradeAppPayRequest(); AlipayTradePayModel model = new AlipayTradePayModel(); log.info("封装ali支付请求参数=================="); //支付编号 String orderNum = PayUtils.getOrderNumber(); log.info("支付订单号:{}",orderNum); model.setOutTradeNo(orderNum); //订单名称 model.setSubject("订单描述"); //支付总金额 String money = totalAmount.toString()+".00"; //此处是测试demo的时候临时加的,自行处理下String的,小数点后两位. model.setTotalAmount(money); //超时则关闭订单 model.setTimeoutExpress("30m"); aliPayRequest.setBizModel(model); //异步回调地址 aliPayRequest.setNotifyUrl(ALIPAY_ORDER_CALLBACK); AlipayTradeAppPayResponse response = alipayClient.sdkExecute(aliPayRequest); map.put("aliPaySdk", response.getBody()); log.info("支付宝支付加密结果:{}",map); log.info("alipay 参数封装完成,开始入库流水=================订单号:{}",orderNum); return map; } @Override public AliPayVO aliPayCallBack(HttpServletRequest request) throws Exception { AlipayConfig alipayConfig = new AlipayConfig(); //获取支付宝反馈信息 Map<String, String> params = new HashMap<String, String>(); Map<String, String[]> requestParams = request.getParameterMap(); for (Iterator<String> 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] + ","; } params.put(name, valueStr); } //调用SDK验证签名 boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayConfig.getAlipayPublicKey(), alipayConfig.getCharset(), alipayConfig.getSignType()); //校验签名 if (!signVerified) { log.error("1.支付宝支付回调签名不正确"); throw new Exception("支付宝支付回调签名不正确"); } //校验支付状态 if (!request.getParameter("trade_status").equals("TRADE_SUCCESS")) { log.error("2.支付宝支付回调支付状态不正确"); throw new Exception("支付宝支付回调支付状态不正确"); } AliPayVO aliPayVo = new AliPayVO(); //商户订单号-系统生成订单号 String outTradeNo = request.getParameter("out_trade_no"); aliPayVo.setOutTradeNo(outTradeNo); //支付订单号-阿里28位订单号 String tradeNo = request.getParameter("trade_no"); aliPayVo.setPayOrderNo(tradeNo); //交易状态 String tradeStatus = request.getParameter("trade_status"); aliPayVo.setTradeStatus(tradeStatus); //资金总额 String totalAmount = request.getParameter("total_amount"); aliPayVo.setTotalAmount(totalAmount); //卖家支付宝账户 String sellerId = request.getParameter("seller_id"); aliPayVo.setSellerId(sellerId); //买家支付宝账户 String buyerLogonId = request.getParameter("buyer_logon_id"); aliPayVo.setBuyerLogoinId(buyerLogonId); //签名方式 aliPayVo.setSignType(alipayConfig.getSignType()); //签名 String sign = request.getParameter("sign"); aliPayVo.setSign(sign); return aliPayVo; } @Override public void aliPayOrderCallBack(AliPayVO aliPayVo) throws Exception { log.info("3.alipay 回调开始============:{}",aliPayVo); if (aliPayVo != null && StringUtil.isNotEmpty(aliPayVo.getOutTradeNo())) { synchronized (aliPayVo.getOutTradeNo()) { MallUserRechargeDetail order = friendsMallUserRechargeDetailService.selectByOrderId(aliPayVo.getOutTradeNo()); if (order.getStatus().equals(0)) { if (aliPayVo.getTradeStatus().equals("TRADE_FINISHED")) { //交易创建,等待买家付款 log.info("4.交易创建,等待买家付款:TRADE_FINISHED"); } else if (aliPayVo.getTradeStatus().equals("WAIT_BUYER_PAY")) { //未付款交易超时关闭,或支付完成后全额退款 log.info("5.未付款交易超时关闭,或支付完成后全额退款:WAIT_BUYER_PAY"); } else if (aliPayVo.getTradeStatus().equals("TRADE_CLOSED")) { //交易结束,不可退款 log.info("6.交易结束,不可退款:TRADE_CLOSED"); } else if (aliPayVo.getTradeStatus().equals("TRADE_SUCCESS")) { //交易支付成功 log.info("7.支付状态成功:TRADE_SUCCESS"); MallUserRechargeDetail mallUserRechargeDetail = friendsMallUserRechargeDetailService.selectByOrderId(aliPayVo.getOutTradeNo()); log.info("8.根据订单号,查询用户充值记录,订单号:{},充值记录:{}",aliPayVo.getOutTradeNo(),mallUserRechargeDetail); //订单金额 BigDecimal payNumSum = new BigDecimal(mallUserRechargeDetail.getMoney()); log.info("9.订单金额:{}",payNumSum); //资金总额 BigDecimal totalAmount = new BigDecimal(aliPayVo.getTotalAmount()); log.info("10.回调金额:{}",totalAmount); //以防万一,校验金额 if (payNumSum.compareTo(totalAmount) != 0) { log.error("11.***订单号: " + aliPayVo.getOutTradeNo() + "***支付宝支付金额与订单需支付金额不一致***支付宝支付金额为:" + totalAmount + " ***订单需支付金额总为:" + payNumSum + "***日期:" + new Date()); throw new Exception("订单金额和回调金额不一致."); } log.info("12.校验订单状态,防止重复回调======================"); if (mallUserRechargeDetail.getStatus() == 1){ log.error("13.***订单号: " + aliPayVo.getOutTradeNo() + "**********支付状态为已成功,人工处理=============="); throw new Exception("订单状态为已支付,后续不处理,有异常人工处理."); } //中间的数据校验和业务处理省略了 log.info( i > 0 ? " 28.交易日志记录保存成功!" : "交易日志记录保存失败!"); } } else { log.info("29.该订单已支付处理,交易编号为: " + aliPayVo.getOutTradeNo()); } } } } }
实体:
@Data @Builder @AllArgsConstructor @NoArgsConstructor public class AlipayConfig { //appId private String appId; //私钥 private String rsaPrivateKey; //请求网关地址 private String url; //编码格式 private String charset; //返回格式 private String format; //支付宝公钥 private String alipayPublicKey; //签名方式 private String signType; }
依赖:
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>${alipay-sdk-java.version}</version>
</dependency>
<alipay-sdk-java.version>4.9.71.ALL</alipay-sdk-java.version>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。