1 package com.foxinmy.weixin4j.api;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.HashMap;
8 import java.util.Map;
9 import java.util.ResourceBundle;
10
11 import com.foxinmy.weixin4j.exception.WeixinException;
12 import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
13 import com.foxinmy.weixin4j.model.WeixinPayAccount;
14 import com.foxinmy.weixin4j.payment.mch.MerchantResult;
15 import com.foxinmy.weixin4j.sign.WeixinPaymentSignature;
16 import com.foxinmy.weixin4j.sign.WeixinSignature;
17 import com.foxinmy.weixin4j.type.IdQuery;
18 import com.foxinmy.weixin4j.util.RandomUtil;
19 import com.foxinmy.weixin4j.util.StringUtil;
20 import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
21
22
23
24
25
26
27
28
29
30
31
32 @Deprecated
33 public class MchApi extends BaseApi {
34
35 private final static ResourceBundle WEIXIN_BUNDLE;
36
37 private final static String PEM_CERT_PREFIX = "-----BEGIN CERTIFICATE-----";
38
39 static {
40 WEIXIN_BUNDLE = ResourceBundle.getBundle("com/foxinmy/weixin4j/payment/weixin");
41 }
42
43 protected final WeixinPayAccount weixinAccount;
44 protected final WeixinSignature weixinSignature;
45 private volatile WeixinRequestExecutor weixinSSLExecutor;
46
47 public MchApi(WeixinPayAccount weixinAccount) {
48 this.weixinAccount = weixinAccount;
49 this.weixinSignature = new WeixinPaymentSignature(weixinAccount.getPaySignKey());
50 }
51
52 @Override
53 protected ResourceBundle weixinBundle() {
54 return WEIXIN_BUNDLE;
55 }
56
57
58
59
60
61
62
63
64 protected Map<String, String> createBaseRequestMap(IdQuery idQuery) {
65 Map<String, String> map = new HashMap<String, String>();
66 map.put("appid", weixinAccount.getId());
67 map.put("mch_id", weixinAccount.getMchId());
68 map.put("nonce_str", RandomUtil.generateString(16));
69 if (StringUtil.isNotBlank(weixinAccount.getDeviceInfo())) {
70 map.put("device_info", weixinAccount.getDeviceInfo());
71 }
72 if (StringUtil.isNotBlank(weixinAccount.getSubId())) {
73 map.put("sub_appid", weixinAccount.getSubId());
74 }
75 if (StringUtil.isNotBlank(weixinAccount.getSubMchId())) {
76 map.put("sub_mch_id", weixinAccount.getSubMchId());
77 }
78 if (idQuery != null) {
79 map.put(idQuery.getType().getName(), idQuery.getId());
80 }
81 return map;
82 }
83
84
85
86
87
88
89 public WeixinSignature getWeixinSignature() {
90 return this.weixinSignature;
91 }
92
93
94
95
96
97
98 protected WeixinRequestExecutor getWeixinSSLExecutor() throws WeixinException {
99 if (weixinSSLExecutor == null) {
100 if(weixinAccount.getCertificateFile().startsWith(PEM_CERT_PREFIX)){
101
102 this.weixinSSLExecutor = weixinExecutor.createSSLRequestExecutor(weixinAccount.getMchId(),
103 weixinAccount.getCertificateFile(), weixinAccount.getCertificateKey());
104 }else {
105
106 try {
107 InputStream is = null;
108 File certificate = new File(
109 Weixin4jConfigUtil.replaceClassPathValue(weixinAccount.getCertificateFile()));
110 if (!certificate.exists() || !certificate.isFile()) {
111 is = Weixin4jConfigUtil.CLASSLOADER.getResourceAsStream(certificate.getName());
112 } else {
113 is = new FileInputStream(certificate);
114 }
115 if (is == null) {
116 throw new WeixinException("Invalid certificate file : " + certificate.toString());
117 }
118 this.weixinSSLExecutor = weixinExecutor.createSSLRequestExecutor(weixinAccount.getCertificateKey(), is);
119 } catch (IOException e) {
120 throw new WeixinException("IO Error on createSSLRequestExecutor", e);
121 }
122 }
123 }
124 return this.weixinSSLExecutor;
125 }
126
127
128
129
130
131
132 protected <T extends MerchantResult> void declareMerchant(T merchant) {
133 merchant.setAppId(weixinAccount.getId());
134 merchant.setMchId(weixinAccount.getMchId());
135 merchant.setDeviceInfo(weixinAccount.getDeviceInfo());
136 merchant.setSubAppId(weixinAccount.getSubId());
137 merchant.setSubMchId(weixinAccount.getSubMchId());
138 merchant.setNonceStr(RandomUtil.generateString(16));
139 }
140 }