Skip to content
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
34 changes: 17 additions & 17 deletions src/main/java/com/phonepe/sdk/pg/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@

@Getter
public enum Env {
SANDBOX(
BaseUrl.SANDBOX_PG_HOST_URL,
BaseUrl.SANDBOX_OAUTH_HOST_URL,
BaseUrl.SANDBOX_EVENTS_HOST_URL),
PRODUCTION(
BaseUrl.PRODUCTION_PG_HOST_URL,
BaseUrl.PRODUCTION_OAUTH_HOST_URL,
BaseUrl.PRODUCTION_EVENTS_HOST_URL),
TEST(BaseUrl.TESTING_URL, BaseUrl.TESTING_URL, BaseUrl.TESTING_URL);

private final String pgHostUrl;
private final String oAuthHostUrl;
private final String eventsHostUrl;
SANDBOX(
BaseUrl.SANDBOX_PG_HOST_URL,
BaseUrl.SANDBOX_OAUTH_HOST_URL,
BaseUrl.SANDBOX_EVENTS_HOST_URL), PRODUCTION(
BaseUrl.PRODUCTION_PG_HOST_URL,
BaseUrl.PRODUCTION_OAUTH_HOST_URL,
BaseUrl.PRODUCTION_EVENTS_HOST_URL), TEST(BaseUrl.TESTING_URL, BaseUrl.TESTING_URL,
BaseUrl.TESTING_URL);

Env(String pgHostUrl, String oAuthHostUrl, String eventsHostUrl) {
this.pgHostUrl = pgHostUrl;
this.oAuthHostUrl = oAuthHostUrl;
this.eventsHostUrl = eventsHostUrl;
}
private final String pgHostUrl;
private final String oAuthHostUrl;
private final String eventsHostUrl;

Env(String pgHostUrl, String oAuthHostUrl, String eventsHostUrl) {
this.pgHostUrl = pgHostUrl;
this.oAuthHostUrl = oAuthHostUrl;
this.eventsHostUrl = eventsHostUrl;
}
}
146 changes: 71 additions & 75 deletions src/main/java/com/phonepe/sdk/pg/common/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,84 +39,80 @@
@Getter
public abstract class BaseClient {

private final ObjectMapper objectMapper;
private final OkHttpClient okHttpClient;
private final Env env;
private TokenService tokenService;
private CredentialConfig credentialConfig;
protected EventPublisherFactory eventPublisherFactory;
protected EventPublisher eventPublisher;
private boolean shouldPublishEvents;
private final ObjectMapper objectMapper;
private final OkHttpClient okHttpClient;
private final Env env;
private TokenService tokenService;
private CredentialConfig credentialConfig;
protected EventPublisherFactory eventPublisherFactory;
protected EventPublisher eventPublisher;
private boolean shouldPublishEvents;

protected BaseClient(
String clientId,
String clientSecret,
Integer clientVersion,
Env env,
boolean shouldPublishEvents) {
this.okHttpClient = new OkHttpClient();
this.objectMapper = new ObjectMapper();
this.env = env;
this.credentialConfig =
CredentialConfig.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.clientVersion(clientVersion)
.build();
this.shouldPublishEvents = shouldPublishEvents;
this.eventPublisherFactory =
new EventPublisherFactory(
this.getObjectMapper(), this.getOkHttpClient(), env.getEventsHostUrl());
this.eventPublisher = this.eventPublisherFactory.getEventPublisher(shouldPublishEvents);
this.tokenService =
new TokenService(
this.okHttpClient,
this.objectMapper,
credentialConfig,
this.env,
this.eventPublisher);
this.eventPublisher.startPublishingEvents(tokenService::getAuthToken);
}
protected BaseClient(
String clientId,
String clientSecret,
Integer clientVersion,
Env env,
boolean shouldPublishEvents) {
this.okHttpClient = new OkHttpClient();
this.objectMapper = new ObjectMapper();
this.env = env;
this.credentialConfig = CredentialConfig.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.clientVersion(clientVersion)
.build();
this.shouldPublishEvents = shouldPublishEvents;
this.eventPublisherFactory = new EventPublisherFactory(
this.getObjectMapper(), this.getOkHttpClient(), env.getEventsHostUrl());
this.eventPublisher = this.eventPublisherFactory.getEventPublisher(shouldPublishEvents);
this.tokenService = new TokenService(
this.okHttpClient,
this.objectMapper,
credentialConfig,
this.env,
this.eventPublisher);
this.eventPublisher.startPublishingEvents(tokenService::getAuthToken);
}

@SneakyThrows
protected <T, R> T requestViaAuthRefresh(
HttpMethodType methodName,
R requestData,
String url,
Map<String, String> queryParams,
TypeReference<T> responseTypeReference,
List<HttpHeaderPair> headers) {
List<HttpHeaderPair> httpHeaders = new ArrayList<>(headers);
HttpCommand<T, R> httpCommand =
HttpCommand.<T, R>builder()
.client(this.okHttpClient)
.objectMapper(this.objectMapper)
.responseTypeReference(responseTypeReference)
.methodName(methodName)
.headers(addAuthHeader(httpHeaders))
.requestData(requestData)
.hostURL(this.env.getPgHostUrl())
.encodingType(APPLICATION_JSON)
.queryParams(queryParams)
.url(url)
.build();
@SneakyThrows
protected <T, R> T requestViaAuthRefresh(
HttpMethodType methodName,
R requestData,
String url,
Map<String, String> queryParams,
TypeReference<T> responseTypeReference,
List<HttpHeaderPair> headers) {
List<HttpHeaderPair> httpHeaders = new ArrayList<>(headers);
HttpCommand<T, R> httpCommand = HttpCommand.<T, R>builder()
.client(this.okHttpClient)
.objectMapper(this.objectMapper)
.responseTypeReference(responseTypeReference)
.methodName(methodName)
.headers(addAuthHeader(httpHeaders))
.requestData(requestData)
.hostURL(this.env.getPgHostUrl())
.encodingType(APPLICATION_JSON)
.queryParams(queryParams)
.url(url)
.build();

try {
return httpCommand.execute();
} catch (UnauthorizedAccess unauthorizedAccess) {
tokenService.forceRefreshToken();
throw unauthorizedAccess;
}
}
try {
return httpCommand.execute();
} catch (UnauthorizedAccess unauthorizedAccess) {
tokenService.forceRefreshToken();
throw unauthorizedAccess;
}
}

@SneakyThrows
protected List<HttpHeaderPair> addAuthHeader(List<HttpHeaderPair> headers) {
headers.add(
HttpHeaderPair.builder()
.key(Headers.OAUTH_AUTHORIZATION)
.value(tokenService.getAuthToken())
.build());
@SneakyThrows
protected List<HttpHeaderPair> addAuthHeader(List<HttpHeaderPair> headers) {
headers.add(
HttpHeaderPair.builder()
.key(Headers.OAUTH_AUTHORIZATION)
.value(tokenService.getAuthToken())
.build());

return headers;
}
return headers;
}
}
82 changes: 41 additions & 41 deletions src/main/java/com/phonepe/sdk/pg/common/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,49 @@
@UtilityClass
public class CommonUtils {

/**
* Computes the SHA-256 hash of the provided data using the SHA-256 algorithm.
*
* @param args the objects to compute the hash for
* @return the computed hash as a string
*/
public String calculateSha256(Object... args) {
List<String> listOfArgs = convertArgsToList(args);
String data = String.join(":", listOfArgs);
return shaHex(data, ShaAlgorithm.SHA256);
}
/**
* Computes the SHA-256 hash of the provided data using the SHA-256 algorithm.
*
* @param args the objects to compute the hash for
* @return the computed hash as a string
*/
public String calculateSha256(Object... args) {
List<String> listOfArgs = convertArgsToList(args);
String data = String.join(":", listOfArgs);
return shaHex(data, ShaAlgorithm.SHA256);
}

public List<String> convertArgsToList(Object... args) {
List<String> list = new ArrayList<>();
for (Object arg : args) {
list.add(arg.toString());
}
return list;
}
public List<String> convertArgsToList(Object... args) {
List<String> list = new ArrayList<>();
for (Object arg : args) {
list.add(arg.toString());
}
return list;
}

/**
* Computes the SHA-256 hash of the provided data using the specified algorithm.
*
* @param data the data to compute the hash for
* @param algorithm the SHA algorithm to use (currently only supports SHA-256)
* @return the computed hash as a hexadecimal string
*/
public String shaHex(String data, ShaAlgorithm algorithm) {
switch (algorithm) {
case SHA256:
return DigestUtils.sha256Hex(data);
default:
return data;
}
}
/**
* Computes the SHA-256 hash of the provided data using the specified algorithm.
*
* @param data the data to compute the hash for
* @param algorithm the SHA algorithm to use (currently only supports SHA-256)
* @return the computed hash as a hexadecimal string
*/
public String shaHex(String data, ShaAlgorithm algorithm) {
switch (algorithm) {
case SHA256:
return DigestUtils.sha256Hex(data);
default:
return data;
}
}

/** Enum representing the supported SHA algorithms. Currently, only SHA-256 is supported. */
private enum ShaAlgorithm {
SHA256;
}
/** Enum representing the supported SHA algorithms. Currently, only SHA-256 is supported. */
private enum ShaAlgorithm {
SHA256;
}

public boolean isCallbackValid(String username, String password, String authorization) {
String sha256hash = calculateSha256(username, password);
return Objects.equals(sha256hash, authorization);
}
public boolean isCallbackValid(String username, String password, String authorization) {
String sha256hash = calculateSha256(username, password);
return Objects.equals(sha256hash, authorization);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
@NoArgsConstructor
public class CredentialConfig {

@NotBlank private String clientId;
@NotBlank
private String clientId;

@NotBlank private String clientSecret;
@NotBlank
private String clientSecret;

@NotBlank private Integer clientVersion;
@NotBlank
private Integer clientVersion;
}
18 changes: 7 additions & 11 deletions src/main/java/com/phonepe/sdk/pg/common/constants/BaseUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@
@UtilityClass
public class BaseUrl {

public static final String PRODUCTION_PG_HOST_URL = "https://api.phonepe.com/apis/pg";
public static final String SANDBOX_PG_HOST_URL =
"https://api-preprod.phonepe.com/apis/pg-sandbox";
public static final String PRODUCTION_PG_HOST_URL = "https://api.phonepe.com/apis/pg";
public static final String SANDBOX_PG_HOST_URL = "https://api-preprod.phonepe.com/apis/pg-sandbox";

public static final String PRODUCTION_OAUTH_HOST_URL =
"https://api.phonepe.com/apis/identity-manager";
public static final String SANDBOX_OAUTH_HOST_URL =
"https://api-preprod.phonepe.com/apis/pg-sandbox";
public static final String PRODUCTION_OAUTH_HOST_URL = "https://api.phonepe.com/apis/identity-manager";
public static final String SANDBOX_OAUTH_HOST_URL = "https://api-preprod.phonepe.com/apis/pg-sandbox";

public static final String PRODUCTION_EVENTS_HOST_URL =
"https://api.phonepe.com/apis/pg-ingestion";
public static final String SANDBOX_EVENTS_HOST_URL = "http://locahost";
public static final String PRODUCTION_EVENTS_HOST_URL = "https://api.phonepe.com/apis/pg-ingestion";
public static final String SANDBOX_EVENTS_HOST_URL = "http://locahost";

public static final String TESTING_URL = "http://localhost:30419";
public static final String TESTING_URL = "http://localhost:30419";
}
52 changes: 26 additions & 26 deletions src/main/java/com/phonepe/sdk/pg/common/constants/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@
@Slf4j
public class Headers {

private final Properties properties = new Properties();
private final String PROPERTIES_FILE_NAME = "/sdk.properties";
private final Properties properties = new Properties();
private final String PROPERTIES_FILE_NAME = "/sdk.properties";

static {
try (InputStream input = Headers.class.getResourceAsStream(PROPERTIES_FILE_NAME)) {
if (input == null) {
log.error("Could not find {}", PROPERTIES_FILE_NAME);
} else {
properties.load(input);
}
} catch (Exception e) {
log.error("Failed to load SDK properties: {}", e.getMessage());
}
}
static {
try (InputStream input = Headers.class.getResourceAsStream(PROPERTIES_FILE_NAME)) {
if (input == null) {
log.error("Could not find {}", PROPERTIES_FILE_NAME);
} else {
properties.load(input);
}
} catch (Exception e) {
log.error("Failed to load SDK properties: {}", e.getMessage());
}
}

public static final String API_VERSION = "V2";
public static final String SUBSCRIPTION_API_VERSION = properties.getProperty("sdk.version");
public static final String INTEGRATION = "API";
public static final String SDK_VERSION = properties.getProperty("sdk.version");
public static final String SDK_TYPE = "BACKEND_JAVA_SDK";
public static final String SOURCE = "x-source";
public static final String SOURCE_VERSION = "x-source-version";
public static final String SOURCE_PLATFORM = "x-source-platform";
public static final String SOURCE_PLATFORM_VERSION = "x-source-platform-version";
public static final String OAUTH_AUTHORIZATION = "Authorization";
public static final String CONTENT_TYPE = "Content-Type";
public static final String ACCEPT = "Accept";
public static final String X_DEVICE_OS = "x-device-os";
public static final String API_VERSION = "V2";
public static final String SUBSCRIPTION_API_VERSION = properties.getProperty("sdk.version");
public static final String INTEGRATION = "API";
public static final String SDK_VERSION = properties.getProperty("sdk.version");
public static final String SDK_TYPE = "BACKEND_JAVA_SDK";
public static final String SOURCE = "x-source";
public static final String SOURCE_VERSION = "x-source-version";
public static final String SOURCE_PLATFORM = "x-source-platform";
public static final String SOURCE_PLATFORM_VERSION = "x-source-platform-version";
public static final String OAUTH_AUTHORIZATION = "Authorization";
public static final String CONTENT_TYPE = "Content-Type";
public static final String ACCEPT = "Accept";
public static final String X_DEVICE_OS = "x-device-os";
}
Loading
Loading