Browse Source

#895 小程序增加用户支付完获取UnionId的接口

master
Binary Wang 6 years ago
parent
commit
0eccfb6a82
3 changed files with 62 additions and 4 deletions
  1. +23
    -0
      weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java
  2. +33
    -2
      weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java
  3. +6
    -2
      weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImplTest.java

+ 23
- 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java View File

@@ -17,6 +17,10 @@ public interface WxMaService {
String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";

String JSCODE_TO_SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session";
/**
* getPaidUnionId
*/
String GET_PAID_UNION_ID_URL = "https://api.weixin.qq.com/wxa/getpaidunionid";

/**
* 获取登录后的session信息.
@@ -56,6 +60,22 @@ public interface WxMaService {
*/
String getAccessToken(boolean forceRefresh) throws WxErrorException;

/**
* <pre>
* 用户支付完成后,获取该用户的 UnionId,无需用户授权。本接口支持第三方平台代理查询。
*
* 注意:调用前需要用户完成支付,且在支付后的五分钟内有效。
* 请求地址: GET https://api.weixin.qq.com/wxa/getpaidunionid?access_token=ACCESS_TOKEN&openid=OPENID
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/getPaidUnionId.html
* </pre>
*
* @param openid 必填 支付用户唯一标识
* @param transactionId 非必填 微信支付订单号
* @param mchId 非必填 微信支付分配的商户号,和商户订单号配合使用
* @param outTradeNo 非必填 微信支付商户订单号,和商户号配合使用
*/
String getPaidUnionId(String openid, String transactionId, String mchId, String outTradeNo) throws WxErrorException;

/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求.
*/
@@ -168,18 +188,21 @@ public interface WxMaService {

/**
* 返回分享相关查询服务.
*
* @return WxMaShareService
*/
WxMaShareService getShareService();

/**
* 返回微信运动相关接口服务对象.
*
* @return WxMaShareService
*/
WxMaRunService getRunService();

/**
* 返回内容安全相关接口服务对象.
*
* @return WxMaShareService
*/
WxMaSecCheckService getSecCheckService();


+ 33
- 2
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java View File

@@ -5,6 +5,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -29,7 +30,9 @@ import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import com.google.common.base.Joiner;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
@@ -50,6 +53,7 @@ import static cn.binarywang.wx.miniapp.constant.WxMaConstants.ErrorCode.*;
*/
@Slf4j
public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpClient, HttpHost> {
private static final JsonParser JSON_PARSER = new JsonParser();
private CloseableHttpClient httpClient;
private HttpHost httpProxy;
private WxMaConfig wxMaConfig;
@@ -150,6 +154,33 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
return this.getWxMaConfig().getAccessToken();
}

@Override
public String getPaidUnionId(String openid, String transactionId, String mchId, String outTradeNo)
throws WxErrorException {
Map<String, String> params = new HashMap<>(8);
params.put("openid", openid);

if (StringUtils.isNotEmpty(transactionId)) {
params.put("transaction_id", transactionId);
}

if (StringUtils.isNotEmpty(mchId)) {
params.put("mch_id", mchId);
}

if (StringUtils.isNotEmpty(outTradeNo)) {
params.put("out_trade_no", outTradeNo);
}

String responseContent = this.get(GET_PAID_UNION_ID_URL, Joiner.on("&").withKeyValueSeparator("=").join(params));
WxError error = WxError.fromJson(responseContent, WxType.MiniApp);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}

return JSON_PARSER.parse(responseContent).getAsJsonObject().get("unionid").getAsString();
}

@Override
public WxMaJscode2SessionResult jsCode2SessionInfo(String jsCode) throws WxErrorException {
final WxMaConfig config = getWxMaConfig();
@@ -168,7 +199,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
try {
return SHA1.gen(this.getWxMaConfig().getToken(), timestamp, nonce).equals(signature);
} catch (Exception e) {
this.log.error("Checking signature failed, and the reason is :" + e.getMessage());
log.error("Checking signature failed, and the reason is :" + e.getMessage());
return false;
}
}
@@ -246,7 +277,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
if (error.getErrorCode() == ERR_40001
|| error.getErrorCode() == ERR_42001
|| error.getErrorCode() == ERR_40014) {
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
// 强制设置WxMaConfig的access token过期了,这样在下一次请求里就会刷新access token
this.getWxMaConfig().expireAccessToken();
if (this.getWxMaConfig().autoRefreshToken()) {
return this.execute(executor, uri, data);


+ 6
- 2
weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImplTest.java View File

@@ -1,7 +1,5 @@
package cn.binarywang.wx.miniapp.api.impl;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.testng.annotations.*;

@@ -11,6 +9,7 @@ import cn.binarywang.wx.miniapp.test.ApiTestModule;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.*;

/**
@@ -33,4 +32,9 @@ public class WxMaServiceImplTest {
assertTrue(StringUtils.isNotBlank(after));
}

@Test(expectedExceptions = {WxErrorException.class})
public void testGetPaidUnionId() throws WxErrorException {
final String unionId = this.wxService.getPaidUnionId("1", null, "3", "4");
assertThat(unionId).isNotEmpty();
}
}

Loading…
Cancel
Save