Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 입출금 계좌 memo 구현 #146

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public class AccountService {

private final AccountRepository accountRepository;
private final MemberRepository memberRepository;
private final PetRepository petRepository;
private final AccountCodeRepository accountCodeRepository;
private final RestTemplate restTemplate;
private final ObjectMapper objectMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.fav.daengnyang.domain.targetDetail.repository.TargetDetailRepository;
import com.fav.daengnyang.global.exception.CustomException;
import com.fav.daengnyang.global.exception.ErrorCode;
import com.fav.daengnyang.global.transaction.service.TransactionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -36,6 +37,7 @@ public class TargetService {
private final TargetDetailRepository targetDetailRepository;
private final AccountRepository accountRepository;
private final PetRepository petRepository;
private final TransactionService transactionService;
private final Random random = new Random();

// 타겟 추천 메소드
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import com.fav.daengnyang.domain.target.service.dto.request.TransferHeaderRequest;
import com.fav.daengnyang.domain.targetDetail.entity.TargetDetail;
import com.fav.daengnyang.domain.targetDetail.repository.TargetDetailRepository;
import com.fav.daengnyang.global.transaction.service.TransactionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand All @@ -24,20 +26,23 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class TargetTransferService {

private final TargetRepository targetRepository;
private final TargetDetailRepository targetDetailRepository;
private final MemberRepository memberRepository;
private final RestTemplate restTemplate;
private final ObjectMapper objectMapper;
private final TransactionService transactionService;

@Value("${api.key}")
private String apiKey;
Expand All @@ -58,6 +63,7 @@ public void transferToTarget(Long memberId, String userKey, Long targetId, Targe
// 금융 API 호출하여 이체 수행
callTransferApi(depositAccountNo, withdrawalAccountNo, amount, userKey);


// 타겟의 current_amount 업데이트 및 목표 달성 여부 확인
int updatedAmount = target.getCurrentAmount() + amount;
target.setCurrentAmount(updatedAmount);
Expand Down Expand Up @@ -101,7 +107,7 @@ public void updateTarget(Long memberId, String userKey, Long targetId) throws Js

// 총 합계금액을 Member의 depositAccount로 이체
if (totalAmount > 0) {
callTransferApi(depositAccountNo, withdrawalAccountNo, totalAmount, userKey); // 재사용
callTransferApi(depositAccountNo, withdrawalAccountNo, totalAmount, userKey);
}

// 출금 처리
Expand Down Expand Up @@ -158,8 +164,8 @@ void callTransferApi(String depositAccountNo, String withdrawalAccountNo, int am
body.put("depositAccountNo", depositAccountNo);
body.put("withdrawalAccountNo", withdrawalAccountNo);
body.put("transactionBalance", amount);
body.put("depositTransactionSummary", "(수시입출금) : 입금(이체)");
body.put("withdrawalTransactionSummary", "(수시입출금) : 출금(이체)");
body.put("depositTransactionSummary", "입금");
body.put("withdrawalTransactionSummary", "출금");

// HttpHeaders 설정
HttpHeaders headers = new HttpHeaders();
Expand All @@ -172,15 +178,15 @@ void callTransferApi(String depositAccountNo, String withdrawalAccountNo, int am
String url = "https://finopenapi.ssafy.io/ssafy/api/v1/edu/demandDeposit/updateDemandDepositAccountTransfer";
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);

if (response.getStatusCode().is2xxSuccessful()) {
// 성공적으로 이체된 경우 처리
JsonNode responseBody = objectMapper.readTree(response.getBody());
String responseCode = responseBody.path("Header").path("responseCode").asText();
if (!"H0000".equals(responseCode)) {
throw new RuntimeException("이체 오류: " + responseBody.path("Header").path("responseMessage").asText());
}
} else {
throw new RuntimeException("API 호출 오류: " + response.getBody());
}
// 5. 거래 번호 추출
List<String> transactionUniqueNos = new ArrayList<>();

JsonNode rootNode = objectMapper.readTree((response.getBody()));
JsonNode recNode = rootNode.path("REC");

String memo = "투자계좌";
transactionService.makeMemo(withdrawalAccountNo, recNode.get(0).path("transactionUniqueNo").asText(), memo, userKey);
transactionService.makeMemo(depositAccountNo, recNode.get(1).path("transactionUniqueNo").asText(), memo, userKey);

}
}
Loading