From 8195512ba8576f7da5378fd5e5f4f6ab0ce692e3 Mon Sep 17 00:00:00 2001 From: jieyuu <645634619@qq.com> Date: Sat, 28 Sep 2024 16:13:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=8A=9F=E8=83=BD=E5=B7=A5?= =?UTF-8?q?=E5=8E=82=E6=A8=A1=E5=BC=8F=E5=BC=95=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/jieyuu/component/AlipayStrategy.java | 31 +++++++++ .../java/net/jieyuu/component/PayFactory.java | 63 +++++++++++++++++++ .../net/jieyuu/component/PayStrategy.java | 36 +++++++++++ .../jieyuu/component/PayStrategyContext.java | 35 +++++++++++ .../jieyuu/component/WechatPayStrategy.java | 30 +++++++++ .../service/impl/ProductOrderServiceImpl.java | 4 +- .../net/jieyuu/{model => vo}/OrderItemVO.java | 2 +- .../main/java/net/jieyuu/vo/PayInfoVO.java | 45 +++++++++++++ 8 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 xdclass-order-service/src/main/java/net/jieyuu/component/AlipayStrategy.java create mode 100644 xdclass-order-service/src/main/java/net/jieyuu/component/PayFactory.java create mode 100644 xdclass-order-service/src/main/java/net/jieyuu/component/PayStrategy.java create mode 100644 xdclass-order-service/src/main/java/net/jieyuu/component/PayStrategyContext.java create mode 100644 xdclass-order-service/src/main/java/net/jieyuu/component/WechatPayStrategy.java rename xdclass-order-service/src/main/java/net/jieyuu/{model => vo}/OrderItemVO.java (98%) create mode 100644 xdclass-order-service/src/main/java/net/jieyuu/vo/PayInfoVO.java diff --git a/xdclass-order-service/src/main/java/net/jieyuu/component/AlipayStrategy.java b/xdclass-order-service/src/main/java/net/jieyuu/component/AlipayStrategy.java new file mode 100644 index 0000000..7bccdb1 --- /dev/null +++ b/xdclass-order-service/src/main/java/net/jieyuu/component/AlipayStrategy.java @@ -0,0 +1,31 @@ +package net.jieyuu.component; + +import lombok.extern.slf4j.Slf4j; +import net.jieyuu.config.PayUrlConfig; +import net.jieyuu.vo.PayInfoVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + + +@Slf4j +@Service +public class AlipayStrategy implements PayStrategy { + + @Autowired + private PayUrlConfig payUrlConfig; + + @Override + public String unifiedorder(PayInfoVO payInfoVO) { + return PayStrategy.super.unifiedorder(payInfoVO); + } + + @Override + public String refund(PayInfoVO payInfoVO) { + return PayStrategy.super.refund(payInfoVO); + } + + @Override + public String queryPaySuccess(PayInfoVO payInfoVO) { + return PayStrategy.super.queryPaySuccess(payInfoVO); + } +} diff --git a/xdclass-order-service/src/main/java/net/jieyuu/component/PayFactory.java b/xdclass-order-service/src/main/java/net/jieyuu/component/PayFactory.java new file mode 100644 index 0000000..70e1c8f --- /dev/null +++ b/xdclass-order-service/src/main/java/net/jieyuu/component/PayFactory.java @@ -0,0 +1,63 @@ +package net.jieyuu.component; + + +import lombok.extern.slf4j.Slf4j; +import net.jieyuu.enums.ProductOrderPayTypeEnum; +import net.jieyuu.vo.PayInfoVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class PayFactory { + + @Autowired + private AlipayStrategy alipayStrategy; + + @Autowired + private WechatPayStrategy wechatPayStrategy; + + /** + * 创建支付,简单工厂模式 + * + * @param payInfoVO + * @return + */ + public String pay(PayInfoVO payInfoVO) { + + String payType = payInfoVO.getPayType(); + if (ProductOrderPayTypeEnum.ALIPAY.name().equalsIgnoreCase(payType)) {// 支付宝 + PayStrategyContext payStrategyContext = new PayStrategyContext(alipayStrategy); + return payStrategyContext.executeUnifiedorder(payInfoVO); + + } else if (ProductOrderPayTypeEnum.WECHAT.name().equalsIgnoreCase(payType)) {// 微信支付 + PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy); + return payStrategyContext.executeUnifiedorder(payInfoVO); + + } + return ""; + } + + + /** + * 查询订单支付状态 + * + * @param payInfoVO + * @return + */ + public String querySuccess(PayInfoVO payInfoVO) { + + String payType = payInfoVO.getPayType(); + if (ProductOrderPayTypeEnum.ALIPAY.name().equalsIgnoreCase(payType)) {// 支付宝 + PayStrategyContext payStrategyContext = new PayStrategyContext(alipayStrategy); + return payStrategyContext.executeQueryPaySuccess(payInfoVO); + + } else if (ProductOrderPayTypeEnum.WECHAT.name().equalsIgnoreCase(payType)) {// 微信支付 + PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy); + return payStrategyContext.executeQueryPaySuccess(payInfoVO); + + } + return ""; + } + +} diff --git a/xdclass-order-service/src/main/java/net/jieyuu/component/PayStrategy.java b/xdclass-order-service/src/main/java/net/jieyuu/component/PayStrategy.java new file mode 100644 index 0000000..48bff0c --- /dev/null +++ b/xdclass-order-service/src/main/java/net/jieyuu/component/PayStrategy.java @@ -0,0 +1,36 @@ +package net.jieyuu.component; + +import net.jieyuu.vo.PayInfoVO; + +public interface PayStrategy { + + /** + * 下单 + * + * @param payInfoVO + * @return + */ + default String unifiedorder(PayInfoVO payInfoVO) { + return ""; + } + + /** + * 退款 + * + * @param payInfoVO + * @return + */ + default String refund(PayInfoVO payInfoVO) { + return ""; + } + + /** + * 查询订单是否成功 + * + * @param payInfoVO + * @return + */ + default String queryPaySuccess(PayInfoVO payInfoVO) { + return ""; + } +} diff --git a/xdclass-order-service/src/main/java/net/jieyuu/component/PayStrategyContext.java b/xdclass-order-service/src/main/java/net/jieyuu/component/PayStrategyContext.java new file mode 100644 index 0000000..9ffeb00 --- /dev/null +++ b/xdclass-order-service/src/main/java/net/jieyuu/component/PayStrategyContext.java @@ -0,0 +1,35 @@ +package net.jieyuu.component; + +import net.jieyuu.vo.PayInfoVO; + +public class PayStrategyContext { + + private PayStrategy payStrategy; + + public PayStrategyContext(PayStrategy payStrategy) { + this.payStrategy = payStrategy; + } + + /** + * 根据支付策略,调用不同支付 + * + * @param payInfoVO + * @return + */ + public String executeUnifiedorder(PayInfoVO payInfoVO) { + return this.payStrategy.unifiedorder(payInfoVO); + } + + /** + * 根据支付策略,调用不同查询订单支持状态 + * + * @param payInfoVO + * @return + */ + public String executeQueryPaySuccess(PayInfoVO payInfoVO) { + return this.payStrategy.queryPaySuccess(payInfoVO); + } + +} + + diff --git a/xdclass-order-service/src/main/java/net/jieyuu/component/WechatPayStrategy.java b/xdclass-order-service/src/main/java/net/jieyuu/component/WechatPayStrategy.java new file mode 100644 index 0000000..90d11ce --- /dev/null +++ b/xdclass-order-service/src/main/java/net/jieyuu/component/WechatPayStrategy.java @@ -0,0 +1,30 @@ +package net.jieyuu.component; + +import lombok.extern.slf4j.Slf4j; +import net.jieyuu.config.PayUrlConfig; +import net.jieyuu.vo.PayInfoVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +public class WechatPayStrategy implements PayStrategy { + + @Autowired + private PayUrlConfig payUrlConfig; + + @Override + public String unifiedorder(PayInfoVO payInfoVO) { + return PayStrategy.super.unifiedorder(payInfoVO); + } + + @Override + public String refund(PayInfoVO payInfoVO) { + return PayStrategy.super.refund(payInfoVO); + } + + @Override + public String queryPaySuccess(PayInfoVO payInfoVO) { + return PayStrategy.super.queryPaySuccess(payInfoVO); + } +} diff --git a/xdclass-order-service/src/main/java/net/jieyuu/service/impl/ProductOrderServiceImpl.java b/xdclass-order-service/src/main/java/net/jieyuu/service/impl/ProductOrderServiceImpl.java index 8bcc926..c1601e4 100644 --- a/xdclass-order-service/src/main/java/net/jieyuu/service/impl/ProductOrderServiceImpl.java +++ b/xdclass-order-service/src/main/java/net/jieyuu/service/impl/ProductOrderServiceImpl.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import lombok.extern.slf4j.Slf4j; +import net.jieyuu.component.PayFactory; import net.jieyuu.config.RabbitMQConfig; import net.jieyuu.enums.*; import net.jieyuu.exception.BizException; @@ -23,6 +24,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import net.jieyuu.utils.CommonUtil; import net.jieyuu.utils.JsonData; import net.jieyuu.vo.CouponRecordVO; +import net.jieyuu.vo.OrderItemVO; import net.jieyuu.vo.ProductOrderAddressVO; import org.apache.commons.lang3.StringUtils; import org.springframework.amqp.rabbit.core.RabbitTemplate; @@ -125,6 +127,7 @@ public class ProductOrderServiceImpl extends ServiceImpl