新增:余额支付接口

This commit is contained in:
2024-07-31 16:21:03 +08:00
parent e38489e567
commit c8ff6ecdfe
7 changed files with 262 additions and 215 deletions
@@ -36,23 +36,49 @@ public class RequestUtil {
ACCESS_TOKEN = token; ACCESS_TOKEN = token;
} }
// 微信支付通知 // 预付请求付款(余额支付)
public void pushWxPayNotify(Transaction transaction){ public Object balancePay(Order order){
System.out.println("transaction = " + transaction);
// 设置租户ID // 设置租户ID
// setTenantId(payment.getTenantId().toString()); setTenantId(order.getTenantId().toString());
// 推送支付通知地址 // 设置token
// String path = payment.getNotifyUrl(); setAccessToken(order.getAccessToken());
// 余额支付接口
String path = "/system/payment/balancePay";
try { try {
// 链式构建请求 // 链式构建请求
String result = HttpRequest.post("http://pxf4av.natappfree.cc/api/shop/wx-pay/nbg/paySuccess") final String body = HttpRequest.post(host.concat(path))
.header("Tenantid", "10158") .header("Tenantid", TENANT_ID)
.body(JSONUtil.toJSONString(transaction))//表单内容 .header("Authorization", ACCESS_TOKEN)
.body(JSONUtil.toJSONString(order))//表单内容
.timeout(20000)//超时,毫秒 .timeout(20000)//超时,毫秒
.execute().body(); .execute().body();
return JSONUtil.parseObject(body, ApiResult.class).getData();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return null;
}
// 微信支付通知
public String pushWxPayNotify(Transaction transaction, Payment payment){
// 设置租户ID
setTenantId(payment.getTenantId().toString());
// 推送支付通知地址
String path = payment.getNotifyUrl();
try {
// 链式构建请求
return HttpRequest.post(path)
.header("Tenantid", TENANT_ID)
.body(JSONUtil.toJSONString(transaction))//表单内容
.timeout(20000)//超时,毫秒
.execute().body();
} catch (Exception e) {
e.printStackTrace();
}
return "支付失败";
} }
@@ -43,13 +43,13 @@ public class CartController extends BaseController {
@ApiOperation("我的购物车") @ApiOperation("我的购物车")
@GetMapping("/myCart") @GetMapping("/myCart")
public ApiResult<?> myCart(CartParam param){ public ApiResult<?> myCart(CartParam param) {
HashMap<String, Object> map = computeCart(param); HashMap<String, Object> map = computeCart(param);
// 记录当前登录用户id // 记录当前登录用户id
User loginUser = getLoginUser(); User loginUser = getLoginUser();
if (loginUser == null) { if (loginUser == null) {
return fail("请先登录",null); return fail("请先登录", null);
} }
// 查询购物车 // 查询购物车
@@ -66,13 +66,13 @@ public class CartController extends BaseController {
// 收集店铺ID // 收集店铺ID
final Set<Integer> merchantIds = carts.stream().map(Cart::getMerchantId).collect(Collectors.toSet()); final Set<Integer> merchantIds = carts.stream().map(Cart::getMerchantId).collect(Collectors.toSet());
if (CollectionUtils.isEmpty(merchantIds)) { if (CollectionUtils.isEmpty(merchantIds)) {
map.put("selectAll",false); map.put("selectAll", false);
return success(map); return success(map);
} }
// 查询店铺列 // 查询店铺列
final List<Merchant> merchants = merchantService.list(new LambdaQueryWrapper<Merchant>() final List<Merchant> merchants = merchantService.list(new LambdaQueryWrapper<Merchant>()
.select(Merchant::getMerchantId,Merchant::getMerchantName,Merchant::getImage,Merchant::getShopType, Merchant::getAddress) .select(Merchant::getMerchantId, Merchant::getMerchantName, Merchant::getImage, Merchant::getShopType, Merchant::getAddress)
.in(Merchant::getMerchantId, merchantIds)); .in(Merchant::getMerchantId, merchantIds));
// 返回空列表 // 返回空列表
@@ -88,7 +88,7 @@ public class CartController extends BaseController {
}); });
// 计算购物车金额 // 计算购物车金额
map.put("merchants",merchants); map.put("merchants", merchants);
return success(map); return success(map);
} }
@@ -102,37 +102,53 @@ public class CartController extends BaseController {
if (loginUser == null) { if (loginUser == null) {
return fail("请先登录"); return fail("请先登录");
} }
// 附加用户ID
// 查询参数
final CartParam param = new CartParam();
param.setType(cart.getType());
// 购物车是否已存在
final Cart item = cartService.getOne(new LambdaQueryWrapper<Cart>()
.eq(Cart::getUserId, loginUser.getUserId())
.eq(Cart::getGoodsId, cart.getGoodsId())
.eq(Cart::getType,cart.getType())
.last("limit 1"));
cart.setUserId(loginUser.getUserId()); cart.setUserId(loginUser.getUserId());
// 字段验证 // 字段验证
if(cart.getCartNum() == null){ if (cart.getCartNum() == null) {
return fail("购买数量不能为空"); return fail("购买数量不能为空");
} }
System.out.println("cart = " + cart); // 立即购买按钮
if(cart.getIsNew() != null && cart.getIsNew()){
cartService.save(cart);
return success(cart);
}
// 修改购物车 LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
if (item != null) {
if (cart.getIsNew() != null) {
queryWrapper.eq(Cart::getIsNew,cart.getIsNew());
}
// 购物车是否已存在
queryWrapper.eq(Cart::getUserId, loginUser.getUserId())
.eq(Cart::getGoodsId, cart.getGoodsId())
.eq(Cart::getType, cart.getType())
.last("limit 1");
Cart item = cartService.getOne(queryWrapper);
// 查询购物车的参数
final CartParam param = new CartParam();
param.setType(cart.getType());
if (cart.getIsNew() != null) {
param.setIsNew(cart.getIsNew());
}
// 重复商品叠加数量
if(item != null){
System.out.println("item = " + item);
item.setCartNum(item.getCartNum() + cart.getCartNum()); item.setCartNum(item.getCartNum() + cart.getCartNum());
cartService.updateById(item); if (cartService.updateById(item)) {
return success("更新成功", computeCart(param));
return success("添加成功",computeCart(param)); }
} }
// 新添加商品 // 新添加商品
if (cartService.save(cart)) { if (cartService.save(cart)) {
return success("添加成功",computeCart(param)); return success("添加成功", computeCart(param));
} }
return fail("添加失败"); return fail("添加失败");
@@ -144,7 +160,7 @@ public class CartController extends BaseController {
if (cartService.updateById(cart)) { if (cartService.updateById(cart)) {
final CartParam param = new CartParam(); final CartParam param = new CartParam();
param.setType(cart.getType()); param.setType(cart.getType());
return success("修改成功",computeCart(param)); return success("修改成功", computeCart(param));
} }
return fail("修改失败"); return fail("修改失败");
} }
@@ -154,10 +170,10 @@ public class CartController extends BaseController {
public HashMap<String, Object> computeCart(CartParam param) { public HashMap<String, Object> computeCart(CartParam param) {
final LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>(); final LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(Cart::getId,Cart::getPrice,Cart::getCartNum) queryWrapper.select(Cart::getId, Cart::getPrice, Cart::getCartNum)
.eq(Cart::getUserId, getLoginUserId()) .eq(Cart::getUserId, getLoginUserId())
.eq(Cart::getType,param.getType()) .eq(Cart::getType, param.getType())
.eq(Cart::getSelected,1) .eq(Cart::getSelected, 1)
.eq(Cart::getIsPay, 0); .eq(Cart::getIsPay, 0);
// 查询购物车数据 // 查询购物车数据
@@ -185,9 +201,9 @@ public class CartController extends BaseController {
// 返回给前端 // 返回给前端
final HashMap<String, Object> map = new HashMap<>(); final HashMap<String, Object> map = new HashMap<>();
map.put("totalCartNum",reduce); map.put("totalCartNum", reduce);
map.put("totalPrice",reduced); map.put("totalPrice", reduced);
map.put("selectAll",selectAll); map.put("selectAll", selectAll);
return map; return map;
} }
@@ -204,24 +220,22 @@ public class CartController extends BaseController {
cartService.update(new LambdaUpdateWrapper<Cart>() cartService.update(new LambdaUpdateWrapper<Cart>()
.eq(Cart::getUserId, getLoginUserId()) .eq(Cart::getUserId, getLoginUserId())
.eq(Cart::getIsPay, 0) .eq(Cart::getIsPay, 0)
.eq(Cart::getSelected,0) .eq(Cart::getSelected, 0)
.set(Cart::getSelected,1) .set(Cart::getSelected, 1)
); );
return success("修改成功",computeCart(param)); return success("修改成功", computeCart(param));
} }
// 全不勾选 // 全不勾选
cartService.update(new LambdaUpdateWrapper<Cart>() cartService.update(new LambdaUpdateWrapper<Cart>()
.eq(Cart::getUserId, getLoginUserId()) .eq(Cart::getUserId, getLoginUserId())
.eq(Cart::getIsPay, 0) .eq(Cart::getIsPay, 0)
.eq(Cart::getSelected,1) .eq(Cart::getSelected, 1)
.set(Cart::getSelected,0) .set(Cart::getSelected, 0)
); );
return success("修改成功",computeCart(param)); return success("修改成功", computeCart(param));
} }
// @ApiOperation("按店铺ID更新选择状态") // @ApiOperation("按店铺ID更新选择状态")
// @PutMapping("/updateCartByMerchantId/{id}") // @PutMapping("/updateCartByMerchantId/{id}")
// public ApiResult<?> updateCartByMerchantId(@PathVariable("id") Long id) { // public ApiResult<?> updateCartByMerchantId(@PathVariable("id") Long id) {
@@ -314,7 +314,7 @@ public class OrderController extends BaseController {
// TODO 余额支付 // TODO 余额支付
if (order.getPayType().equals(0)) { if (order.getPayType().equals(0)) {
order.setAccessToken(order.getAccessToken()); order.setAccessToken(order.getAccessToken());
return success("下单成功", orderService.payByBalance(order)); return success("下单成功", requestUtil.balancePay(order));
} }
// TODO 微信统一下单接口 // TODO 微信统一下单接口
@@ -35,7 +35,7 @@ import java.util.stream.Collectors;
*/ */
@Api(tags = "微信支付结果通知") @Api(tags = "微信支付结果通知")
@RestController @RestController
@RequestMapping("/api/shop/wx-pay/nbg") @RequestMapping("/api/shop/wx-pay/notify/nbg")
public class WxPayNotifyNbgController extends BaseController { public class WxPayNotifyNbgController extends BaseController {
@Resource @Resource
private OrderService orderService; private OrderService orderService;
@@ -52,7 +52,7 @@ public class WxPayNotifyNbgController extends BaseController {
private RedisUtil redisUtil; private RedisUtil redisUtil;
@ApiModelProperty("微信支付通知") @ApiModelProperty("微信支付通知")
@PostMapping("/notify") @PostMapping()
public String demo(@RequestBody Transaction transaction){ public String demo(@RequestBody Transaction transaction){
System.out.println("支付通知 >>>>> "); System.out.println("支付通知 >>>>> ");
@@ -66,6 +66,9 @@ public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements Ca
if(param.getType() != null){ if(param.getType() != null){
mpjLambdaWrapper.eq(Cart::getType,param.getType()); mpjLambdaWrapper.eq(Cart::getType,param.getType());
} }
if(param.getIsNew() != null){
mpjLambdaWrapper.eq(Cart::getIsNew,param.getIsNew());
}
return baseMapper.selectJoinList(Cart.class, mpjLambdaWrapper); return baseMapper.selectJoinList(Cart.class, mpjLambdaWrapper);
} }
@@ -123,6 +123,7 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
.select(GoodsSpec::getSpecValue) .select(GoodsSpec::getSpecValue)
.select(GoodsSpec::getSpecName) .select(GoodsSpec::getSpecName)
.leftJoin(GoodsSpec.class,GoodsSpec::getGoodsId,GoodsSpec::getGoodsId) .leftJoin(GoodsSpec.class,GoodsSpec::getGoodsId,GoodsSpec::getGoodsId)
.gt(Goods::getStock,0)
.eq(Goods::getType,0); .eq(Goods::getType,0);
return baseMapper.selectJoinList(Goods.class, mpjLambdaWrapper); return baseMapper.selectJoinList(Goods.class, mpjLambdaWrapper);
} }
@@ -135,6 +136,7 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
.select(GoodsSpec::getSpecValue) .select(GoodsSpec::getSpecValue)
.select(GoodsSpec::getSpecName) .select(GoodsSpec::getSpecName)
.leftJoin(GoodsSpec.class,GoodsSpec::getGoodsId,GoodsSpec::getGoodsId) .leftJoin(GoodsSpec.class,GoodsSpec::getGoodsId,GoodsSpec::getGoodsId)
.gt(Goods::getStock,0)
.eq(Goods::getType,1); .eq(Goods::getType,1);
return baseMapper.selectJoinList(Goods.class, mpjLambdaWrapper); return baseMapper.selectJoinList(Goods.class, mpjLambdaWrapper);
} }
@@ -29,6 +29,8 @@ import com.gxwebsoft.shop.param.OrderParam;
import com.gxwebsoft.shop.service.*; import com.gxwebsoft.shop.service.*;
import com.wechat.pay.java.core.Config; import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAConfig; import com.wechat.pay.java.core.RSAConfig;
import com.gxwebsoft.common.system.entity.Payment;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount; import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer; import com.wechat.pay.java.service.payments.jsapi.model.Payer;