Skip to content

Commit 0418f17

Browse files
committed
增加微信支付 授权码查询OPENID接口API #101
1 parent 96d72fe commit 0418f17

File tree

5 files changed

+144
-7
lines changed

5 files changed

+144
-7
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.binarywang.wxpay.bean.request;
2+
3+
import com.thoughtworks.xstream.annotations.XStreamAlias;
4+
5+
/**
6+
* <pre>
7+
* 授权码查询openid接口请求对象类
8+
* Created by Binary Wang on 2017-3-27.
9+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
10+
* </pre>
11+
*/
12+
@XStreamAlias("xml")
13+
public class WxPayAuthcode2OpenidRequest extends WxPayBaseRequest {
14+
15+
/**
16+
* <pre>
17+
* 授权码
18+
* auth_code
19+
* 是
20+
* String(128)
21+
* 扫码支付授权码,设备读取用户微信中的条码或者二维码信息
22+
* </pre>
23+
*/
24+
@XStreamAlias("auth_code")
25+
private String authCode;
26+
27+
public WxPayAuthcode2OpenidRequest() {
28+
}
29+
30+
public WxPayAuthcode2OpenidRequest(String authCode) {
31+
this.authCode = authCode;
32+
}
33+
34+
public String getAuthCode() {
35+
return this.authCode;
36+
}
37+
38+
public void setAuthCode(String authCode) {
39+
this.authCode = authCode;
40+
}
41+
42+
@Override
43+
protected void checkConstraints() {
44+
// nothing to do
45+
}
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.github.binarywang.wxpay.bean.result;
2+
3+
import com.thoughtworks.xstream.annotations.XStreamAlias;
4+
5+
/**
6+
* <pre>
7+
* 授权码查询openid接口请求结果类
8+
* Created by Binary Wang on 2017-3-27.
9+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
10+
* </pre>
11+
*/
12+
@XStreamAlias("xml")
13+
public class WxPayAuthcode2OpenidResult extends WxPayBaseResult {
14+
/**
15+
* <pre>
16+
* 用户标识
17+
* openid
18+
* 是
19+
* String(128)
20+
* 用户在商户appid下的唯一标识
21+
* </pre>
22+
*/
23+
@XStreamAlias("openid")
24+
private String openid;
25+
26+
public WxPayAuthcode2OpenidResult() {
27+
}
28+
29+
public WxPayAuthcode2OpenidResult(String openid) {
30+
this.openid = openid;
31+
}
32+
33+
public String getOpenid() {
34+
return this.openid;
35+
}
36+
37+
public void setOpenid(String openid) {
38+
this.openid = openid;
39+
}
40+
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
281281
/**
282282
* <pre>
283283
* 转换短链接
284+
* 文档地址:
285+
* https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_9&index=8
284286
* 应用场景:
285287
* 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),减小二维码数据量,提升扫描速度和精确度。
286288
* 接口地址:https://api.mch.weixin.qq.com/tools/shorturl
@@ -293,13 +295,33 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
293295
/**
294296
* <pre>
295297
* 转换短链接
296-
* 应用场景:
297-
* 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),减小二维码数据量,提升扫描速度和精确度。
298-
* 接口地址:https://api.mch.weixin.qq.com/tools/shorturl
299-
* 是否需要证书:否
300298
* </pre>
299+
* @see WxPayService#shorturl(WxPayShorturlRequest)
301300
* @param longUrl 需要被压缩的网址
302301
*/
303302
String shorturl(String longUrl) throws WxErrorException;
304303

304+
/**
305+
* <pre>
306+
* 授权码查询OPENID接口
307+
* 通过授权码查询公众号Openid,调用查询后,该授权码只能由此商户号发起扣款,直至授权码更新。
308+
* 文档地址:
309+
* https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_13&index=9
310+
* 接口链接:
311+
* https://api.mch.weixin.qq.com/tools/authcodetoopenid
312+
* </pre>
313+
* @param request 请求对象
314+
* @return openid
315+
*/
316+
String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxErrorException;
317+
318+
/**
319+
* <pre>
320+
* 授权码查询OPENID接口
321+
* </pre>
322+
* @see WxPayService#authcode2Openid(WxPayAuthcode2OpenidRequest)
323+
* @param authCode 授权码
324+
* @return openid
325+
*/
326+
String authcode2Openid(String authCode) throws WxErrorException;
305327
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,22 @@ public String shorturl(String longUrl) throws WxErrorException {
344344
return this.shorturl(new WxPayShorturlRequest(longUrl));
345345
}
346346

347+
@Override
348+
public String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxErrorException {
349+
request.checkAndSign(this.getConfig());
350+
351+
String url = this.getPayBaseUrl() + "/tools/authcodetoopenid";
352+
String responseContent = this.post(url, request.toXML());
353+
WxPayAuthcode2OpenidResult result = WxPayBaseResult.fromXML(responseContent, WxPayAuthcode2OpenidResult.class);
354+
result.checkResult(this);
355+
return result.getOpenid();
356+
}
357+
358+
@Override
359+
public String authcode2Openid(String authCode) throws WxErrorException {
360+
return this.authcode2Openid(new WxPayAuthcode2OpenidRequest(authCode));
361+
}
362+
347363
private String post(String url, String xmlParam) {
348364
String requestString = xmlParam;
349365
try {

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
@Test
2929
@Guice(modules = ApiTestModule.class)
3030
public class WxPayServiceImplTest {
31-
3231
private final Logger logger = LoggerFactory.getLogger(this.getClass());
3332

3433
@Inject
@@ -245,10 +244,24 @@ public void testShorturl() throws Exception {
245244

246245
String result = this.payService.shorturl(new WxPayShorturlRequest(longUrl));
247246
assertNotNull(result);
248-
this.logger.info(result.toString());
247+
this.logger.info(result);
249248

250249
result = this.payService.shorturl(longUrl);
251250
assertNotNull(result);
252-
this.logger.info(result.toString());
251+
this.logger.info(result);
253252
}
253+
254+
@Test
255+
public void testAuthcode2Openid() throws Exception {
256+
String authCode = "11111";
257+
258+
String result = this.payService.authcode2Openid(new WxPayAuthcode2OpenidRequest(authCode));
259+
assertNotNull(result);
260+
this.logger.info(result);
261+
262+
result = this.payService.authcode2Openid(authCode);
263+
assertNotNull(result);
264+
this.logger.info(result);
265+
}
266+
254267
}

0 commit comments

Comments
 (0)