废话不多说,直接看代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
RequestHandler requestHandler = new RequestHandler(super.getRequest(),super.getResponse()); //获取token //两小时内有效,两小时后重新获取 Token = requestHandler.GetToken(); //更新token 到应用中 requestHandler.getTokenReal(); System.out.println("微信支付获取token=======================:" +Token); //requestHandler 初始化 requestHandler.init(); requestHandler.init(appid,appsecret, appkey,partnerkey, key); |
代码:本地系统生成订单
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
|
// 设置package订单参数 SortedMap<String, String> packageParams = new TreeMap<String, String>(); packageParams.put( "bank_type" , "WX" ); // 支付类型 packageParams.put( "body" , "xxxx" ); // 商品描述 packageParams.put( "fee_type" , "1" ); // 银行币种 packageParams.put( "input_charset" , "UTF-8" ); // 字符集 packageParams.put( "notify_url" , "http://xxxx.com/xxxx/wxcallback" ); // 通知地址 这里的通知地址使用外网地址测试,注意 80 端口是否打开。 packageParams.put( "out_trade_no" , no); // 商户订单号 packageParams.put( "partner" , partenerid); // 设置商户号 packageParams.put( "spbill_create_ip" , super .getRequest().getRemoteHost()); // 订单生成的机器IP,指用户浏览器端IP packageParams.put( "total_fee" , String.valueOf(rstotal)); // 商品总金额,以分为单位 // 设置支付参数 SortedMap<String, String> signParams = new TreeMap<String, String>(); signParams.put( "appid" , appid); signParams.put( "noncestr" , noncestr); signParams.put( "traceid" , PropertiesUtils.getOrderNO()); signParams.put( "timestamp" , timestamp); signParams.put( "package" , packageValue); signParams.put( "appkey" , this .appkey); // 生成支付签名,要采用URLENCODER的原始值进行SHA1算法! String sign = "" ; try { sign = Sha1Util.createSHA1Sign(signParams); } catch (Exception e) { e.printStackTrace(); } // 增加非参与签名的额外参数 signParams.put( "sign_method" , "sha1" ); signParams.put( "app_signature" , sign); // api支付拼包结束------------------------------------ //获取prepayid String prepayid = requestHandler.sendPrepay(signParams); System.out.println( "prepayid :" + prepayid); |
代码:生成预付快订单完成
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
|
//生成预付快订单完成,返回给android,ios 掉起微信所需要的参数。 SortedMap<String, String> payParams = new TreeMap<String, String>(); payParams.put( "appid" , appid); payParams.put( "noncestr" , noncestr); payParams.put( "package" , "Sign=WXPay" ); payParams.put( "partnerid" , partenerid); payParams.put( "prepayid" , prepayid); payParams.put( "appkey" , this .appkey); //这里除1000 是因为参数长度限制。 int time = ( int ) (System.currentTimeMillis() / 1000 ); payParams.put( "timestamp" ,String.valueOf(time)); System.out.println( "timestamp:" + time); //签名 String paysign = "" ; try { paysign = Sha1Util.createSHA1Sign(payParams); } catch (Exception e) { e.printStackTrace(); } payParams.put( "sign" , paysign); //拼json 数据返回给客户端 BasicDBObject backObject = new BasicDBObject(); backObject.put( "appid" , appid); backObject.put( "noncestr" , payParams.get( "noncestr" )); backObject.put( "package" , "Sign=WXPay" ); backObject.put( "partnerid" , payParams.get( "partnerid" )); backObject.put( "prepayid" , payParams.get( "prepayid" )); backObject.put( "appkey" , this .appkey); backObject.put( "timestamp" ,payParams.get( "timestamp" )); backObject.put( "sign" ,payParams.get( "sign" )); String backstr = dataObject.toString(); System.out.println( "backstr:" + backstr); return backstr; |
到此为止,预付款订单已生成,并且已返回客户端,坐等微信服务器通知,通知的地址就是生成预付款订单的notify_url
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
|
ResponseHandler resHandler = new ResponseHandler(request, response); resHandler.setKey(partnerkey); //创建请求对象 //RequestHandler queryReq = new RequestHandler(request, response); //queryReq.init(); if (resHandler.isTenpaySign() == true ) { //商户订单号 String out_trade_no = resHandler.getParameter( "out_trade_no" ); System.out.println( "out_trade_no:" + out_trade_no); //财付通订单号 String transaction_id = resHandler.getParameter( "transaction_id" ); System.out.println( "transaction_id:" + transaction_id); //金额,以分为单位 String total_fee = resHandler.getParameter( "total_fee" ); //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee String discount = resHandler.getParameter( "discount" ); //支付结果 String trade_state = resHandler.getParameter( "trade_state" ); //判断签名及结果 if ( "0" .equals(trade_state)) { //------------------------------ //即时到账处理业务开始 //------------------------------ System.out.println( "----------------业务逻辑执行-----------------" ); //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— System.out.println( "----------------业务逻辑执行完毕-----------------" ); System.out.println( "success" ); // 请不要修改或删除 System.out.println( "即时到账支付成功" ); //给财付通系统发送成功信息,财付通系统收到此结果后不再进行后续通知 resHandler.sendToCFT( "success" ); //给微信服务器返回success 否则30分钟通知8次 return "success" ; } else { System.out.println( "通知签名验证失败" ); resHandler.sendToCFT( "fail" ); response.setCharacterEncoding( "utf-8" ); } } else { System.out.println( "fail -Md5 failed" ); } |
以上就是java实现微信支付服务端所要提供的代码,希望大家仔细研究,实现微信支付。