From 0418f172f5452e3888749d06517cd50aee5d844d Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Mon, 27 Mar 2017 15:35:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BE=AE=E4=BF=A1=E6=94=AF?= =?UTF-8?q?=E4=BB=98=20=E6=8E=88=E6=9D=83=E7=A0=81=E6=9F=A5=E8=AF=A2OPENID?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3API=20#101?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/WxPayAuthcode2OpenidRequest.java | 46 +++++++++++++++++++ .../result/WxPayAuthcode2OpenidResult.java | 40 ++++++++++++++++ .../wxpay/service/WxPayService.java | 30 ++++++++++-- .../wxpay/service/impl/WxPayServiceImpl.java | 16 +++++++ .../service/impl/WxPayServiceImplTest.java | 19 ++++++-- 5 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayAuthcode2OpenidRequest.java create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayAuthcode2OpenidResult.java 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 000000000..594fa4960 --- /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 000000000..f56c61a77 --- /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 e61ff8633..e336cd21f 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 aea069433..c26d05839 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 3898d1377..93b0b8b55 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); + } + }