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] 출금하기 #104

Merged
merged 5 commits into from
Aug 28, 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 @@ -5,13 +5,15 @@
import com.fav.daengnyang.domain.account.service.dto.request.AccountCreateColorRequest;
import com.fav.daengnyang.domain.account.service.dto.request.AccountCreateRequest;
import com.fav.daengnyang.domain.account.service.dto.request.ColorUpdateRequest;
import com.fav.daengnyang.domain.account.service.dto.request.TransferRequest;
import com.fav.daengnyang.domain.account.service.dto.response.AccountCreateColorResponse;
import com.fav.daengnyang.domain.account.service.dto.response.AccountCreateResponse;
import com.fav.daengnyang.domain.account.service.dto.response.AccountInfoResponse;
import com.fav.daengnyang.domain.account.service.dto.response.AccountResponse;
import com.fav.daengnyang.domain.targetDetail.service.dto.response.AccountHistoryResponse;
import com.fav.daengnyang.global.auth.dto.MemberPrincipal;
import com.fav.daengnyang.global.web.dto.response.SuccessResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand Down Expand Up @@ -62,6 +64,13 @@ public ResponseEntity<SuccessResponse<AccountInfoResponse>> getAccountInfo(
AccountInfoResponse response = accountService.getAccountInfo(memberPrincipal.getMemberId(), memberPrincipal.getUserKey());
return ResponseEntity.ok(SuccessResponse.ok(response));
}

@PostMapping("/transfer")
public SuccessResponse<?> getAccountTransfer(@AuthenticationPrincipal MemberPrincipal memberPrincipal, @Valid @RequestBody TransferRequest transferRequest) {
accountService.transferMoney(memberPrincipal, transferRequest);
return SuccessResponse.ok();
}

@PatchMapping("/create-color")
public ResponseEntity<SuccessResponse<AccountCreateColorResponse>> updateAccountColor(
@AuthenticationPrincipal MemberPrincipal memberPrincipal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import com.fav.daengnyang.domain.account.repository.AccountRepository;
import com.fav.daengnyang.domain.account.service.dto.request.AccountCreateColorRequest;
import com.fav.daengnyang.domain.account.service.dto.request.AccountCreateRequest;
import com.fav.daengnyang.domain.account.service.dto.request.TransferHeaderRequest;
import com.fav.daengnyang.domain.account.service.dto.request.TransferRequest;
import com.fav.daengnyang.domain.account.service.dto.response.AccountCreateColorResponse;
import com.fav.daengnyang.domain.account.service.dto.response.AccountCreateResponse;
import com.fav.daengnyang.domain.account.service.dto.response.AccountInfoResponse;
import com.fav.daengnyang.domain.account.service.dto.response.AccountResponse;
import com.fav.daengnyang.domain.member.entity.Member;
import com.fav.daengnyang.domain.member.repository.MemberRepository;
import com.fav.daengnyang.domain.targetDetail.service.dto.response.AccountHistoryResponse;
import com.fav.daengnyang.global.auth.dto.MemberPrincipal;
import com.fav.daengnyang.global.exception.CustomException;
import com.fav.daengnyang.global.exception.ErrorCode;
import com.fav.daengnyang.global.web.dto.response.TransactionUtil;
Expand Down Expand Up @@ -126,6 +129,38 @@ public AccountResponse updateAccountColor(Long memberId, String newColor) {
.build();
}

// 출금하기
public void transferMoney(MemberPrincipal memberPrincipal, TransferRequest transferRequest) {
// 1. 금융 API 호출
callTransferMoney(memberPrincipal.getMemberId(), memberPrincipal.getUserKey(), transferRequest);
}

private void callTransferMoney(Long memberId, String userKey, TransferRequest transferRequest) {
//0. 계좌 번호
Member member = memberRepository.findByMemberId(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

//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("transactionBalance", transferRequest.getAmount());
body.put("transferSummary", "출금");

// 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/demandDeposit/updateDemandDepositAccountWithdrawal";
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
}

// 외부 금융 API 호출 (계좌 생성)
private String callCreateAccountApi(AccountCreateRequest request, String userKey) throws JsonProcessingException {
LocalDateTime now = LocalDateTime.now();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.fav.daengnyang.domain.account.service.dto.request;

import com.fav.daengnyang.global.web.dto.response.TransactionUtil;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class TransferHeaderRequest {
private String apiName;
private String transmissionDate;
private String transmissionTime;
private String institutionCode;
private String fintechAppNo;
private String apiServiceCode;
private String institutionTransactionUniqueNo;
private String apiKey;
private String userKey;

@Builder
private TransferHeaderRequest(String apiName, String transmissionDate, String transmissionTime,
String institutionCode, String fintechAppNo, String institutionTransactionUniqueNo, String apiServiceCode,
String apiKey, String userKey) {
this.apiName = apiName;
this.transmissionDate = transmissionDate;
this.transmissionTime = transmissionTime;
this.institutionCode = institutionCode;
this.fintechAppNo = fintechAppNo;
this.apiServiceCode = apiServiceCode;
this.institutionTransactionUniqueNo = institutionTransactionUniqueNo;
this.apiKey = apiKey;
this.userKey = userKey;
}

public static TransferHeaderRequest createTransferHeaderRequest(String apiKey, String userKey) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HHmmss");


return TransferHeaderRequest.builder()
.apiName("updateDemandDepositAccountWithdrawal")
.transmissionDate(now.format(dateFormatter))
.transmissionTime(now.format(timeFormatter))
.institutionCode("00100")
.fintechAppNo("001")
.apiServiceCode("updateDemandDepositAccountWithdrawal")
.institutionTransactionUniqueNo(TransactionUtil.generateUniqueTransactionNo())
.apiKey(apiKey)
.userKey(userKey)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.fav.daengnyang.domain.account.service.dto.request;

import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.logging.log4j.core.config.plugins.validation.constraints.NotBlank;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class TransferRequest {

@NotBlank
private String account;
@NotNull
private Long amount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public void makeDeposit(String accountNo, String userKey){
String url = "/edu/demandDeposit/updateDemandDepositAccountDeposit";
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);

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