微信支付相关代码独立成一个子模块

This commit is contained in:
Binary Wang
2017-03-05 22:49:17 +08:00
parent 8a270c8f7e
commit eee954b736
49 changed files with 1596 additions and 1308 deletions

View File

@@ -0,0 +1,50 @@
package com.github.binarywang.wxpay.bean.result;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
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());
}
}

View File

@@ -0,0 +1,65 @@
package com.github.binarywang.wxpay.bean.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");
}
}

View File

@@ -0,0 +1,48 @@
package com.github.binarywang.wxpay.bean.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");
}
}

View File

@@ -0,0 +1,60 @@
package com.github.binarywang.wxpay.bean.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());
}
}