-
Notifications
You must be signed in to change notification settings - Fork 5
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 #237 from Team-BC-1/feat/toss-payment-event-publish
토스 API 호출 핸들러를 EventPublisher & Callback 을 통해 분리
- Loading branch information
Showing
9 changed files
with
153 additions
and
61 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
44 changes: 44 additions & 0 deletions
44
src/main/java/bc1/gream/domain/payment/toss/service/event/TossPaymentEventListener.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,44 @@ | ||
package bc1.gream.domain.payment.toss.service.event; | ||
|
||
import bc1.gream.domain.payment.toss.dto.response.TossPaymentSuccessResponseDto; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.Collections; | ||
import net.minidev.json.JSONObject; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Component | ||
public class TossPaymentEventListener { | ||
|
||
@Async | ||
@TransactionalEventListener | ||
public void handleTossPaymentSuccess(TossPaymentSuccessEvent event) { | ||
RestTemplate rest = new RestTemplate(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
|
||
String testSecretApiKey = event.getTestSecretApiKey() + ":"; | ||
String encodedAuth = new String(Base64.getEncoder().encode(testSecretApiKey.getBytes(StandardCharsets.UTF_8))); | ||
headers.setBasicAuth(encodedAuth); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); | ||
|
||
JSONObject param = new JSONObject(); | ||
param.put("orderId", event.getOrderId()); | ||
param.put("amount", event.getAmount()); | ||
|
||
TossPaymentSuccessResponseDto responseDto = rest.postForObject( | ||
event.getSuccessUrl() + event.getPaymentKey(), | ||
new HttpEntity<>(param, headers), | ||
TossPaymentSuccessResponseDto.class | ||
); | ||
|
||
// Use the functional interface to pass the result back to TossPaymentController | ||
event.getCallback().handle(responseDto); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/bc1/gream/domain/payment/toss/service/event/TossPaymentSuccessCallback.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,9 @@ | ||
package bc1.gream.domain.payment.toss.service.event; | ||
|
||
import bc1.gream.domain.payment.toss.dto.response.TossPaymentSuccessResponseDto; | ||
|
||
@FunctionalInterface | ||
public interface TossPaymentSuccessCallback { | ||
|
||
void handle(TossPaymentSuccessResponseDto responseDto); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/bc1/gream/domain/payment/toss/service/event/TossPaymentSuccessEvent.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,27 @@ | ||
package bc1.gream.domain.payment.toss.service.event; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
@Getter | ||
public class TossPaymentSuccessEvent extends ApplicationEvent { | ||
|
||
private final String paymentKey; | ||
private final Long orderId; | ||
private final Long amount; | ||
private final String successUrl; | ||
private final String testSecretApiKey; | ||
|
||
private final TossPaymentSuccessCallback callback; | ||
|
||
public TossPaymentSuccessEvent(Object source, String paymentKey, Long orderId, Long amount, String successUrl, String testSecretApiKey, | ||
TossPaymentSuccessCallback callback) { | ||
super(source); | ||
this.paymentKey = paymentKey; | ||
this.orderId = orderId; | ||
this.amount = amount; | ||
this.successUrl = successUrl; | ||
this.testSecretApiKey = testSecretApiKey; | ||
this.callback = callback; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package bc1.gream.global.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
|
||
@EnableAsync | ||
@Configuration | ||
public class AsyncConfig { | ||
|
||
} |
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