com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult Java Examples

The following examples show how to use com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult. 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: MyParkAPI.java    From springboot-seed with MIT License 4 votes vote down vote up
@ApiOperation(value = "支付停车费")
@PostMapping(value = "/pay")
public ResponseEntity<?> pay(HttpServletRequest httpRequest,
                             @ApiParam("停车记录") @RequestParam("carFee") Long carFeeId) throws Exception {
    OAuth2Authentication auth = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
    WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
    SecurityUser securityUser = (SecurityUser) (auth.getPrincipal());
    CarFee carFee = carFeeService.selectByID(carFeeId).get();
    if (carFee.getPaymentTime() != null) {
        return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body("Already Paid");
    }
    Park park = parkService.selectByID(carFee.getParkId()).get();
    Fee fee = feeService.selectByID(park.getFeeId()).get();
    BigDecimal money = ParkThirdAPI.calculateMoney(fee, carFee.getInTime(), new Date());
    money = money.multiply(BigDecimal.TEN).multiply(BigDecimal.TEN);
    Dictionary<String, String> map = new Hashtable<>();
    if (money == BigDecimal.ZERO) {
        carFee.setPaymentAmount(money);
        carFee.setPaymentTime(new Date());
        carFee.setPaymentMode("免费");
        carFee.setUserId(securityUser.getId());
        carFeeService.modifyById(carFee);
        map.put("money", "0");
    } else {
        if (securityUser.getId() < 15) {
            money = BigDecimal.ONE;
        }
        request.setOpenid(securityUser.getOpenId());
        request.setDeviceInfo("ma");
        request.setBody(park.getName());
        request.setDetail(carFee.getCarNumber() + "-停车费");
        request.setFeeType("CNY");
        request.setTotalFee(money.intValue());
        request.setSpbillCreateIp(httpRequest.getLocalAddr());
        request.setTradeType("JSAPI");
        // save db
        Payment payment = new Payment();
        payment.setBody(request.getBody());
        payment.setDetail(request.getDetail());
        payment.setTotalFee(money.intValue());
        payment.setFeeType(request.getFeeType());
        payment.setTradeType(request.getTradeType());
        payment.setIp(request.getSpbillCreateIp());
        paymentService.add(payment);
        // post wx request
        request.setNotifyUrl(String.format("https://app.lhzh.tech/third/park/pay_callback/1/%d/%d/%d", payment.getId(), carFeeId, securityUser.getId()));
        request.setOutTradeNo(String.format("1-%d-%d-%d-%d", securityUser.getId(), payment.getId(), carFeeId, new Random().nextInt(9999)));
        WxPayUnifiedOrderResult result = wxPayAPI.unifiedOrder(request);
        map.put("nonceStr", result.getNonceStr());
        String timeStamp = Long.toString(new Date().getTime() / 1000);
        map.put("timeStamp", timeStamp);
        String pkg = "prepay_id=" + result.getPrepayId();
        map.put("package", pkg);
        map.put("signType", "MD5");
        String paySignString = String.format("appId=%s&nonceStr=%s&package=%s&signType=MD5&timeStamp=%s&key=%s",
                wxPayProperties.getAppId(), result.getNonceStr(), pkg, timeStamp, wxPayProperties.getMchKey());
        String paySign = new BigInteger(1, MessageDigest.getInstance("MD5").digest(paySignString.getBytes())).toString(16);
        map.put("paySign", paySign);
        map.put("money", money.toString());
    }
    return ResponseEntity.status(HttpStatus.OK).body(map);
}
 
Example #2
Source File: WxPayAPI.java    From springboot-seed with MIT License 2 votes vote down vote up
/**
 * 统一下单(详见https://pay.weixin.qq.com/wiki/doc/third/jsapi.php?chapter=9_1)
 * 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
 * 接口地址:https://api.mch.weixin.qq.com/pay/unifiedorder
 *
 * @param request 请求对象,注意一些参数如appid、mchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置)
 */
@PostMapping("/unifiedOrder")
public WxPayUnifiedOrderResult unifiedOrder(@RequestBody WxPayUnifiedOrderRequest request) throws WxPayException {
    return this.wxService.unifiedOrder(request);
}