支付宝单笔转账到账户(支付宝账号or银行账号)实现

package com.pay.utils;

import java.util.HashMap;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.bendian.pay.config.Configs;

public class TransferUtil2 {
	
	/**
	 * 引入日志,注意都是"org.slf4j"包下
	 */
	private final static Logger logger = LoggerFactory.getLogger(TransferUtil2.class);
	
	
    /**
     * 带公钥证书验证(方式)
     * @param oid 业务单号,唯一
     * @param amount 金额
     * @param identity 支付宝账号(手机号或者邮箱)
     * @param name 姓名
     * @param businessType 业务类型
     * @return 返回处理结果
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
	public static HashMap  transferWithCert(String oid, int amount,String identity,String name,String businessType) throws Exception{
    	//订单号
//    	String uUid = getUUid();
    	//返回信息
    	boolean result = false;
    	HashMap<String, String> map = new HashMap<>();
		//构造client
    	CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
    	certAlipayRequest.setServerUrl(Configs.gatewayUrl);
    	certAlipayRequest.setAppId(Configs.app_id);
    	certAlipayRequest.setPrivateKey(Configs.merchant_private_key);
    	certAlipayRequest.setFormat(Configs.format);
    	certAlipayRequest.setCharset(Configs.charset);
    	certAlipayRequest.setSignType(Configs.sign_type);
    	certAlipayRequest.setCertPath(Configs.path + Configs.app_cert_path);
    	certAlipayRequest.setAlipayPublicCertPath(Configs.path+ Configs.alipay_cert_path);
    	certAlipayRequest.setRootCertPath(Configs.path+Configs.alipay_root_cert_path);
    	try {
			DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
			//发送API请求
			AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest(); 
			request.setBizContent("{" + "\"out_biz_no\":\""+ oid+ "\"," + "\"trans_amount\":\""+ amount +"\"," + "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," + "\"biz_scene\":\"DIRECT_TRANSFER\"," + "\"order_title\":\"" + businessType + "\"," +
										"\"payee_info\":{" 
												+ "\"identity\":\"" + identity +"\"," 
												+ "\"identity_type\":\"ALIPAY_LOGON_ID\"," 
												+ "\"name\":\"" + name + "\"" 
												+ "}," 
												+ "\"remark\":\"共享收益\"" + "}"); 
			
				AlipayFundTransUniTransferResponse response =  alipayClient.certificateExecute(request);
				result = response.isSuccess();
				String code = response.getCode();
				String msg = response.getSubMsg();
				map.put("code", code);
				map.put("msg", msg);
				if(response.isSuccess()){
					logger.info("调用成功!"  + " 业务类型:"+ businessType + ",支付信息:"+ oid +","+ amount+","+identity+","+name);				
				} else {
					logger.error("调用失败!错误信息提示:"+ msg + "; 业务类型:"+ businessType + ",支付信息:"+ oid +","+ amount+","+identity+","+name);
				}
				return map;
		} catch (AlipayApiException e) {
			logger.error("未知异常,调用失败!" + " 业务类型:"+ businessType + ",支付信息:"+ oid +","+ amount+","+identity+","+name);
			e.printStackTrace();
			map.put("code", "5000");
			map.put("msg", "调用失败,未知异常!(自定义)");
		}
		return map;
    }
	
    /**
     * 获取不重复的订单id
     * 
     * @return
     */
    public  String getUUid(){
    	//生成唯一id
        String id= UUID.randomUUID().toString();
        //替换uuid中的"-"
        id=id.replace("-", "");
        System.out.println("订单id :" + id);
		return id;
    }
}

注意事项:
1.证书路径,必须填写绝对路径;必须签约单笔转账支付;

2.订单号必须唯一;

3.测试环境请放在main 方法中进行;

4.设置白名单,固定ip才能调用转账接口;

5.官方接口参考文档:https://docs.open.alipay.com/api_28/alipay.fund.trans.uni.transfer/
API错误提示:https://docs.open.alipay.com/common/105806

你可能感兴趣的:(java基础)