增加微信支付转换短链接API #101

This commit is contained in:
Binary Wang 2017-03-27 14:22:33 +08:00
parent d7d5b169d1
commit 96d72fe542
6 changed files with 143 additions and 4 deletions

View File

@ -0,0 +1,46 @@
package com.github.binarywang.wxpay.bean.request;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* <pre>
* 转换短链接请求对象类
* Created by Binary Wang on 2017-3-27.
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*/
@XStreamAlias("xml")
public class WxPayShorturlRequest extends WxPayBaseRequest {
/**
* <pre>
* URL链接
* long_url
*
* String(512)
* weixin//wxpay/bizpayurl?sign=XXXXX&appid=XXXXX&mch_id=XXXXX&product_id=XXXXXX&time_stamp=XXXXXX&nonce_str=XXXXX
* 需要转换的URL签名用原串传输需URLencode
* </pre>
*/
@XStreamAlias("long_url")
private String longUrl;
public String getLongUrl() {
return this.longUrl;
}
public void setLongUrl(String longUrl) {
this.longUrl = longUrl;
}
public WxPayShorturlRequest() {
}
public WxPayShorturlRequest(String longUrl) {
this.longUrl = longUrl;
}
@Override
protected void checkConstraints() {
//do nothing
}
}

View File

@ -9,6 +9,7 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*/
@XStreamAlias("xml")
public class WxPayOrderReverseResult extends WxPayBaseResult {
/**

View File

@ -0,0 +1,34 @@
package com.github.binarywang.wxpay.bean.result;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* <pre>
* 转换短链接结果对象类
* Created by Binary Wang on 2017-3-27.
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*/
@XStreamAlias("xml")
public class WxPayShorturlResult extends WxPayBaseResult {
/**
* <pre>
* URL链接
* short_url
*
* String(64)
* weixin//wxpay/s/XXXXXX
* 转换后的URL
* </pre>
*/
@XStreamAlias("short_url")
private String shortUrl;
public String getShortUrl() {
return this.shortUrl;
}
public void setShortUrl(String shortUrl) {
this.shortUrl = shortUrl;
}
}

View File

@ -267,12 +267,39 @@ public interface WxPayService {
* 撤销订单API
* 文档地址https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_11&index=3
* 应用场景
* 支付交易返回失败或支付系统超时调用该接口撤销交易如果此订单用户支付失败微信支付系统会将此订单关闭如果用户支付成功微信支付系统会将此订单资金退还给用户
* 注意7天以内的交易单可调用撤销其他正常支付的单如需实现相同功能请调用申请退款API提交支付交易后调用查询订单API没有明确的支付结果再调用撤销订单API
* 支付交易返回失败或支付系统超时调用该接口撤销交易如果此订单用户支付失败微信支付系统会将此订单关闭
* 如果用户支付成功微信支付系统会将此订单资金退还给用户
* 注意7天以内的交易单可调用撤销其他正常支付的单如需实现相同功能请调用申请退款API
* 提交支付交易后调用查询订单API没有明确的支付结果再调用撤销订单API
* 调用支付接口后请勿立即调用撤销订单API建议支付后至少15s后再调用撤销订单接口
* 接口链接 https://api.mch.weixin.qq.com/secapi/pay/reverse
* 是否需要证书请求需要双向证书
*</pre>
* </pre>
*/
WxPayOrderReverseResult reverseOrder(WxPayOrderReverseRequest request) throws WxErrorException;
/**
* <pre>
* 转换短链接
* 应用场景
* 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX)减小二维码数据量提升扫描速度和精确度
* 接口地址https://api.mch.weixin.qq.com/tools/shorturl
* 是否需要证书
* </pre>
* @param request 请求对象
*/
String shorturl(WxPayShorturlRequest request) throws WxErrorException;
/**
* <pre>
* 转换短链接
* 应用场景
* 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX)减小二维码数据量提升扫描速度和精确度
* 接口地址https://api.mch.weixin.qq.com/tools/shorturl
* 是否需要证书
* </pre>
* @param longUrl 需要被压缩的网址
*/
String shorturl(String longUrl) throws WxErrorException;
}

View File

@ -328,6 +328,22 @@ public class WxPayServiceImpl implements WxPayService {
return result;
}
@Override
public String shorturl(WxPayShorturlRequest request) throws WxErrorException {
request.checkAndSign(this.getConfig());
String url = this.getPayBaseUrl() + "/tools/shorturl";
String responseContent = this.post(url, request.toXML());
WxPayShorturlResult result = WxPayBaseResult.fromXML(responseContent, WxPayShorturlResult.class);
result.checkResult(this);
return result.getShortUrl();
}
@Override
public String shorturl(String longUrl) throws WxErrorException {
return this.shorturl(new WxPayShorturlRequest(longUrl));
}
private String post(String url, String xmlParam) {
String requestString = xmlParam;
try {

View File

@ -28,10 +28,11 @@ import static org.testng.Assert.*;
@Test
@Guice(modules = ApiTestModule.class)
public class WxPayServiceImplTest {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Inject
protected WxPayService payService;
private WxPayService payService;
@Test
public void testGetPayInfo() throws Exception {
@ -221,10 +222,12 @@ public class WxPayServiceImplTest {
@Test
public void testGetConfig() throws Exception {
// no need to test
}
@Test
public void testSetConfig() throws Exception {
// no need to test
}
@Test
@ -236,4 +239,16 @@ public class WxPayServiceImplTest {
this.logger.info(result.toString());
}
@Test
public void testShorturl() throws Exception {
String longUrl = "weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXX&mch_id=XXXXX&product_id=XXXXXX&time_stamp=XXXXXX&nonce_str=XXXXX";
String result = this.payService.shorturl(new WxPayShorturlRequest(longUrl));
assertNotNull(result);
this.logger.info(result.toString());
result = this.payService.shorturl(longUrl);
assertNotNull(result);
this.logger.info(result.toString());
}
}