Skip to content

Commit

Permalink
Merge pull request #117 from Orange-Co/feature/payment
Browse files Browse the repository at this point in the history
Feature/payment
  • Loading branch information
Kang1221 authored Oct 8, 2024
2 parents cd91f94 + 2783358 commit 17ac69f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 20 deletions.
2 changes: 1 addition & 1 deletion DDANZI_Server_yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.orange.ddanzi.dto.payment;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class PortOneTokenRequestDto {
@JsonProperty("imp_key")
private String impKey; // 필드 이름을 camelCase로 수정

@JsonProperty("imp_secret")
private String impSecret;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package co.orange.ddanzi.dto.payment;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class PortOneTokenResponseDto {
private int code;
private String message;
private Response response;

@Getter
@Builder
public static class Response {
private String access_token;
private long now;
private long expired_at;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
@Getter
@Builder
public class RefundRequestDto {
private Integer amount;
private Integer taxFreeAmount;
private String merchant_uid;
private String reason;
}
4 changes: 2 additions & 2 deletions src/main/java/co/orange/ddanzi/service/AlarmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public ApiResponse<?> getAlarms(){
.content(alarmCase.getBody())
.time(createAlarmTime(alarm.getCreatedAt()))
.isChecked(alarm.getIsChecked())
.orderId(order.getId())
.itemId(order.getItem().getId())
.orderId(order==null? null:order.getId())
.itemId(order==null? null: order.getItem().getId())
.build();
myAlarmList.add(myAlarm);
}
Expand Down
49 changes: 34 additions & 15 deletions src/main/java/co/orange/ddanzi/service/PaymentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
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;
import org.springframework.http.MediaType;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
Expand All @@ -49,10 +47,10 @@ public class PaymentService {
private final HistoryService historyService;
private final FcmService fcmService;

@Value("${ddanzi.portone.key}")
private String key;
@Value("${ddanzi.portone.store-id}")
private String storeId;
@Value("${ddanzi.portone.access-key}")
private String accessKey;
@Value("${ddanzi.portone.access-secret}")
private String accessSecret;

@Transactional
public ApiResponse<?> startPayment(CreatePaymentRequestDto requestDto){
Expand Down Expand Up @@ -94,7 +92,7 @@ public ApiResponse<?> endPayment(UpdatePaymentRequestDto requestDto){
if(newItem == null){
log.info("환불을 진행합니다.");
try {
refundPayment(buyer, order, payment);
refundPayment(buyer, order);
payment.updatePaymentStatusAndEndedAt(PayStatus.CANCELLED);
historyService.createPaymentHistoryWithError(buyer, payment, "재고 없음- 환불 처리");
fcmService.sendMessageToAdmin(FcmCase.C3);
Expand Down Expand Up @@ -148,7 +146,7 @@ public ApiResponse<?> refundTest(UpdatePaymentRequestDto requestDto){
User buyer = authUtils.getUser();
Order order = orderService.getOrderRecord(requestDto.getOrderId());
Payment payment = paymentRepository.findByOrder(order);
refundPayment(buyer, order, payment);
refundPayment(buyer, order);
return ApiResponse.onSuccess(Success.SUCCESS, true);
}

Expand All @@ -161,22 +159,43 @@ private boolean isAvailableToChangePayment(User user, Payment payment){
return payment.getOrder().getBuyer().equals(user) && payment.getPayStatus().equals(PayStatus.PENDING);
}

public void refundPayment(User user, Order order, Payment payment){
public String getPortOneAccessToken(){
String baseUrl = "https://api.iamport.kr/users/getToken";
String url = UriComponentsBuilder.fromUriString(baseUrl)
.toUriString();
log.info("포트원 Access key를 받아오는 url 생성, url-> {}", url);

HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");

PortOneTokenRequestDto requestBody = PortOneTokenRequestDto.builder()
.impKey(accessKey)
.impSecret(accessSecret)
.build();

HttpEntity<PortOneTokenRequestDto> entity = new HttpEntity<>(requestBody, headers);

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<PortOneTokenResponseDto> response = restTemplate.exchange(url, HttpMethod.POST, entity, PortOneTokenResponseDto.class);
log.info("포트원 Access key Get 성공");
return response.getBody().getResponse().getAccess_token();
}

public void refundPayment(User user, Order order){
if(!user.equals(order.getBuyer()))
throw new RuntimeException("결제자와 요청자가 다르므로 환불이 어렵습니다.");
String baseUrl = "https://api.portone.io/payments/{paymentId}/cancel";
String baseUrl = "https://api.iamport.kr/payments/cancel";
String url = UriComponentsBuilder.fromUriString(baseUrl)
.buildAndExpand(order.getId())
.toUriString();
log.info("결제 취소 url 생성, url-> {}", url);

String key = getPortOneAccessToken();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "PortOne "+ key);
headers.set("Authorization", key);

RefundRequestDto requestDto = RefundRequestDto.builder()
.amount(payment.getTotalPrice())
.taxFreeAmount(payment.getTotalPrice())
.merchant_uid(order.getId())
.reason("현재 남은 재고가 없어 고객에게 결제 금액 환불합니다.")
.build();

Expand Down

0 comments on commit 17ac69f

Please sign in to comment.