| | |
| | | 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; |
| | | |
| | | /** |
| | | * 支付宝小程序帮助类 |
| | |
| | | * @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"); |
| | |
| | | if (!response.isSuccess()) { |
| | | throw new AlipayXcxException(response.getCode(), response.getSubMsg(), response.getBody()); |
| | | } |
| | | return response.getUserId(); |
| | | return response; |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取支付宝用户信息 |
| | | * |
| | | * @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; |
| | | } |
| | | |
| | | } |