feat(subscription): 实现订阅订单创建与支付功能
- 新增创建订阅订单接口,支持生成订单号及价格试算 - 新增订阅订单支付接口,集成微信Native支付生成二维码 - 添加订单创建与支付结果返回类 - 注入微信支付控制器并调用其生成支付二维码方法 - 校验用户登录状态、套餐ID及支付金额有效性 - 构建订单对象用于支付二维码生成,并返回支付链接
This commit is contained in:
@@ -2,10 +2,15 @@ package com.gxwebsoft.common.system.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import com.gxwebsoft.common.system.entity.Order;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.param.SubscriptionOrderParam;
|
||||
import com.gxwebsoft.common.system.result.SubscriptionOrderCreateResult;
|
||||
import com.gxwebsoft.common.system.result.SubscriptionOrderPayResult;
|
||||
import com.gxwebsoft.common.system.result.SubscriptionPriceResult;
|
||||
import com.gxwebsoft.common.system.service.SettingService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@@ -31,6 +36,8 @@ public class SubscriptionOrderController extends BaseController {
|
||||
|
||||
@Resource
|
||||
private SettingService settingService;
|
||||
@Resource
|
||||
private WxNativePayController wxNativePayController;
|
||||
|
||||
@Operation(summary = "计算订阅订单价格")
|
||||
@PostMapping("/calculate-price")
|
||||
@@ -48,6 +55,64 @@ public class SubscriptionOrderController extends BaseController {
|
||||
return success(result);
|
||||
}
|
||||
|
||||
@Operation(summary = "创建订阅订单")
|
||||
@PostMapping("/create")
|
||||
public ApiResult<SubscriptionOrderCreateResult> create(@RequestBody SubscriptionOrderParam param) {
|
||||
final User loginUser = getLoginUser();
|
||||
if (loginUser == null) {
|
||||
return fail("请先登录", null);
|
||||
}
|
||||
if (param.getPackageId() == null) {
|
||||
return fail("套餐ID不能为空", null);
|
||||
}
|
||||
|
||||
JSONObject config = loadSubscriptionConfig();
|
||||
final SubscriptionPriceResult price = buildPriceResult(param, config);
|
||||
|
||||
SubscriptionOrderCreateResult result = new SubscriptionOrderCreateResult();
|
||||
result.setOrderNo(IdUtil.getSnowflakeNextIdStr());
|
||||
result.setPrice(price);
|
||||
return success(result);
|
||||
}
|
||||
|
||||
@Operation(summary = "订阅订单支付(生成微信Native二维码)")
|
||||
@PostMapping("/pay")
|
||||
public ApiResult<SubscriptionOrderPayResult> pay(@RequestBody SubscriptionOrderParam param) {
|
||||
final User loginUser = getLoginUser();
|
||||
if (loginUser == null) {
|
||||
return fail("请先登录", null);
|
||||
}
|
||||
if (param.getPackageId() == null) {
|
||||
return fail("套餐ID不能为空", null);
|
||||
}
|
||||
|
||||
JSONObject config = loadSubscriptionConfig();
|
||||
final SubscriptionPriceResult price = buildPriceResult(param, config);
|
||||
if (price.getPayPrice() == null || price.getPayPrice().compareTo(BigDecimal.ZERO) <= 0) {
|
||||
return fail("支付金额必须大于0", null);
|
||||
}
|
||||
|
||||
// 构造订单用于生成支付二维码
|
||||
Order order = new Order();
|
||||
order.setPayPrice(price.getPayPrice());
|
||||
order.setTotalPrice(price.getTotalPrice());
|
||||
order.setComments("订阅套餐-" + param.getPackageId());
|
||||
order.setPayType(param.getPayType());
|
||||
order.setUserId(loginUser.getUserId());
|
||||
order.setTenantId(loginUser.getTenantId());
|
||||
|
||||
ApiResult<?> payResp = wxNativePayController.getCodeUrl(order);
|
||||
if (payResp.getCode() == null || !payResp.getCode().equals(Constants.RESULT_OK_CODE)) {
|
||||
return fail(payResp.getMessage(), null);
|
||||
}
|
||||
|
||||
SubscriptionOrderPayResult result = new SubscriptionOrderPayResult();
|
||||
result.setOrderNo(order.getOrderNo());
|
||||
result.setCodeUrl(String.valueOf(payResp.getData()));
|
||||
result.setPrice(price);
|
||||
return success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从配置表读取订阅套餐配置(尝试多种key以兼容历史数据)
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.gxwebsoft.common.system.result;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 订阅订单创建结果
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "订阅订单创建结果")
|
||||
public class SubscriptionOrderCreateResult {
|
||||
|
||||
@Schema(description = "订单号")
|
||||
private String orderNo;
|
||||
|
||||
@Schema(description = "价格试算结果")
|
||||
private SubscriptionPriceResult price;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.gxwebsoft.common.system.result;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 订阅订单支付结果
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "订阅订单支付结果")
|
||||
public class SubscriptionOrderPayResult {
|
||||
|
||||
@Schema(description = "订单号")
|
||||
private String orderNo;
|
||||
|
||||
@Schema(description = "支付二维码链接")
|
||||
private String codeUrl;
|
||||
|
||||
@Schema(description = "价格信息")
|
||||
private SubscriptionPriceResult price;
|
||||
}
|
||||
Reference in New Issue
Block a user