微信支付总金额

master
litao 2 years ago
parent 1d14b1a929
commit 974e531429
  1. 156
      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
@ -36,74 +31,85 @@ import java.util.concurrent.locks.ReentrantLock;
@Slf4j
public class AppEbLoginController {
private final IWeChatUserService weChatUserService;
/**
* 获取openid
*/
@PostMapping("/login")
public R<Map> login(@RequestBody WeChatPhone weChatPhone) {
//小程序 appId = "wx432c2efe6df3b97a" appSecret = "859df8b167e74223e9237dee1b344524"
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId");
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret");
//小程序需要传来一个code
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getCode2Session(weChatPhone.getCode(), appId, appSecret);
String openid = accessTokenJson.get("openid", String.class);
System.out.println("accessTokenJson:" + accessTokenJson.toString());
Map<String, Object> map = new HashMap<>();
map.put("openid", openid);
map.put("userInfo", null);
//根据openid获取本地用户信息
WeChatUser user = weChatUserService.getOne(Wrappers.<WeChatUser>lambdaQuery().eq(WeChatUser::getOpenId, openid));
if (user != null) {
map.put("userInfo", user);
BladeTokenEndPoint point = SpringUtil.getBean(BladeTokenEndPoint.class);
}
private final IWeChatUserService weChatUserService;
private final IOrderService orderService;
/**
* 获取openid
*/
@PostMapping("/login")
public R<Map> login(@RequestBody WeChatPhone weChatPhone) {
//小程序 appId = "wx432c2efe6df3b97a" appSecret = "859df8b167e74223e9237dee1b344524"
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId");
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret");
//小程序需要传来一个code
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getCode2Session(weChatPhone.getCode(), appId, appSecret);
String openid = accessTokenJson.get("openid", String.class);
System.out.println("accessTokenJson:" + accessTokenJson.toString());
Map<String, Object> map = new HashMap<>();
map.put("openid", openid);
map.put("userInfo", null);
//根据openid获取本地用户信息
WeChatUser user = weChatUserService.getOne(Wrappers.<WeChatUser>lambdaQuery().eq(WeChatUser::getOpenId, openid));
if (user != null) {
map.put("userInfo", user);
BladeTokenEndPoint point = SpringUtil.getBean(BladeTokenEndPoint.class);
}
//这里可以对用户信息进行一些操作
return R.data(map);
}
/**
* 用前端请求接口获取的code换取用户手机号
* 前端需要请求的接口https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
*
* @param weChatPhone
* @return
*/
@PostMapping("/phone")
public R getPhone(@RequestBody WeChatPhone weChatPhone) {
//小程序appId appSecret
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId");
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret");
// 1.请求微信接口服务,获取accessToken
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getAccessToken(appId, appSecret);
System.out.println("accessTokenJson:" + accessTokenJson);
String accessToken = accessTokenJson.get("access_token", String.class);
System.out.println("accessToken:" + accessToken);
// 2.请求微信接口服务,获取用户手机号信息
if (StringUtils.isBlank(accessToken)) {
throw new ServiceException("获取access_token失败");
}
cn.hutool.json.JSONObject phoneNumberJson = WeChatUtil.getPhoneNumber(weChatPhone.getCode(), accessToken);
System.out.println("phoneNumberJson:" + phoneNumberJson);
WeChatPhoneInfo phoneInfo = phoneNumberJson.get("phone_info", WeChatPhoneInfo.class);
System.out.println("phoneInfo:" + phoneInfo.toString());
return R.data(phoneInfo.getPurePhoneNumber());
}
//这里可以对用户信息进行一些操作
return R.data(map);
}
/**
* 用前端请求接口获取的code换取用户手机号
* 前端需要请求的接口https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
*
* @param weChatPhone
* @return
*/
@PostMapping("/phone")
public R getPhone(@RequestBody WeChatPhone weChatPhone) {
//小程序appId appSecret
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId");
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret");
// 1.请求微信接口服务,获取accessToken
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getAccessToken(appId, appSecret);
System.out.println("accessTokenJson:" + accessTokenJson);
String accessToken = accessTokenJson.get("access_token", String.class);
System.out.println("accessToken:" + accessToken);
// 2.请求微信接口服务,获取用户手机号信息
if (StringUtils.isBlank(accessToken)) {
throw new ServiceException("获取access_token失败");
/**
* 微信下单
*
* @param openId
*/
@PostMapping("/createOrder")
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("该订单不存在");
}
cn.hutool.json.JSONObject phoneNumberJson = WeChatUtil.getPhoneNumber(weChatPhone.getCode(), accessToken);
System.out.println("phoneNumberJson:" + phoneNumberJson);
WeChatPhoneInfo phoneInfo = phoneNumberJson.get("phone_info", WeChatPhoneInfo.class);
System.out.println("phoneInfo:" + phoneInfo.toString());
return R.data(phoneInfo.getPurePhoneNumber());
}
/**
* 微信下单
* @param openId
*/
@PostMapping("/createOrder")
public R createOrder(@RequestParam String openId,@RequestParam String outTradeNo) {
return R.data(WxPayUtils.getPayParam(openId,outTradeNo));
}
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