diff --git a/payment-paypal/src/main/java/com/yas/payment/paypal/service/OrderService.java b/payment-paypal/src/main/java/com/yas/payment/paypal/service/OrderService.java deleted file mode 100644 index 92234db226..0000000000 --- a/payment-paypal/src/main/java/com/yas/payment/paypal/service/OrderService.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.yas.payment.paypal.service; - -import com.yas.paymentpaypal.config.ServiceUrlConfig; -import com.yas.payment.paypal.viewmodel.OrderVm; -import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; -import io.github.resilience4j.retry.annotation.Retry; -import java.net.URI; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.oauth2.jwt.Jwt; -import org.springframework.stereotype.Service; -import org.springframework.web.client.RestClient; -import org.springframework.web.util.UriComponentsBuilder; - -@Service -@Slf4j -@RequiredArgsConstructor -public class OrderService extends AbstractCircuitBreakFallbackHandler { - private final RestClient restClient; - private final ServiceUrlConfig serviceUrlConfig; - - @Retry(name = "restApi") - @CircuitBreaker(name = "restCircuitBreaker", fallbackMethod = "handleBodilessFallback") - public OrderVm getOrderByCheckoutId(String checkoutId) { - final String jwt = - ((Jwt) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getTokenValue(); - final URI url = UriComponentsBuilder - .fromHttpUrl(serviceUrlConfig.order()) - .path("/storefront/orders/checkout/" + checkoutId) - .buildAndExpand() - .toUri(); - - return restClient.get() - .uri(url) - .headers(h -> h.setBearerAuth(jwt)) - .retrieve() - .body(OrderVm.class); - } -} diff --git a/payment-paypal/src/main/java/com/yas/payment/paypal/viewmodel/OrderVm.java b/payment-paypal/src/main/java/com/yas/payment/paypal/viewmodel/OrderVm.java deleted file mode 100644 index bcfcab267e..0000000000 --- a/payment-paypal/src/main/java/com/yas/payment/paypal/viewmodel/OrderVm.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.yas.payment.paypal.viewmodel; - -public record OrderVm(Long id) { -} diff --git a/payment-paypal/src/test/java/com/yas/payment/paypal/service/OrderServiceTest.java b/payment-paypal/src/test/java/com/yas/payment/paypal/service/OrderServiceTest.java deleted file mode 100644 index 1c4fe66378..0000000000 --- a/payment-paypal/src/test/java/com/yas/payment/paypal/service/OrderServiceTest.java +++ /dev/null @@ -1,60 +0,0 @@ -//package com.yas.paymentpaypal.service; -// -//import static com.yas.paymentpaypal.utils.SecurityContextUtils.setUpSecurityContext; -//import static org.mockito.ArgumentMatchers.any; -//import static org.mockito.Mockito.mock; -//import static org.mockito.Mockito.times; -//import static org.mockito.Mockito.verify; -//import static org.mockito.Mockito.when; -// -//import com.yas.paymentpaypal.config.ServiceUrlConfig; -//import java.net.URI; -//import java.util.UUID; -//import org.junit.jupiter.api.BeforeEach; -//import org.junit.jupiter.api.Test; -//import org.mockito.Mockito; -//import org.springframework.web.client.RestClient; -//import org.springframework.web.util.UriComponentsBuilder; -// -//public class OrderServiceTest { -// private RestClient restClient; -// -// private ServiceUrlConfig serviceUrlConfig; -// -// private OrderService orderService; -// -// private RestClient.ResponseSpec responseSpec; -// -// private static final String ORDER_URL = "http://api.yas.local/order"; -// -// @BeforeEach -// void setUp() { -// restClient = mock(RestClient.class); -// serviceUrlConfig = mock(ServiceUrlConfig.class); -// orderService = new OrderService(restClient, serviceUrlConfig); -// responseSpec = Mockito.mock(RestClient.ResponseSpec.class); -// setUpSecurityContext("test"); -// when(serviceUrlConfig.order()).thenReturn(ORDER_URL); -// } -// -// @Test -// void testGetOrderByCheckoutId_ifNormalCase_returnOrderVm() { -// String checkoutId = UUID.randomUUID().toString(); -// -// final URI url = UriComponentsBuilder -// .fromHttpUrl(serviceUrlConfig.order()) -// .path("/storefront/orders/checkout/" + checkoutId) -// .buildAndExpand() -// .toUri(); -// -// RestClient.RequestHeadersUriSpec requestBodyUriSpec = mock(RestClient.RequestHeadersUriSpec.class); -// when(restClient.get()).thenReturn(requestBodyUriSpec); -// when(requestBodyUriSpec.uri(url)).thenReturn(requestBodyUriSpec); -// when(requestBodyUriSpec.headers(any())).thenReturn(requestBodyUriSpec); -// when(requestBodyUriSpec.retrieve()).thenReturn(responseSpec); -// -// orderService.getOrderByCheckoutId(checkoutId); -// -// verify(restClient, times(1)).get(); -// } -//} diff --git a/payment-paypal/src/test/java/com/yas/payment/paypal/service/PaypalServiceTest.java b/payment-paypal/src/test/java/com/yas/payment/paypal/service/PaypalServiceTest.java index 5c49aeb626..5a840c1eda 100644 --- a/payment-paypal/src/test/java/com/yas/payment/paypal/service/PaypalServiceTest.java +++ b/payment-paypal/src/test/java/com/yas/payment/paypal/service/PaypalServiceTest.java @@ -22,18 +22,18 @@ class PaypalServiceTest { private PaypalService paypalService; + private OrderService orderService; private PayPalHttpClient payPalHttpClient; private PayPalHttpClientInitializer payPalHttpClientInitializer; private String paymentSettings = "{\"clientId\": \"abc\", \"clientSecret\": \"123\", \"mode\": \"sandbox\"}"; - private OrderService orderService; - @BeforeEach void setUp() { payPalHttpClient = mock(PayPalHttpClient.class); payPalHttpClientInitializer = mock(PayPalHttpClientInitializer.class); when(payPalHttpClientInitializer.createPaypalClient(anyString())).thenReturn(payPalHttpClient); - paypalService = new PaypalService(payPalHttpClientInitializer); + orderService = mock(OrderService.class); + paypalService = new PaypalService(payPalHttpClientInitializer, orderService); CheckoutIdHelper.setCheckoutId("test-checkout-id"); } diff --git a/payment-paypal/src/test/resources/application.properties b/payment-paypal/src/test/resources/application.properties index 4b994f1546..670833ef0b 100644 --- a/payment-paypal/src/test/resources/application.properties +++ b/payment-paypal/src/test/resources/application.properties @@ -11,8 +11,7 @@ springdoc.api-docs.enabled=true # swagger-ui custom path springdoc.swagger-ui.path=/swagger-ui.html -springdoc.packagesToScan=com.yas.paymentpaypal - +springdoc.packagesToScan=com.yas.payment.paypal spring.security.oauth2.resourceserver.jwt.issuer-uri=http://identity/realms/Yas springdoc.oauthflow.authorization-url=http://identity/realms/Yas/protocol/openid-connect/auth diff --git a/payment/src/main/java/com/yas/payment/controller/PaymentController.java b/payment/src/main/java/com/yas/payment/controller/PaymentController.java index cdb61cbdf1..c922b5ef28 100644 --- a/payment/src/main/java/com/yas/payment/controller/PaymentController.java +++ b/payment/src/main/java/com/yas/payment/controller/PaymentController.java @@ -3,8 +3,8 @@ import com.yas.payment.service.PaymentService; import com.yas.payment.viewmodel.CapturePaymentRequest; import com.yas.payment.viewmodel.CapturePaymentResponse; -import com.yas.payment.viewmodel.CreatePaymentRequest; -import com.yas.payment.viewmodel.CreatePaymentResponse; +import com.yas.payment.viewmodel.InitPaymentRequest; +import com.yas.payment.viewmodel.InitPaymentResponse; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -19,9 +19,9 @@ public class PaymentController { private final PaymentService paymentService; - @PostMapping(value = "/storefront/payments/create") - public CreatePaymentResponse createPayment(@Valid @RequestBody CreatePaymentRequest createPaymentRequest) { - return paymentService.createPayment(createPaymentRequest); + @PostMapping(value = "/storefront/payments/init") + public InitPaymentResponse initPayment(@Valid @RequestBody InitPaymentRequest initPaymentRequest) { + return paymentService.initPayment(initPaymentRequest); } @PostMapping(value = "/storefront/payments/capture") diff --git a/payment/src/main/java/com/yas/payment/service/PaymentService.java b/payment/src/main/java/com/yas/payment/service/PaymentService.java index 44070682d1..eec1ae7d37 100644 --- a/payment/src/main/java/com/yas/payment/service/PaymentService.java +++ b/payment/src/main/java/com/yas/payment/service/PaymentService.java @@ -40,16 +40,16 @@ private PaymentHandler getPaymentHandler(String providerName) { return handler; } - public CreatePaymentResponse createPayment(CreatePaymentRequest createPaymentRequest) { - PaymentHandler paymentHandler = getPaymentHandler(createPaymentRequest.paymentMethod()); - return paymentHandler.createPayment(createPaymentRequest); + public InitPaymentResponse initPayment(InitPaymentRequest initPaymentRequest) { + PaymentHandler paymentHandler = getPaymentHandler(initPaymentRequest.paymentMethod()); + return paymentHandler.initPayment(initPaymentRequest); } public CapturePaymentResponse capturePayment(CapturePaymentRequest capturePaymentRequest) { PaymentHandler paymentHandler = getPaymentHandler(capturePaymentRequest.paymentMethod()); CapturePaymentResponse capturePaymentResponse = paymentHandler.capturePayment(capturePaymentRequest); - Payment payment = createPayment(capturePaymentResponse); Long orderId = orderService.updateCheckoutStatus(capturePaymentResponse); + Payment payment = createPayment(capturePaymentResponse, orderId); PaymentOrderStatusVm orderPaymentStatusVm = PaymentOrderStatusVm.builder() .paymentId(payment.getId()) @@ -60,10 +60,10 @@ public CapturePaymentResponse capturePayment(CapturePaymentRequest capturePaymen return capturePaymentResponse; } - private Payment createPayment(CapturePaymentResponse completedPayment) { + private Payment createPayment(CapturePaymentResponse completedPayment, Long orderId) { Payment payment = Payment.builder() .checkoutId(completedPayment.checkoutId()) - .orderId(completedPayment.orderId()) + .orderId(orderId) .paymentStatus(completedPayment.paymentStatus()) .paymentFee(completedPayment.paymentFee()) .paymentMethod(completedPayment.paymentMethod()) diff --git a/payment/src/main/java/com/yas/payment/service/provider/handler/PaymentHandler.java b/payment/src/main/java/com/yas/payment/service/provider/handler/PaymentHandler.java index 8032c571cb..ca879812c5 100644 --- a/payment/src/main/java/com/yas/payment/service/provider/handler/PaymentHandler.java +++ b/payment/src/main/java/com/yas/payment/service/provider/handler/PaymentHandler.java @@ -2,11 +2,11 @@ import com.yas.payment.viewmodel.CapturePaymentRequest; import com.yas.payment.viewmodel.CapturePaymentResponse; -import com.yas.payment.viewmodel.CreatePaymentRequest; -import com.yas.payment.viewmodel.CreatePaymentResponse; +import com.yas.payment.viewmodel.InitPaymentRequest; +import com.yas.payment.viewmodel.InitPaymentResponse; public interface PaymentHandler { String getProviderId(); - CreatePaymentResponse createPayment(CreatePaymentRequest createPaymentRequest); + InitPaymentResponse initPayment(InitPaymentRequest initPaymentRequest); CapturePaymentResponse capturePayment(CapturePaymentRequest capturePaymentRequest); } diff --git a/payment/src/main/java/com/yas/payment/service/provider/handler/PaypalHandler.java b/payment/src/main/java/com/yas/payment/service/provider/handler/PaypalHandler.java index ace0e3a19c..59f13e552b 100644 --- a/payment/src/main/java/com/yas/payment/service/provider/handler/PaypalHandler.java +++ b/payment/src/main/java/com/yas/payment/service/provider/handler/PaypalHandler.java @@ -5,8 +5,8 @@ import com.yas.payment.service.PaymentProviderService; import com.yas.payment.viewmodel.CapturePaymentRequest; import com.yas.payment.viewmodel.CapturePaymentResponse; -import com.yas.payment.viewmodel.CreatePaymentRequest; -import com.yas.payment.viewmodel.CreatePaymentResponse; +import com.yas.payment.viewmodel.InitPaymentRequest; +import com.yas.payment.viewmodel.InitPaymentResponse; import com.yas.payment.paypal.service.PaypalService; import com.yas.payment.paypal.viewmodel.PaypalCapturePaymentRequest; import com.yas.payment.paypal.viewmodel.PaypalCapturePaymentResponse; @@ -30,14 +30,14 @@ public String getProviderId() { } @Override - public CreatePaymentResponse createPayment(CreatePaymentRequest createPaymentRequest) { + public InitPaymentResponse initPayment(InitPaymentRequest initPaymentRequest) { PaypalCreatePaymentRequest requestPayment = new PaypalCreatePaymentRequest( - createPaymentRequest.totalPrice(), - createPaymentRequest.checkoutId(), - createPaymentRequest.paymentMethod(), - getPaymentSettings(createPaymentRequest.paymentMethod())); + initPaymentRequest.totalPrice(), + initPaymentRequest.checkoutId(), + initPaymentRequest.paymentMethod(), + getPaymentSettings(initPaymentRequest.paymentMethod())); PaypalCreatePaymentResponse paypalCreatePaymentResponse = paypalService.createPayment(requestPayment); - return new CreatePaymentResponse(paypalCreatePaymentResponse.status(), paypalCreatePaymentResponse.paymentId(), paypalCreatePaymentResponse.redirectUrl()); + return new InitPaymentResponse(paypalCreatePaymentResponse.status(), paypalCreatePaymentResponse.paymentId(), paypalCreatePaymentResponse.redirectUrl()); } @Override diff --git a/payment/src/main/java/com/yas/payment/viewmodel/CreatePaymentRequest.java b/payment/src/main/java/com/yas/payment/viewmodel/CreatePaymentRequest.java deleted file mode 100644 index 90fa60b36b..0000000000 --- a/payment/src/main/java/com/yas/payment/viewmodel/CreatePaymentRequest.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.yas.payment.viewmodel; - -import java.math.BigDecimal; - -public record CreatePaymentRequest(String paymentMethod, BigDecimal totalPrice, String checkoutId) { -} diff --git a/payment/src/main/java/com/yas/payment/viewmodel/CreatePaymentResponse.java b/payment/src/main/java/com/yas/payment/viewmodel/CreatePaymentResponse.java deleted file mode 100644 index 9c731c1190..0000000000 --- a/payment/src/main/java/com/yas/payment/viewmodel/CreatePaymentResponse.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.yas.payment.viewmodel; - -public record CreatePaymentResponse(String status, String paymentId, String redirectUrl) { -} diff --git a/payment/src/main/java/com/yas/payment/viewmodel/InitPaymentRequest.java b/payment/src/main/java/com/yas/payment/viewmodel/InitPaymentRequest.java new file mode 100644 index 0000000000..e26dc117a1 --- /dev/null +++ b/payment/src/main/java/com/yas/payment/viewmodel/InitPaymentRequest.java @@ -0,0 +1,6 @@ +package com.yas.payment.viewmodel; + +import java.math.BigDecimal; + +public record InitPaymentRequest(String paymentMethod, BigDecimal totalPrice, String checkoutId) { +} diff --git a/payment/src/main/java/com/yas/payment/viewmodel/InitPaymentResponse.java b/payment/src/main/java/com/yas/payment/viewmodel/InitPaymentResponse.java new file mode 100644 index 0000000000..d5e37b1b04 --- /dev/null +++ b/payment/src/main/java/com/yas/payment/viewmodel/InitPaymentResponse.java @@ -0,0 +1,4 @@ +package com.yas.payment.viewmodel; + +public record InitPaymentResponse(String status, String paymentId, String redirectUrl) { +} diff --git a/storefront/modules/paymentPaypal/services/PaymentPaypalService.ts b/storefront/modules/paymentPaypal/services/PaymentPaypalService.ts index 0ff242c7ad..993547e59d 100644 --- a/storefront/modules/paymentPaypal/services/PaymentPaypalService.ts +++ b/storefront/modules/paymentPaypal/services/PaymentPaypalService.ts @@ -9,7 +9,7 @@ const baseUrl = '/api/payment/storefront'; export async function initPaymentPaypal( paymentPaypalRequest: InitPaymentPaypalRequest ): Promise { - const res = await apiClientService.post(`${baseUrl}/payments/create`, JSON.stringify(paymentPaypalRequest)); + const res = await apiClientService.post(`${baseUrl}/payments/init`, JSON.stringify(paymentPaypalRequest)); if (res.ok) { return res.json(); }