-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from autumn-radiata/feat/add-payment-cancel
[#112] feat(payment): 결제 취소 보상 트랜잭션 리스너 추가
- Loading branch information
Showing
11 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ice/payment/payment-api/src/main/java/radiata/service/payment/api/config/KafkaConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package radiata.service.payment.api.config; | ||
|
||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.annotation.EnableKafka; | ||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; | ||
import org.springframework.kafka.core.ConsumerFactory; | ||
import org.springframework.kafka.core.DefaultKafkaConsumerFactory; | ||
import org.springframework.kafka.core.DefaultKafkaProducerFactory; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.kafka.core.ProducerFactory; | ||
import org.springframework.kafka.support.serializer.JsonSerializer; | ||
|
||
@EnableKafka | ||
@Configuration | ||
public class KafkaConfig { | ||
|
||
@Value("${spring.kafka.bootstrap-servers}") | ||
private String bootstrapServers; | ||
|
||
@Bean | ||
public ProducerFactory<String, String> producerFactory() { | ||
Map<String, Object> configProds = new HashMap<>(); | ||
configProds.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
configProds.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | ||
configProds.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); | ||
return new DefaultKafkaProducerFactory<>(configProds); | ||
} | ||
|
||
@Bean | ||
public ConsumerFactory<String, String> consumerFactory() { | ||
HashMap<String, Object> config = new HashMap<>(); | ||
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
|
||
return new DefaultKafkaConsumerFactory<>(config); | ||
} | ||
|
||
@Bean | ||
public KafkaTemplate<String, String> kafkaTemplate() { | ||
return new KafkaTemplate<>(producerFactory()); | ||
} | ||
|
||
@Bean | ||
public ConcurrentKafkaListenerContainerFactory<String, String> KafkaListenerContainerFactory() { | ||
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>(); | ||
factory.setConsumerFactory(consumerFactory()); | ||
return factory; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...yment-api/src/main/java/radiata/service/payment/api/listener/PaymentRollbackListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package radiata.service.payment.api.listener; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.stereotype.Component; | ||
import radiata.common.domain.payment.dto.request.PaymentCancelRequestDto; | ||
import radiata.service.payment.core.service.PaymentRollbackService; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class PaymentRollbackListener { | ||
|
||
private final ObjectMapper objectMapper; | ||
private final PaymentRollbackService paymentRollbackService; | ||
|
||
@KafkaListener(topics = "payment.cancel") | ||
public void cancelPayment(String message) throws JsonProcessingException { | ||
PaymentCancelRequestDto request = objectMapper.readValue(message, PaymentCancelRequestDto.class); | ||
paymentRollbackService.rollbackPayment(request.paymentId()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...-core/src/main/java/radiata/service/payment/core/domain/repository/PaymentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package radiata.service.payment.core.domain.repository; | ||
|
||
import java.util.Optional; | ||
import org.springframework.stereotype.Repository; | ||
import radiata.service.payment.core.domain.model.entity.Payment; | ||
|
||
@Repository | ||
public interface PaymentRepository { | ||
|
||
Payment save(Payment payment); | ||
|
||
Optional<Payment> findById(String id); | ||
} |
22 changes: 22 additions & 0 deletions
22
...payment-core/src/main/java/radiata/service/payment/core/implementation/PaymentReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package radiata.service.payment.core.implementation; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import radiata.common.annotation.Implementation; | ||
import radiata.common.exception.BusinessException; | ||
import radiata.common.message.ExceptionMessage; | ||
import radiata.service.payment.core.domain.model.entity.Payment; | ||
import radiata.service.payment.core.domain.repository.PaymentRepository; | ||
|
||
@Slf4j | ||
@Implementation | ||
@RequiredArgsConstructor | ||
public class PaymentReader { | ||
|
||
private final PaymentRepository paymentRepository; | ||
|
||
public Payment getPaymentById(String paymentId) { | ||
return paymentRepository.findById(paymentId) | ||
.orElseThrow(() -> new BusinessException(ExceptionMessage.PAYMENT_NOT_FOUND)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...yment-core/src/main/java/radiata/service/payment/core/service/PaymentRollbackService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package radiata.service.payment.core.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import radiata.common.domain.payment.constant.PaymentType; | ||
import radiata.service.payment.core.domain.model.entity.PayUser; | ||
import radiata.service.payment.core.domain.model.entity.Payment; | ||
import radiata.service.payment.core.implementation.PayUserReader; | ||
import radiata.service.payment.core.implementation.PaymentReader; | ||
import radiata.service.payment.core.implementation.PaymentRequester; | ||
import radiata.service.payment.core.implementation.PaymentSaver; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class PaymentRollbackService { | ||
|
||
private final PaymentSaver paymentSaver; | ||
private final PayUserReader payUserReader; | ||
private final PaymentReader paymentReader; | ||
private final PaymentRequester paymentRequester; | ||
|
||
@Transactional | ||
public void rollbackPayment(String paymentId) { | ||
// 결제 조회 | ||
Payment payment = paymentReader.getPaymentById(paymentId); | ||
// 결제 취소 | ||
if (payment.getType().equals(PaymentType.EASY_PAY)) { | ||
// 간편결제 유저 조회 | ||
PayUser payUser = payUserReader.getPayUserByUserId(payment.getUserId()); | ||
// 간편결제 취소 | ||
paymentRequester.cancelEasyPay(payment, payUser); | ||
} else if (payment.getType().equals(PaymentType.TOSS_PAYMENTS)) { | ||
// 토스 결제 취소 | ||
paymentRequester.cancelTossPayment(payment); | ||
} | ||
|
||
// 결제 저장 | ||
paymentSaver.save(payment); | ||
} | ||
} |