重构Weixin4jSettings...

This commit is contained in:
jinyu
2016-02-02 19:48:30 +08:00
parent baff97e9db
commit 8a9f93e54e
39 changed files with 636 additions and 1020 deletions
-64
View File
@@ -35,70 +35,6 @@ weixin4j-mp
如何使用
--------
0.maven依赖(1.6.6,2015-12-31 released)
<dependency>
<groupId>com.foxinmy</groupId>
<artifactId>weixin4j-mp</artifactId>
<version>1.6.6</version>
</dependency>
1.需新增或拷贝`weixin4j.properties`文件到项目的`classpath`
weixin4j.properties说明
| 属性名 | 说明 |
| :---------- | :-------------- |
| weixin4j.account | 微信公众号信息 `json格式`(使用new WeixinProxy()缺省构造器时须填写) |
| weixin4j.token.path | 使用FileTokenStorager时token保存的物理路径(非必须填写) |
| weixin4j.qrcode.path | 调用二维码接口时保存二维码图片的物理路径(非必须填写) |
| weixin4j.media.path | 调用媒体接口时保存媒体文件的物理路径(非必须填写) |
| weixin4j.bill.path | 调用下载对账单接口保存文件的物理路径(非必须填写) |
| weixin4j.certificate.file | 调用某些接口(支付相关)强制需要auth的ca授权文件(按须填写) |
| weixin4j.user.oauth.redirect.uri | 调用OauthApi接口时需要填写的重定向路径(非必须填写) |
完整填写示例(properties中换行用右斜杆\\)
weixin4j.account={"id":"appid","secret":"appsecret",\
"mchId":"V3.x版本下的微信商户号 微信支付时需要填入",\
"certificateKey":"加载支付证书文件的密码 如果不填写则默认获取mchId作为密码",\
"partnerId":"V2版本下的财付通的商户号 微信支付时需要填入",\
"partnerKey":"V2版本下的财付通商户权限密钥Key 微信支付时需要填入",\
"paySignKey":"微信支付中调用API的密钥 微信支付时需要填入"}
# 使用FileTokenStorager时token的存放路径(如果不填则默认为Weixin4jConst#DEFAULT_TOKEN_PATH)
weixin4j.token.path=/tmp/weixin4j/token
# 二维码保存路径(如果不填则默认为Weixin4jConst#DEFAULT_TOKEN_PATH)
weixin4j.qrcode.path=/tmp/weixin4j/qrcode
# 媒体文件保存路径(如果不填则默认为Weixin4jConst#DEFAULT_MEDIA_PATH)
weixin4j.media.path=/tmp/weixin4j/media
# 对账单保存路径(如果不填则默认为Weixin4jConst#DEFAULT_BILL_PATH)
weixin4j.bill.path=/tmp/weixin4j/bill
# ca证书存放的完整路径 (V2版本后缀为*.pfx,V3版本后缀为*.p12)
weixin4j.certificate.file=/tmp/weixin4j/xxxxx.p12
# classpath路径下可以这么写(如果不填则默认为Weixin4jConst#DEFAULT_CAFILE_PATH)
# weixin4j.certificate.file=classpath:xxxxx.pfx
# 用户oauth授权后重定向的url(在使用OauthApi时填写)
weixin4j.user.oauth.redirect.uri=http://xxx
2.实例化微信公众号接口代理对象,调用具体的API方法
// 微信公众号API 使用classpath的weixin4j.properties
WeixinProxy weixinProxy = new WeixinProxy();
// 直接传入公众号信息
// weixinProxy = new WeixinProxy(appid,appsecret);
weixinProxy.getUser(openId);
// 微信支付API 使用classpath的weixin4j.properties
WeixinPayProxy weixinPayProxy = new WeixinPayProxy();
// 直接构造WexinAccount对象
// weixinPayProxy = new WeixinPayProxy(weixinAccount);
weixinPayProxy.orderQuery(idQuery);
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenStorager`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用[RedisTokenStorager](../weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/RedisTokenStorager.java).
>
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenStorager());
> // weixinProxy = new WeixinProxy(new RedisTokenStorager(appid,appsecret));
[更新LOG](./CHANGE.md)
----------------------
@@ -47,7 +47,6 @@ import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.mp.type.DatacubeType;
import com.foxinmy.weixin4j.mp.type.IndustryType;
import com.foxinmy.weixin4j.mp.type.Lang;
import com.foxinmy.weixin4j.settings.Weixin4jSettings;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.tuple.MassTuple;
import com.foxinmy.weixin4j.tuple.MpArticle;
@@ -55,6 +54,7 @@ import com.foxinmy.weixin4j.tuple.MpVideo;
import com.foxinmy.weixin4j.tuple.Tuple;
import com.foxinmy.weixin4j.type.MediaType;
import com.foxinmy.weixin4j.type.TicketType;
import com.foxinmy.weixin4j.util.Weixin4jSettings;
/**
* 微信公众平台接口实现
@@ -92,13 +92,13 @@ public class WeixinProxy {
/**
*
* @param settings
* 配置信息
* @see com.foxinmy.weixin4j.settings.Weixin4jSettings
* 微信配置信息
* @see com.foxinmy.weixin4j.util.Weixin4jSettings
*/
public WeixinProxy(Weixin4jSettings settings) {
this(new TokenHolder(new WeixinTokenCreator(settings.getAccount()
.getId(), settings.getAccount().getSecret()),
settings.getTokenStorager()));
this(new TokenHolder(new WeixinTokenCreator(settings.getWeixinAccount()
.getId(), settings.getWeixinAccount().getSecret()),
settings.getTokenStorager0()));
this.settings = settings;
}
@@ -129,7 +129,7 @@ public class WeixinProxy {
* @return
*/
public WeixinAccount getWeixinAccount() {
return this.settings.getAccount();
return this.settings.getWeixinAccount();
}
/**
@@ -222,29 +222,6 @@ public class WeixinProxy {
return mediaApi.uploadMedia(isMaterial, is, fileName);
}
/**
* 下载媒体文件
* <p>
* 正常情况下返回表头如Content-Type: image/jpeg,否则抛出异常.
* </p>
*
* @param mediaId
* 存储在微信服务器上的媒体标识
* @param isMaterial
* 是否下载永久素材
* @return 写入硬盘后的文件对象
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html">上传下载说明</a>
* @see com.foxinmy.weixin4j.mp.api.MediaApi
* @see {@link #downloadMedia(String)}
*/
public File downloadMediaFile(String mediaId, boolean isMaterial)
throws WeixinException {
return mediaApi.downloadMediaFile(mediaId, isMaterial,
settings.getMediaPath());
}
/**
* 下载媒体文件
*
@@ -1192,19 +1169,6 @@ public class WeixinProxy {
public QRResult createQR(QRParameter parameter) throws WeixinException {
return qrApi.createQR(parameter);
}
/**
* 生成带参数的二维码
*
* @return 硬盘存储的文件对象
* @param parameter
* 二维码参数
* @throws WeixinException
* @see {@link #createQR(QRParameter)}
*/
public File createQRFile(QRParameter parameter) throws WeixinException {
return qrApi.createQRFile(parameter, settings.getQrcodePath());
}
/**
* 设置所属行业(每月可修改行业1次,账号仅可使用所属行业中相关的模板)
@@ -1,12 +1,8 @@
package com.foxinmy.weixin4j.mp.api;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
@@ -228,63 +224,6 @@ public class MediaApi extends MpApi {
}
}
/**
* 下载媒体素材
* <p>
* 正常情况下返回表头如Content-Type: image/jpeg,否则抛出异常.
* </p>
*
* @param mediaId
* 存储在微信服务器上的媒体标识
* @param isMaterial
* 是否下载永久素材
* @param mediaPath
* 媒体素材保存路径
* @return 写入硬盘后的文件对象
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/11/07b6b76a6b6e8848e855a435d5e34a5f.html">下载临时媒体文件</a>
* @see <a
* href="http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html">下载永久媒体素材</a>
* @see {@link #downloadMedia(String,boolean)}
*/
public File downloadMediaFile(String mediaId, boolean isMaterial,
String mediaPath) throws WeixinException {
final String prefixName = String.format("%s.", mediaId);
File[] files = new File(mediaPath).listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith(prefixName);
}
});
if (files.length > 0) {
return files[0];
}
MediaDownloadResult result = downloadMedia(mediaId, isMaterial);
File file = new File(mediaPath + File.separator + result.getFileName());
OutputStream os = null;
try {
if (file.createNewFile()) {
os = new FileOutputStream(file);
os.write(result.getContent());
} else {
throw new WeixinException(String.format("create file fail:%s",
file.getAbsolutePath()));
}
} catch (IOException e) {
throw new WeixinException(e);
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
;
}
}
return file;
}
/**
* 下载媒体素材
*
@@ -40,11 +40,7 @@ import com.foxinmy.weixin4j.mp.payment.v2.RefundRecordV2;
import com.foxinmy.weixin4j.mp.payment.v2.RefundResultV2;
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.payment.PayRequest;
import com.foxinmy.weixin4j.settings.Weixin4jSettings;
import com.foxinmy.weixin4j.settings.Weixin4jPaySettings;
import com.foxinmy.weixin4j.token.FileTokenStorager;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.token.TokenStorager;
import com.foxinmy.weixin4j.type.BillType;
import com.foxinmy.weixin4j.type.IdQuery;
import com.foxinmy.weixin4j.type.RefundType;
@@ -54,6 +50,7 @@ import com.foxinmy.weixin4j.util.DigestUtil;
import com.foxinmy.weixin4j.util.MapUtil;
import com.foxinmy.weixin4j.util.RandomUtil;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jSettings;
import com.foxinmy.weixin4j.xml.ListsuffixResultDeserializer;
/**
@@ -67,27 +64,22 @@ import com.foxinmy.weixin4j.xml.ListsuffixResultDeserializer;
*/
public class Pay2Api extends MpApi {
private final Weixin4jPaySettings settings;
private final Weixin4jSettings settings;
private final TokenHolder tokenHolder;
public Pay2Api() {
this(new Weixin4jPaySettings());
this(new Weixin4jSettings());
}
public Pay2Api(Weixin4jPaySettings settings) {
this(settings, new FileTokenStorager(
Weixin4jSettings.DEFAULT_TOKEN_PATH));
}
public Pay2Api(Weixin4jPaySettings settings, TokenStorager tokenStorager) {
public Pay2Api(Weixin4jSettings settings) {
this.tokenHolder = new TokenHolder(new WeixinTokenCreator(settings
.getWeixinAccount().getId(), settings.getWeixinAccount()
.getSecret()), settings.getTokenStorager0());
this.settings = settings;
this.tokenHolder = new TokenHolder(
new WeixinTokenCreator(settings.getPayAccount().getId(),
settings.getPayAccount().getSecret()), tokenStorager);
}
public WeixinPayAccount getPayAccount() {
return this.settings.getPayAccount();
return this.settings.getWeixinPayAccount();
}
/**
@@ -142,7 +134,7 @@ public class Pay2Api extends MpApi {
double totalFee, String notifyUrl, String createIp, String attach,
Date timeStart, Date timeExpire, double transportFee,
double productFee, String goodsTag) {
PayPackageV2 payPackage = new PayPackageV2(settings.getPayAccount()
PayPackageV2 payPackage = new PayPackageV2(getPayAccount()
.getPartnerId(), body, outTradeNo, totalFee, notifyUrl,
createIp);
payPackage.setAttach(attach);
@@ -458,8 +450,8 @@ public class Pay2Api extends MpApi {
String formatBillDate = DateUtil.fortmat2yyyyMMdd(billDate);
String fileName = String.format("%s_%s_%s.txt", formatBillDate,
billType.name().toLowerCase(), getPayAccount().getId());
File file = new File(String.format("%s/%s", settings.getBillPath(),
fileName));
File file = new File(String.format("%s/weixin4j_bill_%s",
settings.getTmpdir0(), fileName));
if (file.exists()) {
return file;
}
@@ -1,9 +1,6 @@
package com.foxinmy.weixin4j.mp.api;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.exception.WeixinException;
@@ -62,49 +59,4 @@ public class QrApi extends MpApi {
}
return result;
}
/**
* 生成带参数的二维码
* <p>
* 二维码分为临时跟永久两种,扫描时触发推送带参数事件
* </p>
*
* @param parameter
* 二维码参数
* @param qrcodePath
* 二维码保存路径
* @return 硬盘存储的文件对象
* @throws WeixinException
* @see <a
* href="mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html">二维码</a>
* @see #createQR(QRParameter)
* @see com.foxinmy.weixin4j.mp.model.QRParameter
*/
public File createQRFile(QRParameter parameter, String qrcodePath)
throws WeixinException {
String filename = String.format("%s_%s_%d.jpg", parameter.getQrType()
.name(), parameter.getSceneValue(), parameter
.getExpireSeconds());
File file = new File(qrcodePath + File.separator + filename);
if (parameter.getQrType().ordinal() > 0 && file.exists()) {
return file;
}
QRResult qrResult = createQR(parameter);
OutputStream os = null;
try {
os = new FileOutputStream(file);
os.write(qrResult.getContent());
} catch (IOException e) {
throw new WeixinException(e);
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
;
}
}
return file;
}
}
@@ -1,3 +0,0 @@
NotifyMessage: (客服消息)[http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html#.E5.AE.A2.E6.9C.8D.E6.8E.A5.E5.8F.A3-.E5.8F.91.E6.B6.88.E6.81.AF]
TemplateMessage: (模板消息)[http://mp.weixin.qq.com/wiki/17/304c1885ea66dbedf7dc170d84999a9d.html]
@@ -48,7 +48,7 @@ public class WeixinTicketCreator implements TokenCreator {
@Override
public String getCacheKey() {
return String.format("wx_mp_ticket_%s_%s", appid, ticketType.name());
return String.format("weixin4j_mp_ticket_%s_%s", appid, ticketType.name());
}
@Override
@@ -40,7 +40,7 @@ public class WeixinTokenCreator implements TokenCreator {
@Override
public String getCacheKey() {
return String.format("wx_mp_token_%s", appid);
return String.format("weixin4j_mp_token_%s", appid);
}
@Override
@@ -1,24 +1,22 @@
# \u6d4b\u8bd5\u4e4b\u7528 \u6b63\u5f0f\u73af\u5883\u4e0bcopy\u4e00\u4efd\u5230classpath
# \u516c\u4f17\u53f7\u4fe1\u606f
# \u516c\u4f17\u53f7\u4fe1\u606f \u8bf7\u6309\u9700\u586b\u5199
weixin4j.account={"id":"wx4ab8f8de58159a57","secret":"1d4eb0f4bf556aaed539f30ed05ca795",\
"mchId":"V3.x\u7248\u672c\u4e0b\u7684\u5fae\u4fe1\u5546\u6237\u53f7 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
"mchId":"\u5fae\u4fe1\u5546\u6237\u53f7 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
"certificateKey":"\u52a0\u8f7d\u652f\u4ed8\u8bc1\u4e66\u6587\u4ef6\u7684\u5bc6\u7801 \u5982\u679c\u4e0d\u586b\u5199\u5219\u9ed8\u8ba4\u83b7\u53d6mchId\u4f5c\u4e3a\u5bc6\u7801",\
"partnerId":"V2\u7248\u672c\u4e0b\u7684\u8d22\u4ed8\u901a\u7684\u5546\u6237\u53f7 \u8001\u7248\u672c\u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
"partnerKey":"V2\u7248\u672c\u4e0b\u7684\u8d22\u4ed8\u901a\u5546\u6237\u6743\u9650\u5bc6\u94a5Key \u8001\u7248\u672c\u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
"paySignKey":"\u5fae\u4fe1\u652f\u4ed8\u4e2d\u8c03\u7528API\u7684\u5bc6\u94a5 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165"}
# \u4f7f\u7528FileTokenStorager\u65f6token\u7684\u5b58\u653e\u8def\u5f84(\u5982\u679c\u4e0d\u586b\u5219\u9ed8\u8ba4\u4e3aWeixin4jConst#DEFAULT_TOKEN_PATH)
weixin4j.token.path=/tmp/weixin4j/token
# \u4e8c\u7ef4\u7801\u4fdd\u5b58\u8def\u5f84(\u5982\u679c\u4e0d\u586b\u5219\u9ed8\u8ba4\u4e3aWeixin4jConst#DEFAULT_TOKEN_PATH)
weixin4j.qrcode.path=/tmp/weixin4j/qrcode
# \u5a92\u4f53\u6587\u4ef6\u4fdd\u5b58\u8def\u5f84(\u5982\u679c\u4e0d\u586b\u5219\u9ed8\u8ba4\u4e3aWeixin4jConst#DEFAULT_MEDIA_PATH)
weixin4j.media.path=/tmp/weixin4j/media
# \u5bf9\u8d26\u5355\u4fdd\u5b58\u8def\u5f84(\u5982\u679c\u4e0d\u586b\u5219\u9ed8\u8ba4\u4e3aWeixin4jConst#DEFAULT_BILL_PATH)
weixin4j.bill.path=/tmp/weixin4j/bill
# ca\u8bc1\u4e66\u5b58\u653e\u7684\u5b8c\u6574\u8def\u5f84 (V2\u7248\u672c\u540e\u7f00\u4e3a*.pfx,V3\u7248\u672c\u540e\u7f00\u4e3a*.p12)
# weixin4j\u7684\u4e34\u65f6\u76ee\u5f55
# \u53ef\u80fd\u5b58\u653etoken\u6587\u4ef6\u3001\u4e8c\u7ef4\u7801\u6587\u4ef6\u3001\u5a92\u4f53\u6587\u4ef6\u3001\u5bf9\u8d26\u5355\u6587\u4ef6\u7b49
# \u4e3a\u7a7a\u65f6\u5219\u83b7\u53d6java.io.tmpdir\u4e34\u65f6\u76ee\u5f55
weixin4j.tmpdir=
# \u5fae\u4fe1\u652f\u4ed8\u67d0\u4e9b\u63a5\u53e3\u9700\u8981\u7684ca\u8bc1\u4e66\u5b58\u653e\u7684\u5b8c\u6574\u8def\u5f84
# classpath\u8def\u5f84\u4e0b\u53ef\u4ee5\u8fd9\u4e48\u5199
# weixin4j.certificate.file=classpath:xxxxx.p12
# \u4e3a\u7a7a\u65f6\u5219\u83b7\u53d6classpath\u6839\u76ee\u5f55\u4e0b\u7684ca.p12\u6587\u4ef6
weixin4j.certificate.file=/tmp/weixin4j/xxxxx.p12
# classpath\u8def\u5f84\u4e0b\u53ef\u4ee5\u8fd9\u4e48\u5199(\u5982\u679c\u4e0d\u586b\u5219\u9ed8\u8ba4\u4e3aWeixin4jConst#DEFAULT_CAFILE_PATH)
# weixin4j.certificate.file=classpath:xxxxx.pfx
# \u7528\u6237oauth\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528OauthApi\u65f6\u586b\u5199)
weixin4j.user.oauth.redirect.uri=
@@ -1,57 +0,0 @@
package com.foxinmy.weixin4j.mp.test;
import java.io.FileInputStream;
import java.io.IOException;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.payment.mch.MPPayment;
import com.foxinmy.weixin4j.payment.mch.MPPaymentResult;
import com.foxinmy.weixin4j.payment.mch.Redpacket;
import com.foxinmy.weixin4j.payment.mch.RedpacketRecord;
import com.foxinmy.weixin4j.payment.mch.RedpacketSendResult;
import com.foxinmy.weixin4j.type.MPPaymentCheckNameType;
/**
* 现金发放测试
*
* @className CashTest
* @author jy
* @date 2015年4月1日
* @since JDK 1.6
* @see
*/
public class CashTest extends PayTest {
@Test
public void sendRedpacket() throws WeixinException, IOException {
Redpacket redpacket = new Redpacket("HB001", "无忧钱庄",
"oyFLst1bqtuTcxK-ojF8hOGtLQao", 1d, 1);
redpacket.setActName("红包测试");
redpacket.setClientIp("127.0.0.1");
redpacket.setRemark("快来领取红包吧!");
redpacket.setWishing("来就送钱");
RedpacketSendResult result = PAY3.sendRedpack(new FileInputStream(
caFile), redpacket);
System.err.println(result);
}
@Test
public void queryRedpacket() throws WeixinException, IOException {
String outTradeNo = "HB001";
RedpacketRecord record = PAY3.queryRedpack(new FileInputStream(caFile),
outTradeNo);
System.err.println(record);
}
@Test
public void mpPayment() throws WeixinException, IOException {
MPPayment payment = new MPPayment("MP001",
"oyFLst1bqtuTcxK-ojF8hOGtLQao",
MPPaymentCheckNameType.NO_CHECK, "企业付款测试", 0.01d, "127.0.0.1");
MPPaymentResult result = PAY3.mpPayment(new FileInputStream(caFile),
payment);
System.err.println(result);
}
}
@@ -1,48 +0,0 @@
package com.foxinmy.weixin4j.mp.test;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Consts;
import com.foxinmy.weixin4j.payment.coupon.CouponDetail;
import com.foxinmy.weixin4j.payment.coupon.CouponResult;
import com.foxinmy.weixin4j.payment.coupon.CouponStock;
import com.foxinmy.weixin4j.util.DateUtil;
/**
* 代金券测试
*
* @className CouponTest
* @author jy
* @date 2015年3月25日
* @since JDK 1.6
* @see
*/
public class CouponTest extends PayTest {
@Test
public void sendCoupon() throws WeixinException, IOException {
String partnerTradeNo = String.format("%s%s%s", ACCOUNT3.getMchId(),
DateUtil.fortmat2yyyyMMdd(new Date()), "1");
CouponResult result = PAY3.sendCoupon(new FileInputStream(caFile),
"123", partnerTradeNo, "oyFLst1bqtuTcxK-ojF8hOGtLQao", null);
Assert.assertTrue(result.getRetCode().equalsIgnoreCase(Consts.SUCCESS));
}
@Test
public void queryCouponStock() throws WeixinException {
CouponStock couponStock = PAY3.queryCouponStock("couponStockId");
System.err.println(couponStock);
}
@Test
public void queryCouponDetail() throws WeixinException {
CouponDetail couponDetail = PAY3.queryCouponDetail("couponId");
System.err.println(couponDetail);
}
}
@@ -92,13 +92,6 @@ public class MediaTest extends TokenTest {
System.err.println(mediaId);
}
@Test
public void download2() throws WeixinException, IOException {
File file = mediaApi.downloadMediaFile("8790403529", true,
"/tmp/weixin4j/media");
Assert.assertTrue(file.exists());
}
@Test
public void downloadArticle() throws WeixinException {
List<MpArticle> articles = mediaApi.downloadArticle("17385064953");
@@ -1,44 +1,34 @@
package com.foxinmy.weixin4j.mp.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.exception.WeixinPayException;
import com.foxinmy.weixin4j.http.weixin.XmlResult;
import com.foxinmy.weixin4j.model.WeixinPayAccount;
import com.foxinmy.weixin4j.mp.api.Pay2Api;
import com.foxinmy.weixin4j.payment.WeixinPayProxy;
import com.foxinmy.weixin4j.payment.mch.ApiResult;
import com.foxinmy.weixin4j.payment.mch.MchPayPackage;
import com.foxinmy.weixin4j.payment.mch.Order;
import com.foxinmy.weixin4j.payment.mch.PrePay;
import com.foxinmy.weixin4j.settings.Weixin4jPaySettings;
import com.foxinmy.weixin4j.type.IdQuery;
import com.foxinmy.weixin4j.type.IdType;
import com.foxinmy.weixin4j.type.TradeType;
import com.foxinmy.weixin4j.util.DigestUtil;
import com.foxinmy.weixin4j.util.Weixin4jSettings;
/**
* 支付测试(V2版本 2014年9月之前申请微信支付的公众号)
*
* @className PayTest
* @author jy
* @date 2016年1月30日
* @since JDK 1.7
* @see
*/
public class PayTest {
protected final static Pay2Api PAY2;
protected final static WeixinPayProxy PAY3;
protected final static WeixinPayAccount ACCOUNT2;
protected final static WeixinPayAccount ACCOUNT3;
static {
ACCOUNT2 = new WeixinPayAccount("请填入v2版本的appid", "请填入v2版本的appSecret",
"请填入v2版本的paysignkey", null, null, null, null,
"请填入v2版本的partnerId", "请填入v2版本的partnerKey");
PAY2 = new Pay2Api(new Weixin4jPaySettings(ACCOUNT2));
ACCOUNT3 = new WeixinPayAccount("请填入v3版本的appid", "请填入v3版本的appSecret",
"请填入v3版本的paysignkey", "请填入v3版本的mchid", null, null, null, null,
null);
PAY3 = new WeixinPayProxy(new Weixin4jPaySettings(ACCOUNT3));
PAY2 = new Pay2Api(new Weixin4jSettings(ACCOUNT2));
}
/**
* 商户证书文件
@@ -62,7 +52,6 @@ public class PayTest {
public void refundQueryV2() throws WeixinException {
System.err.println(PAY2.refundQuery(new IdQuery("D14123000004",
IdType.TRADENO)));
refundQueryV3();
}
@Test
@@ -74,110 +63,4 @@ public class PayTest {
File file = PAY2.downloadBill(c.getTime(), null);
System.err.println(file);
}
@Test
public void orderQueryV3() throws WeixinException {
Order order = PAY3.orderQuery(new IdQuery("T0002", IdType.TRADENO));
System.err.println(order);
String sign = order.getSign();
order.setSign(null);
String valiSign = DigestUtil
.paysignMd5(order, ACCOUNT3.getPaySignKey());
System.err
.println(String.format("sign=%s,valiSign=%s", sign, valiSign));
Assert.assertEquals(valiSign, sign);
}
@Test
public void refundQueryV3() throws WeixinException {
com.foxinmy.weixin4j.payment.mch.RefundRecord record = PAY3
.refundQuery(new IdQuery("TT_1427183696238", IdType.TRADENO));
System.err.println(record);
// 这里的验证签名需要把details循环拼接
String sign = record.getSign();
record.setSign(null);
String valiSign = DigestUtil.paysignMd5(record,
ACCOUNT3.getPaySignKey());
System.err
.println(String.format("sign=%s,valiSign=%s", sign, valiSign));
Assert.assertEquals(valiSign, sign);
}
@Test
public void downbillV3() throws WeixinException {
Calendar c = Calendar.getInstance();
System.err.println(c.getTime());
c.set(Calendar.YEAR, 2015);
c.set(Calendar.MONTH, 2);
c.set(Calendar.DAY_OF_MONTH, 24);
System.err.println(c.getTime());
File file = PAY3.downloadBill(c.getTime(), null);
System.err.println(file);
}
@Test
public void refundV3() throws WeixinException, IOException {
IdQuery idQuery = new IdQuery("TT_1427183696238", IdType.TRADENO);
com.foxinmy.weixin4j.payment.mch.RefundResult result = PAY3
.refundApply(new FileInputStream(caFile), idQuery, "TT_R"
+ System.currentTimeMillis(), 0.01d, 0.01d, null,
"10020674");
System.err.println(result);
String sign = result.getSign();
result.setSign(null);
String valiSign = DigestUtil.paysignMd5(result,
ACCOUNT3.getPaySignKey());
System.err
.println(String.format("sign=%s,valiSign=%s", sign, valiSign));
Assert.assertEquals(valiSign, sign);
}
@Test
public void nativeV3() throws WeixinException {
MchPayPackage payPackageV3 = new MchPayPackage(ACCOUNT3,
"oyFLst1bqtuTcxK-ojF8hOGtLQao", "native测试", "T0001", 0.1d,
"notify_url", "127.0.0.1", TradeType.NATIVE);
payPackageV3.setProductId("0001");
PrePay prePay = null;
try {
prePay = PAY3.createPrePay(payPackageV3);
} catch (WeixinPayException e) {
e.printStackTrace();
}
System.err.println(prePay);
}
@Test
public void closeOrder() throws WeixinException {
ApiResult result = PAY3.closeOrder("D111");
System.err.println(result);
String sign = result.getSign();
result.setSign(null);
String valiSign = DigestUtil.paysignMd5(result,
ACCOUNT3.getPaySignKey());
System.err
.println(String.format("sign=%s,valiSign=%s", sign, valiSign));
Assert.assertEquals(valiSign, sign);
}
@Test
public void shortUrl() throws WeixinException {
String url = "weixin://wxpay/bizpayurl?xxxxxx";
String shortUrl = PAY3.getPayShorturl(url);
System.err.println(shortUrl);
}
@Test
public void interfaceReport() throws WeixinException {
String interfaceUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
int executeTime = 2500;
String outTradeNo = null;
String ip = "127.0.0.1";
Date time = new Date();
XmlResult returnXml = new XmlResult("SUCCESS", "");
returnXml.setResultCode("SUCCESS");
returnXml = PAY3.interfaceReport(interfaceUrl, executeTime, outTradeNo,
ip, time, returnXml);
System.err.println(returnXml);
}
}
@@ -1,6 +1,5 @@
package com.foxinmy.weixin4j.mp.test;
import java.io.File;
import java.io.IOException;
import org.junit.Assert;
@@ -10,6 +9,7 @@ import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.mp.api.QrApi;
import com.foxinmy.weixin4j.mp.model.QRParameter;
import com.foxinmy.weixin4j.mp.model.QRResult;
/**
* 二维码相关测试
@@ -29,23 +29,21 @@ public class QRTest extends TokenTest {
@Test
public void temp_qr() throws WeixinException, IOException {
File file = qrApi.createQRFile(QRParameter.createTemporary(1200, 1200),
"/tmp/weixin4j/qrcode");
Assert.assertTrue(file.exists());
QRResult result = qrApi.createQR(QRParameter
.createTemporary(1200, 1200));
Assert.assertTrue(!result.getTicket().isEmpty());
}
@Test
public void forever_qr_int() throws WeixinException, IOException {
File file = qrApi.createQRFile(QRParameter.createPermanenceInt(2),
"/tmp/weixin4j/qrcode");
Assert.assertTrue(file.exists());
QRResult result = qrApi.createQR(QRParameter.createPermanenceInt(2));
Assert.assertTrue(!result.getTicket().isEmpty());
}
@Test
public void forever_qr_str() throws WeixinException, IOException {
File file = qrApi.createQRFile(
QRParameter.createPermanenceStr("1200中文"),
"/tmp/weixin4j/qrcode");
Assert.assertTrue(file.exists());
QRResult result = qrApi.createQR(QRParameter
.createPermanenceStr("1200中文"));
Assert.assertTrue(!result.getTicket().isEmpty());
}
}