后台调用微信预支付接口获取prepay_id和nonce_str供前段唤醒微信使用。需二次签名,不可直接使用返回值中的sign。
private SfwDataOut wXPayInit(String ip, String order_id, String description, String tradeType, int totalFee) throws Exception {
SfwDataOut sfwOut = new SfwDataOut();
Map<String, String> paramMap = new HashMap<String, String>();
String key = "4E5D171E22BF2B975ED7F410EC54EF75";
String appId = "wx066b1f5851da7dd5";
String mchId = "1610832735";
paramMap.put("appid", appId); // 商家平台ID
paramMap.put("body", description); // 商家名称-销售商品类目、String(128)
paramMap.put("mch_id", mchId); // 商户ID
paramMap.put("nonce_str", WXPayUtil.generateNonceStr()); // UUID
paramMap.put("out_trade_no", order_id);// 订单号,每次都不同
paramMap.put("spbill_create_ip", ip);
paramMap.put("total_fee", "1"); // 支付金额,单位分
paramMap.put("notify_url", "http://www.zoni8.cn/app/wx/callback");// 此路径是微信服务器调用支付结果通知路径随意写
paramMap.put("trade_type", tradeType); // 支付类型
String sign = WXPayUtil.generateSignature(paramMap, key);
paramMap.put("sign", sign);
String xml = WXPayUtil.mapToXml(paramMap);// 将所有参数(map)转xml格式
URL realUrl = new URL("https://api.mch.weixin.qq.com/pay/unifiedorder");
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(xml);
out.flush();
BufferedReader in = null;
Map<String, Object> vals = new HashMap<String, Object>();
try {
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = "";
String result = "";
while ((line = in.readLine()) != null) {
result += line;
}
Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
GregorianCalendar gc = new GregorianCalendar();
int timestamp = (int) gc.getTimeInMillis();
Map newMap = new HashMap();
newMap.put("appid", resultMap.get("appid"));
newMap.put("partnerid", resultMap.get("mch_id"));
newMap.put("prepayid", resultMap.get("prepay_id"));
newMap.put("package", "Sign=WXPay");
newMap.put("noncestr", resultMap.get("nonce_str"));
newMap.put("timestamp",String.valueOf(timestamp));
String newSign = WXPayUtil.generateSignature(newMap, key);
System.out.println("返回值:" + result);
System.out.println("准备id:" + resultMap.get("prepay_id"));
String returnCode = resultMap.get("return_code");
if (returnCode != null && "SUCCESS".equals(returnCode)) {
vals.put("sign", newSign);
vals.put("prepay_id", resultMap.get("prepay_id"));
vals.put("appid", resultMap.get("appid"));
vals.put("mch_id", resultMap.get("mch_id"));
vals.put("package", "Sign=WXPay");
vals.put("nonce_str", resultMap.get("nonce_str"));
vals.put("timestamp", timestamp);
} else {
vals.put("error", "true");
}
} catch (Exception e) {
// in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
vals.put("error", e.getMessage());
}
sfwOut.addData(vals);
return sfwOut;
}
前端flutter(fluwx插件)
//微信注册APP程序
registerWxApi(
appId: Config.weichat_merchant_id, // zbml appid
doOnAndroid: true,
doOnIOS: true,
universalLink: Config.UNIVERSAL_LINK);
//唤醒微信支付
payWithWeChat(
appId: payInfo['appid'],
partnerId: Config.weichat_mch_id,
prepayId: payInfo['prepay_id'],
packageValue: payInfo['package'],
nonceStr: payInfo['nonce_str'],
timeStamp: payInfo['timestamp'],
sign: payInfo['sign'],
);