diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CashApi.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CashApi.java index cb587710..cd76523b 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CashApi.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CashApi.java @@ -1,7 +1,5 @@ package com.foxinmy.weixin4j.api; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; @@ -48,7 +46,7 @@ public class CashApi { /** * 发放红包 企业向微信用户个人发现金红包 * - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param redpacket * 红包信息 @@ -59,7 +57,7 @@ public class CashApi { * href="http://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5">发放红包接口说明 * @throws WeixinException */ - public RedpacketSendResult sendRedpack(File caFile, Redpacket redpacket) + public RedpacketSendResult sendRedpack(InputStream ca, Redpacket redpacket) throws WeixinException { JSONObject obj = (JSONObject) JSON.toJSON(redpacket); obj.put("nonce_str", RandomUtil.generateString(16)); @@ -70,16 +68,10 @@ public class CashApi { obj.put("sign", sign); String param = XmlStream.map2xml(obj); WeixinResponse response = null; - InputStream ca = null; try { - ca = new FileInputStream(caFile); SSLHttpClinet request = new SSLHttpClinet(weixinAccount.getMchId(), ca); response = request.post(PayURLConsts.MCH_REDPACKSEND_URL, param); - } catch (WeixinException e) { - throw e; - } catch (IOException e) { - throw new WeixinException(e.getMessage()); } finally { if (ca != null) { try { @@ -96,7 +88,7 @@ public class CashApi { /** * 查询红包记录 * - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param outTradeNo * 商户发放红包的商户订单号 @@ -106,7 +98,7 @@ public class CashApi { * href="http://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_6">查询红包接口说明 * @throws WeixinException */ - public RedpacketRecord queryRedpack(File caFile, String outTradeNo) + public RedpacketRecord queryRedpack(InputStream ca, String outTradeNo) throws WeixinException { Map para = new HashMap(); para.put("nonce_str", RandomUtil.generateString(16)); @@ -118,16 +110,10 @@ public class CashApi { para.put("sign", sign); String param = XmlStream.map2xml(para); WeixinResponse response = null; - InputStream ca = null; try { - ca = new FileInputStream(caFile); SSLHttpClinet request = new SSLHttpClinet(weixinAccount.getMchId(), ca); response = request.post(PayURLConsts.MCH_REDPACKQUERY_URL, param); - } catch (WeixinException e) { - throw e; - } catch (IOException e) { - throw new WeixinException(e.getMessage()); } finally { if (ca != null) { try { @@ -144,7 +130,7 @@ public class CashApi { /** * 企业付款 实现企业向个人付款,针对部分有开发能力的商户, 提供通过API完成企业付款的功能。 比如目前的保险行业向客户退保、给付、理赔。 * - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param mpPayment * 付款信息 @@ -155,7 +141,7 @@ public class CashApi { * href="http://pay.weixin.qq.com/wiki/doc/api/mch_pay.php?chapter=14_1">企业付款 * @throws WeixinException */ - public MPPaymentResult mchPayment(File caFile, MPPayment mpPayment) + public MPPaymentResult mchPayment(InputStream ca, MPPayment mpPayment) throws WeixinException { JSONObject obj = (JSONObject) JSON.toJSON(mpPayment); obj.put("nonce_str", RandomUtil.generateString(16)); @@ -167,16 +153,10 @@ public class CashApi { obj.put("sign", sign); String param = XmlStream.map2xml(obj); WeixinResponse response = null; - InputStream ca = null; try { - ca = new FileInputStream(caFile); SSLHttpClinet request = new SSLHttpClinet(weixinAccount.getMchId(), ca); response = request.post(PayURLConsts.MCH_ENPAYMENT_URL, param); - } catch (WeixinException e) { - throw e; - } catch (IOException e) { - throw new WeixinException(e.getMessage()); } finally { if (ca != null) { try { @@ -197,7 +177,7 @@ public class CashApi { /** * 企业付款查询 用于商户的企业付款操作进行结果查询,返回付款操作详细结果 * - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param outTradeNo * 商户调用企业付款API时使用的商户订单号 @@ -207,7 +187,7 @@ public class CashApi { * href="http://pay.weixin.qq.com/wiki/doc/api/mch_pay.php?chapter=14_3">企业付款查询 * @throws WeixinException */ - public MPPaymentRecord mchPaymentQuery(File caFile, String outTradeNo) + public MPPaymentRecord mchPaymentQuery(InputStream ca, String outTradeNo) throws WeixinException { JSONObject obj = new JSONObject(); obj.put("nonce_str", RandomUtil.generateString(16)); @@ -218,16 +198,10 @@ public class CashApi { obj.put("sign", sign); String param = XmlStream.map2xml(obj); WeixinResponse response = null; - InputStream ca = null; try { - ca = new FileInputStream(caFile); SSLHttpClinet request = new SSLHttpClinet(weixinAccount.getMchId(), ca); response = request.post(PayURLConsts.MCH_ENPAYQUERY_URL, param); - } catch (WeixinException e) { - throw e; - } catch (IOException e) { - throw new WeixinException(e.getMessage()); } finally { if (ca != null) { try { diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CouponApi.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CouponApi.java index 2f87569c..b5fe4aae 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CouponApi.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CouponApi.java @@ -1,7 +1,5 @@ package com.foxinmy.weixin4j.api; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; @@ -45,7 +43,7 @@ public class CouponApi { /** * 发放代金券(需要证书) * - * @param caFile + * @param ca * 证书文件(后缀为*.p12) * @param couponStockId * 代金券批次id @@ -61,7 +59,7 @@ public class CouponApi { * href="http://pay.weixin.qq.com/wiki/doc/api/sp_coupon.php?chapter=12_3">发放代金券接口 * @throws WeixinException */ - public CouponResult sendCoupon(File caFile, String couponStockId, + public CouponResult sendCoupon(InputStream ca, String couponStockId, String partnerTradeNo, String openId, String opUserId) throws WeixinException { Map map = baseMap(); @@ -81,16 +79,10 @@ public class CouponApi { map.put("sign", sign); String param = XmlStream.map2xml(map); WeixinResponse response = null; - InputStream ca = null; try { - ca = new FileInputStream(caFile); SSLHttpClinet request = new SSLHttpClinet(weixinAccount.getMchId(), ca); response = request.post(PayURLConsts.MCH_COUPONSEND_URL, param); - } catch (WeixinException e) { - throw e; - } catch (IOException e) { - throw new WeixinException(e.getMessage()); } finally { if (ca != null) { try { diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/Pay3Api.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/Pay3Api.java index dd5e6814..ca98fa8c 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/Pay3Api.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/Pay3Api.java @@ -4,7 +4,6 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; @@ -52,7 +51,7 @@ import com.foxinmy.weixin4j.xml.XmlStream; * @see 商户平台API */ public class Pay3Api { - + private final WeixinHttpClient weixinClient; private final WeixinPayAccount weixinAccount; @@ -102,7 +101,7 @@ public class Pay3Api { * ,要采用原来的退款单号。总退款金额不能超过用户实际支付金额。 *

* - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param idQuery * 商户系统内部的订单号, transaction_id 、 out_trade_no 二选一,如果同时存在优先级: @@ -123,14 +122,11 @@ public class Pay3Api { * @since V3 * @throws WeixinException */ - protected RefundResult refundApply(File caFile, IdQuery idQuery, + protected RefundResult refundApply(InputStream ca, IdQuery idQuery, String outRefundNo, double totalFee, double refundFee, String opUserId, Map mopara) throws WeixinException { WeixinResponse response = null; - InputStream ca = null; try { - ca = new FileInputStream(caFile); - Map map = baseMap(idQuery); map.put("out_refund_no", outRefundNo); map.put("total_fee", DateUtil.formaFee2Fen(totalFee)); @@ -149,10 +145,6 @@ public class Pay3Api { SSLHttpClinet request = new SSLHttpClinet(weixinAccount.getMchId(), ca); response = request.post(PayURLConsts.MCH_REFUNDAPPLY_URL, param); - } catch (WeixinException e) { - throw e; - } catch (IOException e) { - throw new WeixinException(e.getMessage()); } finally { if (ca != null) { try { @@ -169,7 +161,7 @@ public class Pay3Api { /** * 退款申请 * - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param idQuery * 商户系统内部的订单号, transaction_id 、 out_trade_no 二选一,如果同时存在优先级: @@ -184,9 +176,9 @@ public class Pay3Api { * 货币类型,符合ISO 4217标准的三位字母代码,默认人民币:CNY * @param opUserId * 操作员帐号, 默认为商户号 - * @see {@link com.foxinmy.weixin4j.api.Pay3Api#refundApply(File, IdQuery, String, double, double, String, Map)} + * @see {@link com.foxinmy.weixin4j.api.Pay3Api#refundApply(InputStream, IdQuery, String, double, double, String, Map)} */ - public RefundResult refundApply(File caFile, IdQuery idQuery, + public RefundResult refundApply(InputStream ca, IdQuery idQuery, String outRefundNo, double totalFee, double refundFee, CurrencyType refundFeeType, String opUserId) throws WeixinException { Map mopara = new HashMap(); @@ -194,7 +186,7 @@ public class Pay3Api { refundFeeType = CurrencyType.CNY; } mopara.put("refund_fee_type", refundFeeType.name()); - return refundApply(caFile, idQuery, outRefundNo, totalFee, refundFee, + return refundApply(ca, idQuery, outRefundNo, totalFee, refundFee, opUserId, mopara); } @@ -204,7 +196,7 @@ public class Pay3Api { * 如需实现相同功能请调用退款接口
调用扣款接口后请勿立即调用撤销,需要等待5秒以上。先调用查单接口,如果没有确切的返回,再调用撤销
* - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param idQuery * 商户系统内部的订单号, transaction_id 、 out_trade_no 二选一,如果同时存在优先级: @@ -213,11 +205,9 @@ public class Pay3Api { * @since V3 * @throws WeixinException */ - public ApiResult reverseOrder(File caFile, IdQuery idQuery) + public ApiResult reverseOrder(InputStream ca, IdQuery idQuery) throws WeixinException { - InputStream ca = null; try { - ca = new FileInputStream(caFile); SSLHttpClinet request = new SSLHttpClinet(weixinAccount.getMchId(), ca); Map map = baseMap(idQuery); @@ -229,8 +219,6 @@ public class Pay3Api { PayURLConsts.MCH_ORDERREVERSE_URL, param); return response.getAsObject(new TypeReference() { }); - } catch (IOException e) { - throw new WeixinException(e.getMessage()); } finally { if (ca != null) { try { diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/WeixinPayProxy.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/WeixinPayProxy.java index 88f4d943..54f39a95 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/WeixinPayProxy.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/WeixinPayProxy.java @@ -1,6 +1,9 @@ package com.foxinmy.weixin4j.payment; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.Date; import com.alibaba.fastjson.JSON; @@ -45,7 +48,7 @@ public class WeixinPayProxy { private final CouponApi couponApi; private final CashApi cashApi; - private final File DEFAULT_CA_FILE; + private final String DEFAULT_CA_FILE; /** * 使用weixin4j.properties配置的账号信息 @@ -65,8 +68,8 @@ public class WeixinPayProxy { this.pay3Api = new Pay3Api(weixinAccount); this.couponApi = new CouponApi(weixinAccount); this.cashApi = new CashApi(weixinAccount); - this.DEFAULT_CA_FILE = new File(ConfigUtil.getClassPathValue("ca_file", - Weixin4jConst.DEFAULT_MEDIA_PATH)); + this.DEFAULT_CA_FILE = ConfigUtil.getClassPathValue("ca_file", + Weixin4jConst.DEFAULT_MEDIA_PATH); } /** @@ -104,7 +107,7 @@ public class WeixinPayProxy { * ,要采用原来的退款单号。总退款金额不能超过用户实际支付金额。 *

* - * @param caFile + * @param ca * 证书文件(后缀为*.p12) * @param idQuery * 商户系统内部的订单号, transaction_id 、 out_trade_no 二选一,如果同时存在优先级: @@ -130,23 +133,27 @@ public class WeixinPayProxy { * @throws WeixinException */ public com.foxinmy.weixin4j.payment.mch.RefundResult refundApply( - File caFile, IdQuery idQuery, String outRefundNo, double totalFee, - double refundFee, CurrencyType refundFeeType, String opUserId) - throws WeixinException { - return pay3Api.refundApply(caFile, idQuery, outRefundNo, totalFee, + InputStream ca, IdQuery idQuery, String outRefundNo, + double totalFee, double refundFee, CurrencyType refundFeeType, + String opUserId) throws WeixinException { + return pay3Api.refundApply(ca, idQuery, outRefundNo, totalFee, refundFee, refundFeeType, opUserId); } /** * 退款申请采用properties中配置的ca文件 * - * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#refundV3(File, IdQuery, String, double, double,CurrencyType, String)} + * @throws IOException + * + * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#refund(InputStream, IdQuery, String, double, double,CurrencyType, String)} */ public com.foxinmy.weixin4j.payment.mch.RefundResult refundApply( IdQuery idQuery, String outRefundNo, double totalFee, - double refundFee, String opUserId) throws WeixinException { - return pay3Api.refundApply(DEFAULT_CA_FILE, idQuery, outRefundNo, - totalFee, refundFee, CurrencyType.CNY, opUserId); + double refundFee, String opUserId) throws WeixinException, + IOException { + return pay3Api.refundApply(new FileInputStream(DEFAULT_CA_FILE), + idQuery, outRefundNo, totalFee, refundFee, CurrencyType.CNY, + opUserId); } /** @@ -214,9 +221,9 @@ public class WeixinPayProxy { * @since V3 * @throws WeixinException */ - public ApiResult reverseOrder(File caFile, IdQuery idQuery) + public ApiResult reverseOrder(InputStream ca, IdQuery idQuery) throws WeixinException { - return pay3Api.reverseOrder(caFile, idQuery); + return pay3Api.reverseOrder(ca, idQuery); } /** @@ -225,11 +232,14 @@ public class WeixinPayProxy { * @param idQuery * transaction_id、out_trade_no 二选一 * @return 撤销结果 - * @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#reverse(File, IdQuery)} + * @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#reverse(InputStream, IdQuery)} * @throws WeixinException + * @throws IOException */ - public ApiResult reverseOrder(IdQuery idQuery) throws WeixinException { - return pay3Api.reverseOrder(DEFAULT_CA_FILE, idQuery); + public ApiResult reverseOrder(IdQuery idQuery) throws WeixinException, + IOException { + return pay3Api.reverseOrder(new FileInputStream(DEFAULT_CA_FILE), + idQuery); } /** @@ -305,7 +315,7 @@ public class WeixinPayProxy { /** * 发放代金券(需要证书) * - * @param caFile + * @param ca * 证书文件(后缀为*.p12) * @param couponStockId * 代金券批次id @@ -322,22 +332,22 @@ public class WeixinPayProxy { * href="http://pay.weixin.qq.com/wiki/doc/api/sp_coupon.php?chapter=12_3">发放代金券接口 * @throws WeixinException */ - public CouponResult sendCoupon(File caFile, String couponStockId, + public CouponResult sendCoupon(InputStream ca, String couponStockId, String partnerTradeNo, String openId, String opUserId) throws WeixinException { - return couponApi.sendCoupon(caFile, couponStockId, partnerTradeNo, - openId, opUserId); + return couponApi.sendCoupon(ca, couponStockId, partnerTradeNo, openId, + opUserId); } /** * 发放代金券采用properties中配置的ca文件 * - * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#sendCoupon(File, String, String, String, String)} + * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#sendCoupon(InputStream, String, String, String, String)} */ public CouponResult sendCoupon(String couponStockId, String partnerTradeNo, - String openId) throws WeixinException { - return couponApi.sendCoupon(DEFAULT_CA_FILE, couponStockId, - partnerTradeNo, openId, null); + String openId) throws WeixinException, IOException { + return couponApi.sendCoupon(new FileInputStream(DEFAULT_CA_FILE), + couponStockId, partnerTradeNo, openId, null); } /** @@ -377,7 +387,7 @@ public class WeixinPayProxy { /** * 发放红包 企业向微信用户个人发现金红包 * - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param redpacket * 红包信息 @@ -389,25 +399,26 @@ public class WeixinPayProxy { * href="http://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5">红包接口说明 * @throws WeixinException */ - public RedpacketSendResult sendRedpack(File caFile, Redpacket redpacket) + public RedpacketSendResult sendRedpack(InputStream ca, Redpacket redpacket) throws WeixinException { - return cashApi.sendRedpack(caFile, redpacket); + return cashApi.sendRedpack(ca, redpacket); } /** * 发放红包采用properties中配置的ca文件 * - * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#sendRedpack(File, Redpacket)} + * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#sendRedpack(InputStream, Redpacket)} */ public RedpacketSendResult sendRedpack(Redpacket redpacket) - throws WeixinException { - return cashApi.sendRedpack(DEFAULT_CA_FILE, redpacket); + throws WeixinException, IOException { + return cashApi.sendRedpack(new FileInputStream(DEFAULT_CA_FILE), + redpacket); } /** * 查询红包记录 * - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param outTradeNo * 商户发放红包的商户订单号 @@ -418,25 +429,26 @@ public class WeixinPayProxy { * href="http://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_6">查询红包接口说明 * @throws WeixinException */ - public RedpacketRecord queryRedpack(File caFile, String outTradeNo) + public RedpacketRecord queryRedpack(InputStream ca, String outTradeNo) throws WeixinException { - return cashApi.queryRedpack(caFile, outTradeNo); + return cashApi.queryRedpack(ca, outTradeNo); } /** * 查询红包采用properties中配置的ca文件 * - * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#queryRedpack(File,String)} + * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#queryRedpack(InputStream,String)} */ public RedpacketRecord queryRedpack(String outTradeNo) - throws WeixinException { - return cashApi.queryRedpack(DEFAULT_CA_FILE, outTradeNo); + throws WeixinException, IOException { + return cashApi.queryRedpack(new FileInputStream(DEFAULT_CA_FILE), + outTradeNo); } /** * 企业付款 实现企业向个人付款,针对部分有开发能力的商户, 提供通过API完成企业付款的功能。 比如目前的保险行业向客户退保、给付、理赔。 * - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param mpPayment * 付款信息 @@ -448,25 +460,26 @@ public class WeixinPayProxy { * href="http://pay.weixin.qq.com/wiki/doc/api/mch_pay.php?chapter=14_1">企业付款 * @throws WeixinException */ - public MPPaymentResult mpPayment(File caFile, MPPayment mpPayment) + public MPPaymentResult mpPayment(InputStream ca, MPPayment mpPayment) throws WeixinException { - return cashApi.mchPayment(caFile, mpPayment); + return cashApi.mchPayment(ca, mpPayment); } /** * 企业付款采用properties中配置的ca文件 * - * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#mpPayment(File, MPPayment)} + * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#mpPayment(InputStream, MPPayment)} */ public MPPaymentResult mpPayment(MPPayment mpPayment) - throws WeixinException { - return cashApi.mchPayment(DEFAULT_CA_FILE, mpPayment); + throws WeixinException, IOException { + return cashApi.mchPayment(new FileInputStream(DEFAULT_CA_FILE), + mpPayment); } /** * 企业付款查询 用于商户的企业付款操作进行结果查询,返回付款操作详细结果 * - * @param caFile + * @param ca * 证书文件(V3版本后缀为*.p12) * @param outTradeNo * 商户调用企业付款API时使用的商户订单号 @@ -477,18 +490,19 @@ public class WeixinPayProxy { * href="http://pay.weixin.qq.com/wiki/doc/api/mch_pay.php?chapter=14_3">企业付款查询 * @throws WeixinException */ - public MPPaymentRecord mpPaymentQuery(File caFile, String outTradeNo) + public MPPaymentRecord mpPaymentQuery(InputStream ca, String outTradeNo) throws WeixinException { - return cashApi.mchPaymentQuery(caFile, outTradeNo); + return cashApi.mchPaymentQuery(ca, outTradeNo); } /** * 企业付款查询采用properties中配置的ca文件 * - * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#mpPaymentQuery(File, String)} + * @see {@link com.foxinmy.weixin4j.payment.WeixinPayProxy#mpPaymentQuery(InputStream, String)} */ public MPPaymentRecord mpPaymentQuery(String outTradeNo) - throws WeixinException { - return cashApi.mchPaymentQuery(DEFAULT_CA_FILE, outTradeNo); + throws WeixinException, IOException { + return cashApi.mchPaymentQuery(new FileInputStream(DEFAULT_CA_FILE), + outTradeNo); } } diff --git a/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/CashTest.java b/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/CashTest.java index 00f3f4e4..7e2c2f18 100644 --- a/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/CashTest.java +++ b/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/CashTest.java @@ -1,5 +1,8 @@ package com.foxinmy.weixin4j.mp.test; +import java.io.FileInputStream; +import java.io.IOException; + import org.junit.Test; import com.foxinmy.weixin4j.exception.WeixinException; @@ -22,7 +25,7 @@ import com.foxinmy.weixin4j.type.MPPaymentCheckNameType; public class CashTest extends CouponTest { @Test - public void sendRedpacket() throws WeixinException { + public void sendRedpacket() throws WeixinException, IOException { Redpacket redpacket = new Redpacket("HB001", "无忧钱庄", "无忧钱庄", "oyFLst1bqtuTcxK-ojF8hOGtLQao", 1d); redpacket.setActName("红包测试"); @@ -32,23 +35,26 @@ public class CashTest extends CouponTest { redpacket.setRemark("快来领取红包吧!"); redpacket.setTotalNum(1); redpacket.setWishing("来就送钱"); - RedpacketSendResult result = WEIXINPAY.sendRedpack(caFile, redpacket); + RedpacketSendResult result = WEIXINPAY.sendRedpack(new FileInputStream( + caFile), redpacket); System.err.println(result); } @Test - public void queryRedpacket() throws WeixinException { + public void queryRedpacket() throws WeixinException, IOException { String outTradeNo = "HB001"; - RedpacketRecord record = WEIXINPAY.queryRedpack(caFile, outTradeNo); + RedpacketRecord record = WEIXINPAY.queryRedpack(new FileInputStream( + caFile), outTradeNo); System.err.println(record); } @Test - public void mpPayment() throws WeixinException { + public void mpPayment() throws WeixinException, IOException { MPPayment payment = new MPPayment("MP001", "oyFLst1bqtuTcxK-ojF8hOGtLQao", MPPaymentCheckNameType.NO_CHECK, "企业付款测试", 0.01d, "127.0.0.1"); - MPPaymentResult result = WEIXINPAY.mpPayment(caFile, payment); + MPPaymentResult result = WEIXINPAY.mpPayment( + new FileInputStream(caFile), payment); System.err.println(result); } } diff --git a/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/CouponTest.java b/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/CouponTest.java index 7232c019..b8af7326 100644 --- a/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/CouponTest.java +++ b/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/CouponTest.java @@ -1,6 +1,8 @@ package com.foxinmy.weixin4j.mp.test; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.util.Date; import org.junit.Assert; @@ -34,11 +36,11 @@ public class CouponTest { protected final File caFile = new File("证书文件路径(*.p12)"); @Test - public void sendCoupon() throws WeixinException { + public void sendCoupon() throws WeixinException, IOException { String partnerTradeNo = String.format("%s%s%s", ACCOUNT.getMchId(), DateUtil.fortmat2yyyyMMdd(new Date()), "1"); - CouponResult result = WEIXINPAY.sendCoupon(caFile, "123", - partnerTradeNo, "oyFLst1bqtuTcxK-ojF8hOGtLQao", null); + CouponResult result = WEIXINPAY.sendCoupon(new FileInputStream(caFile), + "123", partnerTradeNo, "oyFLst1bqtuTcxK-ojF8hOGtLQao", null); Assert.assertTrue(result.getRetCode().equalsIgnoreCase(Consts.SUCCESS)); } diff --git a/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/PayTest.java b/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/PayTest.java index a39b29ba..f7fdc838 100644 --- a/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/PayTest.java +++ b/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/PayTest.java @@ -1,6 +1,8 @@ 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; @@ -109,13 +111,13 @@ public class PayTest { } @Test - public void refundV3() throws WeixinException { + public void refundV3() throws WeixinException, IOException { File caFile = new File("签名文件如123.p12"); IdQuery idQuery = new IdQuery("TT_1427183696238", IdType.TRADENO); com.foxinmy.weixin4j.payment.mch.RefundResult result = PAY3 - .refundApply(caFile, idQuery, - "TT_R" + System.currentTimeMillis(), 0.01d, 0.01d, - null, "10020674"); + .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); diff --git a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinProxy.java b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinProxy.java index cdcd0df0..b3838ac0 100644 --- a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinProxy.java +++ b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinProxy.java @@ -579,8 +579,6 @@ public class WeixinProxy { * * @param code * 通过员工授权获取到的code,每次员工授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期 - * @param agentid - * 跳转链接时所在的企业应用ID * @see com.foxinmy.weixin4j.qy.model.User * @see com.foxinmy.weixin4j.qy.api.UserApi * @return 成员对象 @@ -592,8 +590,8 @@ public class WeixinProxy { * href="http://qydev.weixin.qq.com/wiki/index.php?title=%E6%A0%B9%E6%8D%AEcode%E8%8E%B7%E5%8F%96%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF">根据code获取成员信息 * @throws WeixinException */ - public User getUserByCode(String code, int agentid) throws WeixinException { - return userApi.getUserByCode(code, agentid); + public User getUserByCode(String code) throws WeixinException { + return userApi.getUserByCode(code); } /** @@ -601,9 +599,8 @@ public class WeixinProxy { * * @param code * 通过员工授权获取到的code,每次员工授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期 - * @param agentid - * 跳转链接时所在的企业应用ID - * @return { "UserId":"USERID", "DeviceId":"DEVICEID" } + * @return { "UserId":"USERID", "DeviceId":"DEVICEID" } or + * {"OpenId":"OPENID","DeviceId":"DEVICEID"} * @see com.foxinmy.weixin4j.qy.api.UserApi * @see 企业获取code @@ -611,9 +608,8 @@ public class WeixinProxy { * href="http://qydev.weixin.qq.com/wiki/index.php?title=%E6%A0%B9%E6%8D%AEcode%E8%8E%B7%E5%8F%96%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF">根据code获取成员信息 * @throws WeixinException */ - public JSONObject getUserIdByCode(String code, int agentid) - throws WeixinException { - return userApi.getUserIdByCode(code, agentid); + public JSONObject getUserIdByCode(String code) throws WeixinException { + return userApi.getUserIdByCode(code); } /** diff --git a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/UserApi.java b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/UserApi.java index 1772563e..b8ec7cfd 100644 --- a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/UserApi.java +++ b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/UserApi.java @@ -156,8 +156,6 @@ public class UserApi extends QyApi { * * @param code * 通过员工授权获取到的code,每次员工授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期 - * @param agentid - * 跳转链接时所在的企业应用ID * @see com.foxinmy.weixin4j.qy.model.User * @see com.foxinmy.weixin4j.qy.api.UserApi * @return 成员对象 @@ -169,8 +167,8 @@ public class UserApi extends QyApi { * href="http://qydev.weixin.qq.com/wiki/index.php?title=%E6%A0%B9%E6%8D%AEcode%E8%8E%B7%E5%8F%96%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF">根据code获取成员信息 * @throws WeixinException */ - public User getUserByCode(String code, int agentid) throws WeixinException { - return getUser(getUserIdByCode(code, agentid).getString("UserId")); + public User getUserByCode(String code) throws WeixinException { + return getUser(getUserIdByCode(code).getString("UserId")); } /** @@ -200,8 +198,6 @@ public class UserApi extends QyApi { * * @param code * 通过员工授权获取到的code,每次员工授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期 - * @param agentid - * 跳转链接时所在的企业应用ID * @return { "UserId":"USERID", "DeviceId":"DEVICEID" } * @see 企业获取code @@ -209,12 +205,11 @@ public class UserApi extends QyApi { * href="http://qydev.weixin.qq.com/wiki/index.php?title=%E6%A0%B9%E6%8D%AEcode%E8%8E%B7%E5%8F%96%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF">根据code获取成员信息 * @throws WeixinException */ - public JSONObject getUserIdByCode(String code, int agentid) - throws WeixinException { + public JSONObject getUserIdByCode(String code) throws WeixinException { String user_getid_uri = getRequestUri("user_getid_uri"); Token token = tokenHolder.getToken(); WeixinResponse response = weixinClient.post(String.format( - user_getid_uri, token.getAccessToken(), code, agentid)); + user_getid_uri, token.getAccessToken(), code)); return response.getAsJson(); } diff --git a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/weixin.properties b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/weixin.properties index d847d32f..2a2f8208 100644 --- a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/weixin.properties +++ b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/api/weixin.properties @@ -20,7 +20,7 @@ user_update_uri={api_base_url}/user/update?access_token=%s # \u83b7\u53d6\u6210\u5458\u4fe1\u606f user_get_uri={api_base_url}/user/get?access_token=%s&userid=%s # code\u83b7\u53d6\u6210\u5458\u4fe1\u606f -user_getid_uri={api_base_url}/user/getuserinfo?access_token=%s&code=%s&agentid=%d +user_getid_uri={api_base_url}/user/getuserinfo?access_token=%s&code=%s # \u83b7\u53d6\u90e8\u95e8\u6210\u5458 user_slist_uri={api_base_url}/user/simplelist?access_token=%s&department_id=%d&fetch_child=%d&status=%d # \u83b7\u53d6\u90e8\u95e8\u6210\u5458(\u8be6\u60c5)