From bc93a225639b3d915b8e22a8839a5d01e427a355 Mon Sep 17 00:00:00 2001 From: jinyu Date: Thu, 26 May 2016 10:12:48 +0800 Subject: [PATCH] =?UTF-8?q?WeixinPayProxy=E6=96=B0=E5=A2=9EgetWeixinSignat?= =?UTF-8?q?ure=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/foxinmy/weixin4j/api/MchApi.java | 15 ++++-- .../weixin4j/payment/WeixinPayProxy.java | 10 ++++ .../foxinmy/weixin4j/mp/api/PayOldApi.java | 50 ++++++++++--------- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/MchApi.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/MchApi.java index 98e98b14..bfa2b8cc 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/MchApi.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/MchApi.java @@ -17,7 +17,7 @@ import com.foxinmy.weixin4j.util.StringUtil; /** * 商户支付 - * + * * @className MchApi * @author jinyu(foxinmy@gmail.com) * @date 2016年3月26日 @@ -49,7 +49,7 @@ public class MchApi extends BaseApi { /** * 创建 SSL支付请求 - * + * * @param certificate * *.p12证书文件 * @return @@ -63,7 +63,7 @@ public class MchApi extends BaseApi { /** * 支付接口请求基本数据 - * + * * @param idQuery * ID信息 可为空 * @return 基础map @@ -87,4 +87,13 @@ public class MchApi extends BaseApi { } return map; } + + /** + * 微信签名类 + * + * @return + */ + public WeixinSignature getWeixinSignature() { + return this.weixinSignature; + } } 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 005839c3..7a29b118 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 @@ -37,6 +37,7 @@ import com.foxinmy.weixin4j.payment.mch.RefundRecord; import com.foxinmy.weixin4j.payment.mch.RefundResult; import com.foxinmy.weixin4j.payment.mch.SettlementRecord; import com.foxinmy.weixin4j.setting.Weixin4jSettings; +import com.foxinmy.weixin4j.sign.WeixinSignature; import com.foxinmy.weixin4j.type.BillType; import com.foxinmy.weixin4j.type.CurrencyType; import com.foxinmy.weixin4j.type.CustomsCity; @@ -104,6 +105,15 @@ public class WeixinPayProxy { return this.settings.getPayAccount(); } + /** + * 获取微信签名类 + * + * @return + */ + public WeixinSignature getWeixinSignature() { + return this.payApi.getWeixinSignature(); + } + /** * 统一下单接口
* 除被扫支付场景以外,商户系统先调用该接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易回话标识后再按扫码、JSAPI diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/PayOldApi.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/PayOldApi.java index b18400e0..58973918 100644 --- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/PayOldApi.java +++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/PayOldApi.java @@ -105,10 +105,14 @@ public class PayOldApi extends MpApi { this.weixinOldSignature = new WeixinOldPaymentSignature(); } - public WeixinOldPayAccount getPayAccount() { + public WeixinOldPayAccount getWeixinPayAccount() { return this.weixinAccount; } + public WeixinOldPaymentSignature getWeixinPaymentSignature(){ + return this.weixinOldSignature; + } + /** * 生成V2.x版本JSAPI支付字符串 * @@ -126,7 +130,7 @@ public class PayOldApi extends MpApi { */ public String createPayJsRequestJson(String body, String outTradeNo, double totalFee, String notifyUrl, String createIp) { - PayPackageV2 payPackage = new PayPackageV2(getPayAccount() + PayPackageV2 payPackage = new PayPackageV2(weixinAccount .getPartnerId(), body, outTradeNo, totalFee, notifyUrl, createIp); return createPayJsRequestJson(payPackage); @@ -140,11 +144,11 @@ public class PayOldApi extends MpApi { * @return 支付json串 */ public String createPayJsRequestJson(PayPackageV2 payPackage) { - PayRequest payRequest = new PayRequest(getPayAccount().getId(), - weixinOldSignature.sign(payPackage, getPayAccount() + PayRequest payRequest = new PayRequest(weixinAccount.getId(), + weixinOldSignature.sign(payPackage, weixinAccount .getPartnerKey())); payRequest.setPaySign(weixinOldSignature.sign(payRequest, - getPayAccount().getPaySignKey())); + weixinAccount.getPaySignKey())); payRequest.setSignType(SignType.SHA1); return JSON.toJSONString(payRequest); } @@ -160,14 +164,14 @@ public class PayOldApi extends MpApi { Map map = new HashMap(); String timestamp = DateUtil.timestamp2string(); String noncestr = RandomUtil.generateString(16); - map.put("appid", getPayAccount().getId()); + map.put("appid", weixinAccount.getId()); map.put("timestamp", timestamp); map.put("noncestr", noncestr); map.put("productid", productId); - map.put("appkey", getPayAccount().getPaySignKey()); + map.put("appkey", weixinAccount.getPaySignKey()); String sign = weixinOldSignature.sign(map); String nativepay_uri = getRequestUri("nativepay_old_uri"); - return String.format(nativepay_uri, sign, getPayAccount().getId(), + return String.format(nativepay_uri, sign, weixinAccount.getId(), productId, timestamp, noncestr); } @@ -187,23 +191,23 @@ public class PayOldApi extends MpApi { StringBuilder sb = new StringBuilder(); sb.append(idQuery.getType().getName()).append("=") .append(idQuery.getId()); - sb.append("&partner=").append(getPayAccount().getPartnerId()); + sb.append("&partner=").append(weixinAccount.getPartnerId()); String part = sb.toString(); - sb.append("&key=").append(getPayAccount().getPartnerKey()); + sb.append("&key=").append(weixinAccount.getPartnerKey()); String sign = DigestUtil.MD5(sb.toString()).toUpperCase(); sb.delete(0, sb.length()); sb.append(part).append("&sign=").append(sign); String timestamp = DateUtil.timestamp2string(); JSONObject obj = new JSONObject(); - obj.put("appid", getPayAccount().getId()); - obj.put("appkey", getPayAccount().getPaySignKey()); + obj.put("appid", weixinAccount.getId()); + obj.put("appkey", weixinAccount.getPaySignKey()); obj.put("package", sb.toString()); obj.put("timestamp", timestamp); String signature = weixinOldSignature.sign(obj); obj.clear(); - obj.put("appid", getPayAccount().getId()); + obj.put("appid", weixinAccount.getId()); obj.put("package", sb.toString()); obj.put("timestamp", timestamp); obj.put("app_signature", signature); @@ -264,13 +268,13 @@ public class PayOldApi extends MpApi { // 填写为 1.0 时,操作员密码为明文 // 填写为 1.1 时,操作员密码为 MD5(密码)值 map.put("service_version", "1.1"); - map.put("partner", getPayAccount().getPartnerId()); + map.put("partner", weixinAccount.getPartnerId()); map.put("out_refund_no", outRefundNo); map.put("total_fee", DateUtil.formaFee2Fen(totalFee)); map.put("refund_fee", DateUtil.formaFee2Fen(refundFee)); map.put(idQuery.getType().getName(), idQuery.getId()); if (StringUtil.isBlank(opUserId)) { - opUserId = getPayAccount().getPartnerId(); + opUserId = weixinAccount.getPartnerId(); } map.put("op_user_id", opUserId); if (mopara != null && !mopara.isEmpty()) { @@ -311,8 +315,8 @@ public class PayOldApi extends MpApi { KeyManagerFactory kmf = KeyManagerFactory .getInstance(Consts.SunX509); ks = KeyStore.getInstance(Consts.PKCS12); - ks.load(certificate, getPayAccount().getPartnerId().toCharArray()); - kmf.init(ks, getPayAccount().getPartnerId().toCharArray()); + ks.load(certificate, weixinAccount.getPartnerId().toCharArray()); + kmf.init(ks, weixinAccount.getPartnerId().toCharArray()); ctx = SSLContext.getInstance(Consts.TLS); ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), @@ -446,7 +450,7 @@ public class PayOldApi extends MpApi { } String formatBillDate = DateUtil.fortmat2yyyyMMdd(billDate); String fileName = String.format("weixin4j_bill_%s_%s_%s.txt", - formatBillDate, billType.name().toLowerCase(), getPayAccount() + formatBillDate, billType.name().toLowerCase(), weixinAccount .getId()); File file = new File(String.format("%s/%s", billPath, fileName)); if (file.exists()) { @@ -455,12 +459,12 @@ public class PayOldApi extends MpApi { String downloadbill_uri = getRequestUri("downloadbill_old_uri"); Map map = new HashMap(); - map.put("spid", getPayAccount().getPartnerId()); + map.put("spid", weixinAccount.getPartnerId()); map.put("trans_time", DateUtil.fortmat2yyyy_MM_dd(billDate)); map.put("stamp", DateUtil.timestamp2string()); map.put("cft_signtype", "0"); map.put("mchtype", Integer.toString(billType.getVal())); - map.put("key", getPayAccount().getPartnerKey()); + map.put("key", weixinAccount.getPartnerKey()); String sign = DigestUtil.MD5(MapUtil.toJoinString(map, false, false)); map.put("sign", sign.toLowerCase()); WeixinResponse response = weixinExecutor.get(downloadbill_uri, map); @@ -510,7 +514,7 @@ public class PayOldApi extends MpApi { String refundquery_uri = getRequestUri("refundquery_old_uri"); Map map = new HashMap(); map.put("input_charset", Consts.UTF_8.name()); - map.put("partner", getPayAccount().getPartnerId()); + map.put("partner", weixinAccount.getPartnerId()); map.put(idQuery.getType().getName(), idQuery.getId()); String sign = weixinMD5Signature.sign(map); map.put("sign", sign.toLowerCase()); @@ -542,8 +546,8 @@ public class PayOldApi extends MpApi { Token token = tokenHolder.getToken(); Map map = new HashMap(); - map.put("appid", getPayAccount().getId()); - map.put("appkey", getPayAccount().getPaySignKey()); + map.put("appid", weixinAccount.getId()); + map.put("appkey", weixinAccount.getPaySignKey()); map.put("openid", openId); map.put("transid", transid); map.put("out_trade_no", outTradeNo);