小程序后端支付
package com.litte.util;
import com.litte.entity.PayUtil;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;
public class TestJxx {
public static void main(String[] args) throws Exception {
PayUtil payUtil = new PayUtil();
payUtil.setAppid("appId");
payUtil.setMch_id("mchId");
payUtil.setPayKey("payKey");
payUtil.setBody("商品名称");
payUtil.setOut_trade_no("20190825155036");
payUtil.setNonce_str("0140944616C441F6B46B4B5660B9CBA7");
payUtil.setTotal_fee(3000);
payUtil.setDevice_info("WEB");
payUtil.setNotify_url("支付回调地址");
payUtil.setOpenid("oppenId");
wxPay(payUtil);
}
public static Map<String, Object> wxPay(PayUtil payUtil) throws Exception {
String stringA = "appid=" + payUtil.getAppid()
+ "&body=" + payUtil.getBody()
+ "&mch_id=" + payUtil.getMch_id()
+ "&nonce_str=" + payUtil.getNonce_str()
+ "¬ify_url=" + payUtil.getNotify_url()
+ "&openid=" + payUtil.getOpenid()
+ "&out_trade_no=" + payUtil.getOut_trade_no()
+ "&spbill_create_ip=IP"
+ "&total_fee=" + payUtil.getTotal_fee()
+ "&trade_type=JSAPI"
+ "&key=" + payUtil.getPayKey();
String sign = Md5Util.md5(stringA).toUpperCase();
String xml = "<xml>" +
" <appid>" + payUtil.getAppid() + "</appid>" +
" <body>" + payUtil.getBody() + "</body>" +
" <mch_id>" + payUtil.getMch_id() + "</mch_id>" +
" <nonce_str>" + payUtil.getNonce_str() + "</nonce_str>" +
" <notify_url>" + payUtil.getNotify_url() + "</notify_url>" +
" <openid>" + payUtil.getOpenid() + "</openid>" +
" <out_trade_no>" + payUtil.getOut_trade_no() + "</out_trade_no>" +
" <spbill_create_ip>IP</spbill_create_ip>" +
" <total_fee>" + payUtil.getTotal_fee() + "</total_fee>" +
" <trade_type>JSAPI</trade_type>" +
" <sign>" + sign + "</sign>" +
"</xml> ";
String result = BasePay.httpRequest("https://api.mch.weixin.qq.com/pay/unifiedorder", "POST", xml);
Map map = doXMLParse(result);
String nonceStr = UUIDUtil.getUUID().toUpperCase();
String dateTime = String.valueOf(new Date().getTime());
String signType = "MD5";
String packages = "prepay_id=" + map.get("prepay_id").toString();
String paySign = "appId=" + map.get("appid")
+ "&nonceStr=" + nonceStr
+ "&package=" + packages
+ "&signType=" + signType
+ "&timeStamp=" + dateTime
+ "&key=" + payUtil.getPayKey();
map.put("timeStamp", dateTime);
map.put("paySign", Md5Util.md5(paySign).toUpperCase());
map.put("nonceStr", nonceStr);
map.put("signType", signType);
map.put("package", packages);
return map;
}
public static Map doXMLParse(String strxml) throws Exception {
if (null == strxml || "".equals(strxml)) {
return null;
}
Map m = new HashMap();
InputStream in = String2Inputstream(strxml);
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(in);
Element root = doc.getRootElement();
List list = root.getChildren();
Iterator it = list.iterator();
while (it.hasNext()) {
Element e = (Element) it.next();
String k = e.getName();
String v = "";
List children = e.getChildren();
if (children.isEmpty()) {
v = e.getTextNormalize();
} else {
v = getChildrenText(children);
}
m.put(k, v);
}
in.close();
return m;
}
public static InputStream String2Inputstream(String str) {
return new ByteArrayInputStream(str.getBytes());
}
public static String getChildrenText(List children) {
StringBuffer sb = new StringBuffer();
if (!children.isEmpty()) {
Iterator it = children.iterator();
while (it.hasNext()) {
Element e = (Element) it.next();
String name = e.getName();
String value = e.getTextNormalize();
List list = e.getChildren();
sb.append("<" + name + ">");
if (!list.isEmpty()) {
sb.append(getChildrenText(list));
}
sb.append(value);
sb.append("</" + name + ">");
}
}
return sb.toString();
}
}
小程序前端实现
pay: function() {
var that = this;
var iMode = that.data.way;
var orderid = that.data.orderid;
app.request("/indent/payIndent", {
id: orderid,
iMode: iMode
}, function(res) {
if (iMode == 0) {
wx.requestPayment({
'timeStamp': res.data.timeStamp,
'nonceStr': res.data.nonceStr,
'package': res.data.package,
'signType': res.data.signType,
'paySign': res.data.paySign,
'success': function(res) {
wx.redirectTo({
url: ''
})
},
'fail': function(res) {
wx.redirectTo({
url: ''
})
}
})
}
})
},