-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from prgrms-be-devcourse/feature/NAYB-138
[NAYB-138] 결제 실패할 경우를 처리한다.
- Loading branch information
Showing
16 changed files
with
348 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
public enum PaymentStatus { | ||
PENDING, | ||
SUCCESS, | ||
CANCELED | ||
CANCELED, | ||
FAILED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/com/prgrms/nabmart/domain/payment/service/PaymentClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.prgrms.nabmart.domain.payment.service; | ||
|
||
import com.prgrms.nabmart.domain.payment.exception.PaymentFailException; | ||
import com.prgrms.nabmart.domain.payment.service.response.TossPaymentApiResponse; | ||
import com.prgrms.nabmart.global.infrastructure.ApiService; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import lombok.RequiredArgsConstructor; | ||
import net.minidev.json.JSONObject; | ||
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.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class PaymentClient { | ||
|
||
private final ApiService apiService; | ||
|
||
@Value("${payment.toss.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${payment.toss.confirm-url}") | ||
private String confirmUrl; | ||
|
||
public void confirmPayment(final String uuid, final String paymentKey, final Integer amount) { | ||
HttpHeaders httpHeaders = getHttpHeaders(); | ||
JSONObject params = getParams(uuid, paymentKey, amount); | ||
TossPaymentApiResponse paymentApiResponse = requestPaymentApi(httpHeaders, params); | ||
|
||
validatePaymentResult(paymentApiResponse); | ||
} | ||
|
||
private HttpHeaders getHttpHeaders() { | ||
HttpHeaders httpHeaders = new HttpHeaders(); | ||
httpHeaders.setBasicAuth(getEncodeAuth()); | ||
httpHeaders.setContentType(MediaType.APPLICATION_JSON); | ||
|
||
return httpHeaders; | ||
} | ||
|
||
private String getEncodeAuth() { | ||
return new String( | ||
Base64.getEncoder() | ||
.encode((secretKey + ":").getBytes(StandardCharsets.UTF_8)) | ||
); | ||
} | ||
|
||
private JSONObject getParams(String uuid, String paymentKey, Integer amount) { | ||
JSONObject params = new JSONObject(); | ||
params.put("paymentKey", paymentKey); | ||
params.put("orderId", uuid); | ||
params.put("amount", amount); | ||
|
||
return params; | ||
} | ||
|
||
private TossPaymentApiResponse requestPaymentApi(HttpHeaders httpHeaders, JSONObject params) { | ||
return apiService.getResult( | ||
new HttpEntity<>(params, httpHeaders), | ||
confirmUrl, | ||
TossPaymentApiResponse.class | ||
); | ||
} | ||
|
||
private void validatePaymentResult(TossPaymentApiResponse paymentApiResponse) { | ||
if (!paymentApiResponse.status().equals("DONE")) { | ||
throw new PaymentFailException("결제가 실패되었습니다."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/com/prgrms/nabmart/domain/payment/service/response/PaymentResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.prgrms.nabmart.domain.payment.service.response; | ||
|
||
public record PaymentResponse(String status) { | ||
public record PaymentResponse(String status, String message) { | ||
|
||
} |
Oops, something went wrong.