Skip to content
Open
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
target
build
.project
.classpath
.settings
.idea
*.iml
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
# paymennt-java
official paymennt.com api client for Java
# Paymennt Client SDK

The PaymentClient library facilitates the processing and management of payments for various applications. To leverage the capabilities of this library, clients are required to possess an API key and API secret. These credentials can be generated within the Payment Admin UI, granting access to the comprehensive functionality of the library.

Please ensure that you securely store and manage your API key and secret, as they are essential for establishing a secure and efficient connection to the payment system.

## How to use

- Open your project's configuration file (e.g., pom.xml for Maven). Navigate to the dependencies section. Add the following dependency to your project:
```sh
<dependency>
<groupId>com.paymennt</groupId>
<artifactId>paymennt-client</artifactId>
<version>1.0</version>
</dependency>
```
- Connect to the PaymenntClient. Use the apiKey, apiSecret generated from the Paymennt admin. Use TEST, LIVE environment based on your requirements for testing or production.
```sh
CheckoutOperations operations = new PaymenntClient("{API_KEY}", "{API_SECRET}", PaymenntClient.PaymenntEnvironment.TEST).checkoutOperations();
```
- To perform checkout operations, such as creating a checkout or retrieving checkout information, ensure that you provide valid parameters (refer official Paymennt API documentation).
```sh
/**
* create checkout
* webCheckoutRequest is the request body required by Paymennt api to create the checkout.
*/
Checkout checkout = operations.createWebCheckout({webCheckoutRequest});

/**
* get checkout details
* use checkoutId retrieved from paymennt API.
*/
Checkout checkout = operations.getCheckout({checkoutId});
```

## Official Paymennt API documentation
[Link](https://docs.paymennt.com/api#section/Introduction)
84 changes: 84 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.paymennt</groupId>
<artifactId>paymennt-client</artifactId>
<version>1.0</version>
<name>paymennt-client</name>
<description>Paymennt Client</description>
<properties>
<java.version>17</java.version>
<spring-boot.version>3.1.2</spring-boot.version>
<jackson.version>2.12.7.1</jackson.version>
<httpclient.version>4.5.13</httpclient.version>
<commons-lang3.version>3.9</commons-lang3.version>
<log4j.version>2.17.1</log4j.version> <!-- Log4j 2 version -->
<hibernate-validator.version>7.0.1.Final</hibernate-validator.version>
<joda-time.version>2.12.5</joda-time.version>
<lombok.version>1.18.22</lombok.version>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>compile</scope>
</dependency>
<!-- Other dependencies... -->
</dependencies>
</project>
131 changes: 131 additions & 0 deletions src/main/java/com/paymennt/client/PaymenntClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/************************************************************************
* Copyright PointCheckout Ltd.
*/
package com.paymennt.client;

import com.paymennt.client.exception.PaymenntClientException;
import com.paymennt.client.operations.CheckoutOperations;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
* A client for interacting with the PointCheckout API.
* Manages the connection to the API and provides access to various operations.
*
* Usage example:
* PaymenntClient client = new PaymenntClient("your-api-key", "your-api-secret");
* CheckoutOperations checkoutOps = client.checkoutOperations();
* Checkout checkout = checkoutOps.getCheckout("checkout-id");
*
* @author Ankur
*/
public class PaymenntClient {

// Constants for API headers and default prefix
private static final String API_KEY_HEADER = "X-Paymennt-Api-Key";
private static final String API_SECRET_HEADER = "X-Paymennt-Api-Secret";
private static final String DEFAULT_PREFIX = "api/mer/v2.0";

// HTTP client instance
private final HttpClient httpClient;

// Checkout operations instance
private final CheckoutOperations checkoutOperations;

/*******************************************************************************************************************
* CONSTRUCTOR AND CONNECTION MANAGEMENT
*/

/**
* Constructs a PaymenntClient instance using the provided API key and API secret.
* Initializes the HTTP client and sets the base URI for the operations.
*
* @param apiKey The API key for authentication.
* @param apiSecret The API secret for authentication.
* @throws PaymenntClientException If API key or API secret is empty.
*/
public PaymenntClient(String apiKey, String apiSecret)
throws PaymenntClientException {
this(apiKey, apiSecret, PaymenntEnvironment.LIVE);
}


/**
* Constructs a PaymenntClient instance using the provided API key, API secret, and environment.
* Initializes the HTTP client and sets the base URI for the operations based on the environment.
*
* @param apiKey The API key for authentication.
* @param apiSecret The API secret for authentication.
* @param environment The environment to connect to (LIVE, TEST, LOCAL).
* @throws PaymenntClientException If API key or API secret is empty, or if URI syntax is invalid.
*/
public PaymenntClient(String apiKey, String apiSecret, PaymenntEnvironment environment) throws PaymenntClientException {
assertTrue(StringUtils.isNotBlank(apiKey), "apiKey cannot be empty");
assertTrue(StringUtils.isNotBlank(apiSecret), "apiSecret cannot be empty");

// CREATE HTTP CLIENT
this.httpClient = HttpClientBuilder.create().setDefaultHeaders(
List.of(
new BasicHeader(API_KEY_HEADER, apiKey),
new BasicHeader(API_SECRET_HEADER, apiSecret)
)
).build();

String scheme = environment.scheme;
String host = environment.host;
int port = environment.port;

URIBuilder builder = new URIBuilder().setScheme(scheme).setHost(host).setPort(port)
.setPath(DEFAULT_PREFIX);

URI baseUri = null;
try {
baseUri = builder.build();
} catch (URISyntaxException e) {
throw new PaymenntClientException("Invalid URI: %s://%s:%d", scheme, host, port);
}

this.checkoutOperations = new CheckoutOperations(httpClient, baseUri);
}

/**
* Get the instance of CheckoutOperations for performing checkout-related operations.
*
* @return The CheckoutOperations instance.
*/
public CheckoutOperations checkoutOperations() {
return this.checkoutOperations;
}

private void assertTrue(boolean condition, String message) throws PaymenntClientException {
if (!condition)
throw new PaymenntClientException(message);
}

/**
* Environment properties
*/
public enum PaymenntEnvironment {
LIVE("https","api.paymennt.com", 443),
TEST("https","api.test.paymennt.com", 443),
LOCAL("http","localhost", 8080);

private final String scheme;
private final String host;
private final int port;

PaymenntEnvironment(String scheme, String host, int port) {
this.scheme = scheme;
this.host = host;
this.port = port;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/************************************************************************
* Copyright PointCheckout Ltd.
*/
package com.paymennt.client.exception;

import lombok.Getter;

import java.io.Serial;

/**
* Custom exception class for handling errors in the PaymenntClient.
* This exception provides a response code and supports formatted messages.
*
* @author Ankur
*/
public class PaymenntClientException extends Exception {
@Serial
private static final long serialVersionUID = 326864452189922315L;

/**
* The HTTP response code associated with the exception.
*/
@Getter
private final int responseCode;

/**
* Constructs a PaymenntClientException with a given message.
*
* @param message The exception message.
*/
public PaymenntClientException(String message) {
this(-1, message);
}

/**
* Constructs a PaymenntClientException with a given response code and message.
*
* @param responseCode The HTTP response code.
* @param message The exception message.
*/
public PaymenntClientException(int responseCode, String message) {
super(message);
this.responseCode = responseCode;
}

/**
* Constructs a PaymenntClientException with a formatted message.
*
* @param message The exception message format.
* @param args Arguments to be formatted into the message.
*/
public PaymenntClientException(String message, Object... args) {
this(-1, String.format(message, args));
}

/**
* Constructs a PaymenntClientException with a formatted message and cause.
*
* @param message The exception message format.
* @param cause The cause of the exception.
* @param args Arguments to be formatted into the message.
*/
public PaymenntClientException(String message, Throwable cause, Object... args) {
this(-1, String.format(message, args), cause);
}

/**
* Constructs a PaymenntClientException with a given response code, formatted message, and cause.
*
* @param responseCode The HTTP response code.
* @param message The exception message format.
* @param cause The cause of the exception.
* @param args Arguments to be formatted into the message.
*/
public PaymenntClientException(int responseCode, String message, Throwable cause, Object... args) {
super(String.format(message, args), cause);
this.responseCode = responseCode;
}
}
Loading