Java Code Examples for com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult#getPackageValue()

The following examples show how to use com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult#getPackageValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WxOrderService.java    From BigDataPlatform with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 付款订单的预支付会话标识
 * <p>
 * 1. 检测当前订单是否能够付款
 * 2. 微信商户平台返回支付订单ID
 * 3. 设置订单付款状态
 *
 * @param userId 用户ID
 * @param body   订单信息,{ orderId:xxx }
 * @return 支付订单ID
 */
@Transactional
public Object prepay(Integer userId, String body, HttpServletRequest request) {
    if (userId == null) {
        return ResponseUtil.unlogin();
    }
    Integer orderId = JacksonUtil.parseInteger(body, "orderId");
    if (orderId == null) {
        return ResponseUtil.badArgument();
    }

    LitemallOrder order = orderService.findById(orderId);
    if (order == null) {
        return ResponseUtil.badArgumentValue();
    }
    if (!order.getUserId().equals(userId)) {
        return ResponseUtil.badArgumentValue();
    }

    // 检测是否能够取消
    OrderHandleOption handleOption = OrderUtil.build(order);
    if (!handleOption.isPay()) {
        return ResponseUtil.fail(ORDER_INVALID_OPERATION, "订单不能支付");
    }

    LitemallUser user = userService.findById(userId);
    String openid = user.getWeixinOpenid();
    if (openid == null) {
        return ResponseUtil.fail(AUTH_OPENID_UNACCESS, "订单不能支付");
    }
    WxPayMpOrderResult result = null;
    try {
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        orderRequest.setOutTradeNo(order.getOrderSn());
        orderRequest.setOpenid(openid);
        orderRequest.setBody("订单:" + order.getOrderSn());
        // 元转成分
        int fee = 0;
        BigDecimal actualPrice = order.getActualPrice();
        fee = actualPrice.multiply(new BigDecimal(100)).intValue();
        orderRequest.setTotalFee(fee);
        orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request));

        result = wxPayService.createOrder(orderRequest);

        //缓存prepayID用于后续模版通知
        String prepayId = result.getPackageValue();
        prepayId = prepayId.replace("prepay_id=", "");
        LitemallUserFormid userFormid = new LitemallUserFormid();
        userFormid.setOpenid(user.getWeixinOpenid());
        userFormid.setFormid(prepayId);
        userFormid.setIsprepay(true);
        userFormid.setUseamount(3);
        userFormid.setExpireTime(LocalDateTime.now().plusDays(7));
        formIdService.addUserFormid(userFormid);

    } catch (Exception e) {
        e.printStackTrace();
        return ResponseUtil.fail(ORDER_PAY_FAIL, "订单不能支付");
    }

    if (orderService.updateWithOptimisticLocker(order) == 0) {
        return ResponseUtil.updatedDateExpired();
    }
    return ResponseUtil.ok(result);
}
 
Example 2
Source File: WxOrderService.java    From dts-shop with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 付款订单的预支付会话标识
 * <p>
 * 1. 检测当前订单是否能够付款 2. 微信商户平台返回支付订单ID 3. 设置订单付款状态
 *
 * @param userId
 *            用户ID
 * @param body
 *            订单信息,{ orderId:xxx }
 * @return 支付订单ID
 */
@Transactional
public Object prepay(Integer userId, String body, HttpServletRequest request) {
	if (userId == null) {
		logger.error("付款订单的预支付会话标识失败:用户未登录!");
		return ResponseUtil.unlogin();
	}
	Integer orderId = JacksonUtil.parseInteger(body, "orderId");
	if (orderId == null) {
		return ResponseUtil.badArgument();
	}

	DtsOrder order = orderService.findById(orderId);
	if (order == null) {
		return ResponseUtil.badArgumentValue();
	}
	if (!order.getUserId().equals(userId)) {
		return ResponseUtil.badArgumentValue();
	}

	// 检测是否能够取消
	OrderHandleOption handleOption = OrderUtil.build(order);
	if (!handleOption.isPay()) {
		logger.error("付款订单的预支付会话标识失败:{}", WxResponseCode.ORDER_REPAY_OPERATION.desc());
		return WxResponseUtil.fail(WxResponseCode.ORDER_REPAY_OPERATION);
	}

	DtsUser user = userService.findById(userId);
	String openid = user.getWeixinOpenid();
	if (openid == null) {
		logger.error("付款订单的预支付会话标识失败:{}", AUTH_OPENID_UNACCESS.desc());
		return WxResponseUtil.fail(AUTH_OPENID_UNACCESS);
	}
	WxPayMpOrderResult result = null;
	try {
		WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
		orderRequest.setOutTradeNo(order.getOrderSn());
		orderRequest.setOpenid(openid);
		orderRequest.setBody(CommConsts.DEFAULT_ORDER_FIX + order.getOrderSn());
		// 元转成分
		int fee = 0;
		BigDecimal actualPrice = order.getActualPrice();
		fee = actualPrice.multiply(new BigDecimal(100)).intValue();
		orderRequest.setTotalFee(fee);
		orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request));

		result = wxPayService.createOrder(orderRequest);

		// 缓存prepayID用于后续模版通知
		String prepayId = result.getPackageValue();
		prepayId = prepayId.replace("prepay_id=", "");
		DtsUserFormid userFormid = new DtsUserFormid();
		userFormid.setOpenid(user.getWeixinOpenid());
		userFormid.setFormid(prepayId);
		userFormid.setIsprepay(true);
		userFormid.setUseamount(3);
		userFormid.setExpireTime(LocalDateTime.now().plusDays(7));
		formIdService.addUserFormid(userFormid);

	} catch (Exception e) {
		logger.error("付款订单的预支付会话标识失败:{}", ORDER_PAY_FAIL.desc());
		e.printStackTrace();
		return WxResponseUtil.fail(ORDER_PAY_FAIL);
	}

	if (orderService.updateWithOptimisticLocker(order) == 0) {
		logger.error("付款订单的预支付会话标识失败:{}", "更新订单信息失败");
		return ResponseUtil.updatedDateExpired();
	}

	logger.info("【请求结束】购物车商品删除成功:清理的productIds:{}", JSONObject.toJSONString(result));
	return ResponseUtil.ok(result);
}
 
Example 3
Source File: WxOrderService.java    From mall with MIT License 4 votes vote down vote up
/**
 * 付款订单的预支付会话标识
 * <p>
 * 1. 检测当前订单是否能够付款
 * 2. 微信商户平台返回支付订单ID
 * 3. 设置订单付款状态
 *
 * @param userId 用户ID
 * @param body   订单信息,{ orderId:xxx }
 * @return 支付订单ID
 */
@Transactional
public Object prepay(Integer userId, String body, HttpServletRequest request) {
    if (userId == null) {
        return ResponseUtil.unlogin();
    }
    Integer orderId = JacksonUtil.parseInteger(body, "orderId");
    if (orderId == null) {
        return ResponseUtil.badArgument();
    }

    LitemallOrder order = orderService.findById(orderId);
    if (order == null) {
        return ResponseUtil.badArgumentValue();
    }
    if (!order.getUserId().equals(userId)) {
        return ResponseUtil.badArgumentValue();
    }

    // 检测是否能够取消
    OrderHandleOption handleOption = OrderUtil.build(order);
    if (!handleOption.isPay()) {
        return ResponseUtil.fail(ORDER_INVALID_OPERATION, "订单不能支付");
    }

    LitemallUser user = userService.findById(userId);
    String openid = user.getWeixinOpenid();
    if (openid == null) {
        return ResponseUtil.fail(AUTH_OPENID_UNACCESS, "订单不能支付");
    }
    WxPayMpOrderResult result = null;
    try {
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        orderRequest.setOutTradeNo(order.getOrderSn());
        orderRequest.setOpenid(openid);
        orderRequest.setBody("订单:" + order.getOrderSn());
        // 元转成分
        int fee = 0;
        BigDecimal actualPrice = order.getActualPrice();
        fee = actualPrice.multiply(new BigDecimal(100)).intValue();
        orderRequest.setTotalFee(fee);
        orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request));

        result = wxPayService.createOrder(orderRequest);

        //缓存prepayID用于后续模版通知
        String prepayId = result.getPackageValue();
        prepayId = prepayId.replace("prepay_id=", "");
        LitemallUserFormid userFormid = new LitemallUserFormid();
        userFormid.setOpenid(user.getWeixinOpenid());
        userFormid.setFormid(prepayId);
        userFormid.setIsprepay(true);
        userFormid.setUseamount(3);
        userFormid.setExpireTime(LocalDateTime.now().plusDays(7));
        formIdService.addUserFormid(userFormid);

    } catch (Exception e) {
        e.printStackTrace();
        return ResponseUtil.fail(ORDER_PAY_FAIL, "订单不能支付");
    }

    if (orderService.updateWithOptimisticLocker(order) == 0) {
        return ResponseUtil.updatedDateExpired();
    }
    return ResponseUtil.ok(result);
}