1. 迁移支付交易的提交 RPC 接口

2. 迁移支付交易的获取 RPC 接口
pull/4/MERGE
YunaiV 2020-11-29 01:43:30 +08:00
parent d1b6118052
commit c60f9c71bf
79 changed files with 628 additions and 496 deletions

View File

@ -1,50 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>onemall</artifactId>
<groupId>cn.iocoder.mall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ops</artifactId>
<dependencies>
<!-- Web 相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 监控相关 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,15 +0,0 @@
package cn.iocoder.mall.ops;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer
public class OpsApplication {
public static void main(String[] args) {
SpringApplication.run(OpsApplication.class, args);
}
}

View File

@ -1,7 +1,7 @@
package cn.iocoder.mall.payservice.rpc.transaction;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionCreateReqDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.*;
/**
* RPC
@ -16,4 +16,20 @@ public interface PayTransactionRpc {
*/
CommonResult<Integer> createPayTransaction(PayTransactionCreateReqDTO createReqDTO);
/**
*
*
* @param submitReqDTO
* @return
*/
CommonResult<PayTransactionSubmitRespDTO> submitPayTransaction(PayTransactionSubmitReqDTO submitReqDTO);
/**
*
*
* @param getReqDTO
* @return
*/
CommonResult<PayTransactionRespDTO> getPayTransaction(PayTransactionGetReqDTO getReqDTO);
}

View File

@ -17,6 +17,12 @@ import java.util.Date;
@Accessors(chain = true)
public class PayTransactionCreateReqDTO implements Serializable {
/**
*
*/
@NotNull(message = "用户编号不能为空")
private Integer userId;
/**
*
*/

View File

@ -0,0 +1,27 @@
package cn.iocoder.mall.payservice.rpc.transaction.dto;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
/**
* Request DTO
*/
@Data
@Accessors(chain = true)
public class PayTransactionGetReqDTO {
/**
*
*/
@NotEmpty(message = "应用编号不能为空")
private String appId;
/**
*
*/
@NotEmpty(message = "订单号不能为空")
private String orderId;
}

View File

@ -0,0 +1,96 @@
package cn.iocoder.mall.payservice.rpc.transaction.dto;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* Response DTO
*/
@Data
@Accessors(chain = true)
public class PayTransactionRespDTO {
/**
*
*/
private Integer id;
/**
*
*/
private Integer userId;
/**
*
*/
private String appId;
/**
* IP
*/
private String createIp;
/**
* 线
*/
private String orderId;
/**
*
*/
private String orderSubject;
/**
*
*/
private String orderDescription;
/**
*
*/
private String orderMemo;
/**
*
*/
private Integer price;
/**
*
*/
private Integer status;
/**
*
*/
private Date expireTime;
/**
* 线
*/
private Date finishTime;
/**
*
*/
private String notifyUrl;
/**
*
*/
private Integer extensionId;
/**
*
*/
private Integer payChannel;
/**
*
*/
private Date paymentTime;
/**
*
*/
private Date notifyTime;
/**
*
*/
private String tradeNo;
/**
* 退
*/
private Integer refundTotal;
/**
*
*/
private Date createTime;
}

View File

@ -0,0 +1,43 @@
package cn.iocoder.mall.payservice.rpc.transaction.dto;
import cn.iocoder.common.framework.validator.InEnum;
import cn.iocoder.mall.payservice.enums.PayChannelEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* Request VO
*/
@Data
@Accessors(chain = true)
public class PayTransactionSubmitReqDTO {
/**
*
*/
@NotEmpty(message = "应用编号不能为空")
private String appId;
/**
* IP
*/
@NotEmpty(message = "IP 不能为空")
private String createIp;
/**
*
*/
@NotEmpty(message = "订单号不能为空")
private String orderId;
/**
*
*/
@InEnum(value = PayChannelEnum.class, message = "支付渠道必须是 {value}")
@NotNull(message = "支付渠道")
private Integer payChannel;
}

View File

@ -0,0 +1,23 @@
package cn.iocoder.mall.payservice.rpc.transaction.dto;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* Response DTO
*/
@Data
@Accessors(chain = true)
public class PayTransactionSubmitRespDTO {
/**
*
*/
private Integer id;
/**
*
*/
private String invokeResponse;
}

View File

@ -5,7 +5,7 @@ import cn.iocoder.mall.payservice.enums.PayChannelEnum;
import java.util.HashMap;
import java.util.Map;
public class PaySDKFactory {
public class ThirdPayClientFactory {
private static Map<Integer, AbstractThirdPayClient> CLIENTS = new HashMap<>();
@ -13,7 +13,7 @@ public class PaySDKFactory {
CLIENTS.put(PayChannelEnum.PINGXX.getId(), new PingxxThirdPayClient());
}
public static AbstractThirdPayClient getSDK(Integer payChannel) {
public static AbstractThirdPayClient getThirdPayClient(Integer payChannel) {
AbstractThirdPayClient client = CLIENTS.get(payChannel);
if (client == null) {
throw new NullPointerException("找不到合适的 ThirdPayClient " + payChannel);

View File

@ -1,7 +1,10 @@
package cn.iocoder.mall.payservice.convert.transaction;
import cn.iocoder.mall.payservice.dal.mysql.dataobject.transaction.PayTransactionDO;
import cn.iocoder.mall.payservice.dal.mysql.dataobject.transaction.PayTransactionExtensionDO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionCreateReqDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionRespDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionSubmitReqDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@ -12,4 +15,8 @@ public interface PayTransactionConvert {
PayTransactionDO convert(PayTransactionCreateReqDTO bean);
PayTransactionExtensionDO convert(PayTransactionSubmitReqDTO bean);
PayTransactionRespDTO convert(PayTransactionDO bean);
}

View File

@ -21,6 +21,10 @@ public class PayRefundDO extends DeletableDO {
*
*/
private Integer id;
/**
*
*/
private Integer userId;
/**
*
*/

View File

@ -23,6 +23,10 @@ public class PayTransactionDO extends DeletableDO {
*/
@TableId
private Integer id;
/**
*
*/
private Integer userId;
/**
*
*/

View File

@ -0,0 +1,21 @@
package cn.iocoder.mall.payservice.dal.mysql.mapper.transaction;
import cn.iocoder.mall.payservice.dal.mysql.dataobject.transaction.PayTransactionExtensionDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface PayTransactionExtensionMapper extends BaseMapper<PayTransactionExtensionDO> {
default int update(PayTransactionExtensionDO entity, Integer whereStatus) {
return update(entity, new QueryWrapper<PayTransactionExtensionDO>()
.eq("id", entity.getId()).eq("status", whereStatus));
}
default PayTransactionExtensionDO selectByTransactionCode(String transactionCode) {
return selectOne(new QueryWrapper<PayTransactionExtensionDO>()
.eq("transaction_code", transactionCode));
}
}

View File

@ -1,7 +1,7 @@
package cn.iocoder.mall.payservice.rpc.transaction;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionCreateReqDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.*;
import cn.iocoder.mall.payservice.service.transaction.PayTransactionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
@ -19,4 +19,14 @@ public class PayTransactionRpcImpl implements PayTransactionRpc {
return success(payTransactionService.createPayTransaction(createReqDTO));
}
@Override
public CommonResult<PayTransactionSubmitRespDTO> submitPayTransaction(PayTransactionSubmitReqDTO submitReqDTO) {
return success(payTransactionService.submitPayTransaction(submitReqDTO));
}
@Override
public CommonResult<PayTransactionRespDTO> getPayTransaction(PayTransactionGetReqDTO getReqDTO) {
return success(payTransactionService.getPayTransaction(getReqDTO));
}
}

View File

@ -1,6 +1,6 @@
package cn.iocoder.mall.payservice.service.transaction;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionCreateReqDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.*;
/**
* Service
@ -15,4 +15,20 @@ public interface PayTransactionService {
*/
Integer createPayTransaction(PayTransactionCreateReqDTO createReqDTO);
/**
*
*
* @param submitReqDTO
* @return
*/
PayTransactionSubmitRespDTO submitPayTransaction(PayTransactionSubmitReqDTO submitReqDTO);
/**
*
*
* @param getReqDTO
* @return
*/
PayTransactionRespDTO getPayTransaction(PayTransactionGetReqDTO getReqDTO);
}

View File

@ -1,11 +1,19 @@
package cn.iocoder.mall.payservice.service.transaction.impl;
import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.util.DateUtil;
import cn.iocoder.common.framework.util.MathUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.payservice.client.thirdpay.AbstractThirdPayClient;
import cn.iocoder.mall.payservice.client.thirdpay.ThirdPayClientFactory;
import cn.iocoder.mall.payservice.convert.transaction.PayTransactionConvert;
import cn.iocoder.mall.payservice.dal.mysql.dataobject.transaction.PayTransactionDO;
import cn.iocoder.mall.payservice.dal.mysql.dataobject.transaction.PayTransactionExtensionDO;
import cn.iocoder.mall.payservice.dal.mysql.mapper.transaction.PayTransactionExtensionMapper;
import cn.iocoder.mall.payservice.dal.mysql.mapper.transaction.PayTransactionMapper;
import cn.iocoder.mall.payservice.enums.transaction.PayTransactionStatusEnum;
import cn.iocoder.mall.payservice.rpc.app.dto.PayAppRespDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionCreateReqDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.*;
import cn.iocoder.mall.payservice.service.app.PayAppService;
import cn.iocoder.mall.payservice.service.transaction.PayTransactionService;
import lombok.extern.slf4j.Slf4j;
@ -13,6 +21,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.Date;
import static cn.iocoder.mall.payservice.enums.PayErrorCodeConstants.PAY_TRANSACTION_NOT_FOUND;
import static cn.iocoder.mall.payservice.enums.PayErrorCodeConstants.PAY_TRANSACTION_STATUS_IS_NOT_WAITING;
/**
* Service
*/
@ -23,6 +36,8 @@ public class PayTransactionServiceImpl implements PayTransactionService {
@Autowired
private PayTransactionMapper payTransactionMapper;
@Autowired
private PayTransactionExtensionMapper payTransactionExtensionMapper;
@Autowired
private PayAppService payAppService;
@ -50,4 +65,60 @@ public class PayTransactionServiceImpl implements PayTransactionService {
return payTransaction.getId();
}
@Override
public PayTransactionSubmitRespDTO submitPayTransaction(PayTransactionSubmitReqDTO submitReqDTO) {
// TODO 校验支付渠道是否有效
// 校验 App 是否有效
payAppService.validPayApp(submitReqDTO.getAppId());
// 获得 PayTransactionDO ,并校验其是否存在
PayTransactionDO payTransaction = payTransactionMapper.selectByAppIdAndOrderId(
submitReqDTO.getAppId(), submitReqDTO.getOrderId());
if (payTransaction == null) { // 是否存在
throw ServiceExceptionUtil.exception(PAY_TRANSACTION_NOT_FOUND);
}
if (!PayTransactionStatusEnum.WAITING.getValue().equals(payTransaction.getStatus())) { // 校验状态,必须是待支付
throw ServiceExceptionUtil.exception(PAY_TRANSACTION_STATUS_IS_NOT_WAITING);
}
// 插入 PayTransactionExtensionDO
PayTransactionExtensionDO payTransactionExtensionDO = PayTransactionConvert.INSTANCE.convert(submitReqDTO)
.setTransactionId(payTransaction.getId()).setTransactionCode(generateTransactionCode())
.setStatus(PayTransactionStatusEnum.WAITING.getValue());
payTransactionExtensionMapper.insert(payTransactionExtensionDO);
// 调用三方接口
AbstractThirdPayClient thirdPayClient = ThirdPayClientFactory.getThirdPayClient(submitReqDTO.getPayChannel());
CommonResult<String> invokeResult = thirdPayClient.submitTransaction(payTransaction, payTransactionExtensionDO, null); // TODO 暂时传入 extra = null
invokeResult.checkError();
// TODO 轮询三方接口,是否已经支付的任务
// 返回成功
return new PayTransactionSubmitRespDTO().setId(payTransactionExtensionDO.getId()).setInvokeResponse(invokeResult.getData());
}
@Override
public PayTransactionRespDTO getPayTransaction(PayTransactionGetReqDTO getReqDTO) {
return PayTransactionConvert.INSTANCE.convert(payTransactionMapper.selectByAppIdAndOrderId(
getReqDTO.getAppId(), getReqDTO.getOrderId()));
}
private String generateTransactionCode() {
// wx
// 2014
// 10
// 27
// 20
// 09
// 39
// 5522657
// a690389285100
// 目前的算法
// 时间序列,年月日时分秒 14 位
// 纯随机6 位 TODO 此处估计是会有问题的,后续在调整
return DateUtil.format(new Date(), "yyyyMMddHHmmss") + // 时间序列
MathUtil.random(100000, 999999) // 随机。为什么是这个范围,因为偷懒
;
}
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>pay-service-project</artifactId>
<groupId>cn.iocoder.mall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-service-integration-test</artifactId>
<dependencies>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>pay-service-app</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Test 相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,27 @@
package cn.iocoder.mall.payservice.service.transaction.impl;
import cn.iocoder.mall.payservice.enums.PayChannelEnum;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionSubmitReqDTO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PayTransactionServiceImplTest {
@Autowired
private PayTransactionServiceImpl payTransactionService;
@Test
public void testSubmitPayTransaction() {
payTransactionService.submitPayTransaction(new PayTransactionSubmitReqDTO()
.setAppId("POd4RC6a")
.setCreateIp("127.0.0.1")
.setOrderId("239")
.setPayChannel(PayChannelEnum.PINGXX.getId()));
}
}

View File

@ -0,0 +1 @@
package cn.iocoder.mall.payservice.service.transaction;

View File

@ -15,6 +15,7 @@
<modules>
<module>pay-service-api</module>
<module>pay-service-app</module>
<module>pay-service-integration-test</module>
</modules>

View File

@ -14,10 +14,6 @@ import java.util.List;
public interface PayTransactionService {
PayTransactionBO getTransaction(PayTransactionGetDTO payTransactionGetDTO);
PayTransactionSubmitBO submitTransaction(PayTransactionSubmitDTO payTransactionSubmitDTO);
/**
*
*

View File

@ -1,72 +0,0 @@
package cn.iocoder.mall.pay.application.controller.users;
import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.pay.api.PayTransactionService;
import cn.iocoder.mall.pay.api.bo.transaction.PayTransactionBO;
import cn.iocoder.mall.pay.api.bo.transaction.PayTransactionSubmitBO;
import cn.iocoder.mall.pay.api.constant.PayChannelEnum;
import cn.iocoder.mall.pay.api.dto.transaction.PayTransactionGetDTO;
import cn.iocoder.mall.pay.api.dto.transaction.PayTransactionSubmitDTO;
import cn.iocoder.mall.user.sdk.context.UserSecurityContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@RestController
@RequestMapping("users/transaction")
@Api("【用户】支付交易 API")
public class UsersPayTransactionController {
private Logger logger = LoggerFactory.getLogger(getClass());
@Reference(validation = "true", version = "${dubbo.provider.PayTransactionService.version}")
private PayTransactionService payTransactionService;
@GetMapping("/get")
@ApiOperation("获得支付交易")
public CommonResult<PayTransactionBO> get(PayTransactionGetDTO payTransactionGetDTO) {
payTransactionGetDTO.setUserId(UserSecurityContextHolder.getContext().getUserId());
return success(payTransactionService.getTransaction(payTransactionGetDTO));
}
@PostMapping("/submit")
@ApiOperation("提交支付交易")
public CommonResult<PayTransactionSubmitBO> submit(HttpServletRequest request,
PayTransactionSubmitDTO payTransactionSubmitDTO) {
payTransactionSubmitDTO.setCreateIp(HttpUtil.getIp(request));
// 提交支付提交
return success(payTransactionService.submitTransaction(payTransactionSubmitDTO));
}
@PostMapping(value = "pingxx_pay_success", consumes = MediaType.APPLICATION_JSON_VALUE)
// @GetMapping(value = "pingxx_pay_success")
public String pingxxPaySuccess(HttpServletRequest request) throws IOException {
logger.info("[pingxxPaySuccess][被回调]");
// 读取 webhook
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
// JSONObject bodyObj = JSON.parseObject(sb.toString());
// bodyObj.put("webhookId", bodyObj.remove("id"));
// String body = bodyObj.toString();
payTransactionService.updateTransactionPaySuccess(PayChannelEnum.PINGXX.getId(), sb.toString());
return "success";
}
}

View File

@ -79,39 +79,6 @@ public class PayTransactionServiceImpl implements PayTransactionService {
return PayTransactionConvert.INSTANCE.convert(payTransaction);
}
@Override
@SuppressWarnings("Duplicates")
public PayTransactionSubmitBO submitTransaction(PayTransactionSubmitDTO payTransactionSubmitDTO) {
// TODO 校验支付渠道是否有效
// 校验 App 是否有效
payAppService.validPayApp(payTransactionSubmitDTO.getAppId());
// 获得 PayTransactionDO ,并校验其是否存在
PayTransactionDO payTransaction = payTransactionMapper.selectByAppIdAndOrderId(
payTransactionSubmitDTO.getAppId(), payTransactionSubmitDTO.getOrderId());
if (payTransaction == null) { // 是否存在
throw ServiceExceptionUtil.exception(PayErrorCodeEnum.PAY_TRANSACTION_NOT_FOUND.getCode());
}
if (!PayTransactionStatusEnum.WAITING.getValue().equals(payTransaction.getStatus())) { // 校验状态,必须是待支付
throw ServiceExceptionUtil.exception(PayErrorCodeEnum.PAY_TRANSACTION_STATUS_IS_NOT_WAITING.getCode());
}
// 插入 PayTransactionExtensionDO
PayTransactionExtensionDO payTransactionExtensionDO = PayTransactionConvert.INSTANCE.convert(payTransactionSubmitDTO)
.setTransactionId(payTransaction.getId())
.setTransactionCode(generateTransactionCode())
.setStatus(PayTransactionStatusEnum.WAITING.getValue());
payTransactionExtensionMapper.insert(payTransactionExtensionDO);
// 调用三方接口
AbstractPaySDK paySDK = PaySDKFactory.getSDK(payTransactionSubmitDTO.getPayChannel());
CommonResult<String> invokeResult = paySDK.submitTransaction(payTransaction, payTransactionExtensionDO, null); // TODO 暂时传入 extra = null
if (invokeResult.isError()) {
throw ServiceExceptionUtil.exception(invokeResult.getCode(), invokeResult.getMessage());
}
// TODO 轮询三方接口,是否已经支付的任务
// 返回成功
return new PayTransactionSubmitBO().setId(payTransactionExtensionDO.getId())
.setInvokeResponse(invokeResult.getData());
}
@Override
@Transactional
public Boolean updateTransactionPaySuccess(Integer payChannel, String params) {
@ -199,23 +166,7 @@ public class PayTransactionServiceImpl implements PayTransactionService {
return null;
}
private String generateTransactionCode() {
// wx
// 2014
// 10
// 27
// 20
// 09
// 39
// 5522657
// a690389285100
// 目前的算法
// 时间序列,年月日时分秒 14 位
// 纯随机6 位 TODO 此处估计是会有问题的,后续在调整
return DateUtil.format(new Date(), "yyyyMMddHHmmss") + // 时间序列
MathUtil.random(100000, 999999) // 随机。为什么是这个范围,因为偷懒
;
}
}

View File

@ -9,20 +9,6 @@
notify_time, trade_no, refund_total, create_time
</sql>
<insert id="insert" parameterType="PayTransactionDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO transaction (
app_id, create_ip, order_id, order_subject,
order_description, order_memo, price, status, expire_time,
finish_time, notify_url, extension_id, pay_channel, payment_time,
notify_time, trade_no, create_time
) VALUES (
#{appId}, #{createIp}, #{orderId}, #{orderSubject},
#{orderDescription}, #{orderMemo}, #{price}, #{status}, #{expireTime},
#{finishTime}, #{notifyUrl}, #{extensionId}, #{payChannel}, #{paymentTime},
#{notifyTime}, #{tradeNo}, #{createTime}
)
</insert>
<update id="update">
UPDATE transaction
<set>
@ -68,23 +54,6 @@
AND order_id = #{orderId}
</select>
<select id="selectById" parameterType="Integer" resultType="PayTransactionDO">
SELECT
<include refid="FIELDS"/>
FROM transaction
WHERE id = #{id}
</select>
<select id="selectListByIds" resultType="PayTransactionDO">
SELECT
<include refid="FIELDS" />
FROM transaction
WHERE id IN
<foreach item="id" collection="ids" separator="," open="(" close=")" index="">
#{id}
</foreach>
</select>
<select id="selectListByPage" resultType="PayTransactionDO">
SELECT
<include refid="FIELDS"/>

View File

@ -1,28 +0,0 @@
package cn.iocoder.mall.pay.api.dto.transaction;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@ApiModel("支付交易获得 DTO")
@Data
@Accessors(chain = true)
public class PayTransactionGetDTO {
@ApiModelProperty(value = "用户编号", required = true, example = "1", hidden = true) // hidden 的原因是Service DTO 自己传入,无需暴露的 Controller API 里
@NotNull(message = "用户编号不能为空")
private Integer userId;
@ApiModelProperty(value = "应用编号", required = true, example = "POd4RC6a")
@NotEmpty(message = "应用编号不能为空")
private String appId;
@ApiModelProperty(value = "订单号不能为空", required = true, example = "1024")
@NotEmpty(message = "订单号不能为空")
private String orderId;
}

View File

@ -1,136 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>pay</artifactId>
<groupId>cn.iocoder.mall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-service-impl</artifactId>
<dependencies>
<!-- Mall 相关 -->
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>common-framework</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>system-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>pay-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- DB 相关 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- RPC 相关 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- Registry 和 Config 相关 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Transaction 相关 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
</dependency>
<!-- MQ 相关 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>
<!-- Job 相关 -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
</dependency>
<!-- 工具类相关 -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<!-- 云服务相关 -->
<dependency>
<groupId>Pingplusplus</groupId>
<artifactId>pingpp-java</artifactId>
<version>2.2.4</version>
<type>jar</type>
</dependency>
<!-- 测试相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId> <!-- 引入该包,为了写单元测试用 -->
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 提供给 mapstruct 使用 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
</project>

View File

@ -1,19 +0,0 @@
package cn.iocoder.mall.pay.biz.dao;
import cn.iocoder.mall.pay.biz.dataobject.PayTransactionExtensionDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface PayTransactionExtensionMapper {
void insert(PayTransactionExtensionDO entity);
int update(@Param("entity") PayTransactionExtensionDO entity,
@Param("whereStatus") Integer whereStatus);
PayTransactionExtensionDO selectByTransactionCode(@Param("transactionCode") String transactionCode);
PayTransactionExtensionDO selectById(@Param("id") Integer id);
}

View File

@ -1,19 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 使用驼峰命名法转换字段。 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer"/>
<typeAlias alias="Long" type="java.lang.Long"/>
<typeAlias alias="HashMap" type="java.util.HashMap"/>
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
<typeAlias alias="ArrayList" type="java.util.ArrayList"/>
<typeAlias alias="LinkedList" type="java.util.LinkedList"/>
</typeAliases>
</configuration>

View File

@ -1,7 +0,0 @@
package cn.iocoder.mall.pay.biz;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = {"cn.iocoder.mall.pay"})
public class Application {
}

View File

@ -1,13 +0,0 @@
package cn.iocoder.mall.pay.biz.service;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class PayTransactionServiceImplTest {
}

View File

@ -1,32 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>onemall</artifactId>
<groupId>cn.iocoder.mall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay</artifactId>
<packaging>pom</packaging>
<modules>
<module>pay-application</module>
<module>pay-service-api</module>
<module>pay-service-impl</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>mall-dependencies</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@ -84,6 +84,12 @@
<artifactId>system-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<!-- 支付服务 -->
<groupId>cn.iocoder.mall</groupId>
<artifactId>pay-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Registry 和 Config 相关 -->
<dependency>

View File

@ -0,0 +1,38 @@
package cn.iocoder.mall.shopweb.client.pay;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.payservice.rpc.transaction.PayTransactionRpc;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionGetReqDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionRespDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionSubmitReqDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionSubmitRespDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Service
public class PayTransactionClient {
@DubboReference(version = "${dubbo.consumer.PayTransactionRpc.version}")
private PayTransactionRpc payTransactionRpc;
public PayTransactionRespDTO getPayTransaction(Integer userId, String appId, String orderId) {
CommonResult<PayTransactionRespDTO> getPayTransactionResult = payTransactionRpc.getPayTransaction(new PayTransactionGetReqDTO()
.setAppId(appId).setOrderId(orderId));
getPayTransactionResult.checkError();
if (getPayTransactionResult.getData() == null) {
return null;
}
// 如果用户编号不匹配,则返回 null
return Objects.equals(getPayTransactionResult.getData().getUserId(), userId) ?
getPayTransactionResult.getData() : null;
}
public PayTransactionSubmitRespDTO submitPayTransaction(PayTransactionSubmitReqDTO submitReqDTO) {
CommonResult<PayTransactionSubmitRespDTO> submitPayTransactionResult = payTransactionRpc.submitPayTransaction(submitReqDTO);
submitPayTransactionResult.checkError();
return submitPayTransactionResult.getData();
}
}

View File

@ -0,0 +1,74 @@
package cn.iocoder.mall.shopweb.controller.pay;
import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.security.user.core.context.UserSecurityContextHolder;
import cn.iocoder.mall.shopweb.controller.pay.vo.transaction.PayTransactionRespVO;
import cn.iocoder.mall.shopweb.controller.pay.vo.transaction.PayTransactionSubmitReqVO;
import cn.iocoder.mall.shopweb.controller.pay.vo.transaction.PayTransactionSubmitRespVO;
import cn.iocoder.mall.shopweb.service.pay.PayTransactionService;
import cn.iocoder.security.annotations.RequiresAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@Api("支付交易 API")
@RestController
@RequestMapping("/pay/transaction")
@Validated
@Slf4j
public class PayTransactionController {
@Autowired
private PayTransactionService payTransactionService;
@GetMapping("/get")
@ApiOperation("获得支付交易")
@ApiImplicitParams({
@ApiImplicitParam(name = "appId", required = true, value = "应用编号", example = "POd4RC6a"),
@ApiImplicitParam(name = "orderId", required = true, value = "订单号", example = "1024"),
})
@RequiresAuthenticate
public CommonResult<PayTransactionRespVO> getPayTransaction(@RequestParam("appId") String appId,
@RequestParam("orderId") String orderId) {
return success(payTransactionService.getPayTransaction(UserSecurityContextHolder.getUserId(), appId, orderId));
}
@PostMapping("/submit")
@ApiOperation("提交支付交易")
@RequiresAuthenticate
public CommonResult<PayTransactionSubmitRespVO> submitPayTransaction(HttpServletRequest request,
PayTransactionSubmitReqVO submitReqVO) {
return success(payTransactionService.submitPayTransaction(submitReqVO, HttpUtil.getIp(request)));
}
// @PostMapping(value = "pingxx_pay_success", consumes = MediaType.APPLICATION_JSON_VALUE)
//// @GetMapping(value = "pingxx_pay_success")
// public String pingxxPaySuccess(HttpServletRequest request) throws IOException {
// logger.info("[pingxxPaySuccess][被回调]");
// // 读取 webhook
// StringBuilder sb = new StringBuilder();
// try (BufferedReader reader = request.getReader()) {
// String line;
// while ((line = reader.readLine()) != null) {
// sb.append(line);
// }
// }
//
//// JSONObject bodyObj = JSON.parseObject(sb.toString());
//// bodyObj.put("webhookId", bodyObj.remove("id"));
//// String body = bodyObj.toString();
// payTransactionService.updateTransactionPaySuccess(PayChannelEnum.PINGXX.getId(), sb.toString());
// return "success";
// }
}

View File

@ -0,0 +1,39 @@
package cn.iocoder.mall.shopweb.controller.pay.vo.transaction;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
@ApiModel("支付交易 Response VO")
@Data
@Accessors(chain = true)
public class PayTransactionRespVO {
@ApiModelProperty(value = "交易编号", required = true, example = "1")
private Integer id;
@ApiModelProperty(value = "应用编号", required = true, example = "POd4RC6a")
private String appId;
@ApiModelProperty(value = "订单号不能为空", required = true, example = "1024")
private String orderId;
@ApiModelProperty(value = "商品名", required = true, example = "芋道源码")
private String orderSubject;
@ApiModelProperty(value = "订单商品描述", required = true, example = "绵啾啾的")
private String orderDescription;
@ApiModelProperty(value = "支付金额,单位:分。", required = true, example = "10")
private Integer price;
@ApiModelProperty(value = "订单状态", required = true, example = "1", notes = "参见 PayTransactionStatusEnum 枚举")
private Integer status;
@ApiModelProperty(value = "交易过期时间", required = true)
private Date expireTime;
}

View File

@ -1,7 +1,7 @@
package cn.iocoder.mall.pay.api.dto.transaction;
package cn.iocoder.mall.shopweb.controller.pay.vo.transaction;
import cn.iocoder.common.framework.validator.InEnum;
import cn.iocoder.mall.pay.api.constant.PayChannelEnum;
import cn.iocoder.mall.payservice.enums.PayChannelEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ -10,19 +10,15 @@ import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@ApiModel("支付交易提交 DTO")
@ApiModel("支付交易提交 Request VO")
@Data
@Accessors(chain = true)
public class PayTransactionSubmitDTO {
public class PayTransactionSubmitReqVO {
@ApiModelProperty(value = "应用编号", required = true, example = "POd4RC6a")
@NotEmpty(message = "应用编号不能为空")
private String appId;
@ApiModelProperty(value = "发起交易的 IP", required = true, example = "192.168.10.1", hidden = true) // hidden 的原因是Service DTO 自己传入,无需暴露的 Controller API 里
@NotEmpty(message = "IP 不能为空")
private String createIp;
@ApiModelProperty(value = "订单号", required = true, example = "1024")
@NotEmpty(message = "订单号不能为空")
private String orderId;

View File

@ -1,16 +1,14 @@
package cn.iocoder.mall.pay.api.bo.transaction;
package cn.iocoder.mall.shopweb.controller.pay.vo.transaction;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
@ApiModel("支付交易提交结果 BO")
@ApiModel("支付交易提交 Response VO")
@Data
@Accessors(chain = true)
public class PayTransactionSubmitBO implements Serializable {
public class PayTransactionSubmitRespVO {
@ApiModelProperty(value = "支付交易拓展单编号", required = true, example = "1")
private Integer id;

View File

@ -0,0 +1,23 @@
package cn.iocoder.mall.shopweb.convert.pay;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionRespDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionSubmitReqDTO;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionSubmitRespDTO;
import cn.iocoder.mall.shopweb.controller.pay.vo.transaction.PayTransactionRespVO;
import cn.iocoder.mall.shopweb.controller.pay.vo.transaction.PayTransactionSubmitReqVO;
import cn.iocoder.mall.shopweb.controller.pay.vo.transaction.PayTransactionSubmitRespVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface PayTransactionConvert {
PayTransactionConvert INSTANCE = Mappers.getMapper(PayTransactionConvert.class);
PayTransactionSubmitReqDTO convert(PayTransactionSubmitReqVO bean);
PayTransactionSubmitRespVO convert(PayTransactionSubmitRespDTO bean);
PayTransactionRespVO convert(PayTransactionRespDTO bean);
}

View File

@ -0,0 +1,28 @@
package cn.iocoder.mall.shopweb.service.pay;
import cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionSubmitRespDTO;
import cn.iocoder.mall.shopweb.client.pay.PayTransactionClient;
import cn.iocoder.mall.shopweb.controller.pay.vo.transaction.PayTransactionRespVO;
import cn.iocoder.mall.shopweb.controller.pay.vo.transaction.PayTransactionSubmitReqVO;
import cn.iocoder.mall.shopweb.controller.pay.vo.transaction.PayTransactionSubmitRespVO;
import cn.iocoder.mall.shopweb.convert.pay.PayTransactionConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PayTransactionService {
@Autowired
private PayTransactionClient payTransactionClient;
public PayTransactionSubmitRespVO submitPayTransaction(PayTransactionSubmitReqVO submitReqVO, String ip) {
PayTransactionSubmitRespDTO submitPayTransaction = payTransactionClient.submitPayTransaction(
PayTransactionConvert.INSTANCE.convert(submitReqVO));
return PayTransactionConvert.INSTANCE.convert(submitPayTransaction);
}
public PayTransactionRespVO getPayTransaction(Integer userId, String appId, String orderId) {
return PayTransactionConvert.INSTANCE.convert(payTransactionClient.getPayTransaction(userId, appId, orderId));
}
}

View File

@ -55,6 +55,8 @@ dubbo:
version: 1.0.0
TradeOrderRpc:
version: 1.0.0
PayTransactionRpc:
version: 1.0.0
# Swagger 配置项
swagger:

View File

@ -171,7 +171,8 @@ public class TradeOrderServiceImpl implements TradeOrderService {
String orderSubject = listProductSkus.get(0).getSpu().getName();
Date expireTime = DateUtil.addDate(Calendar.MINUTE, tradeBizProperties.getPayExpireTime());
Integer payTransactionId = payTransactionClient.createPayTransaction(
new PayTransactionCreateReqDTO().setCreateIp(createReqDTO.getIp()).setAppId(tradeBizProperties.getPayAppId())
new PayTransactionCreateReqDTO().setUserId(createReqDTO.getUserId())
.setCreateIp(createReqDTO.getIp()).setAppId(tradeBizProperties.getPayAppId())
.setOrderId(tradeOrderDO.getId().toString()).setExpireTime(expireTime)
.setPrice(tradeOrderDO.getPresentPrice()).setOrderSubject(orderSubject)
.setOrderMemo("测试备注") // TODO 芋艿,后面补充