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

sonar and better code practices #277

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
34 changes: 34 additions & 0 deletions .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow will build a package using Maven and then publish it to GitHub packages when a release is created
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path

name: Maven Package

on:
release:
types: [created]

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
settings-path: ${{ github.workspace }} # location for the settings.xml file

- name: Build with Maven
run: mvn -B package --file pom.xml

- name: Publish to GitHub Packages Apache Maven
run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml
env:
GITHUB_TOKEN: ${{ github.token }}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
**THIS IS UNOFFICIAL UPDATED CODEBASE OF RAZORPAY JAVA SDK.**
The codebase has been updated with Java 17 with sourcecode and compile version with all dependencies with latest versions
The codebase is free from any security checked with checkmarx and sonarlint

# Razorpay Java SDK

Official java bindings for the [Razorpay API](https://docs.razorpay.com/docs/payments).
Expand Down
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
Expand All @@ -54,7 +54,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>2.13.0</version>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -66,7 +66,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
<version>2.14.2</version>
<scope>test</scope>
</dependency>

Expand All @@ -89,13 +89,13 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
<version>20220320</version>
</dependency>

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
<version>1.15</version>
</dependency>

<dependency>
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/razorpay/AddonClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ public Addon fetch(String id) throws RazorpayException {
/**
* It is wrapper of fetchAll with parameter here sending null defines fetchAll
* with a default values without filteration
* @throws RazorpayException
* @throws RazorpayException RazorpayException
* @return List of AddOns
*/
public List<Addon> fetchAll() throws RazorpayException {
return fetchAll(null);
}

/**
* This method get list of Addons filtered by parameters @request
* @throws RazorpayException
* @throws RazorpayException RazorpayException
* @param request JSONObject request
* @return List of AddOns
*/
public List<Addon> fetchAll(JSONObject request) throws RazorpayException {
return getCollection(Constants.ADDON_LIST, request);
Expand Down
49 changes: 24 additions & 25 deletions src/main/java/com/razorpay/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.text.WordUtils;
import org.json.JSONArray;
Expand All @@ -13,23 +11,23 @@
import okhttp3.HttpUrl;
import okhttp3.Response;

import static com.razorpay.Constants.INVALID_RESPONSE_FROM_SERVER;

class ApiClient {

String auth;

private final String ENTITY = "entity";

private final String COLLECTION = "collection";
private static final String ENTITY = "entity";

private final String ERROR = "error";
private static final String ERROR = "error";

private final String DESCRIPTION = "description";
private static final String DESCRIPTION = "description";

private final String STATUS_CODE = "code";
private static final String STATUS_CODE = "code";

private final int STATUS_OK = 200;
private static final int STATUS_OK = 200;

private final int STATUS_MULTIPLE_CHOICE = 300;
private static final int STATUS_MULTIPLE_CHOICE = 300;

ApiClient(String auth) {
this.auth = auth;
Expand Down Expand Up @@ -69,7 +67,7 @@ public <T> T delete(String path, JSONObject requestObject) throws RazorpayExcept

private <T extends Object> T processDeleteResponse(Response response) throws RazorpayException {
if (response == null) {
throw new RazorpayException("Invalid Response from server");
throw new RazorpayException(INVALID_RESPONSE_FROM_SERVER);
}

int statusCode = response.code();
Expand Down Expand Up @@ -99,21 +97,22 @@ else if(response.code()==204){

private <T extends Entity> T parseResponse(JSONObject jsonObject, String entity) throws RazorpayException {
if (entity != null) {
Class<T> cls = getClass(entity);
try {
return cls.getConstructor(JSONObject.class).newInstance(jsonObject);
} catch (Exception e) {
throw new RazorpayException("Unable to parse response because of " + e.getMessage());
Class<T> cls = (Class<T>) getClass(entity);
if (cls != null) {
try {
return cls.getConstructor(JSONObject.class).newInstance(jsonObject);
} catch (Exception e) {
throw new RazorpayException("Unable to parse response because of " + e.getMessage());
}
}
}

throw new RazorpayException("Unable to parse response");
}

private <T extends Entity> ArrayList<T> parseCollectionResponse(JSONArray jsonArray, HttpUrl requestUrl)
throws RazorpayException {

ArrayList<T> modelList = new ArrayList<T>();
ArrayList<T> modelList = new ArrayList<>();
try {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
Expand All @@ -138,7 +137,7 @@ private String getEntityNameFromURL(HttpUrl url) {

<T extends Entity> T processResponse(Response response) throws RazorpayException {
if (response == null) {
throw new RazorpayException("Invalid Response from server");
throw new RazorpayException(INVALID_RESPONSE_FROM_SERVER);
}

int statusCode = response.code();
Expand All @@ -162,7 +161,7 @@ <T extends Entity> T processResponse(Response response) throws RazorpayException
<T extends Entity> ArrayList<T> processCollectionResponse(Response response)
throws RazorpayException {
if (response == null) {
throw new RazorpayException("Invalid Response from server");
throw new RazorpayException(INVALID_RESPONSE_FROM_SERVER);
}

int statusCode = response.code();
Expand All @@ -185,13 +184,13 @@ <T extends Entity> ArrayList<T> processCollectionResponse(Response response)
}

throwException(statusCode, responseJson);
return null;
return (ArrayList)Collections.emptyList();
}

private String getEntity(JSONObject jsonObj, HttpUrl url) {
if(!jsonObj.has(ENTITY)) {
return getEntityNameFromURL(url);
}else if(jsonObj.get("entity").toString().equals("settlement.ondemand")){
}else if(jsonObj.get(ENTITY).toString().equals("settlement.ondemand")){
return "settlement";
}else{
return jsonObj.getString(ENTITY);
Expand All @@ -215,10 +214,10 @@ private void throwServerException(int statusCode, String responseBody) throws Ra
throw new RazorpayException(sb.toString());
}

private Class getClass(String entity) {
private Class<Entity> getClass(String entity) {
try {
String entityClass = "com.razorpay." + WordUtils.capitalize(entity, '_').replaceAll("_", "");
return Class.forName(entityClass);
String entityClass = "com.razorpay." + WordUtils.capitalize(entity, '_').replace("_", "");
return (Class<Entity>) Class.forName(entityClass);
} catch (ClassNotFoundException e) {
return null;
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/razorpay/ApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class ApiUtils {

private static OkHttpClient client;
private static Map<String, String> headers = new HashMap<String, String>();
private static Map<String, String> headers = new HashMap<>();

private static String version = null;

Expand Down Expand Up @@ -173,7 +173,6 @@ private static X509TrustManager createDefaultTrustManager() throws NoSuchAlgorit
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
return trustManager;
return (X509TrustManager) trustManagers[0];
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/razorpay/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

public class Constants {

private Constants(){

}

// API constants
static final String SCHEME = "https";
static final String HOSTNAME = "api.razorpay.com";
Expand Down Expand Up @@ -133,4 +137,10 @@ public class Constants {
static final String VIRTUAL_ACCOUNT_RECEIVERS = "virtual_accounts/%s/receivers";
static final String VIRTUAL_ACCOUNT_ALLOWEDPAYERS = "virtual_accounts/%s/allowed_payers";
static final String VIRTUAL_ACCOUNT_DELETE_ALLOWEDPAYERS = "virtual_accounts/%s/allowed_payers/%s";

static final String INVALID_RESPONSE_FROM_SERVER = "Invalid Response from server";

static final String RAZORPAY_SIGNATURE = "razorpay_signature";

static final String RAZORPAY_PAYMENT_ID = "razorpay_payment_id";
}
7 changes: 3 additions & 4 deletions src/main/java/com/razorpay/CustomTLSSocketFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

Expand Down Expand Up @@ -43,13 +42,13 @@ public Socket createSocket(Socket s, String host, int port, boolean autoClose)
}

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
public Socket createSocket(String host, int port) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
throws IOException, UnknownHostException {
throws IOException {
return enableTLSOnSocket(
internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
}
Expand All @@ -67,7 +66,7 @@ public Socket createSocket(InetAddress address, int port, InetAddress localAddre
}

private Socket enableTLSOnSocket(Socket socket) {
if (socket != null && (socket instanceof SSLSocket)) {
if ( socket instanceof SSLSocket) {
((SSLSocket) socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"});
}
return socket;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/razorpay/CustomerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ public Customer edit(String id, JSONObject request) throws RazorpayException {
/**
* It is wrapper of fetchAll with parameter here sending null defines fetchAll
* with a default values without filteration
* @throws RazorpayException
* @throws RazorpayException RazorpayException
* @return list of customers
*/
public List<Customer> fetchAll() throws RazorpayException {
return fetchAll(null);
}

/**
* This method get list of customers filtered by parameters @request
* @throws RazorpayException
* @throws RazorpayException RazorpayException
* @param request JSONObject request
* @return list of customers
*/
public List<Customer> fetchAll(JSONObject request) throws RazorpayException {
return getCollection(Constants.CUSTOMER_LIST, request);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/razorpay/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public abstract class Entity {

private JSONObject modelJson;

private final String CREATED_AT = "created_at";
private final String CAPTURED_AT = "captured_at";
private static final String CREATED_AT = "created_at";
private static final String CAPTURED_AT = "captured_at";

Entity(JSONObject jsonObject) {
this.modelJson = jsonObject;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/razorpay/FundAccountClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public List<FundAccount> fetchAll() throws RazorpayException {

/**
* This method get list of fundaccounts filtered by parameters @request
* @throws RazorpayException
* @throws RazorpayException RazorpayException
* @param request in JSONObject request
* @return List of fund accounts
*/
public List<FundAccount> fetchAll(JSONObject request) throws RazorpayException {
return getCollection(Constants.FUND_ACCOUNT_LIST, request);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/razorpay/PaymentLinkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.json.JSONObject;

import okhttp3.Response;

public class PaymentLinkClient extends ApiClient {

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/razorpay/QrCodeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.json.JSONObject;

import okhttp3.Response;

public class QrCodeClient extends ApiClient {

Expand All @@ -23,15 +22,18 @@ public QrCode fetch(String id) throws RazorpayException {
/**
* It is wrapper of fetchAll with parameter here sending null defines fetchAll
* with a default values without filteration
* @throws RazorpayException
* @throws RazorpayException RazorpayException
* @return List of QR Codes
*/
public List<QrCode> fetchAll() throws RazorpayException {
return fetchAll(null);
}

/**
* This method get list of QrCodes filtered by parameters @request
* @throws RazorpayException
* @throws RazorpayException RazorpayException
* @param request JSONObject request
* @return list of QR Codes
*/
public List<QrCode> fetchAll(JSONObject request) throws RazorpayException {
return getCollection(Constants.QRCODE_LIST, request);
Expand Down
Loading