55import java .util .Map ;
66import java .util .concurrent .locks .Lock ;
77
8+ import org .apache .commons .lang3 .StringUtils ;
89import org .apache .http .HttpHost ;
910import org .apache .http .client .config .RequestConfig ;
1011import org .apache .http .client .methods .CloseableHttpResponse ;
2930import cn .binarywang .wx .miniapp .config .WxMaConfig ;
3031import com .google .common .base .Joiner ;
3132import com .google .gson .Gson ;
33+ import com .google .gson .JsonParser ;
3234import lombok .extern .slf4j .Slf4j ;
35+ import me .chanjar .weixin .common .WxType ;
3336import me .chanjar .weixin .common .bean .WxAccessToken ;
3437import me .chanjar .weixin .common .error .WxError ;
3538import me .chanjar .weixin .common .error .WxErrorException ;
5053 */
5154@ Slf4j
5255public class WxMaServiceImpl implements WxMaService , RequestHttp <CloseableHttpClient , HttpHost > {
56+ private static final JsonParser JSON_PARSER = new JsonParser ();
5357 private CloseableHttpClient httpClient ;
5458 private HttpHost httpProxy ;
5559 private WxMaConfig wxMaConfig ;
@@ -150,6 +154,33 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
150154 return this .getWxMaConfig ().getAccessToken ();
151155 }
152156
157+ @ Override
158+ public String getPaidUnionId (String openid , String transactionId , String mchId , String outTradeNo )
159+ throws WxErrorException {
160+ Map <String , String > params = new HashMap <>(8 );
161+ params .put ("openid" , openid );
162+
163+ if (StringUtils .isNotEmpty (transactionId )) {
164+ params .put ("transaction_id" , transactionId );
165+ }
166+
167+ if (StringUtils .isNotEmpty (mchId )) {
168+ params .put ("mch_id" , mchId );
169+ }
170+
171+ if (StringUtils .isNotEmpty (outTradeNo )) {
172+ params .put ("out_trade_no" , outTradeNo );
173+ }
174+
175+ String responseContent = this .get (GET_PAID_UNION_ID_URL , Joiner .on ("&" ).withKeyValueSeparator ("=" ).join (params ));
176+ WxError error = WxError .fromJson (responseContent , WxType .MiniApp );
177+ if (error .getErrorCode () != 0 ) {
178+ throw new WxErrorException (error );
179+ }
180+
181+ return JSON_PARSER .parse (responseContent ).getAsJsonObject ().get ("unionid" ).getAsString ();
182+ }
183+
153184 @ Override
154185 public WxMaJscode2SessionResult jsCode2SessionInfo (String jsCode ) throws WxErrorException {
155186 final WxMaConfig config = getWxMaConfig ();
@@ -168,7 +199,7 @@ public boolean checkSignature(String timestamp, String nonce, String signature)
168199 try {
169200 return SHA1 .gen (this .getWxMaConfig ().getToken (), timestamp , nonce ).equals (signature );
170201 } catch (Exception e ) {
171- this . log .error ("Checking signature failed, and the reason is :" + e .getMessage ());
202+ log .error ("Checking signature failed, and the reason is :" + e .getMessage ());
172203 return false ;
173204 }
174205 }
@@ -246,7 +277,7 @@ private <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E d
246277 if (error .getErrorCode () == ERR_40001
247278 || error .getErrorCode () == ERR_42001
248279 || error .getErrorCode () == ERR_40014 ) {
249- // 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
280+ // 强制设置WxMaConfig的access token过期了,这样在下一次请求里就会刷新access token
250281 this .getWxMaConfig ().expireAccessToken ();
251282 if (this .getWxMaConfig ().autoRefreshToken ()) {
252283 return this .execute (executor , uri , data );
0 commit comments