欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > 微信支付Java+uniapp微信小程序

微信支付Java+uniapp微信小程序

2024/10/27 15:25:21 来源:https://blog.csdn.net/u012643122/article/details/143255195  浏览:    关键词:微信支付Java+uniapp微信小程序

JS:

					request.post('/vip/pay', {//这是自己写的java支付接口id: this.vipInfo.id,payWay: 'wechat-mini'}).then((res) => {let success = (res2) => {//前端的支付成功回调函数this.$refs.popup.close();// 支付成功刷新当前页面setTimeout(() => {this.doGetVipInfo(this.vipInfo.id);}, 2500)}let fail = (res) => {//支付失败,进行提示util.showToast(this.$t('pay.fail'))}let payObj = {"provider": "wxpay","timeStamp": res.data.timeStamp,"nonceStr": res.data.nonceStr,"package": res.data.packageValue,"signType": res.data.signType,"paySign": res.data.paySign,"appId": res.data.appId,success,fail};console.log("支付>>>" + JSON.stringify(payObj));uni.requestPayment(payObj);//uniapp提供的统一支付接口,可以在微信小程序内调起微信支付界面}).finally(() => {})

Java:

    /*** 会员卡支付*/@PostMapping("/pay")@RepeatSubmitpublic R<Object> pay(@RequestBody AppVipPayVo pay) {long tradeId = payTradeService.save(pay.payWay(), pay.getPayAmount(), pay.getVipId(), TradeTypeEnum.VIP.getCode());//创建自己的交易订单if (StrUtil.equals(payWay, PayWayEnum.MIN_WECART.getCode()) || StrUtil.equals(payWay, PayWayEnum.WECART.getCode())) {return wxPayRequest(tradeId, pay.getPayAmount(), pay.payWay(), "https://xxx.xxx.xxx/vip/wxpayCallback","会员卡");}return R.fail();}//发起微信支付private Object wxPayRequest(Long tradeId, BigDecimal payMoney, String payWay, String notifyUrl, String subject) {WxPayConfig wechat= new WxPayConfig();//获取商户的支付配置WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();orderRequest.setBody(subject);orderRequest.setOutTradeNo(String.valueOf(tradeId));//存入我们自己的流水号orderRequest.setTotalFee(BaseWxPayRequest.yuanToFen(String.valueOf(payMoney)));orderRequest.setSpbillCreateIp(ServletUtils.getClientIP());orderRequest.setTradeType(WxPayConstants.TradeType.APP);if (StrUtil.equals(payWay, PayWayEnum.MIN_WECHAT.getCode())) {orderRequest.setTradeType(WxPayConstants.TradeType.JSAPI);orderRequest.setOpenid(LoginHelper.getLoginUser().getToken());//获取用户的openid,微信登录时就需要保存openid作为token}WxPayConfig wxPayConfig = new WxPayConfig();wxPayConfig.setAppId(wechat.getAppId());wxPayConfig.setMchKey(wechat.getSecret());wxPayConfig.setMchId(wechat.getMchId());wxPayConfig.setNotifyUrl(notifyUrl);wxPayConfig.setSubMchId(StrUtil.isBlank(wechat.getSubMchId()) ? null : wechat.getSubMchId());//如果有子商户,则设置子商户WxPayService wxPayService = new WxPayServiceImpl();wxPayService.setConfig(wxPayConfig);try {Object payResult = wxPayService.createOrder(orderRequest);JSONObject json = JSONUtil.parseObj(payResult);json.set("tradeId", tradeId);return json;} catch (WxPayException e) {log.error("微信缴费失败" + wechat.getAppId() + ">>>" + wechat.getMchId());throw new ServiceException(e.getMessage());}}/*** 回调*/@PostMapping("/wxpayCallback")@SaIgnorepublic String wxpayCallback(HttpServletRequest request) {String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());StaticLog.info("微信支付回调={}", xmlResult);WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlResult);String outTradeNo = result.getOutTradeNo();//拿到我们自己的流水号LambdaQueryWrapper<PayTrade> eq = Wrappers.<PayTrade>lambdaQuery().eq(PayTrade::getId, outTradeNo).isNull(PayTrade::getOutTradeNo).eq(PayTrade::getTradeStatus, TradeStatusEnum.WAIT_PAY.getCode());PayTrade trade = PayTradeMapper.selectOne(eq);if (ObjectUtil.isNull(trade)) {StaticLog.error("支付订单不存在");return WxPayNotifyResponse.success("OK");}WxPayConfig wechat= new WxPayConfig();//获取商户的支付配置WxPayConfig wxPayConfig = new WxPayConfig();wxPayConfig.setAppId(wechat.getAppId());wxPayConfig.setMchKey(wechat.getSecret());wxPayConfig.setMchId(wechat.getMchId());WxPayService wxPayService = new WxPayServiceImpl();wxPayService.setConfig(wxPayConfig);wxPayService.parseOrderNotifyResult(xmlResult);//解密,如果解密失败,会抛出异常if (result.getResultCode().contains("FAIL")) {return WxPayNotifyResponse.fail("FAIL");}long orderId = trade.getOutPayId();//会员卡IDlong payTime = DateUtil.parse(result.getTimeEnd(), PURE_DATETIME_PATTERN).getTime() / 1000;paySuccess(result.getTransactionId(), payTime, outTradeNo, orderId, trade.getUserId());return WxPayNotifyResponse.success("OK");}//支付成功业务逻辑private void paySuccess(String tradeNo, long payTime, String outTradeNo, long orderId, Long userId) {//修改订单状态payTradeMapper.update(null, new LambdaUpdateWrapper<PayTrade>().set(PayTrade::getTradeStatus, TradeStatusEnum.PAY_SUCCESS.getCode()).set(PayTrade::getOutTradeNo, tradeNo).set(PayTrade::getPayTime, payTime).set(PayTrade::getHasNotify, true).eq(PayTrade::getId, outTradeNo));}

pom.xml:

		<dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-pay</artifactId><version>4.5.0</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com