refactor(支付): 将支付相关URL提取到常量类中
将支付、退款等URL从硬编码改为使用常量类PayApiConstants中的常量,提高代码可维护性和可读性。同时修复退款请求参数的编码问题,确保参数正确传递。
This commit is contained in:
parent
abd8f7aa60
commit
6f2eaf92a8
|
@ -20,9 +20,8 @@ import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调度日志操作处理
|
* 订单控制器,处理与订单相关的HTTP请求
|
||||||
*
|
* 包括订单提交、开柜门、查询订单和退款等操作
|
||||||
* @author valarchie
|
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@ -33,25 +32,48 @@ public class OrderController extends BaseController {
|
||||||
private final OrderApplicationService orderApplicationService;
|
private final OrderApplicationService orderApplicationService;
|
||||||
private final PaymentApplicationService paymentApplicationService;
|
private final PaymentApplicationService paymentApplicationService;
|
||||||
|
|
||||||
// 新增提交订单接口
|
/**
|
||||||
|
* 提交订单接口
|
||||||
|
* @param command 提交订单的请求参数
|
||||||
|
* @return 包含订单创建结果的响应
|
||||||
|
*/
|
||||||
@PostMapping("/submit")
|
@PostMapping("/submit")
|
||||||
public ResponseDTO<CreateOrderResult> submitOrder(@Validated @RequestBody SubmitOrderCommand command) {
|
public ResponseDTO<CreateOrderResult> submitOrder(@Validated @RequestBody SubmitOrderCommand command) {
|
||||||
CreateOrderResult result = orderApplicationService.createOrder(command);
|
CreateOrderResult result = orderApplicationService.createOrder(command);
|
||||||
return ResponseDTO.ok(result);
|
return ResponseDTO.ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开订单商品柜门接口
|
||||||
|
* @param orderId 订单ID
|
||||||
|
* @param orderGoodsId 订单商品ID
|
||||||
|
* @return 空响应
|
||||||
|
*/
|
||||||
@PostMapping("/openCabinet/{orderId}/{orderGoodsId}")
|
@PostMapping("/openCabinet/{orderId}/{orderGoodsId}")
|
||||||
public ResponseDTO<?> openCabinet(@PathVariable Long orderId, @PathVariable Long orderGoodsId) {
|
public ResponseDTO<?> openCabinet(@PathVariable Long orderId, @PathVariable Long orderGoodsId) {
|
||||||
orderApplicationService.openOrderGoodsCabinet(orderId, orderGoodsId);
|
orderApplicationService.openOrderGoodsCabinet(orderId, orderGoodsId);
|
||||||
return ResponseDTO.ok();
|
return ResponseDTO.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户openid获取订单列表
|
||||||
|
* @param openid 用户微信openid
|
||||||
|
* @return 包含订单列表的响应
|
||||||
|
*/
|
||||||
@GetMapping("/user/{openid}")
|
@GetMapping("/user/{openid}")
|
||||||
public ResponseDTO<GetOrdersByOpenIdDTO> getOrdersByOpenId(@PathVariable String openid) {
|
public ResponseDTO<GetOrdersByOpenIdDTO> getOrdersByOpenId(@PathVariable String openid) {
|
||||||
GetOrdersByOpenIdDTO result = orderApplicationService.getOrdersByOpenId(openid);
|
GetOrdersByOpenIdDTO result = orderApplicationService.getOrdersByOpenId(openid);
|
||||||
return ResponseDTO.ok(result);
|
return ResponseDTO.ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单退款接口
|
||||||
|
* @param orderId 订单ID
|
||||||
|
* @param money 退款金额(单位:分)
|
||||||
|
* @return 包含退款结果的响应
|
||||||
|
* @throws IllegalArgumentException 当退款金额为负数或超过订单总额时抛出
|
||||||
|
* @throws RuntimeException 当退款过程中发生其他错误时抛出
|
||||||
|
*/
|
||||||
@PostMapping("/refund/{orderId}")
|
@PostMapping("/refund/{orderId}")
|
||||||
public ResponseDTO<RefundVO> refundOrder(@PathVariable Long orderId, @RequestParam int money) {
|
public ResponseDTO<RefundVO> refundOrder(@PathVariable Long orderId, @RequestParam int money) {
|
||||||
OrderModel orderModel = orderApplicationService.loadById(orderId);
|
OrderModel orderModel = orderApplicationService.loadById(orderId);
|
||||||
|
|
|
@ -3,5 +3,8 @@ package com.agileboot.common.constant;
|
||||||
public class PayApiConstants {
|
public class PayApiConstants {
|
||||||
public static final String biz_id = "wxshop";
|
public static final String biz_id = "wxshop";
|
||||||
public static final String appkey = "wxshop202503081132";
|
public static final String appkey = "wxshop202503081132";
|
||||||
|
public static final String pay_url = "http://222.218.10.217:7890/open/trade/wx/jsapi/precreate";
|
||||||
|
public static final String pay_callback_url = "http://wxshop.ab98.cn/shop-api/api/payment/callback";
|
||||||
|
public static final String refund_url = "http://222.218.10.217:7890/open/trade/refund";
|
||||||
|
public static final String refund_callback_url = "http://wxshop.ab98.cn/shop-api/api/payment/refund/callback";
|
||||||
}
|
}
|
||||||
|
|
|
@ -169,7 +169,7 @@ public class OrderApplicationService {
|
||||||
request.setPay_amount(amountInFen.toPlainString()); // 单位转换为分
|
request.setPay_amount(amountInFen.toPlainString()); // 单位转换为分
|
||||||
|
|
||||||
request.setTitle("商品订单支付");
|
request.setTitle("商品订单支付");
|
||||||
request.setNotify_url("http://wxshop.ab98.cn/shop-api/api/payment/callback");
|
request.setNotify_url(PayApiConstants.pay_callback_url);
|
||||||
request.setBiz_id(PayApiConstants.biz_id);
|
request.setBiz_id(PayApiConstants.biz_id);
|
||||||
request.setUcid(orderModel.getUcid());
|
request.setUcid(orderModel.getUcid());
|
||||||
request.setExtra("");
|
request.setExtra("");
|
||||||
|
|
|
@ -21,6 +21,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -34,7 +35,7 @@ public class PaymentApplicationService {
|
||||||
private static final Object LOCKER = new Object();
|
private static final Object LOCKER = new Object();
|
||||||
|
|
||||||
public WxJsApiPreCreateResponse callJsApiPreCreate(WxJsApiPreCreateRequest request) {
|
public WxJsApiPreCreateResponse callJsApiPreCreate(WxJsApiPreCreateRequest request) {
|
||||||
String gatewayUrl = "http://222.218.10.217:7890/open/trade/wx/jsapi/precreate";
|
String gatewayUrl = PayApiConstants.pay_url;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String jsonBody = JSONUtil.toJsonStr(request);
|
String jsonBody = JSONUtil.toJsonStr(request);
|
||||||
|
@ -115,7 +116,7 @@ public class PaymentApplicationService {
|
||||||
* @param money 退款金额,单位:分
|
* @param money 退款金额,单位:分
|
||||||
*/
|
*/
|
||||||
public RefundVO refund(String bizId, String appKey, String orderId, String uid, String reason, int money) throws Exception {
|
public RefundVO refund(String bizId, String appKey, String orderId, String uid, String reason, int money) throws Exception {
|
||||||
String url = "http://222.218.10.217:7890/open/trade/refund";
|
String url = PayApiConstants.refund_url;
|
||||||
JSONObject bizContent = new JSONObject();
|
JSONObject bizContent = new JSONObject();
|
||||||
bizContent.set("userId", uid);
|
bizContent.set("userId", uid);
|
||||||
bizContent.set("bizId", bizId);
|
bizContent.set("bizId", bizId);
|
||||||
|
@ -129,7 +130,8 @@ public class PaymentApplicationService {
|
||||||
params.put("timestamp", String.valueOf(System.currentTimeMillis()));
|
params.put("timestamp", String.valueOf(System.currentTimeMillis()));
|
||||||
params.put("version", "1.0");
|
params.put("version", "1.0");
|
||||||
params.put("nonce_str", UUID.randomUUID().toString());
|
params.put("nonce_str", UUID.randomUUID().toString());
|
||||||
params.put("biz_content", URLUtil.encode(JSONUtil.toJsonStr(bizContent)));
|
params.put("biz_content", URLEncoder.encode(JSONUtil.toJsonStr(bizContent), StandardCharsets.UTF_8.name()));
|
||||||
|
log.info("退款请求参数:{}", JSONUtil.toJsonStr(params));
|
||||||
params.put("sign", SignUtils.openSign(appKey, params));
|
params.put("sign", SignUtils.openSign(appKey, params));
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (String key : params.keySet()) {
|
for (String key : params.keySet()) {
|
||||||
|
@ -202,8 +204,8 @@ public class PaymentApplicationService {
|
||||||
|
|
||||||
String orderNO = bizContent.getBizOrderId();
|
String orderNO = bizContent.getBizOrderId();
|
||||||
if (StringUtils.isBlank(orderNO)) {
|
if (StringUtils.isBlank(orderNO)) {
|
||||||
res.put("callback_code", 1);
|
res.set("callback_code", 1);
|
||||||
res.put("callback_msg", "缺少退款订单ID");
|
res.set("callback_msg", "缺少退款订单ID");
|
||||||
return JSONUtil.toJsonStr(res);
|
return JSONUtil.toJsonStr(res);
|
||||||
}
|
}
|
||||||
synchronized (LOCKER) {
|
synchronized (LOCKER) {
|
||||||
|
|
Loading…
Reference in New Issue