diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayAuthcode2OpenidRequest.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayAuthcode2OpenidRequest.java new file mode 100644 index 00000000..594fa496 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayAuthcode2OpenidRequest.java @@ -0,0 +1,46 @@ +package com.github.binarywang.wxpay.bean.request; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +/** + *
+ * 授权码查询openid接口请求对象类 + * Created by Binary Wang on 2017-3-27. + * @author binarywang(Binary Wang) + *+ */ +@XStreamAlias("xml") +public class WxPayAuthcode2OpenidRequest extends WxPayBaseRequest { + + /** + *
+ * 授权码 + * auth_code + * 是 + * String(128) + * 扫码支付授权码,设备读取用户微信中的条码或者二维码信息 + *+ */ + @XStreamAlias("auth_code") + private String authCode; + + public WxPayAuthcode2OpenidRequest() { + } + + public WxPayAuthcode2OpenidRequest(String authCode) { + this.authCode = authCode; + } + + public String getAuthCode() { + return this.authCode; + } + + public void setAuthCode(String authCode) { + this.authCode = authCode; + } + + @Override + protected void checkConstraints() { + // nothing to do + } +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayAuthcode2OpenidResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayAuthcode2OpenidResult.java new file mode 100644 index 00000000..f56c61a7 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayAuthcode2OpenidResult.java @@ -0,0 +1,40 @@ +package com.github.binarywang.wxpay.bean.result; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +/** + *
+ * 授权码查询openid接口请求结果类 + * Created by Binary Wang on 2017-3-27. + * @author binarywang(Binary Wang) + *+ */ +@XStreamAlias("xml") +public class WxPayAuthcode2OpenidResult extends WxPayBaseResult { + /** + *
+ * 用户标识 + * openid + * 是 + * String(128) + * 用户在商户appid下的唯一标识 + *+ */ + @XStreamAlias("openid") + private String openid; + + public WxPayAuthcode2OpenidResult() { + } + + public WxPayAuthcode2OpenidResult(String openid) { + this.openid = openid; + } + + public String getOpenid() { + return this.openid; + } + + public void setOpenid(String openid) { + this.openid = openid; + } +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java index e61ff863..e336cd21 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java @@ -281,6 +281,8 @@ public interface WxPayService { /** *
* 转换短链接 + * 文档地址: + * https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_9&index=8 * 应用场景: * 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),减小二维码数据量,提升扫描速度和精确度。 * 接口地址:https://api.mch.weixin.qq.com/tools/shorturl @@ -293,13 +295,33 @@ public interface WxPayService { /** ** 转换短链接 - * 应用场景: - * 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),减小二维码数据量,提升扫描速度和精确度。 - * 接口地址:https://api.mch.weixin.qq.com/tools/shorturl - * 是否需要证书:否 *+ * @see WxPayService#shorturl(WxPayShorturlRequest) * @param longUrl 需要被压缩的网址 */ String shorturl(String longUrl) throws WxErrorException; + /** + *+ * 授权码查询OPENID接口 + * 通过授权码查询公众号Openid,调用查询后,该授权码只能由此商户号发起扣款,直至授权码更新。 + * 文档地址: + * https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_13&index=9 + * 接口链接: + * https://api.mch.weixin.qq.com/tools/authcodetoopenid + *+ * @param request 请求对象 + * @return openid + */ + String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxErrorException; + + /** + *+ * 授权码查询OPENID接口 + *+ * @see WxPayService#authcode2Openid(WxPayAuthcode2OpenidRequest) + * @param authCode 授权码 + * @return openid + */ + String authcode2Openid(String authCode) throws WxErrorException; } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java index aea06943..c26d0583 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java @@ -344,6 +344,22 @@ public class WxPayServiceImpl implements WxPayService { return this.shorturl(new WxPayShorturlRequest(longUrl)); } + @Override + public String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxErrorException { + request.checkAndSign(this.getConfig()); + + String url = this.getPayBaseUrl() + "/tools/authcodetoopenid"; + String responseContent = this.post(url, request.toXML()); + WxPayAuthcode2OpenidResult result = WxPayBaseResult.fromXML(responseContent, WxPayAuthcode2OpenidResult.class); + result.checkResult(this); + return result.getOpenid(); + } + + @Override + public String authcode2Openid(String authCode) throws WxErrorException { + return this.authcode2Openid(new WxPayAuthcode2OpenidRequest(authCode)); + } + private String post(String url, String xmlParam) { String requestString = xmlParam; try { diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java index 3898d137..93b0b8b5 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java @@ -28,7 +28,6 @@ import static org.testng.Assert.*; @Test @Guice(modules = ApiTestModule.class) public class WxPayServiceImplTest { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Inject @@ -245,10 +244,24 @@ public class WxPayServiceImplTest { String result = this.payService.shorturl(new WxPayShorturlRequest(longUrl)); assertNotNull(result); - this.logger.info(result.toString()); + this.logger.info(result); result = this.payService.shorturl(longUrl); assertNotNull(result); - this.logger.info(result.toString()); + this.logger.info(result); } + + @Test + public void testAuthcode2Openid() throws Exception { + String authCode = "11111"; + + String result = this.payService.authcode2Openid(new WxPayAuthcode2OpenidRequest(authCode)); + assertNotNull(result); + this.logger.info(result); + + result = this.payService.authcode2Openid(authCode); + assertNotNull(result); + this.logger.info(result); + } + }