1 package com.foxinmy.weixin4j.base.test;
2
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.util.Calendar;
7 import java.util.Date;
8
9 import org.junit.Assert;
10 import org.junit.Test;
11
12 import com.foxinmy.weixin4j.exception.WeixinException;
13 import com.foxinmy.weixin4j.http.weixin.XmlResult;
14 import com.foxinmy.weixin4j.model.WeixinPayAccount;
15 import com.foxinmy.weixin4j.payment.WeixinPayProxy;
16 import com.foxinmy.weixin4j.payment.mch.MchPayPackage;
17 import com.foxinmy.weixin4j.payment.mch.MchPayRequest;
18 import com.foxinmy.weixin4j.payment.mch.MerchantResult;
19 import com.foxinmy.weixin4j.payment.mch.Order;
20 import com.foxinmy.weixin4j.payment.mch.PrePay;
21 import com.foxinmy.weixin4j.payment.mch.RefundRecord;
22 import com.foxinmy.weixin4j.payment.mch.RefundResult;
23 import com.foxinmy.weixin4j.sign.WeixinPaymentSignature;
24 import com.foxinmy.weixin4j.sign.WeixinSignature;
25 import com.foxinmy.weixin4j.type.CurrencyType;
26 import com.foxinmy.weixin4j.type.IdQuery;
27 import com.foxinmy.weixin4j.type.IdType;
28 import com.foxinmy.weixin4j.type.TradeType;
29 import com.foxinmy.weixin4j.type.mch.BillType;
30 import com.foxinmy.weixin4j.type.mch.RefundAccountType;
31 import com.foxinmy.weixin4j.util.Consts;
32
33
34
35
36
37
38
39
40
41
42 public class PayTest {
43 protected final static WeixinPayAccount ACCOUNT;
44 protected final static WeixinSignature SIGNATURE;
45 protected final static WeixinPayProxy PAY;
46
47 static {
48 ACCOUNT = new WeixinPayAccount("id", "支付秘钥", "商户号", "加载证书的密码,默认为商户号",
49 "证书文件路径");
50 SIGNATURE = new WeixinPaymentSignature(ACCOUNT.getPaySignKey());
51 PAY = new WeixinPayProxy(ACCOUNT);
52 }
53
54 @Test
55 public void queryOrder() throws WeixinException {
56 Order order = PAY.queryOrder(new IdQuery("BY2016010800025",
57 IdType.TRADENO));
58 Assert.assertEquals(Consts.SUCCESS, order.getReturnCode());
59 Assert.assertEquals(Consts.SUCCESS, order.getResultCode());
60 System.err.println(order);
61 String sign = order.getSign();
62 order.setSign(null);
63 String valiSign = SIGNATURE.sign(order);
64 System.err
65 .println(String.format("sign=%s,valiSign=%s", sign, valiSign));
66 Assert.assertEquals(valiSign, sign);
67 }
68
69 @Test
70 public void queryRefund() throws WeixinException {
71 RefundRecord record = PAY.queryRefund(new IdQuery("TT_1427183696238",
72 IdType.TRADENO));
73 Assert.assertEquals(Consts.SUCCESS, record.getReturnCode());
74 Assert.assertEquals(Consts.SUCCESS, record.getResultCode());
75 System.err.println(record);
76 String sign = record.getSign();
77 record.setSign(null);
78 String valiSign = SIGNATURE.sign(record);
79 System.err
80 .println(String.format("sign=%s,valiSign=%s", sign, valiSign));
81 Assert.assertEquals(valiSign, sign);
82 }
83
84 @Test
85 public void downbill() throws WeixinException, IOException {
86 Calendar c = Calendar.getInstance();
87
88
89
90 c.add(Calendar.DAY_OF_MONTH, -1);
91 System.err.println(c.getTime());
92 OutputStream os = new FileOutputStream("/tmp/bill20160813.txt");
93 PAY.downloadBill(c.getTime(), BillType.ALL, os, null);
94 }
95
96 @Test
97 public void refund() throws WeixinException, IOException {
98 IdQuery idQuery = new IdQuery("TT_1427183696238", IdType.TRADENO);
99 RefundResult result = PAY.applyRefund(idQuery,
100 "TT_R" + System.currentTimeMillis(), 0.01d, 0.01d,
101 CurrencyType.CNY, "10020674", "退款描述",
102 RefundAccountType.REFUND_SOURCE_RECHARGE_FUNDS);
103 Assert.assertEquals(Consts.SUCCESS, result.getReturnCode());
104 Assert.assertEquals(Consts.SUCCESS, result.getResultCode());
105 System.err.println(result);
106 String sign = result.getSign();
107 result.setSign(null);
108 String valiSign = SIGNATURE.sign(result);
109 System.err
110 .println(String.format("sign=%s,valiSign=%s", sign, valiSign));
111 Assert.assertEquals(valiSign, sign);
112 }
113
114 @Test
115 public void nativePay() throws WeixinException {
116 MchPayPackage payPackageV3 = new MchPayPackage("native测试", "T0001",
117 0.1d, "notify_url", "127.0.0.1", TradeType.NATIVE, null, null,
118 "productId", null);
119 PrePay result = PAY.createPrePay(payPackageV3);
120 Assert.assertEquals(Consts.SUCCESS, result.getReturnCode());
121 Assert.assertEquals(Consts.SUCCESS, result.getResultCode());
122 System.err.println(result);
123 }
124
125 @Test
126 public void closeOrder() throws WeixinException {
127 MerchantResult result = PAY.closeOrder("D111");
128 Assert.assertEquals(Consts.SUCCESS, result.getReturnCode());
129 Assert.assertEquals(Consts.SUCCESS, result.getResultCode());
130 System.err.println(result);
131 String sign = result.getSign();
132 result.setSign(null);
133 String valiSign = SIGNATURE.sign(result);
134 System.err
135 .println(String.format("sign=%s,valiSign=%s", sign, valiSign));
136 Assert.assertEquals(valiSign, sign);
137 }
138
139 @Test
140 public void shortUrl() throws WeixinException {
141 String url = "weixin://wxpay/bizpayurl?xxxxxx";
142 String shortUrl = PAY.getPayShorturl(url);
143 System.err.println(shortUrl);
144 }
145
146 @Test
147 public void interfaceReport() throws WeixinException {
148 String interfaceUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
149 int executeTime = 2500;
150 String outTradeNo = null;
151 String ip = "127.0.0.1";
152 Date time = new Date();
153 XmlResult returnXml = new XmlResult("SUCCESS", "");
154 returnXml.setResultCode("SUCCESS");
155 returnXml = PAY.reportInterface(interfaceUrl, executeTime, outTradeNo,
156 ip, time, returnXml);
157 Assert.assertEquals(Consts.SUCCESS, returnXml.getReturnCode());
158 Assert.assertEquals(Consts.SUCCESS, returnXml.getResultCode());
159 System.err.println(returnXml);
160 }
161
162 @Test
163 public void testMicroPay() throws WeixinException {
164 String authCode = "扫描码";
165 String body = "商品描述";
166 String outTradeNo = "M001";
167 double totalFee = 1d;
168 String createIp = "127.0.0.1";
169 MchPayRequest request = PAY.createMicroPayRequest(authCode, body,
170 outTradeNo, totalFee, createIp, null, null);
171 System.err.println(request);
172 }
173 }