Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1003 payment provider #1243

Merged
merged 9 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 1 addition & 30 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ services:
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/payment
- SERVER_SERVLET_CONTEXT_PATH=/payment
- YAS_PUBLIC_URL=${YAS_PUBLIC_API_URL}/payment
- YAS_PUBLIC_URL=http://storefront/complete-payment
- YAS_SERVICES_ORDER
- SERVER_PORT
- LOGGING_CONFIG
Expand All @@ -301,35 +301,6 @@ services:
- ./deployment/app-config:/app-config
networks:
- yas-network
payment-paypal:
build: ./payment-paypal
image: ghcr.io/nashtech-garage/yas-payment-paypal:latest
environment:
- SERVER_SERVLET_CONTEXT_PATH=/payment-paypal
- YAS_PUBLIC_URL=http://storefront/complete-payment
- YAS_SERVICES_PAYMENT
- SERVER_PORT
- LOGGING_CONFIG
- JAVA_TOOL_OPTIONS
- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_EXPORTER_OTLP_PROTOCOL
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
- OTEL_RESOURCE_ATTRIBUTES
- OTEL_SERVICE_NAME=payment-paypal-service
- OTEL_LOGS_EXPORTER
- OTEL_TRACES_EXPORTER
- OTEL_METRICS_EXPORTER
- OTEL_INSTRUMENTATION_LOGBACK-MDC_ADD-BAGGAGE
- OTEL_JAVAAGENT_LOGGING
- OTEL_JAVAAGENT_ENABLED
- OTEL_JAVAAGENT_DEBUG
- YAS_CURRENCY_UNIT
- YAS_PRICE_INCLUDES_TAX
volumes:
- ./docker/libs/opentelemetry-javaagent.jar:/opentelemetry-javaagent.jar
- ./deployment/app-config:/app-config
networks:
- yas-network
location:
build: ./location
image: ghcr.io/nashtech-garage/yas-location:latest
Expand Down
3 changes: 0 additions & 3 deletions nginx/templates/default.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ server {
location /payment/ {
proxy_pass http://payment;
}
location /payment-paypal/ {
proxy_pass http://payment-paypal;
}
location /webhook/ {
proxy_pass http://webhook;
}
Expand Down
16 changes: 12 additions & 4 deletions payment-paypal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</parent>
<artifactId>payment-paypal</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>
<name>payment-paypal</name>
<description>Payment with paypal service for yas project</description>
<properties>
Expand Down Expand Up @@ -80,10 +81,6 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -92,6 +89,17 @@
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yas.paymentpaypal.config;
package com.yas.payment.paypal.config;

import dasniko.testcontainers.keycloak.KeycloakContainer;
import org.springframework.boot.test.context.TestConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yas.paymentpaypal.model;
package com.yas.payment.paypal.model;

public class CheckoutIdHelper {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yas.paymentpaypal.model;
package com.yas.payment.paypal.model;

public class PaymentProviderHelper {
public static final String PAYPAL_PAYMENT_PROVIDER_ID = "PAYPAL";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yas.paymentpaypal.service;
package com.yas.payment.paypal.service;

import lombok.extern.slf4j.Slf4j;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.yas.payment.paypal.service;

import com.nimbusds.jose.shaded.gson.JsonObject;
import com.nimbusds.jose.shaded.gson.JsonParser;
import com.paypal.core.PayPalEnvironment;
import com.paypal.core.PayPalHttpClient;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Component
public class PayPalHttpClientInitializer {

public PayPalHttpClient createPaypalClient(String additionalSettings) {
Assert.notNull(additionalSettings, "The additionalSettings can not be null.");
// Parse the additionalSettings field to extract clientId and clientSecret
JsonObject settingsJson = JsonParser.parseString(additionalSettings).getAsJsonObject();
String clientId = settingsJson.get("clientId").getAsString();
String clientSecret = settingsJson.get("clientSecret").getAsString();
String mode = settingsJson.get("mode").getAsString();
if (mode.equals("sandbox")) {
// Create PayPalHttpClient with the retrieved clientId and clientSecret
return new PayPalHttpClient(new PayPalEnvironment.Sandbox(clientId, clientSecret));
}
return new PayPalHttpClient(new PayPalEnvironment.Live(clientId, clientSecret));
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
package com.yas.paymentpaypal.service;
package com.yas.payment.paypal.service;

import com.paypal.core.PayPalHttpClient;
import com.paypal.http.HttpResponse;
import com.paypal.orders.AmountWithBreakdown;
import com.paypal.orders.ApplicationContext;
import com.paypal.orders.Capture;
import com.paypal.orders.Order;
import com.paypal.orders.OrderRequest;
import com.paypal.orders.OrdersCaptureRequest;
import com.paypal.orders.OrdersCreateRequest;
import com.paypal.orders.PurchaseUnitRequest;
import com.yas.paymentpaypal.model.CheckoutIdHelper;
import com.yas.paymentpaypal.utils.Constants;
import com.yas.paymentpaypal.viewmodel.CapturedPaymentVm;
import com.yas.paymentpaypal.viewmodel.PaypalRequestPayment;
import com.yas.paymentpaypal.viewmodel.RequestPayment;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.NoSuchElementException;
import com.paypal.orders.*;

Check warning on line 5 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck

Using the '.*' form of import should be avoided - com.paypal.orders.*.

Check warning on line 5 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck

Using the '.*' form of import should be avoided - com.paypal.orders.*.
import com.yas.payment.paypal.model.CheckoutIdHelper;
import com.yas.payment.paypal.utils.Constants;
import com.yas.payment.paypal.viewmodel.PaypalCapturePaymentRequest;
import com.yas.payment.paypal.viewmodel.PaypalCapturePaymentResponse;
import com.yas.payment.paypal.viewmodel.PaypalCreatePaymentRequest;
import com.yas.payment.paypal.viewmodel.PaypalCreatePaymentResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

Check warning on line 17 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'java.io.IOException'

Check warning on line 17 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.io.IOException' import. Should be before 'org.springframework.stereotype.Service'.

Check warning on line 17 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'java.io.IOException'

Check warning on line 17 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.io.IOException' import. Should be before 'org.springframework.stereotype.Service'.
import java.math.BigDecimal;

Check warning on line 18 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.math.BigDecimal' import. Should be before 'org.springframework.stereotype.Service'.

Check warning on line 18 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.math.BigDecimal' import. Should be before 'org.springframework.stereotype.Service'.
import java.util.List;

Check warning on line 19 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.util.List' import. Should be before 'org.springframework.stereotype.Service'.

Check warning on line 19 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.util.List' import. Should be before 'org.springframework.stereotype.Service'.
import java.util.NoSuchElementException;

Check warning on line 20 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.util.NoSuchElementException' import. Should be before 'org.springframework.stereotype.Service'.

Check warning on line 20 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.util.NoSuchElementException' import. Should be before 'org.springframework.stereotype.Service'.

@Service
@Slf4j
@RequiredArgsConstructor
public class PaypalService {
private final PayPalHttpClient payPalHttpClient;
private final PaymentService paymentService;
private final OrderService orderService;
private final PayPalHttpClientInitializer payPalHttpClientInitializer;
private final BigDecimal maxPay = BigDecimal.valueOf(1000);
@Value("${yas.public.url}/capture")
private String returnUrl;
@Value("${yas.public.url}/cancel")
private String cancelUrl;

public PaypalRequestPayment createPayment(RequestPayment requestPayment) {
public PaypalCreatePaymentResponse createPayment(PaypalCreatePaymentRequest createPaymentRequest) {
PayPalHttpClient payPalHttpClient = payPalHttpClientInitializer.createPaypalClient(createPaymentRequest.paymentSettings());

Check warning on line 34 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck

Line is longer than 120 characters (found 131).

Check warning on line 34 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck

Line is longer than 120 characters (found 131).
OrderRequest orderRequest = new OrderRequest();
orderRequest.checkoutPaymentIntent("CAPTURE");

// Workaround to not exceed limit amount of a transaction
BigDecimal totalPrice = requestPayment.totalPrice();
BigDecimal totalPrice = createPaymentRequest.totalPrice();
if (totalPrice.compareTo(maxPay) > 0) {
totalPrice = maxPay;
}
Expand All @@ -51,13 +45,14 @@
.value(totalPrice.toString());
PurchaseUnitRequest purchaseUnitRequest = new PurchaseUnitRequest().amountWithBreakdown(amountWithBreakdown);
orderRequest.purchaseUnits(List.of(purchaseUnitRequest));
String paymentMethodReturnUrl = String.format("%s?paymentMethod=%s", returnUrl, createPaymentRequest.paymentMethod());

Check warning on line 48 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck

Line is longer than 120 characters (found 126).

Check warning on line 48 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck

Line is longer than 120 characters (found 126).
ApplicationContext applicationContext = new ApplicationContext()
.returnUrl(returnUrl)
.cancelUrl(cancelUrl)
.brandName(Constants.Yas.BRAND_NAME)
.landingPage("BILLING")
.userAction("PAY_NOW")
.shippingPreference("NO_SHIPPING");
.returnUrl(paymentMethodReturnUrl)
.cancelUrl(cancelUrl)
.brandName(Constants.Yas.BRAND_NAME)
.landingPage("BILLING")
.userAction("PAY_NOW")
.shippingPreference("NO_SHIPPING");

orderRequest.applicationContext(applicationContext);
OrdersCreateRequest ordersCreateRequest = new OrdersCreateRequest().requestBody(orderRequest);
Expand All @@ -71,17 +66,17 @@
.orElseThrow(NoSuchElementException::new)
.href();

CheckoutIdHelper.setCheckoutId(requestPayment.checkoutId());
return new PaypalRequestPayment("success", order.id(), redirectUrl);
CheckoutIdHelper.setCheckoutId(createPaymentRequest.checkoutId());
return new PaypalCreatePaymentResponse("success", order.id(), redirectUrl);
} catch (IOException e) {
log.error(e.getMessage());
return new PaypalRequestPayment("Error" + e.getMessage(), null, null);
return new PaypalCreatePaymentResponse("Error" + e.getMessage(),null, null);

Check warning on line 73 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck

',' is not followed by whitespace.

Check warning on line 73 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck

',' is not followed by whitespace.
}
}


public CapturedPaymentVm capturePayment(String token) {
OrdersCaptureRequest ordersCaptureRequest = new OrdersCaptureRequest(token);
public PaypalCapturePaymentResponse capturePayment(PaypalCapturePaymentRequest capturePaymentRequest) {
PayPalHttpClient payPalHttpClient = payPalHttpClientInitializer.createPaypalClient(capturePaymentRequest.paymentSettings());

Check warning on line 78 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck

Line is longer than 120 characters (found 132).

Check warning on line 78 in payment-paypal/src/main/java/com/yas/payment/paypal/service/PaypalService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck

Line is longer than 120 characters (found 132).
OrdersCaptureRequest ordersCaptureRequest = new OrdersCaptureRequest(capturePaymentRequest.token());
try {
HttpResponse<Order> httpResponse = payPalHttpClient.execute(ordersCaptureRequest);
if (httpResponse.result().status() != null) {
Expand All @@ -92,25 +87,20 @@
BigDecimal paymentFee = new BigDecimal(paypalFee);
BigDecimal amount = new BigDecimal(capture.amount().value());

var orderVm = orderService.getOrderByCheckoutId(CheckoutIdHelper.getCheckoutId());

CapturedPaymentVm capturedPayment = CapturedPaymentVm.builder()
.orderId(orderVm.id())
.paymentFee(paymentFee)
.gatewayTransactionId(order.id())
.amount(amount)
.paymentStatus(order.status())
.paymentMethod("PAYPAL")
.checkoutId(CheckoutIdHelper.getCheckoutId())
.build();

paymentService.capturePayment(capturedPayment);
PaypalCapturePaymentResponse capturedPayment = PaypalCapturePaymentResponse.builder()
.paymentFee(paymentFee)
.gatewayTransactionId(order.id())
.amount(amount)
.paymentStatus(order.status())
.paymentMethod("PAYPAL")
.checkoutId(CheckoutIdHelper.getCheckoutId())
.build();
return capturedPayment;
}
} catch (IOException e) {
log.error(e.getMessage());
return CapturedPaymentVm.builder().failureMessage(e.getMessage()).build();
return PaypalCapturePaymentResponse.builder().failureMessage(e.getMessage()).build();
}
return CapturedPaymentVm.builder().failureMessage("Something Wrong!").build();
return PaypalCapturePaymentResponse.builder().failureMessage("Something Wrong!").build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yas.paymentpaypal.utils;
package com.yas.payment.paypal.utils;

import com.yas.commonlibrary.exception.SignInRequiredException;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yas.paymentpaypal.utils;
package com.yas.payment.paypal.utils;

public final class Constants {
public final class ErrorCode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yas.paymentpaypal.utils;
package com.yas.payment.paypal.utils;

import java.util.Locale;
import java.util.MissingResourceException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.yas.payment.paypal.viewmodel;

import lombok.Builder;

@Builder
public record PaypalCapturePaymentRequest(String token, String paymentSettings) {
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.yas.paymentpaypal.viewmodel;
package com.yas.payment.paypal.viewmodel;

import java.math.BigDecimal;
import lombok.Builder;

@Builder
public record CapturedPaymentVm(
Long orderId,
public record PaypalCapturePaymentResponse(
String checkoutId,
BigDecimal amount,
BigDecimal paymentFee,
String gatewayTransactionId,
String paymentMethod,
String paymentStatus,
String failureMessage) {

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

import lombok.Builder;

import java.math.BigDecimal;

Check warning on line 5 in payment-paypal/src/main/java/com/yas/payment/paypal/viewmodel/PaypalCreatePaymentRequest.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'java.math.BigDecimal'

Check warning on line 5 in payment-paypal/src/main/java/com/yas/payment/paypal/viewmodel/PaypalCreatePaymentRequest.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.math.BigDecimal' import. Should be before 'lombok.Builder'.

Check warning on line 5 in payment-paypal/src/main/java/com/yas/payment/paypal/viewmodel/PaypalCreatePaymentRequest.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'java.math.BigDecimal'

Check warning on line 5 in payment-paypal/src/main/java/com/yas/payment/paypal/viewmodel/PaypalCreatePaymentRequest.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.math.BigDecimal' import. Should be before 'lombok.Builder'.

@Builder
public record PaypalCreatePaymentRequest(BigDecimal totalPrice, String checkoutId, String paymentMethod, String paymentSettings) {

Check warning on line 8 in payment-paypal/src/main/java/com/yas/payment/paypal/viewmodel/PaypalCreatePaymentRequest.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck

Line is longer than 120 characters (found 130).

Check warning on line 8 in payment-paypal/src/main/java/com/yas/payment/paypal/viewmodel/PaypalCreatePaymentRequest.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck

Line is longer than 120 characters (found 130).
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.yas.payment.paypal.viewmodel;

import lombok.Builder;

@Builder
public record PaypalCreatePaymentResponse(String status, String paymentId, String redirectUrl) {
}

This file was deleted.

Loading
Loading