Skip to content

Commit

Permalink
Added refunding of transactions to SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianberm committed Mar 11, 2021
1 parent a2e9465 commit 853713e
Show file tree
Hide file tree
Showing 8 changed files with 489 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -101,6 +101,24 @@ Retrieving transaction status
System.out.println(result.request.errorMessage);
```

Refunding a transaction
```Java
TransactionRefundService refund = new TransactionRefundService();
refund.setToken("token_here");
refund.setServiceId("SL-code-here");
refund.setAmount(200);
refund.setTransactionId("EX-code-inthis-field");
refund.setDescription("Test refund");
refund.startRequest();
nl.pay.sdk.refund.TransactionResult result = refund.getResult();
if (result.getAmountRefunded() == 0)
{
System.out.println(result.getError());
System.out.println(result.description);
}
System.out.println(result.getAmountRefunded());
```

### Contributing

Please file pull requests to this repository, when checked, this will be merged and included in next releases.
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>nl.pay</groupId>
<artifactId>sdk</artifactId>
<version>0.3.2</version>
<version>0.3.3</version>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand All @@ -24,6 +24,11 @@
<artifactId>commons-codec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
</dependency>
</dependencies>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
140 changes: 140 additions & 0 deletions src/main/java/nl/pay/sdk/TransactionRefundService.java
@@ -0,0 +1,140 @@
package nl.pay.sdk;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import nl.pay.sdk.resulterror.Result;
import nl.pay.sdk.refund.*;

public class TransactionRefundService implements ServiceRequest {
public String serviceId = "";
public String token = "";

public nl.pay.sdk.refund.Transaction refundData = new nl.pay.sdk.refund.Transaction();

private HttpServiceInteraction httpService = null;
private String resultObject = "";

public void setTransactionId(String val)
{
refundData.setTransactionId(val);
}
public void setAmount(Integer val)
{
refundData.setAmount(val);
}
public void setDescription(String val)
{
refundData.setDescription(val);
}
public void setProcessDate(String val) throws Exception {
refundData.setProcessDate(val);
}
public void setExchangeUrl(String val)
{
refundData.setExchangeUrl(val);
}
public void setCurrency(String val)
{
refundData.setCurrency(val);
}

public void setServiceId(String val) {
serviceId = val;
}

public void setToken(String val) {
token = val;
}

/**
* validateInput
* This function will do some basic validation to see if everything that's required has actually been set.
* FIXME: I want to be fixed in the future please
* @return always return true at the moment
*/
private boolean validateInput() {
return true;
}


public String startRequest() {
String urlBase = "https://token:" + token + "@rest-api.pay.nl/v3/Refund/transaction/json";

String data = Helper.addToUrl("serviceId", serviceId);
// For the love of ...... - Why doesn't this use authentication but still requires the token?!
data = data + "&" + Helper.addToUrl("token", token);

data = data + "&" + Helper.addToUrl("transactionId",refundData.getTransactionId());

if (refundData.getAmount() > 0)
{
data = data + "&" + Helper.addToUrl("amount",refundData.getAmount().toString());
}

if (refundData.getDescription().length() > 0)
{
data = data + "&" + Helper.addToUrl("description",refundData.getDescription());
}
if (refundData.getProcessDate().length() > 0)
{
data = data + "&" + Helper.addToUrl("processDate",refundData.getProcessDate());
}
if (refundData.getExchangeUrl().length() > 0)
{
data = data + "&" + Helper.addToUrl("exchangeUrl",refundData.getExchangeUrl());
}
if (refundData.getCurrency().length() > 0)
{
data = data + "&" + Helper.addToUrl("currency",refundData.getCurrency());
}


if (httpService == null) {
httpService = new HttpServiceInteraction();
}

httpService.setURL(urlBase);
httpService.setParamData(data);
try {
this.resultObject = httpService.getResponse();
return this.resultObject;
} catch (Exception ex) {
return ex.getMessage();
}
}

public TransactionResult getResult() {
if (this.resultObject.length() == 0) {
this.startRequest();
}

TransactionResult result;
Gson gson = new Gson();
try {
result = gson.fromJson(this.resultObject, TransactionResult.class);
}
catch (JsonSyntaxException exception) {
// Okay, we've got error... trying again...
try
{
Result error = gson.fromJson(this.resultObject, Result.class);
result = new TransactionResult();
result.internalInit();
result.request.result = error.request.result;
result.request.errorMessage = error.request.errorMessage;
result.request.errorId = error.request.errorId;

}
catch(JsonSyntaxException exception2) {
result = new TransactionResult();
result.internalInit();
result.request.result = "0";
result.request.errorMessage = "Unable to process API response.";
}
}

return result;
}


}
123 changes: 123 additions & 0 deletions src/main/java/nl/pay/sdk/refund/Transaction.java
@@ -0,0 +1,123 @@
package nl.pay.sdk.refund;

import org.apache.commons.validator.GenericValidator;

public class Transaction {
public String transactionId = "";
public Integer amount = 0;
public String description = "";
public String processDate = "";
public String exchangeUrl = "";
public String currency = "EUR";

/**
* Return the Transaction ID
* @return TransactionId
*/
public String getTransactionId() {
return transactionId;
}

/**
* Set the Transaction ID
* @param transactionId Transaction ID of related transaction
*/
public void setTransactionId(String transactionId) {
this.transactionId = transactionId;
}

/**
* Get the amount of refund in cents
* @return amount (cents)
*/
public Integer getAmount() {
return amount;
}

/**
* Set the amount of refund in cents
* @param amount (cents)
*/
public void setAmount(Integer amount) {
this.amount = amount;
}

/**
* Get the description of refund
* @return description
*/
public String getDescription() {
return description;
}

/**
* Set the description for refund (max 32 chars)
* @param description
*/
public void setDescription(String description) {
if (description.length() > 32)
{
this.description = description.substring(0,32);
}
else
{
this.description = description;
}
}

/**
* Get the date for processing the refund
* @return string
*/
public String getProcessDate() {
return processDate;
}

/**
* Set the date for processing the refund (dd-mm-YYYY)
* @param processDate
* @throws Exception
*/
public void setProcessDate(String processDate) throws Exception {
if (GenericValidator.isDate(processDate,"dd-MM-yyyy",true))
{
this.processDate = processDate;
}
else
{
throw new Exception("Invalid date, use dd-mm-yyyy");
}
}

/**
* Get the Exchange URL
* @return URL
*/
public String getExchangeUrl() {
return exchangeUrl;
}

/**
* Set the custom exchange URL
* @param exchangeUrl
*/
public void setExchangeUrl(String exchangeUrl) {
this.exchangeUrl = exchangeUrl;
}

/**
* Get the currency for the refund
* @return
*/
public String getCurrency() {
return currency;
}

/**
* Set the currency for the refund
* @param currency
*/
public void setCurrency(String currency) {
this.currency = currency;
}
}
56 changes: 56 additions & 0 deletions src/main/java/nl/pay/sdk/refund/TransactionResult.java
@@ -0,0 +1,56 @@
package nl.pay.sdk.refund;

import com.google.gson.annotations.SerializedName;

import java.util.List;

public class TransactionResult {
public TransactionResultRequest request;
public Integer amountRefunded;
public String description;

@SerializedName("refundedTransactions")
public List<TransactionResultRefundedTransaction> refundedTransactions;
// @SerializedName("failedTransactions")
// public List<TransactionResultFailedTransaction> failedTransactions;


public void internalInit()
{
request = new TransactionResultRequest();
// refundedTransactions = new ArrayList<TransactionResultRefundedTransactions>();
// failedTransactions = new ArrayList<TransactionResultFailedTransactions>();
}

public String getError()
{
if (request == null)
{
internalInit();
}
return request.errorMessage;
}

private boolean isReady()
{
if (request.result.equalsIgnoreCase("1"))
{
return true;
}
return false;
}

public Integer getAmountRefunded()
{
if (request == null)
{
internalInit();
}
if (!isReady())
{
return 0;
}

return amountRefunded;
}
}

0 comments on commit 853713e

Please sign in to comment.