优化MchPayRequest & ServerToolkits

This commit is contained in:
jinyu 2015-12-27 14:14:20 +08:00
parent 1fc5a34369
commit 66bd25bb58
21 changed files with 74 additions and 100 deletions

View File

@ -130,13 +130,14 @@ public class Pay3Api {
PrePay prePay = createPrePay(payPackage);
String tradeType = payPackage.getTradeType();
if (TradeType.APP.name().equalsIgnoreCase(tradeType)) {
return new APPPayRequest(prePay, weixinAccount);
return new APPPayRequest(prePay.getPrepayId(), weixinAccount);
} else if (TradeType.JSAPI.name().equalsIgnoreCase(tradeType)) {
return new JSAPIPayRequest(prePay, weixinAccount);
return new JSAPIPayRequest(prePay.getPrepayId(), weixinAccount);
} else if (TradeType.NATIVE.name().equalsIgnoreCase(tradeType)) {
return new NATIVEPayRequest(prePay, weixinAccount);
return new NATIVEPayRequest(prePay.getPrepayId(),
prePay.getCodeUrl(), weixinAccount);
} else if (TradeType.WAP.name().equalsIgnoreCase(tradeType)) {
return new WAPPayRequest(prePay, weixinAccount);
return new WAPPayRequest(prePay.getPrepayId(), weixinAccount);
} else if (TradeType.MICROPAY.name().equalsIgnoreCase(tradeType)) {
throw new WeixinPayException("maybe use createMicroPay method?");
} else {

View File

@ -19,8 +19,8 @@ import com.foxinmy.weixin4j.util.MapUtil;
*/
public class APPPayRequest extends AbstractPayRequest {
public APPPayRequest(PrePay prePay, WeixinPayAccount payAccount) {
super(prePay, payAccount);
public APPPayRequest(String prePayId, WeixinPayAccount payAccount) {
super(prePayId, payAccount);
}
/**
@ -30,7 +30,7 @@ public class APPPayRequest extends AbstractPayRequest {
public PayRequest toRequestObject() {
PayRequest payRequest = new PayRequest(getPayAccount().getId(),
"Sign=WXPay");
payRequest.setPrepayId(getPrePay().getPrepayId());
payRequest.setPrepayId(getPrePayId());
payRequest.setPartnerId(getPayAccount().getPartnerId());
return payRequest;
}

View File

@ -1,30 +1,24 @@
package com.foxinmy.weixin4j.payment.mch;
import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.model.WeixinPayAccount;
public abstract class AbstractPayRequest implements MchPayRequest {
private final PrePay prePay;
private final String prePayId;
private final WeixinPayAccount payAccount;
public AbstractPayRequest(PrePay prePay, WeixinPayAccount payAccount) {
this.prePay = prePay;
public AbstractPayRequest(String prePayId, WeixinPayAccount payAccount) {
this.prePayId = prePayId;
this.payAccount = payAccount;
}
@Override
public PrePay getPrePay() {
return this.prePay;
public String getPrePayId() {
return this.prePayId;
}
@Override
public WeixinPayAccount getPayAccount() {
return this.payAccount;
}
@Override
public String toRequestString() {
return JSON.toJSONString(toRequestObject());
}
}

View File

@ -1,5 +1,6 @@
package com.foxinmy.weixin4j.payment.mch;
import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.model.WeixinPayAccount;
import com.foxinmy.weixin4j.payment.PayRequest;
import com.foxinmy.weixin4j.type.SignType;
@ -24,17 +25,22 @@ import com.foxinmy.weixin4j.util.DigestUtil;
*/
public class JSAPIPayRequest extends AbstractPayRequest {
public JSAPIPayRequest(PrePay prePay, WeixinPayAccount payAccount) {
super(prePay, payAccount);
public JSAPIPayRequest(String prePayId, WeixinPayAccount payAccount) {
super(prePayId, payAccount);
}
@Override
public PayRequest toRequestObject() {
PayRequest payRequest = new PayRequest(getPayAccount().getId(),
"prepay_id=" + getPrePay().getPrepayId());
"prepay_id=" + getPrePayId());
payRequest.setSignType(SignType.MD5);
payRequest.setPaySign(DigestUtil.paysignMd5(payRequest, getPayAccount()
.getPaySignKey()));
return payRequest;
}
@Override
public String toRequestString() {
return JSON.toJSONString(toRequestObject());
}
}

View File

@ -17,11 +17,11 @@ import com.foxinmy.weixin4j.payment.PayRequest;
*/
public interface MchPayRequest {
/**
* 预支付对象
* 预支付交易ID
*
* @return
*/
public PrePay getPrePay();
public String getPrePayId();
/**
* 商户信息

View File

@ -17,8 +17,12 @@ import com.foxinmy.weixin4j.payment.PayRequest;
*/
public class NATIVEPayRequest extends AbstractPayRequest {
public NATIVEPayRequest(PrePay prePay, WeixinPayAccount payAccount) {
super(prePay, payAccount);
private final String codeUrl;
public NATIVEPayRequest(String prePayId, String codeUrl,
WeixinPayAccount payAccount) {
super(prePayId, payAccount);
this.codeUrl = codeUrl;
}
/**
@ -26,12 +30,11 @@ public class NATIVEPayRequest extends AbstractPayRequest {
*/
@Override
public PayRequest toRequestObject() {
return new PayRequest(getPayAccount().getId(), "code_url="
+ getPrePay().getCodeUrl());
return new PayRequest(getPayAccount().getId(), "code_url=" + codeUrl);
}
@Override
public String toRequestString() {
return getPrePay().getCodeUrl();
return this.codeUrl;
}
}

View File

@ -22,8 +22,8 @@ import com.foxinmy.weixin4j.util.URLEncodingUtil;
*/
public class WAPPayRequest extends AbstractPayRequest {
public WAPPayRequest(PrePay prePay, WeixinPayAccount payAccount) {
super(prePay, payAccount);
public WAPPayRequest(String prePayId, WeixinPayAccount payAccount) {
super(prePayId, payAccount);
}
/**
@ -32,7 +32,7 @@ public class WAPPayRequest extends AbstractPayRequest {
@Override
public PayRequest toRequestObject() {
PayRequest payRequest = new PayRequest(getPayAccount().getId(), "WAP");
payRequest.setPrepayId(getPrePay().getPrepayId());
payRequest.setPrepayId(getPrePayId());
return payRequest;
}

View File

@ -33,7 +33,6 @@ import com.foxinmy.weixin4j.response.BlankResponse;
import com.foxinmy.weixin4j.response.WeixinResponse;
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
import com.foxinmy.weixin4j.util.ClassUtil;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil;
import com.foxinmy.weixin4j.util.ServerToolkits;
@ -352,7 +351,7 @@ public class WeixinMessageDispatcher {
}
try {
Source source = new StreamSource(new ByteArrayInputStream(
message.getBytes(Consts.UTF_8)));
ServerToolkits.getBytesUtf8(message)));
JAXBElement<M> jaxbElement = getUnmarshaller(clazz).unmarshal(
source, clazz);
return jaxbElement.getValue();

View File

@ -10,8 +10,8 @@ import java.util.List;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.response.SingleResponse;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil;
import com.foxinmy.weixin4j.util.ServerToolkits;
/**
* 单一回复编码类
@ -33,7 +33,7 @@ public class SingleResponseEncoder extends
List<Object> out) throws WeixinException {
String content = response.toContent();
ctx.writeAndFlush(HttpUtil.createHttpResponse(content, OK,
Consts.CONTENTTYPE$TEXT_PLAIN));
ServerToolkits.CONTENTTYPE$TEXT_PLAIN));
logger.info("encode single response:{}", content);
}
}

View File

@ -14,7 +14,6 @@ import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.type.EncryptType;
import com.foxinmy.weixin4j.util.AesToken;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.MessageUtil;
import com.foxinmy.weixin4j.util.ServerToolkits;
import com.foxinmy.weixin4j.xml.EncryptMessageHandler;
@ -44,7 +43,7 @@ public class WeixinMessageDecoder extends
@Override
protected void decode(ChannelHandlerContext ctx, FullHttpRequest req,
List<Object> out) throws WeixinException {
String messageContent = req.content().toString(Consts.UTF_8);
String messageContent = req.content().toString(ServerToolkits.UTF_8);
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri(),
true);
String methodName = req.getMethod().name();

View File

@ -16,7 +16,6 @@ import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.response.SingleResponse;
import com.foxinmy.weixin4j.type.EncryptType;
import com.foxinmy.weixin4j.util.AesToken;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil;
import com.foxinmy.weixin4j.util.MessageUtil;
import com.foxinmy.weixin4j.util.ServerToolkits;
@ -112,7 +111,7 @@ public class WeixinRequestHandler extends
}
WeixinMessageTransfer messageTransfer = MessageTransferHandler
.parser(request);
ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer);
ctx.channel().attr(ServerToolkits.MESSAGE_TRANSFER_KEY).set(messageTransfer);
messageDispatcher.doDispatch(ctx, request, messageTransfer);
}
}

View File

@ -12,7 +12,6 @@ import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.response.WeixinResponse;
import com.foxinmy.weixin4j.type.EncryptType;
import com.foxinmy.weixin4j.util.AesToken;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.HttpUtil;
import com.foxinmy.weixin4j.util.MessageUtil;
import com.foxinmy.weixin4j.util.ServerToolkits;
@ -38,7 +37,7 @@ public class WeixinResponseEncoder extends
protected void encode(ChannelHandlerContext ctx, WeixinResponse response,
List<Object> out) throws WeixinException {
WeixinMessageTransfer messageTransfer = ctx.channel()
.attr(Consts.MESSAGE_TRANSFER_KEY).get();
.attr(ServerToolkits.MESSAGE_TRANSFER_KEY).get();
EncryptType encryptType = messageTransfer.getEncryptType();
StringBuilder content = new StringBuilder();
content.append("<xml>");
@ -76,7 +75,7 @@ public class WeixinResponseEncoder extends
content.append("</xml>");
}
ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(), OK,
Consts.CONTENTTYPE$APPLICATION_XML));
ServerToolkits.CONTENTTYPE$APPLICATION_XML));
logger.info("{} encode weixin response:{}", encryptType, content);
}
}

View File

@ -40,10 +40,10 @@ public final class ClassUtil {
String packageFileName = packageName.replace(".", File.separator);
URL fullPath = getDefaultClassLoader().getResource(packageFileName);
String protocol = fullPath.getProtocol();
if (protocol.equals(Consts.PROTOCOL_FILE)) {
if (protocol.equals(ServerToolkits.PROTOCOL_FILE)) {
File dir = new File(fullPath.getPath());
return findClassesByFile(dir, packageName);
} else if (protocol.equals(Consts.PROTOCOL_JAR)) {
} else if (protocol.equals(ServerToolkits.PROTOCOL_JAR)) {
try {
return findClassesByJar(
((JarURLConnection) fullPath.openConnection())

View File

@ -1,40 +0,0 @@
package com.foxinmy.weixin4j.util;
import io.netty.util.AttributeKey;
import java.nio.charset.Charset;
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
/**
* 常量类
*
* @className Consts
* @author jy
* @date 2015年4月19日
* @since JDK 1.6
* @see
*/
public final class Consts {
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final Charset GBK = Charset.forName("GBK");
public static final String SUCCESS = "SUCCESS";
public static final String FAIL = "FAIL";
public static final String SunX509 = "SunX509";
public static final String JKS = "JKS";
public static final String PKCS12 = "PKCS12";
public static final String TLS = "TLS";
public static final String X509 = "X.509";
public static final String AES = "AES";
public static final String MD5 = "MD5";
public static final String SHA = "SHA";
public static final String SHA1 = "SHA-1";
public static final String PROTOCOL_FILE = "file";
public static final String PROTOCOL_JAR = "jar";
public static final String CONTENTTYPE$APPLICATION_XML = "application/xml";
public static final String CONTENTTYPE$TEXT_PLAIN = "text/plain";
public static final AttributeKey<WeixinMessageTransfer> MESSAGE_TRANSFER_KEY = AttributeKey
.valueOf("$_MESSAGETRANSFER");
}

View File

@ -62,13 +62,13 @@ public class HttpUtil {
FullHttpResponse httpResponse = null;
httpResponse = new DefaultFullHttpResponse(HTTP_1_1, status,
Unpooled.copiedBuffer(content, Consts.UTF_8));
Unpooled.copiedBuffer(content, ServerToolkits.UTF_8));
httpResponse.headers().set(
CONTENT_TYPE,
String.format("%s;encoding=%s", contentType,
Consts.UTF_8.displayName()));
ServerToolkits.UTF_8.displayName()));
httpResponse.headers().set(CONTENT_LENGTH,
content.getBytes(Consts.UTF_8).length);
content.getBytes(ServerToolkits.UTF_8).length);
createHeaders(httpResponse);
return httpResponse;
}

View File

@ -92,7 +92,7 @@ public final class MessageUtil {
byte[] aesKey = NettyBase64.decodeBase64(encodingAesKey + "=");
// 设置加密模式为AES的CBC模式
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec(aesKey, Consts.AES);
SecretKeySpec keySpec = new SecretKeySpec(aesKey, ServerToolkits.AES);
IvParameterSpec iv = new IvParameterSpec(aesKey, 0, 16);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
// 加密
@ -124,7 +124,7 @@ public final class MessageUtil {
try {
// 设置解密模式为AES的CBC模式
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec key_spec = new SecretKeySpec(aesKey, Consts.AES);
SecretKeySpec key_spec = new SecretKeySpec(aesKey, ServerToolkits.AES);
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey,
0, 16));
cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);

View File

@ -50,7 +50,7 @@ public class PKCS7Encoder {
for (int index = 0; index < amountToPad; index++) {
tmp.append(padChr);
}
return tmp.toString().getBytes(Consts.UTF_8);
return tmp.toString().getBytes(ServerToolkits.UTF_8);
}
/**

View File

@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.util;
import io.netty.util.AttributeKey;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
@ -7,6 +9,8 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
/**
* 工具包
*
@ -18,6 +22,16 @@ import java.util.Random;
*/
public final class ServerToolkits {
private static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final String AES = "AES";
public static final String SHA1 = "SHA-1";
public static final String PROTOCOL_FILE = "file";
public static final String PROTOCOL_JAR = "jar";
public static final String CONTENTTYPE$APPLICATION_XML = "application/xml";
public static final String CONTENTTYPE$TEXT_PLAIN = "text/plain";
public static final AttributeKey<WeixinMessageTransfer> MESSAGE_TRANSFER_KEY = AttributeKey
.valueOf("$_MESSAGETRANSFER");
/**
* 返回一个定长的随机字符串(包含数字和大小写字母)
@ -57,8 +71,8 @@ public final class ServerToolkits {
public static String digestSHA1(String content) {
byte[] data = ServerToolkits.getBytesUtf8(content);
try {
return HexUtil.encodeHexString(MessageDigest.getInstance(
Consts.SHA1).digest(data));
return HexUtil.encodeHexString(MessageDigest.getInstance(SHA1)
.digest(data));
} catch (NoSuchAlgorithmException e) {
return null;
}
@ -69,11 +83,11 @@ public final class ServerToolkits {
}
public static byte[] getBytesUtf8(final String content) {
return content != null ? content.getBytes(Consts.UTF_8) : null;
return content != null ? content.getBytes(UTF_8) : null;
}
public static String newStringUtf8(final byte[] bytes) {
return newString(bytes, Consts.UTF_8);
return newString(bytes, UTF_8);
}
/**

View File

@ -10,7 +10,7 @@ import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.ServerToolkits;
/**
* 获取加密的密文内容
@ -71,7 +71,7 @@ public class EncryptMessageHandler extends DefaultHandler {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(global);
xmlReader.parse(new InputSource(new ByteArrayInputStream(xmlContent
.getBytes(Consts.UTF_8))));
.getBytes(ServerToolkits.UTF_8))));
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SAXException e) {

View File

@ -14,7 +14,6 @@ import org.xml.sax.helpers.XMLReaderFactory;
import com.foxinmy.weixin4j.request.WeixinRequest;
import com.foxinmy.weixin4j.socket.WeixinMessageTransfer;
import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.ServerToolkits;
/**
@ -90,7 +89,7 @@ public class MessageTransferHandler extends DefaultHandler {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(global);
xmlReader.parse(new InputSource(new ByteArrayInputStream(request
.getOriginalContent().getBytes(Consts.UTF_8))));
.getOriginalContent().getBytes(ServerToolkits.UTF_8))));
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SAXException e) {

View File

@ -3,6 +3,7 @@ package com.foxinmy.weixin4j.server.test;
import java.io.IOException;
import java.net.URI;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
@ -13,7 +14,7 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.ServerToolkits;
/**
* 发送消息请求到服务器
@ -69,6 +70,6 @@ public class MessagePush {
throw new IOException(Integer.toString(status) + "uri moved");
}
return EntityUtils.toString(httpResponse.getEntity(),
Consts.UTF_8);
ServerToolkits.UTF_8);
}
}