Skip to content

Commit

Permalink
Merge pull request #7 from v-pos/sm-handle-sandbox
Browse files Browse the repository at this point in the history
Sandbox
  • Loading branch information
smaziano authored Sep 22, 2021
2 parents dae90f9 + eb8595a commit f1b2cd2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ App.java
target/*
.idea
*.iml
.classpath
.project
.settings/*
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ interacting with the API using this library:
| `MERCHANT_VPOS_TOKEN` | The API token provided by vPOS | true |
| `PAYMENT_CALLBACK_URL` | The URL that will handle payment notifications | false |
| `REFUND_CALLBACK_URL` | The URL that will handle refund notifications | false |
| `VPOS_ENVIRONMENT` | The vPOS environment, leave empty for `sandbox` mode and use `"prd"` for `production`. | false |

or using one of the optional arguments

Expand All @@ -83,7 +82,6 @@ or using one of the optional arguments
| `supervisor_card` | Merchant Supervisor Card number provided by EMIS | `string`
| `payment_callback_url` | Merchant application JSON endpoint to accept the callback payment response | `string`
| `refund_callback_url` | Merchant application JSON endpoint to accept the callback refund response | `string`
| `environment` | The vPOS environment, leave empty for `sandbox` mode and use `"PRD"` for `production`. | `string` |

Don't have this information? [Talk to us]([email protected])

Expand All @@ -99,13 +97,6 @@ be able to authenticate and communicate effectively with our API using this libr

The next section will show the various payment actions that can be performed by you, the merchant.

### Get all Transactions
This endpoint retrieves all transactions.

```java
var transactions = merchant.getTransactions();
```

### Get a specific Transaction
Retrieves a transaction given a valid transaction ID.

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

<groupId>ao.vpos.vpos</groupId>
<artifactId>vpos</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>

<name>vpos</name>
<url>https://github.com/nextbss/vpos-java</url>
<url>https://github.com/v-pos/vpos-java</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
67 changes: 13 additions & 54 deletions src/main/java/ao/vpos/vpos/Vpos.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
Expand All @@ -24,8 +21,7 @@
import java.util.UUID;

public class Vpos {
private static final String PRODUCTION_BASE_URL = "https://api.vpos.ao";
private static final String SANDBOX_BASE_URL = "https://sandbox.vpos.ao";
private static final String HOST = "https://vpos.ao";
private final HttpClient client = HttpClient.newHttpClient();

private URL baseUrl;
Expand All @@ -37,80 +33,52 @@ public class Vpos {

private static final int BEGIN_LOCATION_INDEX = 18;

public enum Environment {
PRODUCTION,
SANDBOX
}

// constructors
public Vpos() {
public Vpos() throws MalformedURLException {
this.token = System.getenv("MERCHANT_VPOS_TOKEN");
this.posId = System.getenv("GPO_POS_ID");
this.supervisorCard = System.getenv("GPO_SUPERVISOR_CARD");
this.paymentCallbackUrl = System.getenv("PAYMENT_CALLBACK_URL");
this.refundCallbackUrl = System.getenv("REFUND_CALLBACK_URL");

try {
String environmentValue = System.getenv("VPOS_ENVIRONMENT");
if (environmentValue != null &&
environmentValue.equalsIgnoreCase("prd")) {
this.baseUrl = new URL(PRODUCTION_BASE_URL);
} else {
this.baseUrl = new URL(SANDBOX_BASE_URL);
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

public Vpos(@Nonnull Environment environment) {
this();
this.token = System.getenv("MERCHANT_VPOS_TOKEN");
this.baseUrl = getBaseUrlFromEnvironment(environment);
this.baseUrl = new URL(HOST);
}

public Vpos(@Nonnull String newToken) {
public Vpos(@Nonnull String newToken) throws MalformedURLException {
this();
this.token = newToken;
this.baseUrl = new URL(HOST);
}

public Vpos(@Nonnull Environment environment, @Nonnull String newToken) {
public Vpos(@Nonnull String newToken, @Nonnull String posId) throws MalformedURLException {
this(newToken);
this.baseUrl = getBaseUrlFromEnvironment(environment);
}

public Vpos(@Nonnull Environment environment, @Nonnull String newToken, @Nonnull String posId) {
this(environment, newToken);
this.posId = posId;
this.baseUrl = new URL(HOST);
}

public Vpos(
@Nonnull Environment environment,
@Nonnull String newToken,
@Nonnull String posId,
@Nonnull String supervisorCard) {
this(environment, newToken, posId);
@Nonnull String supervisorCard) throws MalformedURLException {
this(newToken, posId);
this.supervisorCard = supervisorCard;
}

public Vpos(
@Nonnull Environment environment,
@Nonnull String newToken,
@Nonnull String posId,
@Nonnull String supervisorCard,
@Nonnull String paymentCallbackUrl) {
this(environment, newToken, posId, supervisorCard);
@Nonnull String paymentCallbackUrl) throws MalformedURLException {
this(newToken, posId, supervisorCard);
this.paymentCallbackUrl = paymentCallbackUrl;
}

public Vpos(
@Nonnull Environment environment,
@Nonnull String newToken,
@Nonnull String posId,
@Nonnull String supervisorCard,
@Nonnull String paymentCallbackUrl,
@Nonnull String refundCallbackUrl) {
this(environment, newToken, posId, supervisorCard, paymentCallbackUrl);
@Nonnull String refundCallbackUrl) throws MalformedURLException {
this(newToken, posId, supervisorCard, paymentCallbackUrl);
this.refundCallbackUrl = refundCallbackUrl;
}

Expand Down Expand Up @@ -576,13 +544,4 @@ public String getTransactionId(String location) throws IOException, InterruptedE
var endLocationIndex = location.length() - 1;
return location.substring(BEGIN_LOCATION_INDEX, endLocationIndex);
}

@NotNull
private URL getBaseUrlFromEnvironment(Environment environment) {
try {
return (environment == Environment.SANDBOX) ? new URL(PRODUCTION_BASE_URL) : new URL(SANDBOX_BASE_URL);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}
35 changes: 2 additions & 33 deletions src/test/java/ao/vpos/vpos/VposTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,13 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;

public class VposTest {
// NEGATIVES
@Test
public void itShouldNotGetAllTransactionsIfTokenInvalid() throws IOException, InterruptedException {
var merchant = new Vpos("invalid-token");
ApiException exception = null;
Response<List<Transaction>> response = null;
try {
response = merchant.getTransactions();
} catch (ApiException e) {
exception = e;
}

assertEquals(401, exception.getStatus());
assertEquals("\"Unauthorized\"", exception.getResponseBody());
}

@Test
public void itShouldNotGetSingleTransactionIfParentTransactionIdDoesNotExist() throws IOException, InterruptedException {

Expand Down Expand Up @@ -140,21 +124,6 @@ public void itShouldGetSingleTransaction() throws IOException, InterruptedExcept
assertEquals("900111222", returnedResponse.getData().getMobile());
}

@Test
public void itShouldGetAllTransactions() throws IOException, InterruptedException, ApiException {
var merchant = new Vpos();
var response = merchant.getTransactions();
assertEquals(200, response.getStatusCode());
assertEquals("OK", response.getMessage());
assertNotNull(response.getData().get(0).getAmount());
assertNotNull(response.getData().get(0).getMobile());
assertNotNull(response.getData().get(0).getId());
assertNotNull(response.getData().get(0).getStatus());
assertNotNull(response.getData().get(0).getPosId());
assertNotNull(response.getData().get(0).getType());
assertNotNull(response.getData().get(0).getStatusDatetime());
}

@Test
public void itShouldCreateNewPaymentTransaction() throws IOException, InterruptedException, ApiException {
var merchant = new Vpos();
Expand All @@ -167,7 +136,7 @@ public void itShouldCreateNewPaymentTransaction() throws IOException, Interrupte
@Test
public void itShouldCreateNewRefundTransaction() throws IOException, InterruptedException, ApiException {
var merchant = new Vpos();
var response = merchant.newPayment("900111222", "123.45");
var response = merchant.newPayment("900000000", "123.45");
var transactionId = merchant.getTransactionId(response.getLocation());

TimeUnit.SECONDS.sleep(10);
Expand All @@ -185,7 +154,7 @@ public void itShouldCreateNewRefundTransaction() throws IOException, Interrupted
@Test
public void itShouldGetRequestWhileTransactionIsQueued() throws IOException, InterruptedException, ApiException {
var merchant = new Vpos();
var response = merchant.newPayment("900111222", "123.45");
var response = merchant.newPayment("900000000", "123.45");
var transactionId = merchant.getTransactionId(response.getLocation());

Response<Request> request = merchant.getRequest(transactionId);
Expand Down

0 comments on commit f1b2cd2

Please sign in to comment.