카드 매입 승인 경로 Spring Boot 3 전환 (ACX-E2E-1785394564819)
Some checks are pending
Verify / source-contract (push) Waiting to run
Some checks are pending
Verify / source-contract (push) Waiting to run
This commit is contained in:
parent
3f27fd49b7
commit
7f2b733f2f
1 changed files with 66 additions and 0 deletions
|
|
@ -0,0 +1,66 @@
|
|||
package com.acquirex.approval.service;
|
||||
|
||||
import com.acquirex.approval.domain.ApprovalRequest;
|
||||
import com.acquirex.approval.domain.ApprovalRequest.ApprovalStatus;
|
||||
import com.acquirex.approval.dto.ApprovalDto.Request;
|
||||
import com.acquirex.approval.dto.ApprovalDto.Response;
|
||||
import com.acquirex.approval.repository.ApprovalRequestRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class ApprovalService {
|
||||
|
||||
private final ApprovalRequestRepository repository;
|
||||
|
||||
public ApprovalService(ApprovalRequestRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Response processApproval(Request requestDto) {
|
||||
ApprovalRequest request = new ApprovalRequest(
|
||||
requestDto.getCardNumber(),
|
||||
requestDto.getMerchantId(),
|
||||
requestDto.getAmount(),
|
||||
requestDto.getCurrency()
|
||||
);
|
||||
|
||||
ApprovalRequest saved = repository.save(request);
|
||||
|
||||
if (validateRequest(requestDto)) {
|
||||
String approvalCode = generateApprovalCode();
|
||||
saved.setStatus(ApprovalStatus.APPROVED);
|
||||
saved.setApprovalCode(approvalCode);
|
||||
repository.save(saved);
|
||||
return Response.approved(saved.getId(), approvalCode, saved.getAmount(), saved.getCurrency());
|
||||
} else {
|
||||
saved.setStatus(ApprovalStatus.DECLINED);
|
||||
repository.save(saved);
|
||||
return Response.declined(saved.getId(), "Validation failed");
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public ApprovalRequest findById(Long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Approval request not found: " + id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public long countPendingApprovals() {
|
||||
return repository.countByStatus(ApprovalStatus.PENDING);
|
||||
}
|
||||
|
||||
private boolean validateRequest(Request dto) {
|
||||
if (dto.getAmount() == null || dto.getAmount().signum() <= 0) return false;
|
||||
if (dto.getCardNumber() == null || dto.getCardNumber().length() < 13) return false;
|
||||
if (dto.getMerchantId() == null || dto.getMerchantId().isEmpty()) return false;
|
||||
return dto.getCurrency() != null && dto.getCurrency().length() == 3;
|
||||
}
|
||||
|
||||
private String generateApprovalCode() {
|
||||
return "APP-" + UUID.randomUUID().toString().substring(0, 8).toUpperCase();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue