微信支付相关代码独立成一个子模块
This commit is contained in:
@@ -1,195 +0,0 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import com.github.binarywang.utils.qrcode.QrcodeUtils;
|
||||
import com.google.inject.Inject;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.api.test.ApiTestModule;
|
||||
import me.chanjar.weixin.mp.api.test.TestConfigStorage;
|
||||
import me.chanjar.weixin.mp.bean.pay.request.*;
|
||||
import me.chanjar.weixin.mp.bean.pay.result.*;
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/**
|
||||
* 测试支付相关接口
|
||||
* Created by Binary Wang on 2016/7/28.
|
||||
*
|
||||
* @author binarywang (https://github.com/binarywang)
|
||||
*/
|
||||
@Test
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMpPayServiceImplTest {
|
||||
|
||||
@Inject
|
||||
protected WxMpService wxService;
|
||||
|
||||
@Test
|
||||
public void testGetPayInfo() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDownloadBill() throws Exception {
|
||||
File file = this.wxService.getPayService().downloadBill("20170101", "ALL", "GZIP", "1111111");
|
||||
assertNotNull(file);
|
||||
//必填字段为空时,抛出异常
|
||||
this.wxService.getPayService().downloadBill("", "", "", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReport() throws Exception {
|
||||
WxPayReportRequest request = new WxPayReportRequest();
|
||||
request.setInterfaceUrl("hahahah");
|
||||
request.setSignType("HMAC-SHA256");//貌似接口未校验此字段
|
||||
request.setExecuteTime(1000);
|
||||
request.setReturnCode("aaa");
|
||||
request.setResultCode("aaa");
|
||||
request.setUserIp("8.8.8");
|
||||
this.wxService.getPayService().report(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 需要证书的接口需要先执行该方法
|
||||
*/
|
||||
@Test
|
||||
public void setSSLKey() {
|
||||
TestConfigStorage config = (TestConfigStorage) this.wxService.getWxMpConfigStorage();
|
||||
config.setSslContextFilePath(config.getKeyPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#refund(WxPayRefundRequest)} .
|
||||
*/
|
||||
@Test(dependsOnMethods = {"setSSLKey"})
|
||||
public void testRefund() throws Exception {
|
||||
WxPayRefundRequest request = new WxPayRefundRequest();
|
||||
request.setOutRefundNo("aaa");
|
||||
request.setOutTradeNo("1111");
|
||||
request.setTotalFee(1222);
|
||||
request.setRefundFee(111);
|
||||
WxPayRefundResult result = this.wxService.getPayService().refund(request);
|
||||
System.err.println(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#refundQuery(String, String, String, String)} .
|
||||
*/
|
||||
@Test
|
||||
public void testRefundQuery() throws Exception {
|
||||
WxPayRefundQueryResult result;
|
||||
|
||||
result = this.wxService.getPayService().refundQuery("1", "", "", "");
|
||||
System.err.println(result);
|
||||
result = this.wxService.getPayService().refundQuery("", "2", "", "");
|
||||
System.err.println(result);
|
||||
result = this.wxService.getPayService().refundQuery("", "", "3", "");
|
||||
System.err.println(result);
|
||||
result = this.wxService.getPayService().refundQuery("", "", "", "4");
|
||||
System.err.println(result);
|
||||
//测试四个参数都填的情况,应该报异常的
|
||||
result = this.wxService.getPayService().refundQuery("1", "2", "3", "4");
|
||||
System.err.println(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#sendRedpack(WxPaySendRedpackRequest)} .
|
||||
*/
|
||||
@Test(dependsOnMethods = {"setSSLKey"})
|
||||
public void testSendRedpack() throws Exception {
|
||||
WxPaySendRedpackRequest request = new WxPaySendRedpackRequest();
|
||||
request.setActName("abc");
|
||||
request.setClientIp("aaa");
|
||||
request.setMchBillNo("aaaa");
|
||||
request
|
||||
.setReOpenid(((TestConfigStorage) this.wxService.getWxMpConfigStorage()).getOpenid());
|
||||
WxPaySendRedpackResult redpackResult = this.wxService.getPayService().sendRedpack(request);
|
||||
System.err.println(redpackResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#queryRedpack(String)}.
|
||||
*/
|
||||
@Test(dependsOnMethods = {"setSSLKey"})
|
||||
public void testQueryRedpack() throws Exception {
|
||||
WxPayRedpackQueryResult redpackResult = this.wxService.getPayService().queryRedpack("aaaa");
|
||||
System.err.println(redpackResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#unifiedOrder(WxPayUnifiedOrderRequest)}.
|
||||
*/
|
||||
@Test
|
||||
public void testUnifiedOrder() throws WxErrorException {
|
||||
WxPayUnifiedOrderResult result = this.wxService.getPayService()
|
||||
.unifiedOrder(WxPayUnifiedOrderRequest.builder().body("1111111")
|
||||
.totalFee(1).spbillCreateIp("111111").notifyURL("111111")
|
||||
.tradeType("JSAPI").openid("122").outTradeNo("111111").build());
|
||||
System.err.println(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#queryOrder(String, String)} .
|
||||
*/
|
||||
@Test
|
||||
public final void testQueryOrder() throws WxErrorException {
|
||||
//System.err.println(this.wxService.getPayService().queryOrder(null, null));
|
||||
System.err.println(this.wxService.getPayService().queryOrder("11212121", null));
|
||||
System.err.println(this.wxService.getPayService().queryOrder(null, "11111"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#closeOrder(String)} .
|
||||
*/
|
||||
@Test
|
||||
public final void testCloseOrder() throws WxErrorException {
|
||||
System.err.println(this.wxService.getPayService().closeOrder("11212121"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#entPay(WxEntPayRequest)}.
|
||||
*/
|
||||
@Test(dependsOnMethods = {"setSSLKey"})
|
||||
public final void testEntPay() throws WxErrorException {
|
||||
WxEntPayRequest request = new WxEntPayRequest();
|
||||
System.err.println(this.wxService.getPayService().entPay(request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#queryEntPay(String)}.
|
||||
*/
|
||||
@Test(dependsOnMethods = {"setSSLKey"})
|
||||
public final void testQueryEntPay() throws WxErrorException {
|
||||
System.err.println(this.wxService.getPayService().queryEntPay("11212121"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateScanPayQrcodeMode1() throws Exception {
|
||||
String productId = "abc";
|
||||
byte[] bytes = this.wxService.getPayService().createScanPayQrcodeMode1(productId, null, null);
|
||||
Path qrcodeFilePath = Files.createTempFile("qrcode_", ".jpg");
|
||||
Files.write(qrcodeFilePath, bytes);
|
||||
String qrcodeContent = QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile());
|
||||
System.out.println(qrcodeContent);
|
||||
|
||||
assertTrue(qrcodeContent.startsWith("weixin://wxpay/bizpayurl?"));
|
||||
assertTrue(qrcodeContent.contains("product_id=" + productId));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateScanPayQrcodeMode2() throws Exception {
|
||||
String qrcodeContent = "abc";
|
||||
byte[] bytes = this.wxService.getPayService().createScanPayQrcodeMode2(qrcodeContent, null, null);
|
||||
Path qrcodeFilePath = Files.createTempFile("qrcode_", ".jpg");
|
||||
Files.write(qrcodeFilePath, bytes);
|
||||
|
||||
assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -64,10 +64,4 @@ public class TestConfigStorage extends WxMpInMemoryConfigStorage {
|
||||
super.accessTokenLock = lock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean useSandboxForWxPay() {
|
||||
return false;
|
||||
//沙箱环境不成熟,有问题无法使用,暂时屏蔽掉
|
||||
// return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
package me.chanjar.weixin.mp.bean.pay.result;
|
||||
|
||||
import org.testng.*;
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* Created by Binary Wang on 2017-01-04.
|
||||
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
|
||||
* </pre>
|
||||
*/
|
||||
public class WxPayBaseResultTest {
|
||||
@Test
|
||||
public void testToMap() throws Exception {
|
||||
WxPayOrderQueryResult result = new WxPayOrderQueryResult();
|
||||
result.setXmlString("<xml>\n" +
|
||||
" <return_code><![CDATA[SUCCESS]]></return_code>\n" +
|
||||
" <return_msg><![CDATA[OK]]></return_msg>\n" +
|
||||
" <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
|
||||
" <mch_id><![CDATA[10000100]]></mch_id>\n" +
|
||||
" <device_info><![CDATA[1000]]></device_info>\n" +
|
||||
" <nonce_str><![CDATA[TN55wO9Pba5yENl8]]></nonce_str>\n" +
|
||||
" <sign><![CDATA[BDF0099C15FF7BC6B1585FBB110AB635]]></sign>\n" +
|
||||
" <result_code><![CDATA[SUCCESS]]></result_code>\n" +
|
||||
" <openid><![CDATA[oUpF8uN95-Ptaags6E_roPHg7AG0]]></openid>\n" +
|
||||
" <is_subscribe><![CDATA[Y]]></is_subscribe>\n" +
|
||||
" <trade_type><![CDATA[MICROPAY]]></trade_type>\n" +
|
||||
" <bank_type><![CDATA[CCB_DEBIT]]></bank_type>\n" +
|
||||
" <total_fee>1</total_fee>\n" +
|
||||
" <fee_type><![CDATA[CNY]]></fee_type>\n" +
|
||||
" <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>\n" +
|
||||
" <out_trade_no><![CDATA[1415757673]]></out_trade_no>\n" +
|
||||
" <attach><![CDATA[订单额外描述]]></attach>\n" +
|
||||
" <time_end><![CDATA[20141111170043]]></time_end>\n" +
|
||||
" <trade_state><![CDATA[SUCCESS]]></trade_state>\n" +
|
||||
"</xml>");
|
||||
Map<String, String> map = result.toMap();
|
||||
System.out.println(map);
|
||||
|
||||
Assert.assertEquals("SUCCESS", map.get("return_code"));
|
||||
Assert.assertEquals("订单额外描述", map.get("attach"));
|
||||
|
||||
result.setXmlString("");
|
||||
System.out.println(result.toMap());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package me.chanjar.weixin.mp.bean.pay.result;
|
||||
|
||||
import org.testng.*;
|
||||
import org.testng.annotations.*;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* Created by Binary Wang on 2017-01-04.
|
||||
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
|
||||
* </pre>
|
||||
*/
|
||||
public class WxPayOrderQueryResultTest {
|
||||
@Test
|
||||
public void testComposeCoupons() throws Exception {
|
||||
/**
|
||||
* xml样例字符串来自于官方文档,并稍加改造加入了coupon相关的数据便于测试
|
||||
*/
|
||||
String xmlString = "<xml>\n" +
|
||||
" <return_code><![CDATA[SUCCESS]]></return_code>\n" +
|
||||
" <return_msg><![CDATA[OK]]></return_msg>\n" +
|
||||
" <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
|
||||
" <mch_id><![CDATA[10000100]]></mch_id>\n" +
|
||||
" <device_info><![CDATA[1000]]></device_info>\n" +
|
||||
" <nonce_str><![CDATA[TN55wO9Pba5yENl8]]></nonce_str>\n" +
|
||||
" <sign><![CDATA[BDF0099C15FF7BC6B1585FBB110AB635]]></sign>\n" +
|
||||
" <result_code><![CDATA[SUCCESS]]></result_code>\n" +
|
||||
" <openid><![CDATA[oUpF8uN95-Ptaags6E_roPHg7AG0]]></openid>\n" +
|
||||
" <is_subscribe><![CDATA[Y]]></is_subscribe>\n" +
|
||||
" <trade_type><![CDATA[MICROPAY]]></trade_type>\n" +
|
||||
" <bank_type><![CDATA[CCB_DEBIT]]></bank_type>\n" +
|
||||
" <total_fee>1</total_fee>\n" +
|
||||
" <fee_type><![CDATA[CNY]]></fee_type>\n" +
|
||||
" <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>\n" +
|
||||
" <out_trade_no><![CDATA[1415757673]]></out_trade_no>\n" +
|
||||
" <attach><![CDATA[订单额外描述]]></attach>\n" +
|
||||
" <time_end><![CDATA[20141111170043]]></time_end>\n" +
|
||||
" <trade_state><![CDATA[SUCCESS]]></trade_state>\n" +
|
||||
" <coupon_count>2</coupon_count>\n" +
|
||||
" <coupon_type_0><![CDATA[CASH]]></coupon_type_0>\n" +
|
||||
" <coupon_id_0>10000</coupon_id_0>\n" +
|
||||
" <coupon_fee_0>100</coupon_fee_0>\n" +
|
||||
" <coupon_type_1><![CDATA[NO_CASH]]></coupon_type_1>\n" +
|
||||
" <coupon_id_1>10001</coupon_id_1>\n" +
|
||||
" <coupon_fee_1>200</coupon_fee_1>\n" +
|
||||
"</xml>";
|
||||
|
||||
WxPayOrderQueryResult orderQueryResult = WxPayOrderQueryResult.fromXML(xmlString, WxPayOrderQueryResult.class);
|
||||
orderQueryResult.composeCoupons();
|
||||
|
||||
Assert.assertEquals(orderQueryResult.getCouponCount().intValue(), 2);
|
||||
Assert.assertNotNull(orderQueryResult.getCoupons());
|
||||
Assert.assertEquals(orderQueryResult.getCoupons().size(), 2);
|
||||
|
||||
Assert.assertEquals(orderQueryResult.getCoupons().get(0).getCouponFee().intValue(), 100);
|
||||
Assert.assertEquals(orderQueryResult.getCoupons().get(1).getCouponFee().intValue(), 200);
|
||||
|
||||
Assert.assertEquals(orderQueryResult.getCoupons().get(0).getCouponType(), "CASH");
|
||||
Assert.assertEquals(orderQueryResult.getCoupons().get(1).getCouponType(), "NO_CASH");
|
||||
|
||||
Assert.assertEquals(orderQueryResult.getCoupons().get(0).getCouponId(), "10000");
|
||||
Assert.assertEquals(orderQueryResult.getCoupons().get(1).getCouponId(), "10001");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package me.chanjar.weixin.mp.bean.pay.result;
|
||||
|
||||
|
||||
import org.testng.*;
|
||||
import org.testng.annotations.*;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* Created by Binary Wang on 2016-12-29.
|
||||
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
|
||||
* </pre>
|
||||
*/
|
||||
public class WxPayRefundQueryResultTest {
|
||||
@Test
|
||||
public void composeRefundRecords() throws Exception {
|
||||
/*
|
||||
该xml字符串来自于官方文档示例
|
||||
*/
|
||||
String xmlString = "<xml>\n" +
|
||||
" <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
|
||||
" <mch_id><![CDATA[10000100]]></mch_id>\n" +
|
||||
" <nonce_str><![CDATA[TeqClE3i0mvn3DrK]]></nonce_str>\n" +
|
||||
" <out_refund_no_0><![CDATA[1415701182]]></out_refund_no_0>\n" +
|
||||
" <out_trade_no><![CDATA[1415757673]]></out_trade_no>\n" +
|
||||
" <refund_count>1</refund_count>\n" +
|
||||
" <refund_fee_0>1</refund_fee_0>\n" +
|
||||
" <refund_id_0><![CDATA[2008450740201411110000174436]]></refund_id_0>\n" +
|
||||
" <refund_status_0><![CDATA[PROCESSING]]></refund_status_0>\n" +
|
||||
" <result_code><![CDATA[SUCCESS]]></result_code>\n" +
|
||||
" <return_code><![CDATA[SUCCESS]]></return_code>\n" +
|
||||
" <return_msg><![CDATA[OK]]></return_msg>\n" +
|
||||
" <sign><![CDATA[1F2841558E233C33ABA71A961D27561C]]></sign>\n" +
|
||||
" <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>\n" +
|
||||
"</xml>";
|
||||
|
||||
WxPayRefundQueryResult result = WxPayRefundQueryResult.fromXML(xmlString, WxPayRefundQueryResult.class);
|
||||
result.composeRefundRecords();
|
||||
|
||||
Assert.assertNotNull(result.getRefundRecords());
|
||||
Assert.assertEquals(result.getRefundRecords().size(), 1);
|
||||
Assert.assertEquals(result.getRefundRecords().get(0).getRefundId(), "2008450740201411110000174436");
|
||||
Assert.assertEquals(result.getRefundRecords().get(0).getRefundFee().intValue(), 1);
|
||||
Assert.assertEquals(result.getRefundRecords().get(0).getOutRefundNo(), "1415701182");
|
||||
Assert.assertEquals(result.getRefundRecords().get(0).getRefundStatus(), "PROCESSING");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package me.chanjar.weixin.mp.bean.pay.result;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||
import org.testng.*;
|
||||
import org.testng.annotations.*;
|
||||
|
||||
public class WxPaySendRedpackResultTest {
|
||||
|
||||
private XStream xstream;
|
||||
|
||||
@BeforeTest
|
||||
public void setup() {
|
||||
this.xstream = XStreamInitializer.getInstance();
|
||||
this.xstream.processAnnotations(WxPaySendRedpackResult.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadSuccessResult() {
|
||||
final String successSample = "<xml>\n" +
|
||||
"<return_code><![CDATA[SUCCESS]]></return_code>\n" +
|
||||
"<return_msg><![CDATA[发放成功.]]></return_msg>\n" +
|
||||
"<result_code><![CDATA[SUCCESS]]></result_code>\n" +
|
||||
"<err_code><![CDATA[0]]></err_code>\n" +
|
||||
"<err_code_des><![CDATA[发放成功.]]></err_code_des>\n" +
|
||||
"<mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno>\n" +
|
||||
"<mch_id>10010404</mch_id>\n" +
|
||||
"<wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid>\n" +
|
||||
"<re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid>\n" +
|
||||
"<total_amount>1</total_amount>\n" +
|
||||
"<send_listid>100000000020150520314766074200</send_listid>\n" +
|
||||
"<send_time>20150520102602</send_time>\n" +
|
||||
"</xml>";
|
||||
WxPaySendRedpackResult wxMpRedpackResult = (WxPaySendRedpackResult) this.xstream.fromXML(successSample);
|
||||
Assert.assertEquals("SUCCESS", wxMpRedpackResult.getReturnCode());
|
||||
Assert.assertEquals("SUCCESS", wxMpRedpackResult.getResultCode());
|
||||
Assert.assertEquals("20150520102602", wxMpRedpackResult.getSendTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadFailureResult() {
|
||||
final String failureSample = "<xml>\n" +
|
||||
"<return_code><![CDATA[FAIL]]></return_code>\n" +
|
||||
"<return_msg><![CDATA[系统繁忙,请稍后再试.]]></return_msg>\n" +
|
||||
"<result_code><![CDATA[FAIL]]></result_code>\n" +
|
||||
"<err_code><![CDATA[268458547]]></err_code>\n" +
|
||||
"<err_code_des><![CDATA[系统繁忙,请稍后再试.]]></err_code_des>\n" +
|
||||
"<mch_billno><![CDATA[0010010404201411170000046542]]></mch_billno>\n" +
|
||||
"<mch_id>10010404</mch_id>\n" +
|
||||
"<wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid>\n" +
|
||||
"<re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid>\n" +
|
||||
"<total_amount>1</total_amount>\n" +
|
||||
"</xml>";
|
||||
WxPaySendRedpackResult wxMpRedpackResult = (WxPaySendRedpackResult) this.xstream.fromXML(failureSample);
|
||||
Assert.assertEquals("FAIL", wxMpRedpackResult.getReturnCode());
|
||||
Assert.assertEquals("FAIL", wxMpRedpackResult.getResultCode());
|
||||
Assert.assertEquals("onqOjjmM1tad-3ROpncN-yUfa6uI", wxMpRedpackResult.getReOpenid());
|
||||
Assert.assertEquals(1, wxMpRedpackResult.getTotalAmount());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user