Administrator
2025-04-25 5af707003f4c7b26f09a554a4d3c75c1c6769ab2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.taoke.autopay.utils;
 
 
import java.math.BigDecimal;
 
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.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
 
import net.sf.json.JSONObject;
 
 
 
public class AlipayUtil {
 
    private static DefaultAlipayClient alipayClient = null;
 
    static {
        String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDhLhILE3YPWHOTJ9ndbdAQ+5DzrQ/+esTwAFnvo7YN+Bto8njOX9CI4/HE78bj7fFfTsPAJv0ga+b6iTCw0HhkI7oPZIJ1S4WH9w/EvzcsfoZvSEOJLHhnqOu8rkn8OVwzMZ5DtmRC0Sy2LZ+zIvdOqzJYAPlbJb+Q99ccVgr6nu+JouO6fHXMQ1xKvv5lSFgGy8EFiEU71UHcxdsA21Qn90ZNXAOSt8qHFydANVSE90pjAj3iLlpxleJGTCgLuAfGzN6pxyykjfR1m2YwWvEu7xcMgSlzO6/Hp0nWS8USehY2llRUAYW5QOq3+rhvZRE3rd9eESeDSpkkMWwQPY8RAgMBAAECggEAeL7BghrvZY8iejgWRPwLY+/hbCjSuLyQObmWgD+i8Z6m5AGQ8ejC44SHMfj5B2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n7rLKVRbhxPimcfGULNQUyeKX/+FKKG9Yi3KTV8Mzy8LxDxwJPpPgmV88fdUKrjuVLZxmrluo10czBM5DLjw==";
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
        certAlipayRequest.setAppId("2021004141681244");
        certAlipayRequest.setPrivateKey(privateKey);
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset("GBK");
        certAlipayRequest.setSignType("RSA2");
        certAlipayRequest.setCertPath(
                AlipayUtil.class.getClassLoader().getResource("alipay/appCertPublicKey_2021004141681244.crt").getPath());
        certAlipayRequest.setAlipayPublicCertPath(
                AlipayUtil.class.getClassLoader().getResource("alipay/alipayCertPublicKey_RSA2.crt").getPath());
        certAlipayRequest.setRootCertPath(
                AlipayUtil.class.getClassLoader().getResource("alipay/alipayRootCert.crt").getPath());
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
 
    }
 
    public static AlipayFundTransUniTransferResponse transfer(String outBizNo, String account, String name,
                                                              BigDecimal money, String orderTitle, String mark)
            throws AlipayApiException, NumberFormatException, AlipayTransferException {
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        JSONObject json = new JSONObject();
        json.put("out_biz_no", outBizNo);
        json.put("trans_amount", money.toString());
        json.put("product_code", "TRANS_ACCOUNT_NO_PWD");
        json.put("order_title", orderTitle);
        JSONObject payee_info = new JSONObject();
        payee_info.put("identity", account);
        payee_info.put("identity_type", "ALIPAY_LOGON_ID");
        payee_info.put("name", name);
        json.put("payee_info", payee_info);
        json.put("remark", mark);
        json.put("biz_scene", "DIRECT_TRANSFER");
 
        request.setBizContent(json.toString());
        AlipayFundTransUniTransferResponse response = null;
        response = alipayClient.certificateExecute(request);
        // 成功转账
        if (response != null && response.isSuccess() && "10000".equals(response.getCode())) {
            return response;
        } else// 转账失败
        {
            throw new AlipayTransferException(Integer.parseInt(response.getCode()), response.getSubCode(),
                    response.getSubMsg());
        }
    }
 
    public static AlipayFundTransUniTransferResponse transferNoThrowException(String outBizNo, String account, String name,
                                                                              BigDecimal money, String orderTitle, String mark) throws AlipayApiException{
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        JSONObject json = new JSONObject();
        json.put("out_biz_no", outBizNo);
        json.put("trans_amount", money.toString());
        json.put("product_code", "TRANS_ACCOUNT_NO_PWD");
        json.put("order_title", orderTitle);
        JSONObject payee_info = new JSONObject();
        payee_info.put("identity", account);
        payee_info.put("identity_type", "ALIPAY_LOGON_ID");
        payee_info.put("name", name);
        json.put("payee_info", payee_info);
        json.put("remark", mark);
        json.put("biz_scene", "DIRECT_TRANSFER");
 
        request.setBizContent(json.toString());
        AlipayFundTransUniTransferResponse response = null;
        response = alipayClient.certificateExecute(request);
        return response;
    }
 
    public static void main(String[] args) throws AlipayTransferException, AlipayApiException {
 
        AlipayUtil.transfer("test-1","18323283413@163.com","夏程培",new BigDecimal("0.1"),"积分兑换","积分兑换");
    }
 
 
   public static class AlipayTransferException extends Exception {
        public AlipayTransferException() {
 
        }
 
        /**
         * 支付宝转账异常
         */
        private static final long serialVersionUID = 1L;
        private int code;
        private String subCode;
        private String msg;
 
        public int getCode() {
            return code;
        }
 
        public String getMsg() {
            return msg;
        }
 
        public String getSubCode() {
            return subCode;
        }
 
        public AlipayTransferException(int code, String subCode, String msg) {
            this.code = code;
            this.msg = msg;
            this.subCode = subCode;
        }
 
        @Override
        public String getMessage() {
            return this.msg;
        }
 
    }
 
 
}