java微信支付

发布时间:2025-07-07 02:12

使用信誉良好的支付平台,如支付宝、微信支付等。 #生活常识# #购物消费技巧# #线上支付安全#

在没做过微信支付时感觉被微信支付搞到头都大了,但是当真的理解了后就感觉很简单其实就是带参数发请求,没做过支付肯定看不懂微信支付官方文档的流程图,看完一脸闷。。。

下面在这里说下流程

1:当用户发起支付请求时在我们自己的后台拼接好必要的参数发送给微信。(生成预支付订单)

1.1:参数(主要参数)

  (1)appid:公众号或者小程序的appid

  (2)mch_id:   商户号id

  (3)nonce_str:    随即字符串

  (4)body:   商品描述

  (5)out_trade_no:     商户订单号

  (6)total_fee:      总金额(注意:以分为单位)

  (7)spbill_create_ip:       终端IP

  (8)notify_url:      通知地址:回调函数

  (9)trade_type:       交易类型

  (10)openid:        用户的openid,在网页支付等不需要

  (11)sign:        签名

2:微信返回给我们一些前段调起支付必要的参数

2.1参数(主要参数)

  (1)appid:公众号或者小程序的appid

  (2)mch_id:   商户号id

  (3)nonce_str:   随机字符串

  (4)sign:   签名

  (5)result_code:    业务结果

  (6)trade_type:    交易类型

  (7)prepay_id:     预支付交易会话标识

3:返回的参数如果是公众号或者小程序支付需要把一些参数再次加密,返回给前台调取支付,否则前段会显示签名错误。

4:用户支付完成后微信回调notify_url所配置的地址(注意:回调处理完成后不管成功或失败一定要给微信返回成功或失败的通知,否则微信会多次回调)。

5:好了流程说完了现在开始上代码。(注意:我是为了演示所以返回的是Map,请根据需求自行返回,下面的参数都是我乱写的请改成自己所需要的)

public static Map<String, Object> wxPay() {

String nonceStr = UUID.randomUUID().toString().replaceAll("-", "");

StringBuffer param = new StringBuffer();

param.append("<xml>");

param.append("<appid>xxxxxxxxx</appid>");

param.append("<mch_id>xxxxxxxxx</mch_id>");

param.append("<nonce_str>" + nonceStr + "</nonce_str>");

param.append("<body>我来测试下</body>");

param.append("<out_trade_no>2019012322987654</out_trade_no>");

param.append("<total_fee>1</total_fee>");

param.append("<spbill_create_ip>192.101.0.1</spbill_create_ip>");

param.append("<notify_url>http:xxx.cn/xxx</notify_url>");

param.append("<trade_type>JSAPI</trade_type>");

param.append("<openid>xxxxxxxxxxxxxxxxx</openid>");

String noncestr = "appid=xxxxxxxxx"

+ "&body=我来测试下"

+ "&mch_id=xxxxxxxxx"

+ "&nonce_str=" + nonceStr

+ "&notify_url=http:xxx.cn/xxx"

+ "&openid=xxxxxxxxxxxxxxxxx"

+ "&out_trade_no=2019012322987654"

+ "&spbill_create_ip=192.101.0.1"

+ "&total_fee=1"

+ "&trade_type=JSAPI"

+ "&key=xxxxxxxxxxxxxxxxxxxxxxxx";

String md5Encode = MD5.MD5Encode(noncestr);

param.append("<sign>" + md5Encode + "</sign>");

param.append("</xml>");

String result = HttpUrlUtil.sendPost(

"https://api.mch.weixin.qq.com/pay/unifiedorder",

param.toString());

System.out.println(new String(result.getBytes(), "UTF-8"));

String return_code = null;

String return_msg = null;

String result_code = null;

String appid = null;

String mch_id = null;

String nonceStr = sb.toString();

String prepay_id = null;

String timeStamp = (System.currentTimeMillis() / 1000) + "";

String signType = "MD5";

signType = new String(signType.getBytes(), "UTF-8");

String paySign = null;

Document doc = DocumentHelper.parseText(result);

Element node = doc.getRootElement();

List<Element> listElement = node.elements();

for (Element e : listElement) {

if (e.getName().equals("return_code")) {

return_code = e.getTextTrim();

} else if (e.getName().equals("return_msg")) {

return_msg = e.getTextTrim();

} else if (e.getName().equals("result_code")) {

result_code = e.getTextTrim();

} else if (e.getName().equals("prepay_id")) {

prepay_id = "prepay_id=" + e.getTextTrim();

} else if (e.getName().equals("appid")) {

appid = e.getTextTrim();

} else if (e.getName().equals("mch_id")) {

mch_id = e.getTextTrim();

}

}

Map<String, Object> map = new HashMap<String, Object>();

if (result_code.equals("SUCCESS")) {

String pay = "appId=" + appid + "&nonceStr=" + nonceStr

+ "&package=" + prepay_id + "&signType=" + signType

+ "&timeStamp=" + timeStamp

+ "&key=xxxxxxxxxxxxxxxxxxxxxxxx";

String md5EncodeXCX = MD5.MD5Encode(pay);

map.put("nonceStr", nonceStr);

map.put("package", prepay_id);

map.put("timeStamp", timeStamp);

map.put("signType", signType);

map.put("appId", appid);

map.put("paySign", md5EncodeXCX);

map.put("order", orderId);

} else {

throw new Exception("操作失败");

}

return map;

}

java

运行

6:参数这样就到了前台了由于写的是小程序或公众号的支付所以只列举这两个

6.1:公众号(注意:把对应的参数放上去)

WeixinJSBridge.invoke(

'getBrandWCPayRequest', {

"appId": xxx,

"timeStamp": xxx,

"nonceStr": xxx,

"package": xxx,

"signType": xxx,

"paySign": xxx

},

function (res) {

if (res.err_msg == "get_brand_wcpay_request:ok") {

alert("支付成功");

}

if (res.err_msg == "get_brand_wcpay_request:cancel") {

alert("支付取消");

}

if (res.err_msg == "get_brand_wcpay_request:fail") {

alert("支付失败");

}

}

);

javascript

运行

6.2:公众号支付(注意:把对应的参数放上去)

wx.requestPayment({

'timeStamp': xxx,

'nonceStr': xxx,

'package': xxx,

'signType': xxx,

'paySign': xxx,

'success': function (res) {

},

'fail': function (res) {

}

})

javascript

运行

7:支付回调

private String Xxx(HttpServletRequest req, HttpServletResponse resp) throws IOException {

java.io.BufferedInputStream bis = new java.io.BufferedInputStream(

req.getInputStream());

byte read[] = new byte[1024 * 1024];

String loc = "";

try {

while ((bis.read(read, 0, 1 * 1)) != -1) {

loc += new String(read, 0, 1 * 1);

}

String total_fee = null;

String result_code = null;

String return_code = null;

String time_end = null;

String is_subscribe = null;

String out_trade_no = null;

Document doc = DocumentHelper.parseText(loc);

Element root = doc.getRootElement();

List<Element> list1 = root.elements();

for (Element e : list1) {

if (e.getName().equals("total_fee")) {

total_fee = e.getTextTrim();

} else if (e.getName().equals("result_code")) {

result_code = e.getTextTrim();

} else if (e.getName().equals("return_code")) {

return_code = e.getTextTrim();

} else if (e.getName().equals("time_end")) {

time_end = e.getTextTrim();

} else if (e.getName().equals("is_subscribe")) {

is_subscribe = e.getTextTrim();

} else if (e.getName().equals("out_trade_no")) {

out_trade_no = e.getTextTrim();

}

}

PrintWriter out = resp.getWriter();

if (result_code.equals("SUCCESS") && return_code.equals("SUCCESS")) {

resXml = "<xml>"

+ "<return_code><![CDATA[SUCCESS]]></return_code>"

+ "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";

resXml = new String(resXml.getBytes(), "UTF-8");

} else {

resXml = "<xml>"

+ "<return_code><![CDATA[FAIL]]></return_code>"

+ "<return_msg><![CDATA[订单已存在或金额错误]]></return_msg>"

+ "</xml> ";

resXml = new String(resXml.getBytes(), "UTF-8");

}

} catch (DocumentException e) {

e.printStackTrace();

} finally {

transaction = null;

bis.close();

}

resp.getWriter().write(resXml);

return resXml;

}

java

运行

好了至此微信支付流程完成。

PS:有人今天弄好了支付,明天就不行了报签名错误,请更换商户秘钥再次尝试

网址:java微信支付 https://www.yuejiaxmz.com/news/view/1136337

相关内容

微信支付
对接支付宝、微信、第三方支付,超详细讲解+demo演示
Android支付集成:支付宝、微信支付实现
java向支付宝生活号推送消息
移动端支付之微信支付
uniapp微信支付、支付宝支付、银联支付经验总结
微信支付:2018微信支付智慧生活行业手册.pdf
第三方支付平台:支付宝与微信支付,让支付更便捷
Android平台微信与支付宝支付集成教程
支付系统集成:支付宝微信支付与银联支付的实战案例

随便看看