Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added refunding of transactions to SDK
- Loading branch information
1 parent
a2e9465
commit 853713e
Showing
8 changed files
with
489 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.