java 对接支付宝单笔转账接口

证书模式及非证书模式转账
  • 查询证书路径
   public String queryPath() throws FileNotFoundException, ServerException {
		String basePath = ResourceUtils.getURL("classpath:").getPath();
		basePath = this.initCart(basePath);

		return basePath;
	}
	private String initCart(String basePath) throws ServerException {
		if (basePath.contains("jar!")) {
			if (basePath.startsWith("file:")) {
				basePath = basePath.replace("file:", "");
			}
			String appCertPath = aliPayProperties.getAppCertPath();
			this.checkAndcopyCart(basePath, appCertPath);
			String alipayCertPath = aliPayProperties.getAlipayCertPath();
			this.checkAndcopyCart(basePath, alipayCertPath);
			String alipayRootCertPath = aliPayProperties.getAlipayRootCertPath();
			this.checkAndcopyCart(basePath, alipayRootCertPath);
		}
		return basePath;
	}
	private void checkAndcopyCart(String basePath, String path) throws ServerException {
		InputStream cartInputStream = null;
		OutputStream cartOutputStream = null;
		String cartPath = basePath + path;
		File cartFile = new File(cartPath);
		File parentFile = cartFile.getParentFile();
		parentFile.mkdirs();
		try {
			if (!cartFile.exists()) {
				cartInputStream = ClassUtils.getDefaultClassLoader().getResourceAsStream(path);
				cartOutputStream = new FileOutputStream(cartFile);
				byte[] buf = new byte[1024];
				int bytesRead;
				while ((bytesRead = cartInputStream.read(buf)) != -1) {
					cartOutputStream.write(buf, 0, bytesRead);
				}
				cartInputStream.close();
				cartOutputStream.close();
			}
		} catch (IOException e) {
			throw new ServerException(e.getMessage());
		}
	}
  • 调用支付宝转账接口 如果非证书模式 则可以直接将 构造client 之前的都不要
       String basePath = this.queryPath();
		// 构造client
		CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
		// 设置网关地址
		certAlipayRequest.setServerUrl(aliPayProperties.getGatewayUrl());
		// 设置应用Id
		certAlipayRequest.setAppId(aliPayProperties.getAppid());
		// 设置应用私钥
		certAlipayRequest.setPrivateKey(aliPayProperties.getAppPrivateKey());
		// 设置请求格式,固定值json
		certAlipayRequest.setFormat("json");
		// 设置字符集
		certAlipayRequest.setCharset(aliPayProperties.getCharset());
		// 设置签名类型
		certAlipayRequest.setSignType(aliPayProperties.getSignType());
		// 设置应用公钥证书路径
		certAlipayRequest.setCertPath(basePath + aliPayProperties.getAppCertPath());
		// 设置支付宝公钥证书路径
		certAlipayRequest.setAlipayPublicCertPath(basePath + aliPayProperties.getAlipayCertPath());
		// 设置支付宝根证书路径
		certAlipayRequest.setRootCertPath(basePath + aliPayProperties.getAlipayRootCertPath());
		// 构造client
		AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
		Map<String, String> map = new HashMap<>();
		map.put("out_biz_no", "自己平台订单Id");
		map.put("biz_scene", "DIRECT_TRANSFER");
		map.put("product_code", "TRANS_ACCOUNT_NO_PWD");
		map.put("trans_amount", "转账金额");
		map.put("order_title", "转账标题");
		map.put("remark", "转账备注");
		// 转账账户
		Map<String, String> map2 = new HashMap<>();
		map2.put("identity", "转账账户支付宝账户");
		map2.put("identity_type", "ALIPAY_LOGON_ID");
		map2.put("name", "转账账户真实姓名");

		map.put("payee_info", FastJsonUtil.beanToJson(map2));
		// 转成json格式放入
		String json = FastJsonUtil.beanToJson(map);
		AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
		request.setBizContent(json);
		log.info("请求参数:" + request.getBizContent());
		AlipayFundTransUniTransferResponse response = null;
		try {
			response = alipayClient.certificateExecute(request);
			if ("10000".equals(response.getCode())) {
				log.info("打印日志返回结果-----" + response.getTransDate());

				return Tuple.createTuple("0", "转账成功!");
			} else {
				log.info("打印日志返回结果-----" + response.getBody());

				return Tuple.createTuple(response.getCode(), response.getSubMsg());
			}
		} catch (AlipayApiException e) {
			log.info("打印日志返回结果-----" + response);
			e.printStackTrace();

			return Tuple.createTuple("1", "转账失败!");
		}

注意 付款方余额必须大于1元 否则会提示付款方余额不够

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值