Skip to content

Commit

Permalink
[Payment] Embedded Payment-Paypal project into Payment project like a…
Browse files Browse the repository at this point in the history
… dependency #1003
  • Loading branch information
khanhtranduy committed Oct 29, 2024
1 parent fd8d658 commit 907d016
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 142 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
3 changes: 1 addition & 2 deletions payment-paypal/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.yas.payment.viewmodel;

import java.math.BigDecimal;

public record InitPaymentRequest(String paymentMethod, BigDecimal totalPrice, String checkoutId) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.yas.payment.viewmodel;

public record InitPaymentResponse(String status, String paymentId, String redirectUrl) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const baseUrl = '/api/payment/storefront';
export async function initPaymentPaypal(
paymentPaypalRequest: InitPaymentPaypalRequest
): Promise<InitPaymentPaypalResponse> {
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();
}
Expand Down

0 comments on commit 907d016

Please sign in to comment.