Skip to content

Commit

Permalink
Merge pull request #15 from lotteon2/LF1-910-batch
Browse files Browse the repository at this point in the history
✨ 배치 정기결제 로직 및 Test Code
  • Loading branch information
qwerty1434 authored Jan 3, 2024
2 parents bce7758 + f91d25d commit 9c3e014
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.kafka:spring-kafka'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'io.github.lotteon-maven:blooming-blooms-utils:202401012350'
implementation 'io.github.lotteon-maven:blooming-blooms-utils:202401030507'
runtimeOnly 'com.h2database:h2'
implementation 'mysql:mysql-connector-java:8.0.33'
testImplementation 'org.mock-server:mockserver-netty:5.11.2' // 사용 중인 MockServer 버전
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/kr/bb/payment/kafka/KafkaConsumer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kr.bb.payment.kafka;

import bloomingblooms.domain.batch.SubscriptionBatchDto;
import java.util.List;
import bloomingblooms.domain.batch.SubscriptionBatchDtoList;
import com.fasterxml.jackson.databind.ObjectMapper;
import kr.bb.payment.service.KakaopayService;
import lombok.RequiredArgsConstructor;
import org.springframework.kafka.annotation.KafkaListener;
Expand All @@ -11,9 +11,10 @@
@RequiredArgsConstructor
public class KafkaConsumer<T> {
private final KakaopayService kakaopayService;
private ObjectMapper objectMapper;

@KafkaListener(topics = "subscription-batch", groupId = "payment-subscription")
public void subscriptionBatch(List<SubscriptionBatchDto> subscriptionBatchDtoList) {
public void subscriptionBatch(SubscriptionBatchDtoList subscriptionBatchDtoList) {
kakaopayService.renewSubscription(subscriptionBatchDtoList);
}
}
12 changes: 9 additions & 3 deletions src/main/java/kr/bb/payment/service/KakaopayService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.bb.payment.service;

import bloomingblooms.domain.batch.SubscriptionBatchDto;
import bloomingblooms.domain.batch.SubscriptionBatchDtoList;
import bloomingblooms.domain.payment.KakaopayApproveRequestDto;
import bloomingblooms.domain.payment.KakaopayReadyRequestDto;
import bloomingblooms.domain.payment.KakaopayReadyResponseDto;
Expand Down Expand Up @@ -86,18 +87,20 @@ public LocalDateTime kakaoPayApprove(KakaopayApproveRequestDto requestDto) {
return paymentService.saveSinglePaymentInfo(requestDto, responseDto);
}

public void renewSubscription(List<SubscriptionBatchDto> subscriptionBatchDtoList) {
public void renewSubscription(SubscriptionBatchDtoList subscriptionBatchDtoList) {
Map<Long, Long> oldDeliveryIdsMap = new HashMap<>(); // <결제기록id, old 배송id>

for(SubscriptionBatchDto subscriptionBatchDto : subscriptionBatchDtoList){
for(SubscriptionBatchDto subscriptionBatchDto : subscriptionBatchDtoList.getSubscriptionBatchDtoList()){
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();

parameters.add("cid", subscriptionBatchDto.getCid());
parameters.add("sid", subscriptionBatchDto.getSid());
parameters.add("partner_order_id", String.valueOf(subscriptionBatchDto.getPartnerOrderId()));
parameters.add("partner_user_id", String.valueOf(subscriptionBatchDto.getPartnerUserId()));
parameters.add("quantity", String.valueOf(subscriptionBatchDto.getQuantity()));
parameters.add("totalAmount", String.valueOf(subscriptionBatchDto.getTotalAmount()));
parameters.add("total_amount", String.valueOf(subscriptionBatchDto.getTotalAmount()));
parameters.add("tax_free_amount", String.valueOf(0));


HttpEntity<MultiValueMap<String, String>> requestEntity =
new HttpEntity<>(parameters, this.getHeaders());
Expand All @@ -114,6 +117,9 @@ public void renewSubscription(List<SubscriptionBatchDto> subscriptionBatchDtoLis
List<Long> newDeliveryIdsList = deliveryServiceClient.createDeliveryForSubscription(oldDeliveryIdsList).getData();
paymentService.saveDeliveryIds(oldDeliveryIdsMap, newDeliveryIdsList);

// SQS 로 구매자에게 주문 발생 알림


}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.mockito.Mockito.when;

import bloomingblooms.domain.batch.SubscriptionBatchDto;
import bloomingblooms.domain.batch.SubscriptionBatchDtoList;
import bloomingblooms.response.CommonResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
Expand Down Expand Up @@ -45,7 +46,6 @@ public class KakaopaySubscriptionTest {

@BeforeEach
void setUp() throws Exception {
// kakaopayService = new KakaopayService(paymentService, restTemplate, deliveryServiceClient);
mockServer = MockRestServiceServer.createServer(restTemplate);

ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -64,7 +64,10 @@ void setUp() throws Exception {
public void batchSubscription() {
// given
SubscriptionBatchDto subscriptionBatchDto = createSubscriptionBatchDto();
List<SubscriptionBatchDto> subscriptionBatchDtoList = List.of(subscriptionBatchDto);
SubscriptionBatchDtoList subscriptionBatchDtoList =
SubscriptionBatchDtoList.builder()
.subscriptionBatchDtoList(List.of(subscriptionBatchDto))
.build();
SubscriptionRecords subscriptionRecords = createSubscriptionRecords();
Subscription subscription = createSubscription();
subscriptionRepository.save(subscription);
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ spring:
url: jdbc:h2:mem:payment-service;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL
jpa:
hibernate:
ddl-auto: create
ddl-auto: update
generate-ddl: true
defer-datasource-initialization: true
properties:
hibernate:
format_sql: true
show_sql: true
kafka:
producer:
bootstrap-servers: localhost:9092
Expand All @@ -35,7 +36,6 @@ spring:
json:
trusted:
packages: "*"

group-id: payment-service

kakao:
Expand Down

0 comments on commit 9c3e014

Please sign in to comment.