admin
2021-06-30 d577762cafe94d9399d77cb4c6955ab176ff35ef
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
142
143
144
145
146
147
148
149
150
151
152
153
package org.yeshi.utils.alipay;
 
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayOpenAppMiniTemplatemessageSendRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipayOpenAppMiniTemplatemessageSendResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import org.yeshi.utils.FileUtil;
import org.yeshi.utils.entity.alipay.AlipayAppInfo;
import org.yeshi.utils.entity.alipay.AlipayUserInfo;
import org.yeshi.utils.exception.AlipayXcxException;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
 
/**
 * 支付宝小程序帮助类
 */
public class AlipayXcxUtil {
 
 
    /**
     * 获取支付宝的用户ID
     *
     * @param app
     * @param code
     * @return
     * @throws AlipayApiException
     */
    public static String getUserId(AlipayAppInfo app, String code) throws AlipayApiException, AlipayXcxException {
        AlipaySystemOauthTokenResponse response = getOauthTokenResponse(app, code);
        return response.getUserId();
    }
 
    /**
     * 获取用户token
     *
     * @param app
     * @param code
     * @return
     * @throws AlipayApiException
     * @throws AlipayXcxException
     */
    private static String getAccessToken(AlipayAppInfo app, String code) throws AlipayApiException, AlipayXcxException {
        AlipaySystemOauthTokenResponse response = getOauthTokenResponse(app, code);
        return response.getAccessToken();
    }
 
    private static AlipaySystemOauthTokenResponse getOauthTokenResponse(AlipayAppInfo app, String code) throws AlipayApiException, AlipayXcxException {
        AlipayClient alipayClient = AlipayUtil.getAlipayClient(app);
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType("authorization_code");
        request.setCode(code);
        AlipaySystemOauthTokenResponse response = app.getCertInfo() != null ? alipayClient.certificateExecute(request) : alipayClient.execute(request);
        if (!response.isSuccess()) {
            throw new AlipayXcxException(response.getCode(), response.getSubMsg(), response.getBody());
        }
        return response;
    }
 
 
    /**
     * 发送模板消息
     *
     * @param app        应用信息
     * @param templateId 模板ID
     * @param page       小程序页面路径
     * @param userId     用户ID
     * @param formId     表单ID
     * @param params
     * @return
     * @throws AlipayApiException
     */
    public static void sendTemplateMsg(AlipayAppInfo app, String templateId, String page, String userId, String formId, String... params) throws AlipayApiException, AlipayXcxException {
        AlipayClient alipayClient = AlipayUtil.getAlipayClient(app);
        AlipayOpenAppMiniTemplatemessageSendRequest request = new AlipayOpenAppMiniTemplatemessageSendRequest();
        JSONObject content = new JSONObject();
        content.put("to_user_id", userId);
        content.put("form_id", formId);
        content.put("user_template_id", templateId);
        content.put("page", page);
 
        if (params != null && params.length > 0) {
            JSONObject data = new JSONObject();
            for (int i = 0; i < params.length; i++) {
                JSONObject item = new JSONObject();
                item.put("value", params[i]);
                data.put("keyword" + (i + 1), item);
            }
            content.put("data", data.toJSONString());
        }
 
        System.out.println(content.toString());
        request.setBizContent(content.toString());
        AlipayOpenAppMiniTemplatemessageSendResponse response = app.getCertInfo() != null ? alipayClient.certificateExecute(request) : alipayClient.execute(request);
        if (!response.isSuccess()) {
            throw new AlipayXcxException(response.getCode(), response.getSubMsg(), response.getBody());
        }
    }
 
 
    /**
     * 获取支付宝用户信息
     *
     * @param app
     * @param code
     * @return
     * @throws AlipayApiException
     * @throws AlipayXcxException
     */
    public static AlipayUserInfo getAlipayUserInfo(AlipayAppInfo app, String code) throws AlipayApiException, AlipayXcxException {
 
        ByteArrayOutputStream publicCert = FileUtil.cloneInputStream(app.getCertInfo().getAlipayPublicCertStream());
        ByteArrayOutputStream alipayRoot = FileUtil.cloneInputStream(app.getCertInfo().getAlipayRootCertStream());
        ByteArrayOutputStream appCert = FileUtil.cloneInputStream(app.getCertInfo().getAppCertPublicKeyStream());
 
        //重置流
        app.getCertInfo().setAlipayPublicCertStream(new ByteArrayInputStream(publicCert.toByteArray()));
        app.getCertInfo().setAlipayRootCertStream(new ByteArrayInputStream(alipayRoot.toByteArray()));
        app.getCertInfo().setAppCertPublicKeyStream(new ByteArrayInputStream(appCert.toByteArray()));
 
 
        String accessToken = getAccessToken(app, code);
 
        //重置流
        app.getCertInfo().setAlipayPublicCertStream(new ByteArrayInputStream(publicCert.toByteArray()));
        app.getCertInfo().setAlipayRootCertStream(new ByteArrayInputStream(alipayRoot.toByteArray()));
        app.getCertInfo().setAppCertPublicKeyStream(new ByteArrayInputStream(appCert.toByteArray()));
 
 
        AlipayClient alipayClient = AlipayUtil.getAlipayClient(app);
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
 
        AlipayUserInfoShareResponse response = app.getCertInfo() != null ? alipayClient.certificateExecute(request, accessToken) : alipayClient.execute(request, accessToken);
 
        if (!response.isSuccess()) {
            throw new AlipayXcxException(response.getCode(), response.getSubMsg(), response.getBody());
        }
        AlipayUserInfo alipayUserInfo = new AlipayUserInfo();
        alipayUserInfo.setAvatar(response.getAvatar());
        alipayUserInfo.setCity(response.getCity());
        alipayUserInfo.setGender(response.getGender());
        alipayUserInfo.setNickName(response.getUserName());
        alipayUserInfo.setProvince(response.getProvince());
        alipayUserInfo.setUserId(response.getUserId());
        return alipayUserInfo;
    }
 
}