新零售支付全家桶


创建支付订单代码实现

创建PayOrderForm.java

@Data
@ApiModel(value = "订单付款的表单")
public class PayOrderForm {
    @ApiModelProperty(value = "订单ID")
    @Min(1)
    private Integer orderId;

}

编写WxController中的创建付款方法,用于创建支付订单

@Login
@PostMapping("/microAppPayOrder")
@ApiOperation("小程序付款")
public R microAppPayOrder(@RequestBody PayOrderForm form, @RequestHeader HashMap header) {
    ValidatorUtils.validateEntity(form);
    String token = header.get("token").toString();
    Long userId = Long.parseLong(jwtUtils.getClaimByToken(token).getSubject());
    int orderId = form.getOrderId();
    UserEntity user = new UserEntity();
    user.setUserId(userId);
    QueryWrapper wrapper = new QueryWrapper(user);
    long count = userService.count(wrapper);
    if (count == 0) {
        return R.error("用户不存在");
    }
    String openId = userService.getOne(wrapper).getOpenId();

    OrderEntity order = new OrderEntity();
    order.setUserId(userId.intValue());
    order.setId(orderId);
    order.setStatus(1);
    wrapper = new QueryWrapper(order);
    count = orderService.count(wrapper);
    if (count == 0) {
    return R.error("不是有效的订单");
    }
    //验证购物券是否有效
    //验证团购活动是否有效

    order = new OrderEntity();
    order.setId(orderId);
    wrapper = new QueryWrapper(order);
    order = orderService.getOne(wrapper);
    //向微信平台发出请求,创建支付订单
    String amount = order.getAmount().multiply(new BigDecimal("100")).intValue() + "";

    try {
        WXPay wxPay = new WXPay(myWXPayConfig);
        HashMap map = new HashMap();
        map.put("nonce_str", WXPayUtil.generateNonceStr()); //随机字符串
        map.put("body", "订单备注");
        map.put("out_trade_no", order.getCode());
        map.put("total_fee", amount);
        map.put("spbill_create_ip", "127.0.0.1");
        map.put("notify_url", "https://127.0.0.1/test");
        map.put("trade_type", "JSAPI");
        map.put("openid", openId);
        Map<String, String> result = wxPay.unifiedOrder(map);
        String prepayId = result.get("prepay_id");
        System.out.println(prepayId);
        if (prepayId != null) {
            order.setPrepayId(prepayId);
            order.setPaymentType(1);
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id", order.getId());
            orderService.update(order, updateWrapper);

            //生成数字签名
            map.clear();
            map.put("appId", appId);
            String timeStamp = new Date().getTime() + "";
            map.put("timeStamp", timeStamp);
            String nonceStr = WXPayUtil.generateNonceStr();
            map.put("nonceStr", nonceStr);
            map.put("package", "prepay_id=" + prepayId);
            map.put("signType", "MD5");
            String paySign = WXPayUtil.generateSignature(map, key);
            return R.ok().put("package", "prepay_id=" + prepayId)
                .put("timeStamp", timeStamp)
                .put("nonceStr", nonceStr)
                .put("paySign", paySign);
        }
        else {
            return R.error("支付订单创建失败");
        }
    }
    catch (Exception e) {
        e.printStackTrace();
        return R.error("微信支付模块故障");
    }
}

编写order.vue

uni.request({
    url: that.url.wx.microAppPayOrder,
    method: "POST",
    header: {
        "token": uni.getStorageSync("token")
    },
    data: {
        "orderId": id
    },
    success: function(resp) {
        // console.log(resp)
        let timeStamp = resp.data.timeStamp
        let nonceStr = resp.data.nonceStr
        let pk = resp.data.package
        let paySign = resp.data.paySign
    }
})

页面列表

ITEM_HTML