1.申请商户号,设置api密钥.
2.在开发电脑上安装安全控件.
3.我所开发的业务逻辑是前端调用我提供的一个支付前的接口,Java后台先去查询订单相关内容,设置好参数(需要的参数可查看https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1)后调用微信方统一下单接口(注意经常会报签名错误,请检查好自己生成的签名和微信的是否一致 https://pay.weixin.qq.com/wiki/tools/signverify/ 点击这个网址可以校验一下,微信生成的和自己生成的是否一致),统一下单接口返回成功后,用返回的参数再次签名,返回给小程序前端所需要的参数.代码如下:
public String userPay(Object object) {
//开发者的订单信息
OrderListVo orderListVo = new OrderListVo(;)
//调统一下单接口
Map unifiedOrderMap = unifiedOrder(orderListVo);
//再次签名返回前端
return signAgain(unifiedOrderMap);
}
/**
* 统一下单
* @param request
*/
public Map unifiedOrder(OrderListVo orderListVo) {
//统一下单url
String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
//小程序的appid
parameters.put("appid", WXPayConstants.AppID);
// 此处传入openid(授权登录后获取的)
parameters.put("openid", orderListVo.getOpenId());
// 微信支付商户号
parameters.put("mch_id", WXPayConstants.MCHID);
// 随机字符串,用UUID生成
String nonceStr = UniqueUtil.generateUUID();
parameters.put("nonce_str", nonceStr);
// 常量MD5
parameters.put("sign_type", WXPayConstants.MD5);
// 商品说明
parameters.put("body", orderListVo.getGoodsName());
// 商品详情
parameters.put("detail", orderListVo.getGoodsDescribe());
// 自定义订单号,不能超过32个字符
parameters.put("out_trade_no", orderListVo.getOrderNo());
// 标价金额:支付金额单位为【分】,参数值不能带小数
//int price = (int) (orderListVo.getPrice()*100);
parameters.put("total_fee","1");
// 终端IP:APP和网页支付提交用户端ip(存在反向代理,需获取用户真实ip)
parameters.put("spbill_create_ip", "127.0.0.1");
// 通知地址:此路径为接收微信支付结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数
parameters.put("notify_url", "www.baidu.com");
// 交易类型:JSAPI 公众号支付 NATIVE 扫码支付 APP APP支付
parameters.put("trade_type", "JSAPI");
try {
// 下单前签名(微信端会对其进行校验)
parameters.put("sign", PayUtil.createSign("utf-8", parameters));
// 将map转换为xml数据
String xml = PayUtil.getRequestXml(parameters);
// 发送post请求,返回xml数据
String resp = PayUtil.httpsRequest(url, "POST", xml);
Map<String, String> unifiedOrderMap = XMLUtil.doXMLParse(resp);
return unifiedOrderMap;
} catch (Exception e) {
logger.error("统一下单,失败", e);
return null;
}
}
private String signAgain(Map unifiedOrderMap) {
String returnCode = (String) unifiedOrderMap.get("return_code");
String resultCode = (String) unifiedOrderMap.get("result_code");
// 需要返回给页面的数据
SortedMap<Object, Object> returnMap = new TreeMap<Object, Object>();
try {
if ("SUCCESS".equals(returnCode) && "SUCCESS".equals(resultCode)) {
// 进行签名校验
SortedMap<Object, Object> map = new TreeMap<Object, Object>();
map.put("return_code", unifiedOrderMap.get("return_code"));
map.put("return_msg", unifiedOrderMap.get("return_msg"));
map.put("