微信支付总金额

master
litao 2 years ago
parent 1d14b1a929
commit 974e531429
  1. 28
      src/main/java/org/springblade/modules/weixin/controller/AppEbLoginController.java
  2. 22
      src/main/java/org/springblade/modules/weixin/utils/WxPayUtils.java

@ -1,20 +1,19 @@
package org.springblade.modules.weixin.controller;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.EntityUtils;
import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.auth.endpoint.BladeTokenEndPoint;
import org.springblade.modules.weixin.config.WechatPayValidator;
import org.springblade.modules.desk.entity.Order;
import org.springblade.modules.desk.service.IOrderService;
import org.springblade.modules.weixin.entity.WeChatPhone;
import org.springblade.modules.weixin.entity.WeChatPhoneInfo;
import org.springblade.modules.weixin.entity.WeChatUser;
@ -23,12 +22,8 @@ import org.springblade.modules.weixin.utils.WeChatUtil;
import org.springblade.modules.weixin.utils.WxPayUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
@RestController
@AllArgsConstructor
@ -38,6 +33,8 @@ public class AppEbLoginController {
private final IWeChatUserService weChatUserService;
private final IOrderService orderService;
/**
* 获取openid
*/
@ -99,11 +96,20 @@ public class AppEbLoginController {
/**
* 微信下单
*
* @param openId
*/
@PostMapping("/createOrder")
public R createOrder(@RequestParam String openId,@RequestParam String outTradeNo) {
return R.data(WxPayUtils.getPayParam(openId,outTradeNo));
public R createOrder(@RequestParam String openId, @RequestParam String outTradeNo) {
// 根据订单号查询订单
Order order = orderService.getOne(Wrappers.<Order>lambdaQuery().eq(Order::getOrderNo, outTradeNo));
if (order == null) {
throw new ServiceException("该订单不存在");
}
Double totalPrice = order.getTotalPrice() * 100;
int total = totalPrice.intValue();
return R.data(WxPayUtils.getPayParam(openId, outTradeNo, total));
}
}

@ -34,7 +34,7 @@ public class WxPayUtils {
//微信下单回调地址,支付成功后会回调这个地址。
//对应文档 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_5.shtml
public static String callbackUri = "";
public static String callbackUri = "https://www.baidu.com";
//APIv3密钥 在微信支付商户平台-账户中心-API安全里设置,如果设置了不知道,就只有修改
public static String apiV3Key = "YLJJyilvjiaju3613369520230822YLJ";
@ -122,12 +122,12 @@ public class WxPayUtils {
* 官方文档路径
* https://pay.weixin.qq.com/wiki/doc/apiv3_partner/open/pay/chapter2_1.shtml
*/
public static String createOrderJSApiV3(String openId, String outTradeNo) {
public static String createOrderJSApiV3(String openId, String outTradeNo,int total) {
payLoading();
//请求URL
HttpPost httpPost = new HttpPost(JSApiPayUrl);
// 请求body参数
StringEntity entity = new StringEntity(JSONObject.toJSONString(buildWxJsApiV3PayJson(openId, outTradeNo, 0)), "utf-8");
StringEntity entity = new StringEntity(JSONObject.toJSONString(buildWxJsApiV3PayJson(openId, outTradeNo, total)), "utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
@ -196,8 +196,8 @@ public class WxPayUtils {
* @param openId openId
* @return 支付参数
*/
public static Object getPayParam(String openId, String outTradeNo) {
JSONObject jsonObject = JSON.parseObject(createOrderJSApiV3(openId, outTradeNo));
public static Object getPayParam(String openId, String outTradeNo,int total) {
JSONObject jsonObject = JSON.parseObject(createOrderJSApiV3(openId, outTradeNo,total));
return getTokenWeiXin(jsonObject.getString("prepay_id"));
}
@ -207,14 +207,14 @@ public class WxPayUtils {
* 构造下单的json(第三步需要)
*
* @param description 商品描述
* @param amount 订单金额
* @param total 订单金额
* @param openId 用户ID onAky51Fojn3NoLrnKwcY
* @return JSONObject
*/
public static JSONObject buildWxJsApiV3PayJson(String description, String amount, String openId, String outTradeNo) {
public static JSONObject buildWxJsApiV3PayJson(String description, int total, String openId, String outTradeNo) {
//订单金额json
JSONObject amountJson = new JSONObject();
amountJson.put("total", Integer.valueOf(amount));
amountJson.put("total", total);
amountJson.put("currency", "CNY");
//支付者json
@ -242,8 +242,8 @@ public class WxPayUtils {
*
* @return JSONObject
*/
public static JSONObject buildWxJsApiV3PayJson(String openId, String outTradeNo, Integer goodsId) {
return buildWxJsApiV3PayJson("测试商品", "1", openId, outTradeNo);
public static JSONObject buildWxJsApiV3PayJson(String openId, String outTradeNo, int total) {
return buildWxJsApiV3PayJson("测试商品", total, openId, outTradeNo);
}
/**
@ -252,7 +252,7 @@ public class WxPayUtils {
* @return JSONObject
*/
public static JSONObject buildWxJsApiV3PayJson() {
return buildWxJsApiV3PayJson("测试商品", "1", "123123123124", "");
return buildWxJsApiV3PayJson("测试商品", 1, "123123123124", "");
}
/**

Loading…
Cancel
Save