admin
2020-11-28 dc5be7d38446f70e6ff86df311119c32b41fe7f8
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
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.response.AlipayOpenAppMiniTemplatemessageSendResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import org.yeshi.utils.entity.alipay.AlipayAppInfo;
import org.yeshi.utils.exception.AlipayXcxException;
 
/**
 * 支付宝小程序帮助类
 */
public class AlipayXcxUtil {
 
 
    /**
     * 获取支付宝的用户ID
     *
     * @param app
     * @param code
     * @return
     * @throws AlipayApiException
     */
    public static String getUserId(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.getUserId();
    }
 
 
    /**
     * 发送模板消息
     *
     * @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());
        }
    }
 
}