优化支付代码&WeixinSignature类新增getSignType方法

This commit is contained in:
jinyu 2016-06-01 09:30:30 +08:00
parent 825d5d6934
commit f24c594def
10 changed files with 57 additions and 35 deletions

View File

@ -65,7 +65,7 @@ public class CashApi extends MchApi {
*/
public RedpacketSendResult sendRedpack(InputStream certificate,
Redpacket redpacket) throws WeixinException {
redpacket.declareWeixinPayAccount(weixinAccount);
super.declareMerchant(redpacket);
JSONObject obj = (JSONObject) JSON.toJSON(redpacket);
obj.put("wxappid", obj.remove("appid"));
obj.put("sign", weixinSignature.sign(obj));
@ -157,7 +157,7 @@ public class CashApi extends MchApi {
*/
public CorpPaymentResult sendCorpPayment(InputStream certificate,
CorpPayment payment) throws WeixinException {
payment.declareWeixinPayAccount(weixinAccount);
super.declareMerchant(payment);
JSONObject obj = (JSONObject) JSON.toJSON(payment);
obj.put("mchid", obj.remove("mch_id"));
obj.put("mch_appid", obj.remove("appid"));

View File

@ -9,6 +9,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
import com.foxinmy.weixin4j.http.weixin.WeixinSSLRequestExecutor;
import com.foxinmy.weixin4j.model.WeixinPayAccount;
import com.foxinmy.weixin4j.payment.mch.MerchantResult;
import com.foxinmy.weixin4j.sign.WeixinPaymentSignature;
import com.foxinmy.weixin4j.sign.WeixinSignature;
import com.foxinmy.weixin4j.type.IdQuery;
@ -96,4 +97,18 @@ public class MchApi extends BaseApi {
public WeixinSignature getWeixinSignature() {
return this.weixinSignature;
}
/**
* 设置商户信息
*
* @param merchant
*/
protected <T extends MerchantResult> void declareMerchant(T merchant) {
merchant.setAppId(weixinAccount.getId());
merchant.setMchId(weixinAccount.getMchId());
merchant.setDeviceInfo(weixinAccount.getDeviceInfo());
merchant.setSubId(weixinAccount.getSubId());
merchant.setSubMchId(weixinAccount.getSubMchId());
merchant.setNonceStr(RandomUtil.generateString(16));
}
}

View File

@ -79,7 +79,7 @@ public class PayApi extends MchApi {
* @return 预支付对象
*/
public PrePay createPrePay(MchPayPackage payPackage) throws WeixinException {
payPackage.declareWeixinPayAccount(weixinAccount);
super.declareMerchant(payPackage);
payPackage.setSign(weixinSignature.sign(payPackage));
String payJsRequestXml = XmlStream.toXML(payPackage);
WeixinResponse response = weixinExecutor.post(
@ -105,7 +105,7 @@ public class PayApi extends MchApi {
throws WeixinException {
String tradeType = payPackage.getTradeType();
if (TradeType.MICROPAY.name().equalsIgnoreCase(tradeType)) {
payPackage.declareWeixinPayAccount(weixinAccount);
super.declareMerchant(payPackage);
payPackage.setSign(weixinSignature.sign(payPackage));
String para = XmlStream.toXML(payPackage);
WeixinResponse response = weixinExecutor.post(

View File

@ -150,6 +150,7 @@ public class WeixinPayProxy {
* @see com.foxinmy.weixin4j.payment.mch.MICROPayRequest 刷卡支付
* @see com.foxinmy.weixin4j.payment.mch.APPPayRequest APP支付
* @see com.foxinmy.weixin4j.payment.mch.WAPPayRequest WAP支付
* @see com.foxinmy.weixin4j.payment.mch.MchPayRequest#toRequestString()
* @throws WeixinException
*/
public MchPayRequest createPayRequest(MchPayPackage payPackage)
@ -176,6 +177,7 @@ public class WeixinPayProxy {
* 附加数据 非必填
* @see com.foxinmy.weixin4j.api.PayApi
* @see com.foxinmy.weixin4j.payment.mch.JSAPIPayRequest
* @see com.foxinmy.weixin4j.payment.mch.MchPayRequest#toRequestString()
* @return JSAPI支付对象
* @throws WeixinException
*/
@ -284,6 +286,7 @@ public class WeixinPayProxy {
* @return Native支付对象
* @see com.foxinmy.weixin4j.api.PayApi
* @see com.foxinmy.weixin4j.payment.mch.NATIVEPayRequest
* @see com.foxinmy.weixin4j.payment.mch.MchPayRequest#toRequestString()
* @see <a href=
* "https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1">扫码支付
* </a>
@ -317,6 +320,7 @@ public class WeixinPayProxy {
* @return APP支付对象
* @see com.foxinmy.weixin4j.api.PayApi
* @see com.foxinmy.weixin4j.payment.mch.APPPayRequest
* @see com.foxinmy.weixin4j.payment.mch.MchPayRequest#toRequestString()
* @see <a href=
* "https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1">
* APP支付</a>
@ -347,6 +351,7 @@ public class WeixinPayProxy {
* @return WAP支付对象
* @see com.foxinmy.weixin4j.api.PayApi
* @see com.foxinmy.weixin4j.payment.mch.WAPPayRequest
* @see com.foxinmy.weixin4j.payment.mch.MchPayRequest#toRequestString()
* @see <a href=
* "https://pay.weixin.qq.com/wiki/doc/api/wap.php?chapter=15_1">WAP支付
* </a>
@ -376,8 +381,9 @@ public class WeixinPayProxy {
* 附加数据 非必填
* @return 支付的订单信息
* @see com.foxinmy.weixin4j.api.PayApi
* @see com.foxinmy.weixin4j.payment.mch.MICROPayRequest
* @see com.foxinmy.weixin4j.payment.mch.Order
* @see com.foxinmy.weixin4j.payment.mch.MICROPayRequest
* @see com.foxinmy.weixin4j.payment.mch.MchPayRequest#toRequestString()
* @see <a href=
* "http://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10">
* 提交被扫支付API</a>

View File

@ -7,9 +7,7 @@ import javax.xml.bind.annotation.XmlRootElement;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.http.weixin.XmlResult;
import com.foxinmy.weixin4j.model.WeixinPayAccount;
import com.foxinmy.weixin4j.type.SignType;
import com.foxinmy.weixin4j.util.RandomUtil;
/**
* 调用商户平台接口返回的公用字段
@ -168,22 +166,6 @@ public class MerchantResult extends XmlResult {
return recall != null && recall.equalsIgnoreCase("y");
}
/**
* 赋值给对应字段
*
* @param weixinPayAccount
* 商户信息
*/
@JSONField(deserialize = false)
public void declareWeixinPayAccount(WeixinPayAccount weixinPayAccount) {
this.appId = weixinPayAccount.getId();
this.mchId = weixinPayAccount.getMchId();
this.deviceInfo = weixinPayAccount.getDeviceInfo();
this.subId = weixinPayAccount.getSubId();
this.subMchId = weixinPayAccount.getSubMchId();
this.nonceStr = RandomUtil.generateString(16);
}
@Override
public String toString() {
return "appId=" + appId + ", mchId=" + mchId + ", subId=" + subId

View File

@ -1,5 +1,6 @@
package com.foxinmy.weixin4j.sign;
import com.foxinmy.weixin4j.type.SignType;
import com.foxinmy.weixin4j.util.DigestUtil;
/**
@ -9,7 +10,8 @@ import com.foxinmy.weixin4j.util.DigestUtil;
* @author jinyu(foxinmy@gmail.com)
* @date 2016年3月26日
* @since JDK 1.6
* @see <a href="https://pay.weixin.qq.com/wiki/doc/api/external/jsapi.php?chapter=4_3">支付签名说明</a>
* @see <a
* href="https://pay.weixin.qq.com/wiki/doc/api/external/jsapi.php?chapter=4_3">支付签名说明</a>
*/
public class WeixinPaymentSignature extends AbstractWeixinSignature {
/**
@ -21,6 +23,11 @@ public class WeixinPaymentSignature extends AbstractWeixinSignature {
this.paySignKey = paySignKey;
}
@Override
public SignType getSignType() {
return SignType.MD5;
}
@Override
public String sign(Object obj) {
StringBuilder sb = join(obj).append("&key=").append(paySignKey);

View File

@ -1,5 +1,6 @@
package com.foxinmy.weixin4j.sign;
import com.foxinmy.weixin4j.type.SignType;
/**
* 微信签名
@ -25,6 +26,13 @@ public interface WeixinSignature {
*/
public boolean lowerCase();
/**
* 签名类型
*
* @return
*/
public SignType getSignType();
/**
* 签名
*

View File

@ -48,14 +48,12 @@
<groupId>com.foxinmy</groupId>
<artifactId>weixin4j-mp</artifactId>
<version>1.6.9</version>
<optional>true</optional>
</dependency>
<!-- 微信企业号 -->
<dependency>
<groupId>com.foxinmy</groupId>
<artifactId>weixin4j-qy</artifactId>
<version>1.6.9</version>
<optional>true</optional>
</dependency>
<!-- 微信被动消息(回调模式) -->
<dependency>

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.foxinmy.weixin4j.sign.AbstractWeixinSignature;
import com.foxinmy.weixin4j.type.SignType;
import com.foxinmy.weixin4j.util.DigestUtil;
import com.foxinmy.weixin4j.util.MapUtil;
@ -48,6 +49,11 @@ public class WeixinOldPaymentSignature extends AbstractWeixinSignature {
return DigestUtil.SHA1(join(obj).toString());
}
@Override
public SignType getSignType() {
return SignType.SHA1;
}
/**
* package拼接签名
*

View File

@ -75,9 +75,9 @@ public class CustomTest extends TokenTest {
@Test
public void uploadKfAvatar() throws WeixinException, IOException {
JsonResult result = customApi.uploadKfAvatar(
"temp1@canyidianzhang", new FileInputStream(new File(
"/Users/jy/Music/简谱/风动草.jpg")), "风动草.jpg");
JsonResult result = customApi.uploadKfAvatar("temp1@canyidianzhang",
new FileInputStream(new File("/Users/jy/Music/简谱/风动草.jpg")),
"风动草.jpg");
Assert.assertEquals(0, result.getCode());
}