微信小程序支付代码

/**

* 商户号,商户秘钥,appid,appsecrect 参数以及订单号,订单金额,时间戳等,一起签名后,请求微信预支付接口,微信端解密后校验商户号,商户秘钥,appid,appsecrect是否和微信公众平台填写的一致,

* 微信支付预付单生成

*/

@GetMapping("payOrder")

public JSONObjectunifiedOrder(@LoginUser String userId, String ordersId)throws Exception {

User user =userService.findUserByUserId(userId);

    if (Objects.isNull(user)) {

return ResponseData.fail("没有查询到该用户");

    }

if (YesOrNo.YES.equals(user.getDisable())) {

return ResponseData.fail("你已被禁用了哦");

    }

Orders orders =orderService.findOrdersByOrderId(ordersId);

    if (Objects.isNull(orders)) {

return ResponseData.fail("没有查询打订单的相关信息");

    }

if (!PayStatus.PAYMENT.equals(orders.getPayStatus())) {

return ResponseData.fail("该订单不是待支付状态");

    }

Map param =new LinkedHashMap<>();

    WXPay wxPay =new WXPay(config);

    param.put("appid", config.getAppID());

    param.put("mch_id", config.getMchID());

    param.put("nonce_str", WXPayUtil.generateNonceStr());

    String sign = WXPayUtil.generateSignature(param, config.getKey(), WXPayConstants.SignType.MD5);

    param.put("sign", sign);

    param.put("body", "订单支付");

    param.put("out_trade_no", orders.getSn());

    BigDecimal a=new BigDecimal(100);

    Stringtotal_fee=  orders.getOrderPrice().multiply(a).stripTrailingZeros().toPlainString();

    param.put("total_fee",  total_fee.trim());

    param.put("spbill_create_ip", SPBILL_CREATE_IP);

    param.put("notify_url", NOTIFY_URL);

    param.put("trade_type", "JSAPI");

    //trade_type=JSAPI时(即JSAPI支付),此参数必传

    param.put("openid", user.getOpenId());

    Map response = wxPay.unifiedOrder(param);

    //调微信官方文档支付

    if ("SUCCESS".equals(response.get("return_code"))) {

String timeStamp = Long.toString(System.currentTimeMillis() /1000);

        String nonce = WXPayUtil.generateNonceStr();

        //装载拼接字符串用于二次签名验证

        StringBuffer sb =new StringBuffer();

        sb.append("appId=").append(response.get("appid"));

        sb.append("&nonceStr=").append(nonce);

        sb.append("&package=").append("prepay_id=").append(response.get("prepay_id"));

        sb.append("&signType=").append("MD5");

        sb.append("&timeStamp=").append(timeStamp).append("&key=").append(config.getKey());

        String paySign = WXPayUtil.MD5(sb.toString());

        //把用于验证签名的返回给前端

        response.put("appId", response.get("appid"));

        response.put("timeStamp", timeStamp);

        response.put("nonceStr", nonce);

        response.put("prepayId", response.get("prepay_id"));

        response.put("signType", "MD5");

        response.put("paySign", paySign);

        response.put("nonceStr", nonce);

        response.put("package", "prepay_id=" + response.get("prepay_id"));

        return ResponseData.success(response);

    }else {

return ResponseData.fail(response.get("return_msg"));

    }

}


微信小程序支付代码_第1张图片
代码图

你可能感兴趣的:(微信小程序支付代码)