一、网站微信扫码支付开发并没有现成的java示例,总结一下自己微信扫码支付心得
二、首先去微信公众平台申请账户
https://mp.weixin.qq.com
**
三、账户开通、开发者认证之后就可以进行微信支付开发了
1、微信统一下单接口调用获取预支付id,以及生成二维码所需的codeurl
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
/**
* 保存订单,并生成二维码所需的codeurl
*
* @param request
* @param response
* @param notifyurlbuf
* @param order
* @return
* @throws exception
*/
@override
public map<string, string> getwechatorderinfo(string ip, cuser user, string notifyurl, order order) throws exception {
map<string, string> resultmap = new hashmap<string, string>();
// 生成并保存订单
order.setuserid(user.getid());
// 支付方式 0:银联 1:支付宝 2:网上银行 3:微信 4:其他
order.setpaytype("3");
// 生成订单号
order.setorderno(ordernogenerator.getorderno());
// 订单类型 1:消费 2:退款
order.setordertype("1");
// 订单创建时间
order.setcreatetime(new date());
// 订单更新时间
order.setupdatetime(new date());
// 订单状态 0: 交易中 1:完成 2:已取消
order.setorderstatus("0");
// 付款状态 0:失败 1:成功 2、待付款
order.setpaystatus("2");
// 设置订单失效时间
calendar calendar = calendar.getinstance();
calendar.add(calendar.hour, 2);
order.setexpiretime(calendar.gettime());
integer orderid = this.balancedao.saveorder(order);
map<string, string> paypreidmap = new hashmap<>();
paypreidmap = wechatutil.getpaypreid(string.valueof(orderid), "体检报告", notifyurl, ip,
string.valueof((order.getmoney().multiply(new bigdecimal(100)).intvalue())), orderid.tostring());
string prepayid = paypreidmap.get("prepay_id");
// 更新
order.setid(orderid);
order.setprepayid(prepayid);
order.setcodeurl(paypreidmap.get("code_url"));
this.balancedao.updateorder(order);
// return wechatutil.qrfromgoogle(order.getcodeurl(), 300, 0);
resultmap.put("codeurl", order.getcodeurl());
resultmap.put("orderid", string.valueof(order.getid()));
return resultmap;
}
|
此方法返回的数据如下
?
|
1
2
3
4
5
6
7
8
9
10
11
|
<xml><return_code><![cdata[success]]></return_code>
<return_msg><![cdata[ok]]></return_msg>
<appid><![cdata[wxaf0b*****8afbf]]></appid>
<mch_id><![cdata[1408****02]]></mch_id>
<nonce_str><![cdata[zf0vgvdtvycbliwb]]></nonce_str>
<sign><![cdata[a2910f16086211153d747058063b3368]]></sign>
<result_code><![cdata[success]]></result_code>
<prepay_id><![cdata[wx201701191109388037e9a12310276591827]]></prepay_id>
<trade_type><![cdata[native]]></trade_type>
<code_url><![cdata[weixin://wxpay/bizpayurl?pr=1ujornx]]></code_url>
</xml>
|
2、服务器端接受微信支付结果通知
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
/**
* 保存微信通知结果
*
* @param request
* @param response
* @return
* @throws exception
*/
@override
public string savewechatnotify(string notifyinfoxml) throws exception {
map<string, string> noticemap = xmlutil.doxmlparse(notifyinfoxml);
// 这个其实是订单 的id
string outtradeno = noticemap.get("out_trade_no");
order order = this.balancedao.getorderbyid(integer.valueof(outtradeno));
// 如果支付通知信息不为,说明请求已经处理过,直接返回
if (stringutil.isnotempty(order.getnotifyinfo())) {
return "success";
}
string sign = noticemap.get("sign");
noticemap.remove("sign");
// 验签通过
if (wechatutil.getsignveryfy(noticemap, sign)) {
// 通信成功此字段是通信标识,非交易标识,交易是否成功需要查看result_code来判断
if ("success".equals(noticemap.get("return_code"))) {
// 交易成功
if ("success".equals(noticemap.get("result_code"))) {
// 商户订单号
// 订单更新时间
order.setupdatetime(new date());
// ------------------------------
// 处理业务开始
// ------------------------------
// 是否交易成功,1:成功0:失败
// 微信支付成功
order.setpaystatus("1");
// 订单状态 0: 交易中 1:完成 2:已取消
order.setorderstatus("1");
// 保存通知信息
order.setnotifyinfo(notifyinfoxml);
this.balancedao.updateorder(order);
// 处理业务完毕
} else {
// 错误时,返回结果未签名,记录retcode、retmsg看失败详情。
logger.info("查询验证签名失败或业务错误");
logger.info("retcode:" + noticemap.get("retcode") + " retmsg:" + noticemap.get("retmsg"));
}
return "success";
} else {
logger.info("后台调用通信失败");
}
return "success";
} else {
logger.info("通知签名验证失败");
}
return null;
}
|
3、上面代码用到的工具方法都在wechatutil.java工具类中
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
