@@ -0,0 +1,46 @@ | |||
package com.github.binarywang.wxpay.bean.request; | |||
import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
/** | |||
* <pre> | |||
* 授权码查询openid接口请求对象类 | |||
* Created by Binary Wang on 2017-3-27. | |||
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
* </pre> | |||
*/ | |||
@XStreamAlias("xml") | |||
public class WxPayAuthcode2OpenidRequest extends WxPayBaseRequest { | |||
/** | |||
* <pre> | |||
* 授权码 | |||
* auth_code | |||
* 是 | |||
* String(128) | |||
* 扫码支付授权码,设备读取用户微信中的条码或者二维码信息 | |||
* </pre> | |||
*/ | |||
@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 | |||
} | |||
} |
@@ -0,0 +1,40 @@ | |||
package com.github.binarywang.wxpay.bean.result; | |||
import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
/** | |||
* <pre> | |||
* 授权码查询openid接口请求结果类 | |||
* Created by Binary Wang on 2017-3-27. | |||
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
* </pre> | |||
*/ | |||
@XStreamAlias("xml") | |||
public class WxPayAuthcode2OpenidResult extends WxPayBaseResult { | |||
/** | |||
* <pre> | |||
* 用户标识 | |||
* openid | |||
* 是 | |||
* String(128) | |||
* 用户在商户appid下的唯一标识 | |||
* </pre> | |||
*/ | |||
@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; | |||
} | |||
} |
@@ -281,6 +281,8 @@ public interface WxPayService { | |||
/** | |||
* <pre> | |||
* 转换短链接 | |||
* 文档地址: | |||
* 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 { | |||
/** | |||
* <pre> | |||
* 转换短链接 | |||
* 应用场景: | |||
* 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),减小二维码数据量,提升扫描速度和精确度。 | |||
* 接口地址:https://api.mch.weixin.qq.com/tools/shorturl | |||
* 是否需要证书:否 | |||
* </pre> | |||
* @see WxPayService#shorturl(WxPayShorturlRequest) | |||
* @param longUrl 需要被压缩的网址 | |||
*/ | |||
String shorturl(String longUrl) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 授权码查询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 | |||
* </pre> | |||
* @param request 请求对象 | |||
* @return openid | |||
*/ | |||
String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 授权码查询OPENID接口 | |||
* </pre> | |||
* @see WxPayService#authcode2Openid(WxPayAuthcode2OpenidRequest) | |||
* @param authCode 授权码 | |||
* @return openid | |||
*/ | |||
String authcode2Openid(String authCode) throws WxErrorException; | |||
} |
@@ -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 { | |||
@@ -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); | |||
} | |||
} |