[230319] 결제완료 command 개발

- 각 폴더 구조 생성
- 도메인 Event 구조 생성
- kafka message sender 테스트까지 확인
- 결제 완료 예시 application service
This commit is contained in:
appleg
2022-03-19 22:13:36 +09:00
parent 202d9c5d0d
commit 28c84888d1
33 changed files with 578 additions and 35 deletions

View File

@@ -12,7 +12,7 @@ services:
- "3306:3306"
volumes:
- ./db:/var/lib/mysql
# platform: linux/amd64 # for m1 mac
# platform: linux/amd64 # for m1 mac
mysql-query:
container_name: query-mysql
image: mysql:8.0.21
@@ -22,10 +22,10 @@ services:
- MYSQL_ROOT_PASSWORD=payment
- TZ=UTC
ports:
- "3307:3307"
- "3307:3306"
volumes:
- ./db:/var/lib/mysql
# platform: linux/amd64 # for m1 mac
- ./db2:/var/lib/mysql
# platform: linux/amd64 # for m1 mac
zookeeper:
container_name: payment-zookeeper
image: wurstmeister/zookeeper

View File

@@ -1,14 +0,0 @@
package com.example;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class Listener {
@KafkaListener(topics = "topic", groupId = "group1")
public void consume(String message) {
System.out.println("receive message : " + message);
}
}

View File

@@ -0,0 +1,20 @@
package com.example.common;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class IdResponse<T> {
private T id;
private IdResponse(T id) {
this.id = id;
}
public static <T> IdResponse<T> of(T id) {
return new IdResponse<>(id);
}
}

View File

@@ -0,0 +1,9 @@
package com.example.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@Configuration
@EnableJpaAuditing
public class JpaConfig {
}

View File

@@ -0,0 +1,6 @@
package com.example.event;
public interface DomainEvent {
String topic();
}

View File

@@ -0,0 +1,6 @@
package com.example.event;
public interface EventPublisher {
void publish(DomainEvent event);
}

View File

@@ -0,0 +1,19 @@
package com.example.event;
import lombok.RequiredArgsConstructor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class KafkaEventPublisher implements EventPublisher {
private final KafkaTemplate<String, DomainEvent> kafkaProducer;
@Override
public void publish(DomainEvent event) {
String topic = event.topic();
kafkaProducer.send(new ProducerRecord<>(topic, event));
}
}

View File

@@ -0,0 +1,8 @@
package com.example.payment.application;
import com.example.payment.infra.http.request.PaymentCompleteRequest;
public interface PaymentService {
String completePayment(PaymentCompleteRequest request);
}

View File

@@ -0,0 +1,40 @@
package com.example.payment.application;
import com.example.user.domain.Cash;
import com.example.event.EventPublisher;
import com.example.payment.domain.Payment;
import com.example.payment.domain.PaymentRepository;
import com.example.payment.domain.event.PaymentCreated;
import com.example.payment.infra.http.request.PaymentCompleteRequest;
import com.example.user.domain.User;
import com.example.user.domain.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
public class PaymentServiceImpl implements PaymentService {
private final EventPublisher eventPublisher;
private final UserRepository userRepository;
private final PaymentRepository paymentRepository;
@Override
@Transactional
public String completePayment(PaymentCompleteRequest request) {
// 1. 유저의 잔액 추가 / 캐시 생성
User user = userRepository.getByIdOrDefault(request.getUserId());
user.increaseBalance(request.getAmount());
String cashId = user.addNewCash(Cash.of(request.getAmount(), user));
userRepository.save(user);
// 2. 결제 데이터 생성
String paymentId = paymentRepository.save(Payment.of(request.getPgId(), request.getPayBy(), user));
// 3. 이벤트 발행
eventPublisher.publish(new PaymentCreated(request, user.getUserId(), paymentId, cashId));
return paymentId;
}
}

View File

@@ -0,0 +1,47 @@
package com.example.payment.domain;
import com.example.user.domain.User;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Payment {
@Id
private String id;
private String pgId;
private String payBy;
@ManyToOne
@JoinColumn
private User user;
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
public String getId() {
return id;
}
public static Payment of(String pgId, String payBy, User user) {
return new Payment(UUID.fromString("payment").toString(), pgId, payBy, user, null, null);
}
}

View File

@@ -0,0 +1,10 @@
package com.example.payment.domain;
import java.util.Optional;
public interface PaymentRepository {
String save(Payment purchase);
Optional<Payment> getById(String id);
}

View File

@@ -0,0 +1,46 @@
package com.example.payment.domain.event;
import com.example.event.DomainEvent;
import com.example.payment.infra.http.request.PaymentCompleteRequest;
import java.io.Serializable;
import java.math.BigDecimal;
import lombok.Getter;
@Getter
public class PaymentCreated implements DomainEvent, Serializable {
private final String userId;
private final String paymentId;
private final String cashId;
private final BigDecimal amount;
private final String payBy;
private final String pgId;
@Override
public String topic() {
return "PaymentCreated";
}
public PaymentCreated(PaymentCompleteRequest request, String userId, String paymentId, String cashId) {
this.userId = userId;
this.paymentId = paymentId;
this.cashId = cashId;
this.amount = request.getAmount();
this.payBy = request.getPayBy();
this.pgId = request.getPgId();
}
public PaymentCreated(String userId, String paymentId, String cashId, BigDecimal amount, String payBy, String pgId) {
this.userId = userId;
this.paymentId = paymentId;
this.cashId = cashId;
this.amount = amount;
this.payBy = payBy;
this.pgId = pgId;
}
}

View File

@@ -0,0 +1,23 @@
package com.example.payment.infra.http;
import com.example.common.IdResponse;
import com.example.payment.application.PaymentService;
import com.example.payment.infra.http.request.PaymentCompleteRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(("/payments"))
@RequiredArgsConstructor
public class PaymentController {
private final PaymentService paymentService;
@PostMapping("/complete")
public IdResponse<String> paymentComplete(@RequestBody PaymentCompleteRequest request) {
return IdResponse.of(paymentService.completePayment(request));
}
}

View File

@@ -0,0 +1,16 @@
package com.example.payment.infra.http.request;
import java.math.BigDecimal;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class PaymentCompleteRequest {
private String userId;
private BigDecimal amount;
private String payBy;
private String pgId;
}

View File

@@ -0,0 +1,7 @@
package com.example.payment.infra.persistence;
import com.example.payment.domain.Payment;
import org.springframework.data.repository.CrudRepository;
public interface PaymentJpaRepository extends CrudRepository<Payment, String> {
}

View File

@@ -0,0 +1,24 @@
package com.example.payment.infra.persistence;
import com.example.payment.domain.Payment;
import com.example.payment.domain.PaymentRepository;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
@Repository
@RequiredArgsConstructor
public class PaymentRepositoryImpl implements PaymentRepository {
private final PaymentJpaRepository jpaRepository;
@Override
public String save(Payment payment) {
return jpaRepository.save(payment).getId();
}
@Override
public Optional<Payment> getById(String id) {
return jpaRepository.findById(id);
}
}

View File

@@ -0,0 +1,34 @@
package com.example.purchase.domain;
import com.example.user.domain.User;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Purchase {
@Id
private String id;
@ManyToOne
@JoinColumn
private User user;
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
public String getId() {
return id;
}
}

View File

@@ -0,0 +1,10 @@
package com.example.purchase.domain;
import java.util.Optional;
public interface PurchaseRepository {
String save(Purchase purchase);
Optional<Purchase> getById(String id);
}

View File

@@ -0,0 +1,18 @@
package com.example.purchase.infra.http;
import com.example.common.IdResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/purchases")
@RequiredArgsConstructor
public class PurchaseController {
@PostMapping()
public IdResponse<Long> purchase() {
return IdResponse.of(1L);
}
}

View File

@@ -0,0 +1,7 @@
package com.example.purchase.infra.persistence;
import com.example.purchase.domain.Purchase;
import org.springframework.data.repository.CrudRepository;
public interface PurchaseJpaRepository extends CrudRepository<Purchase, String> {
}

View File

@@ -0,0 +1,24 @@
package com.example.purchase.infra.persistence;
import com.example.purchase.domain.Purchase;
import com.example.purchase.domain.PurchaseRepository;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
@Repository
@RequiredArgsConstructor
public class PurchaseRepositoryImpl implements PurchaseRepository {
private final PurchaseJpaRepository jpaRepository;
@Override
public String save(Purchase purchase) {
return jpaRepository.save(purchase).getId();
}
@Override
public Optional<Purchase> getById(String id) {
return jpaRepository.findById(id);
}
}

View File

@@ -0,0 +1,43 @@
package com.example.user.domain;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Cash {
@Id
private String id;
private BigDecimal amount;
@ManyToOne
@JoinColumn
private User user;
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
public String getId() {
return id;
}
public static Cash of(BigDecimal amount, User user) {
return new Cash(UUID.fromString("cash").toString(), amount, user, null, null);
}
}

View File

@@ -0,0 +1,43 @@
package com.example.user.domain;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
@Entity
public class User {
@Id
private String userId;
private BigDecimal balance;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Cash> cashes = new ArrayList<>();
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
public String getUserId() {
return userId;
}
public void increaseBalance(BigDecimal amount) {
balance = balance.add(amount);
}
public String addNewCash(Cash cash) {
this.cashes.add(cash);
return cash.getId();
}
}

View File

@@ -0,0 +1,8 @@
package com.example.user.domain;
public interface UserRepository {
User getByIdOrDefault(String id);
String save(User user);
}

View File

@@ -0,0 +1,7 @@
package com.example.user.infra.persistence;
import com.example.user.domain.User;
import org.springframework.data.repository.CrudRepository;
public interface UserJpaRepositroy extends CrudRepository<User, String> {
}

View File

@@ -0,0 +1,24 @@
package com.example.user.infra.persistence;
import com.example.user.domain.User;
import com.example.user.domain.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
@Repository
@RequiredArgsConstructor
public class UserRepositoryImpl implements UserRepository {
private final UserJpaRepositroy jpaRepository;
@Override
public String save(User user) {
return jpaRepository.save(user).getUserId();
}
@Override
public User getByIdOrDefault(String id) {
return null;
}
}

View File

@@ -12,3 +12,7 @@ spring:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL8Dialect
kafka:
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer

View File

@@ -1,5 +1,7 @@
package com.example.payment;
import com.example.payment.domain.event.PaymentCreated;
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@@ -7,12 +9,14 @@ import org.springframework.kafka.core.KafkaTemplate;
@SpringBootTest
class PaymentApplicationTests {
@Autowired
KafkaTemplate<String, String> template;
KafkaTemplate<String, PaymentCreated> template;
@Test
void contextLoads() {
template.send("topic", "hello this is new topic2!!!!!!");
PaymentCreated paymentCreated = new PaymentCreated("user", "payment", "cash", BigDecimal.ONE, "payBy", "pgId");
template.send("PaymentCreated", paymentCreated);
}
}

View File

@@ -0,0 +1,14 @@
package com.example;
import com.example.payment.domain.event.PaymentCreated;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
@Service
public class PaymentCreatedListener {
@KafkaListener(topics = "PaymentCreated", groupId = "group1")
public void consume(PaymentCreated message) {
System.out.println("receive message : " + message.toString());
}
}

View File

@@ -1,4 +1,4 @@
package java.com.example;
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

@@ -0,0 +1,27 @@
package com.example.payment.domain.event;
import java.io.Serializable;
import java.math.BigDecimal;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class PaymentCreated implements Serializable {
private String userId;
private String paymentId;
private String cashId;
private BigDecimal amount;
private String payBy;
private String pgId;
}

View File

@@ -0,0 +1,23 @@
server:
port: 8082
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3307/payment?serverTimezone=Asia/Seoul&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: payment
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL8Dialect
kafka:
consumer:
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
properties:
spring:
json:
trusted:
packages: '*'

View File

@@ -1,14 +0,0 @@
server:
port: 8082
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3307/payment?serverTimezone=Asia/Seoul&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: payment
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL8Dialect