微信支付分账与退款,调整logback日志输出,调整cdn文件上传

This commit is contained in:
DB 2023-11-01 20:18:26 +08:00
parent 2ef8150f19
commit 6652f37473
14 changed files with 199 additions and 74 deletions

View File

@ -2,7 +2,7 @@ package com.cpop.core.handler;
import com.cpop.core.base.exception.UtilException;
import com.cpop.core.config.TencentCosProperties;
import com.qcloud.cos.COS;
import com.cpop.core.utils.uuid.IdUtils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
@ -131,10 +131,10 @@ public class TencentCosHandler {
objectMetadata.setContentLength(file.getSize());
objectMetadata.setContentType(file.getContentType());
InputStream inputStream = file.getInputStream();
PutObjectRequest putObjectRequest = new PutObjectRequest(properties.getBucketName(), properties.getSecretKey(),inputStream , objectMetadata);
PutObjectRequest putObjectRequest = new PutObjectRequest(properties.getBucketName(), IdUtils.fastSimpleUUID(), inputStream, objectMetadata);
// 设置存储类型如有需要不需要请忽略此行代码, 默认是标准(Standard), 低频(standard_ia)
// 更多存储类型请参见 https://cloud.tencent.com/document/product/436/33417
putObjectRequest.setStorageClass(StorageClass.Standard_IA);
putObjectRequest.setStorageClass(StorageClass.Standard);
Upload upload = transferManager.upload(putObjectRequest);
UploadResult uploadResult = upload.waitForUploadResult();
inputStream.close();

View File

@ -39,6 +39,9 @@ public class SecurityUtils {
**/
public LoginUser getLoginUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null){
return null;
}
//匿名用户
if (authentication.getPrincipal() == "anonymousUser") {
return null;

View File

@ -4,7 +4,7 @@ cpop:
profile: E:/Cpop/uploadPath
jwt:
#白名单
whiteList: /login,/miniLogin,/wxPay/callback/notify/**,/profile/**,/doc.html,/webjars/**,/favicon.ico,/v2/api-docs/**,/swagger-resources
whiteList: /login,/miniLogin,/wxPay/callback/notify/**,/mini/brand/jamboxBrandIsInMall/*,/profile/**,/doc.html,/webjars/**,/favicon.ico,/v2/api-docs/**,/swagger-resources
gateway:
rsa-keypair:
# 公钥文件

View File

@ -137,3 +137,5 @@ wx:
#微信支付商户密钥
mchKey: JamBox20230919174000000000000002
apiV3Key: JamBox20230919174000000000000002
#服务商账号
sharingAccount: 1618436087

View File

@ -58,9 +58,7 @@
<!--info和error分开打印-->
<root level="INFO">
<springProfile name="dev">
<appender-ref ref="CONSOLE"/>
</springProfile>
<springProfile name="test,prod">
<appender-ref ref="SYS_INFO"/>
<appender-ref ref="SYS_ERROR"/>

View File

@ -0,0 +1,60 @@
package com.cpop.mall.web;
import com.cpop.core.utils.SpringUtils;
import com.cpop.core.utils.uuid.IdUtils;
import com.cpop.mall.business.entity.Order;
import com.cpop.mall.business.service.OrderService;
import com.cpop.mall.framework.config.wxPay.WxPayProperties;
import com.cpop.system.business.entity.Brand;
import com.cpop.system.business.service.BrandService;
import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author DB
* @createTime 2023/11/01 18:15
* @description 微信支付
*/
@SpringBootTest
public class CpopWxPayTests {
@Autowired
private WxPayService wxPayService;
@Autowired
private WxPayProperties wxPayProperties;
/**
* @descriptions 微信支付退款
* @author DB
* @date 2023/11/01 18:20
* @return: void
*/
@Test
public void refund() throws WxPayException {
Brand brand = SpringUtils.getBean(BrandService.class).getById("75140168047210496");
Order order = SpringUtils.getBean(OrderService.class).getById("77257142464598016");
WxPayRefundV3Request request = new WxPayRefundV3Request();
WxPayRefundV3Request.Amount amount = new WxPayRefundV3Request.Amount();
//退款金额(单位分)
int refund = order.getTotalAmount().scaleByPowerOfTen(2).intValue();
amount.setRefund(refund)
.setTotal(refund)
.setCurrency("CNY");
request.setTransactionId(order.getOutOrderNo())
.setSubMchid(wxPayService.getConfig().getSubMchId())
.setNotifyUrl(wxPayProperties.getNotifyRefund())
.setOutRefundNo(IdUtils.fastSimpleUUID())
.setOutTradeNo(order.getId())
.setSubMchid(brand.getWxMchId())
.setReason("接口测试退款")
.setAmount(amount);
WxPayRefundV3Result result = wxPayService.refundV3(request);
System.out.println(result);
}
}

View File

@ -1,41 +0,0 @@
package com.cpop.mall.web;
import com.cpop.mall.business.service.ProductRecordService;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author DB
* @createTime 2023/11/01 12:25
* @description 订单测试
*/
@SpringBootTest
public class OrderTests {
@Autowired
private ProductRecordService productRecordService;
/**
* @descriptions 并发更新库存
* @author DB
* @date 2023/11/01 12:26
* @return: void
*/
@Test
public void concurrencyUpdateStock(){
ExecutorService threadPool = Executors.newFixedThreadPool(10);
for (int i = 0; i < 100; i++) {
threadPool.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "正在执行任务");
}
});
}
}
}

View File

@ -93,7 +93,7 @@ public class ProductBo implements Serializable {
* 限制数量
*/
@ApiModelProperty("限制数量")
private Long limitNum;
private Integer limitNum;
/**
* 规格集合

View File

@ -0,0 +1,44 @@
package com.cpop.mall.business.controller.mini;
import com.cpop.core.base.R;
import com.cpop.jambox.business.service.BrandExtendService;
import com.cpop.mall.business.vo.OrderPageVo;
import com.cpop.system.business.service.BrandService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static com.cpop.jambox.business.entity.table.BrandExtendTableDef.BRAND_EXTEND;
/**
* @author DB
* @createTime 2023/11/01 16:36
* @description
*/
@RestController
@Api(tags = "小程序商城-品牌相关接口")
@RequestMapping("/mini/brand")
public class MiniBrandController {
@Autowired
private BrandExtendService brandExtendService;
/**
* @descriptions 分页查询商城-商品
* @author DB
* @date 2023/10/23 11:56
* @param cloudBrandId 云品牌id
* @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.entity.Product>>
*/
@GetMapping("/jamboxBrandIsInMall/{cloudBrandId}")
@ApiOperation("小程序-果酱品牌是否在商城内")
public R<Boolean> jamboxBrandIsInMall(@PathVariable String cloudBrandId) {
long count = brandExtendService.queryChain().where(BRAND_EXTEND.BRAND_CLOUD_ID.eq(cloudBrandId)).count();
return R.ok(count > 0);
}
}

View File

@ -13,6 +13,7 @@ import com.cpop.mall.business.mapper.OrderRefundMapper;
import com.cpop.mall.business.service.OrderRefundService;
import com.cpop.mall.business.service.OrderService;
import com.cpop.mall.business.vo.OrderRefundPageVo;
import com.cpop.mall.framework.config.wxPay.WxPayProperties;
import com.cpop.mall.framework.handler.WxPayHandler;
import com.cpop.system.business.entity.ProfitSharing;
import com.cpop.system.business.service.ProfitSharingService;
@ -46,8 +47,8 @@ public class OrderRefundServiceImpl extends ServiceImpl<OrderRefundMapper, Order
@Autowired
private WxPayHandler wxPayHandler;
@Value("${wx.pay.notifyRefund}")
private String notifyRefund;
@Autowired
private WxPayProperties wxPayProperties;
/**
* @descriptions 同意退款
@ -82,12 +83,13 @@ public class OrderRefundServiceImpl extends ServiceImpl<OrderRefundMapper, Order
.setTotal(refund)
.setCurrency("CNY");
request.setTransactionId(order.getOutOrderNo())
.setNotifyUrl(notifyRefund)
.setSubMchid(wxPayService.getConfig().getSubMchId())
.setNotifyUrl(wxPayProperties.getNotifyRefund())
.setOutRefundNo(id)
.setReason(orderRefund.getRefundReason())
.setAmount(amount);
WxPayRefundV3Result result = wxPayService.refundV3(request);
this.updateChain().set(ORDER_REFUND.OUT_REFUND_ID, result.getRefundId()).update();
this.updateChain().set(ORDER_REFUND.REFUND_STATUS, 1).where(ORDER_REFUND.OUT_REFUND_ID.eq(result.getRefundId())).update();
} catch (WxPayException e) {
throw new ServiceException(e.getMessage());
}
@ -140,7 +142,8 @@ public class OrderRefundServiceImpl extends ServiceImpl<OrderRefundMapper, Order
.leftJoin(BRAND).on(BRAND.ID.eq(ORDER.BRAND_ID))
.and(ORDER.PRODUCT_NAMES.like(bo.getProductName()))
.and(ORDER.PAY_USER_NAME.like(bo.getPayUserName()))
.and(ORDER.RECEIVE_PHONE.like(bo.getReceivePhone())),
.and(ORDER.RECEIVE_PHONE.like(bo.getReceivePhone()))
.orderBy(ORDER_REFUND.CREATE_TIME.desc()),
OrderRefundPageVo.class);
}

View File

@ -5,6 +5,7 @@ import com.cpop.common.utils.StringUtils;
import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.common.utils.ip.IpUtils;
import com.cpop.core.base.entity.PageDomain;
import com.cpop.core.base.enums.OrderSource;
import com.cpop.core.base.enums.UserType;
import com.cpop.core.base.exception.ServiceException;
import com.cpop.core.service.RedisService;
@ -20,9 +21,15 @@ import com.cpop.mall.business.entity.*;
import com.cpop.mall.business.mapper.OrderMapper;
import com.cpop.mall.business.service.*;
import com.cpop.mall.business.vo.OrderPageVo;
import com.cpop.mall.framework.config.wxPay.WxPayProperties;
import com.cpop.mall.framework.constant.MallRedisConstant;
import com.cpop.mall.framework.handler.WxPayHandler;
import com.cpop.system.business.entity.ProfitSharing;
import com.cpop.system.business.service.ProfitSharingService;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.profitsharingV3.ProfitSharingReceiver;
import com.github.binarywang.wxpay.bean.profitsharingV3.ProfitSharingRequest;
import com.github.binarywang.wxpay.bean.profitsharingV3.ProfitSharingResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
@ -35,10 +42,7 @@ import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;
@ -48,6 +52,7 @@ import static com.cpop.mall.business.entity.table.OrderRefundTableDef.ORDER_REFU
import static com.cpop.mall.business.entity.table.OrderTableDef.ORDER;
import static com.cpop.mall.business.entity.table.ProductRecordTableDef.PRODUCT_RECORD;
import static com.cpop.mall.business.entity.table.ProductTableDef.PRODUCT;
import static com.cpop.system.business.entity.table.ProfitSharingTableDef.PROFIT_SHARING;
import static com.cpop.system.business.entity.table.StoreTableDef.STORE;
@ -63,6 +68,9 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
@Autowired
private WxPayHandler wxPayHandler;
@Autowired
private WxPayProperties wxPayProperties;
/**
* @descriptions 分页查询商城-订单
* @author DB
@ -315,9 +323,10 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
productRecords.forEach(item -> {
item.setRecordNum(item.getRecordNum() - orderNumMap.get(item.getId()));
});
productRecordService.updateBatch(productRecords);
//异步更新
asyncUpdateRecords(productRecords, orderNumMap);
//TODO:分账先注释
/*ProfitSharing profitSharing = new ProfitSharing();
ProfitSharing profitSharing = new ProfitSharing();
profitSharing.setOrderId(orderId)
.setProfitSharingStatus(0)
.setOrderSource(OrderSource.MALL.toString());
@ -325,21 +334,25 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
profitSharingService.save(profitSharing);
ProfitSharingRequest sharingRequest = new ProfitSharingRequest();
ProfitSharingReceiver profitSharingReceiver = new ProfitSharingReceiver();
List<ProfitSharingReceiver> profitSharingReceivers = new ArrayList<>();
Double ceil = Math.ceil(notifyResult.getTotalFee() * OrderSource.MALL.getRate());
profitSharingReceiver.setAmount(ceil.longValue());
profitSharingReceiver.setDescription("向服务商分账");
profitSharingReceiver.setType("MERCHANT_ID");
profitSharingReceiver.setRelationType("SERVICE_PROVIDER");
profitSharingReceiver.setAccount("1618436087");
profitSharingReceiver.setAccount(wxPayProperties.getSharingAccount());
profitSharingReceivers.add(profitSharingReceiver);
sharingRequest.setTransactionId(notifyResult.getTransactionId());
sharingRequest.setOutOrderNo(profitSharing.getOrderId());
//分账结束
sharingRequest.setUnfreezeUnsplit(true);
sharingRequest.setAppid(wxPayProperties.getAppId());
sharingRequest.setReceivers(profitSharingReceivers);
ProfitSharingResult profitSharingResult = wxPayService.getProfitSharingV3Service().profitSharing(sharingRequest);
//更新分账订单
profitSharingService.updateChain().set(PROFIT_SHARING.OUT_PROFIT_SHARING_ID,profitSharingResult.getOrderId())
.set(PROFIT_SHARING.AMOUNT,ceil.longValue()).set(PROFIT_SHARING.PROFIT_SHARING_STATUS,1).set(PROFIT_SHARING.PAY_ACCOUNT,"1618436087")
.where(PROFIT_SHARING.ID.eq(profitSharing.getId())).update();*/
.set(PROFIT_SHARING.AMOUNT, ceil.longValue()).set(PROFIT_SHARING.PROFIT_SHARING_STATUS, 1).set(PROFIT_SHARING.PAY_ACCOUNT, wxPayProperties.getSharingAccount())
.where(PROFIT_SHARING.ID.eq(profitSharing.getId())).update();
} catch (WxPayException e) {
throw new ServiceException(e.getMessage());
}
@ -348,23 +361,39 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
/**
* @Description: 异步更新数据库
* @param productRecords 产品记录
* @param flag 成功标志
* @Author DB
* @Date: 2023/11/1 0:09
*/
@Async("customAsyncThreadPool")
public void asyncUpdateRecords(List<ProductRecord> productRecords, Integer flag) {
ProductRecordService productRecordService = SpringUtils.getBean(ProductRecordService.class);
if (flag > 5) {
//TODO:通知到系统库存减扣失败需要人为操作
throw new ServiceException("更新订单失败");
} else {
boolean tx = Db.tx(() -> productRecordService.updateBatch(productRecords));
if (!tx) {
//更新失败
flag++;
asyncUpdateRecords(productRecords, flag);
public void asyncUpdateRecords(List<ProductRecord> productRecords,Map<String, Integer> orderNumMap) {
loopUpdateStock(productRecords, orderNumMap);
}
/**
* @descriptions 循环插入库存修改记录
* @author DB
* @date 2023/11/01 17:01
* @param productRecords 库存修改记录
* @return: void
*/
private void loopUpdateStock(List<ProductRecord> productRecords,Map<String, Integer> orderNumMap) {
ProductRecordService productRecordService = SpringUtils.getBean(ProductRecordService.class);
//用迭代器
Iterator<ProductRecord> iterator = productRecords.iterator();
while (iterator.hasNext()) {
ProductRecord next = iterator.next();
next.setRecordNum(next.getRecordNum() - orderNumMap.get(next.getId()));
boolean update = productRecordService.updateById(next);
//如果更新成功移除
if (update) {
iterator.remove();
}
}
//存在更新失败,需要重新更新,直到全部成功
if (!productRecords.isEmpty()){
//获取最新数据进行下一次循环
loopUpdateStock(productRecordService.listByIds(productRecords.stream().map(ProductRecord::getId).collect(Collectors.toSet())),
orderNumMap);
}
}

View File

@ -72,6 +72,12 @@ public class ProductPageVo implements Serializable {
@ApiModelProperty("上下架")
private Boolean isUp;
/**
* 是否置顶
*/
@ApiModelProperty("是否置顶")
private Boolean isTop;
/**
* 商品图地址
*/
@ -102,6 +108,12 @@ public class ProductPageVo implements Serializable {
@ApiModelProperty("最低价")
private BigDecimal minPrice;
/**
* 限购数量
*/
@ApiModelProperty("限购数量")
private Integer limitNum;
/**
* 支付方式(微信支付:0;积分支付:1)
*/

View File

@ -45,4 +45,14 @@ public class WxPayConfiguration {
return wxPayService;
}
/**
* @descriptions 读取微信支付配置文件
* @author DB
* @date 2023/11/01 17:27
* @return: com.cpop.mall.framework.config.wxPay.WxPayProperties
*/
public WxPayProperties getProperties() {
return properties;
}
}

View File

@ -70,4 +70,9 @@ public class WxPayProperties {
* 退款通知
*/
private String notifyRefund;
/**
* 分账账号
*/
private String sharingAccount;
}