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] 계좌 메모 API 구현 #139

Merged
merged 2 commits into from
Aug 30, 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 @@ -72,7 +72,7 @@ public ResponseEntity<SuccessResponse<AccountInfoResponse>> getAccountInfo(
}

@PostMapping("/transfer")
public SuccessResponse<?> getAccountTransfer(@AuthenticationPrincipal MemberPrincipal memberPrincipal, @Valid @RequestBody TransferRequest transferRequest) {
public SuccessResponse<?> getAccountTransfer(@AuthenticationPrincipal MemberPrincipal memberPrincipal, @Valid @RequestBody TransferRequest transferRequest) throws JsonProcessingException {
accountService.transferMoney(memberPrincipal, transferRequest);
return SuccessResponse.ok();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import com.fav.daengnyang.global.aws.service.AwsService;
import com.fav.daengnyang.global.exception.CustomException;
import com.fav.daengnyang.global.exception.ErrorCode;
import com.fav.daengnyang.global.transaction.service.TransactionService;
import com.fav.daengnyang.global.web.dto.response.TransactionUtil;
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 @@ -37,14 +39,12 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class AccountService {

private final AccountRepository accountRepository;
Expand All @@ -54,10 +54,22 @@ public class AccountService {
private final RestTemplate restTemplate;
private final ObjectMapper objectMapper;
private final AwsService awsService;
private final TransactionService transactionService;

@Value("${api.key}")
private String apiKey;

private final List<String> categories = initializeCategories();

private List<String> initializeCategories() {
List<String> categoryList = new ArrayList<>();
for (int i = 0; i < 4; i++) categoryList.add("식비");
for (int i = 0; i < 2; i++) categoryList.add("교통");
for (int i = 0; i < 3; i++) categoryList.add("쇼핑");
categoryList.add("기타");
return categoryList;
}

public AccountCreateResponse createAccount(AccountCreateRequest request, String userKey, Long memberId) throws JsonProcessingException {
// 외부 API를 통해 계좌를 생성
String accountNo = callCreateAccountApi(request, userKey);
Expand Down Expand Up @@ -158,22 +170,31 @@ public String updateAccountImage(Long memberId, MultipartFile newImage) {
}

// 출금하기
public void transferMoney(MemberPrincipal memberPrincipal, TransferRequest transferRequest) {
public void transferMoney(MemberPrincipal memberPrincipal, TransferRequest transferRequest) throws JsonProcessingException {
//0. 계좌 번호
Member member = memberRepository.findByMemberId(memberPrincipal.getMemberId())
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

// 1. 금융 API 호출
callTransferMoney(memberPrincipal.getMemberId(), memberPrincipal.getUserKey(), transferRequest);
String transferNo = callTransferMoney(member.getDepositAccount(), memberPrincipal.getUserKey(), transferRequest);
// 2. 메모하기
log.info("transferNo: {}", transferNo);
transactionService.makeMemo(member.getDepositAccount(), transferNo, setMemoCategory(), memberPrincipal.getUserKey());
}

private void callTransferMoney(Long memberId, String userKey, TransferRequest transferRequest) {
//0. 계좌 번호
Member member = memberRepository.findByMemberId(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
private String setMemoCategory(){
Random random = new Random();
int index = random.nextInt(categories.size());
return categories.get(index);
}

//1. body 객체 생성
private String callTransferMoney(String accountNo, String userKey, TransferRequest transferRequest) throws JsonProcessingException {
//1. body 객체 생성
TransferHeaderRequest header = TransferHeaderRequest
.createTransferHeaderRequest(apiKey, userKey);
HashMap<String, Object> body = new HashMap<>();
body.put("Header", header);
body.put("accountNo", member.getDepositAccount());
body.put("accountNo", accountNo);
body.put("transactionBalance", transferRequest.getAmount());
body.put("transferSummary", "출금");

Expand All @@ -187,6 +208,11 @@ private void callTransferMoney(Long memberId, String userKey, TransferRequest tr
// 4. 외부 API 호출
String url = "/edu/demandDeposit/updateDemandDepositAccountWithdrawal";
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);

// 5. 거래 번호 추출
JsonNode rootNode = objectMapper.readTree((response.getBody()));
JsonNode recNode = rootNode.path("REC");
return recNode.path("transactionUniqueNo").asText();
}

// 외부 금융 API 호출 (계좌 생성)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void makeDeposit(String accountNo, String userKey){
// 1. body 객체 생성
HashMap<String, Object> body = new HashMap<>();

DepositHeaderRequest header = DepositHeaderRequest.createDepositHeader(apiKey, userKey);
DepositHeaderRequest header = DepositHeaderRequest.createDepositHeader("updateDemandDepositAccountDeposit", apiKey, userKey);
body.put("Header", header);
body.put("accountNo", accountNo);
body.put("transactionBalance", "1000000"); // 금액
Expand All @@ -50,4 +50,29 @@ public void makeDeposit(String accountNo, String userKey){

log.info("입금하기 API 결과: " + response.getBody());
}

// 거래내역 메모하기
public void makeMemo(String accountNo, String transactionNo, String memo, String userKey){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 잘 이해한건지 모르겠는데, 금융 API를 사용할 때 금융 API 메소드 내부에서 이 친구를 호출하면 되는건가요?

// 1. body 객체 생성
HashMap<String, Object> body = new HashMap<>();

DepositHeaderRequest header = DepositHeaderRequest.createDepositHeader("transactionMemo", apiKey, userKey);
body.put("Header", header);
body.put("accountNo", accountNo);
body.put("transactionUniqueNo", transactionNo); // 금액
body.put("transactionMemo", memo);

// 2. HttpHeaders 설정
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// 3. HttpEntity 객체 생성
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(body, headers);

// 4. 외부 API 호출
String url = "/edu/transactionMemo";
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);

log.info("메모하기 API 결과: " + response.getBody());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private DepositHeaderRequest(String apiName, String transmissionDate, String tra
this.userKey = userKey;
}

public static DepositHeaderRequest createDepositHeader(String apiKey, String userKey){
public static DepositHeaderRequest createDepositHeader(String apiName, String apiKey, String userKey){
LocalDateTime today = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String transmissionDate = today.format(formatter);
Expand All @@ -45,12 +45,12 @@ public static DepositHeaderRequest createDepositHeader(String apiKey, String use
String transmissionTime = today.format(timeFormatter);

return DepositHeaderRequest.builder()
.apiName("updateDemandDepositAccountDeposit")
.apiName(apiName)
.transmissionDate(transmissionDate)
.transmissionTime(transmissionTime)
.institutionCode("00100")
.fintechAppNo("001")
.apiServiceCode("updateDemandDepositAccountDeposit")
.apiServiceCode(apiName)
.institutionTransactionUniqueNo(TransactionUtil.generateUniqueTransactionNo())
.apiKey(apiKey)
.userKey(userKey)
Expand Down
Loading