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; } }