1. vo
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PayVO {
/**
*支付类型: eg sendwyre
*/
private String payType;
/**
* 端类型,App/h5/pc
*/
private String clientType;
}
2. 支付策略
/**
* 支付策略 (在这里抽离公共的支付功能)
* @author
* @date 2022/5/25 21:26
*/
public interface PayStrategy {
/**
* 统一下单接口
* @param payInfoVO
* @return
*/
String unifiedOrder(PayVO payVO);
/**
* 退款接口
* @param payInfoVO
* @return
*/
default String refund(PayVO payVO){ return ""; }
}
3. 支付工厂
/**
* 支付工厂
* @author:
* @date: 2022/09/14 21:26
*/
@Slf4j
@Component
public class PayFactory {
@Autowired
private CheckOutPayStrategy checkOutPayStrategy;
@Autowired
private SendWyrePayStrategy sendWyrePayStrategy;
/**
* 创建支付,简单工厂模式
* @param payVO
* @return
*/
public String pay(PayVO payVO){
String payType =payVO.getPayType();
if (PayType.sendwyre.getName() == payType) {
//支付宝支付
PayStrategyContext payStrategyContext = new PayStrategyContext(sendWyrePayStrategy);
return payStrategyContext.executeUnifiedOrder(payInfoVO);
} else if(PayType.checkout.name().equals(payType)){
// checkout
PayStrategyContext payStrategyContext = new PayStrategyContext(checkOutPayStrategy);
return payStrategyContext.executeUnifiedOrder(payInfoVO);
}
return "";
}
/**
* 退款接口
* @param payVO
* @return
*/
public String refund(PayVO payVO){
String payType = payVO.getPayType();
return "";
}
}
4. 支付策略上线文
import io.pay.vo.PayVO;
/**
* 支付策略
* @author:
* @date: 2022/09/14 21:26
*/
public class PayStrategyContext {
private PayStrategy payStrategy;
public PayStrategyContext(PayStrategy payStrategy){
this.payStrategy = payStrategy;
}
/**
* 根据策略对象,执行不同的下单接口
* @return
*/
public String executeUnifiedOrder(PayInfoVO payInfoVO){
return payStrategy.unifiedOrder(payInfoVO);
}
/**
* 根据策略对象,执行不同的退款接口
* @return
*/
public String executeRefund(PayInfoVO payInfoVO){
return payStrategy.refund(payInfoVO);
}
}
5. checkout 支付策略
@Service
@Slf4j
public class CheckOutPayStrategy implements PayStrategy {
@Override
public String unifiedOrder(PayVO payVO) {
return null;
}
@Override
public String refund(PayVO payVO) {
return null;
}
}
6. sendWyre 支付策略
@Service标签:基于,return,String,改造,PayVO,模式,payVO,payInfoVO,public From: https://blog.51cto.com/u_4981212/5790933
@Slf4j
public class SendWyrePayStrategy implements PayStrategy {
@Override
public String unifiedOrder(PayVO payVO) {
return "";
}
@Override
public String refund(PayVO payVO) {
return null;
}
}