Hello World

This commit is contained in:
Alawn
2023-05-16 15:15:00 +08:00
parent 453ec98220
commit fb1e881d7e
12 changed files with 65 additions and 36 deletions
@@ -5,7 +5,8 @@ import com.murong.ecp.payment.api.Payment;
import com.murong.ecp.payment.api.PlaceOrderRequest;
import com.murong.ecp.payment.domain.ConsumeOrder;
import com.murong.ecp.payment.domain.PaymentPart;
import com.murong.ecp.payment.processor.order.OrderRiskProcessor;
import com.murong.ecp.payment.processor.flow.OrderNotificationProcessor;
import com.murong.ecp.payment.processor.flow.OrderRiskProcessor;
import com.murong.ecp.payment.processor.payment.BankCardPaymentPartProcessor;
import com.murong.ecp.payment.processor.payment.WalletPaymentPartProcessor;
import com.murong.ecp.payment.service.ConsumeOrderRepository;
@@ -28,6 +29,10 @@ public class OrderController {
@Autowired
OrderRiskProcessor orderRiskProcessor;
@Autowired
OrderNotificationProcessor orderNotificationProcessor;
@Autowired
ConsumeOrderRepository consumeOrderRepository;
@@ -81,10 +86,15 @@ public class OrderController {
.build());
}
/** configure chain */
/**
* configure chain
*
* Order Risk -> Wallet Payment -> BankCard Payment -> Order Notification
*/
orderRiskProcessor.setNext(walletPaymentPartProcessor);
walletPaymentPartProcessor.setNext(bankCardPaymentPartProcessor);
bankCardPaymentPartProcessor.setNext(null);
bankCardPaymentPartProcessor.setNext(orderNotificationProcessor);
orderNotificationProcessor.setNext(null);
/** start chain */
orderRiskProcessor.entry(order);
@@ -1,11 +1,10 @@
package com.murong.ecp.payment.domain;
import com.murong.ecp.payment.api.PayOrderRequest;
import com.murong.ecp.payment.api.PlaceOrderRequest;
import com.murong.ecp.payment.enums.OrderStatus;
import com.murong.ecp.payment.enums.PaymentMethod;
import com.murong.ecp.payment.enums.RiskStatus;
import com.murong.ecp.payment.exception.DuplicatePaymentMethodException;
import com.murong.ecp.payment.exception.DuplicatedPaymentMethodException;
import lombok.ToString;
import java.util.ArrayList;
@@ -19,9 +18,12 @@ import java.util.UUID;
@ToString(callSuper = true)
public class ConsumeOrder extends Order {
private List<PaymentPart> breakdown = new ArrayList<>();;
private List<Fee> orderFees = new ArrayList<>();
private List<Tax> orderTaxes = new ArrayList<>();
private RiskStatus riskStatus = RiskStatus.UNTREATED;
private List<Fee> orderFees = new ArrayList<>();
private List<Tax> orderTaxes = new ArrayList<>();
private RiskStatus riskStatus = RiskStatus.UNTREATED;
private boolean notified = false;
private OrderStatus orderStatus = OrderStatus.UNTREATED;
public boolean containsPaymentMethod(PaymentMethod paymentMethod){
return breakdown.stream().anyMatch(part -> part.getPaymentMethod() == paymentMethod);
@@ -65,7 +67,7 @@ public class ConsumeOrder extends Order {
public void addPaymentItem(PaymentPart item) {
if (breakdown.stream().anyMatch(b -> b.getPaymentMethod() == item.getPaymentMethod())) {
throw new DuplicatePaymentMethodException("Payment method already exists in breakdown.");
throw new DuplicatedPaymentMethodException("Payment method already exists in breakdown.");
}
breakdown.add(item);
}
@@ -121,5 +123,13 @@ public class ConsumeOrder extends Order {
order.setMerchantId(request.getMerchantId());
return order;
}
public boolean isNotified(){
return this.notified;
}
public boolean isFinished(){
return orderStatus.equals(OrderStatus.FAILURE) || orderStatus.equals(OrderStatus.SUCCESS) ? true : false;
}
}
@@ -18,6 +18,5 @@ public class Order {
protected Date orderTime;
protected String refId;
protected String requestId;
protected OrderStatus orderStatus = OrderStatus.UNTREATED;
}
@@ -3,9 +3,10 @@ package com.murong.ecp.payment.enums;
public enum OrderStatus {
UNTREATED("U", "Untreated"),
INITIALIZED("I", "Initialized"),
PROCESSING("P", "Processing"),
SUCCESS("S", "Success"),
FAILURE("F", "Failure"),
PROCESSING("P", "Processing");
FAILURE("F", "Failure");
private String code;
private String description;
@@ -1,7 +0,0 @@
package com.murong.ecp.payment.exception;
public class DuplicatePaymentMethodException extends RuntimeException{
public DuplicatePaymentMethodException(String s) {
super(s);
}
}
@@ -0,0 +1,14 @@
package com.murong.ecp.payment.exception;
import com.murong.ecp.payment.enums.ResponseMessage;
public class DuplicatedPaymentMethodException extends DefaultException {
public DuplicatedPaymentMethodException() {
super(ResponseMessage.DUPLICATED_PAYMENT_METHOD);
}
public DuplicatedPaymentMethodException(String message) {
super(ResponseMessage.DUPLICATED_PAYMENT_METHOD);
super.message = message;
}
}
@@ -3,5 +3,5 @@ package com.murong.ecp.payment.processor;
public interface ProcessorChain<T> {
void entry(T request);
void setNext(ProcessorChain next);
boolean precheck(T request);
boolean checkParams(T request);
}
@@ -1,4 +1,4 @@
package com.murong.ecp.payment.processor.order;
package com.murong.ecp.payment.processor.flow;
import com.murong.ecp.payment.domain.ConsumeOrder;
import com.murong.ecp.payment.processor.ProcessorChain;
@@ -12,7 +12,7 @@ public abstract class OrderDefaultProcessor implements ProcessorChain<ConsumeOrd
log.info("DefaultProcessor.entry");
if (next != null){
log.info("DefaultProcessor.precheck");
if(!precheck(order)) return;
if(!checkParams(order)) return;
/** process */
}
if (next != null){
@@ -22,9 +22,7 @@ public abstract class OrderDefaultProcessor implements ProcessorChain<ConsumeOrd
/** the end */
}
protected void orderHandling(ConsumeOrder order){
/** calculate fee */
}
protected abstract void process(ConsumeOrder order);
public void onSuccess(ConsumeOrder order){
}
@@ -1,4 +1,4 @@
package com.murong.ecp.payment.processor.order;
package com.murong.ecp.payment.processor.flow;
import com.murong.ecp.payment.domain.ConsumeOrder;
import lombok.extern.slf4j.Slf4j;
@@ -8,10 +8,14 @@ import org.springframework.stereotype.Service;
@Service
public class OrderRiskProcessor extends OrderDefaultProcessor {
@Override
public boolean precheck(ConsumeOrder order) {
public boolean checkParams(ConsumeOrder order) {
log.info("RiskProcessor.precheck");
return !order.orderCreated() && !order.riskUntreated() ? true : false;
}
@Override
protected void process(ConsumeOrder order) {
/** risk processes */
}
}
@@ -17,13 +17,13 @@ public class BankCardPaymentPartProcessor extends PaymentPartDefaultProcessor {
}
@Override
public void orderHandling(ConsumeOrder order){
public void process(ConsumeOrder order){
log.info("BankCardPaymentPartProcessor.orderHandling");
/** calculate fee */
}
@Override
public boolean precheck(ConsumeOrder order){
public boolean checkParams(ConsumeOrder order){
log.info("BankCardPaymentPartProcessor.precheck");
// if (order.riskPassed() && order. )
@@ -16,12 +16,12 @@ public abstract class PaymentPartDefaultProcessor implements ProcessorChain<Cons
}
public void entry(ConsumeOrder order) {
log.info("PaymentPartProcessor.entry:{}",order);
if(!precheck(order)) return;
if(!checkParams(order)) return;
for (PaymentMethod paymentMethod:this.paymentMethods ) {
if (order.containsPaymentMethod(paymentMethod)){
/** payment process */
orderHandling(order);
process(order);
}
}
if (next != null){
@@ -32,7 +32,7 @@ public abstract class PaymentPartDefaultProcessor implements ProcessorChain<Cons
/** the end */
}
protected void orderHandling(ConsumeOrder order){
protected void process(ConsumeOrder order){
log.info("PaymentPartProcessor.orderHandling");
@@ -59,7 +59,7 @@ public abstract class PaymentPartDefaultProcessor implements ProcessorChain<Cons
* @return
*/
@Override
public boolean precheck(ConsumeOrder order){
public boolean checkParams(ConsumeOrder order){
log.info("PaymentPartProcessor.precheck");
return order.orderCreated() && order.riskPassed() ? true : false;
}
@@ -16,13 +16,13 @@ public class WalletPaymentPartProcessor extends PaymentPartDefaultProcessor {
}
@Override
public void orderHandling(ConsumeOrder order){
public void process(ConsumeOrder order){
log.info("WalletPaymentPartProcessor.orderHandling");
/** calculate fee */
}
@Override
public boolean precheck(ConsumeOrder order){
public boolean checkParams(ConsumeOrder order){
log.info("WalletPaymentPartProcessor.precheck");
return false;
}