fi.foyt.paytrail.PaytrailService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk Show documentation
Show all versions of sdk Show documentation
Java REST API SDK for Paytrail (http://paytrail.com/)
The newest version!
package fi.foyt.paytrail;
import java.io.IOException;
import java.io.Serializable;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import fi.foyt.paytrail.io.IOHandler;
import fi.foyt.paytrail.io.IOHandlerResult;
import fi.foyt.paytrail.json.Marshaller;
import fi.foyt.paytrail.rest.JsonError;
import fi.foyt.paytrail.rest.OrderDetails;
import fi.foyt.paytrail.rest.Payment;
import fi.foyt.paytrail.rest.Product;
import fi.foyt.paytrail.rest.Result;
public class PaytrailService implements Serializable {
private static final long serialVersionUID = 3162475976498158463L;
private final static String SERVICE_URL = "https://payment.verkkomaksut.fi";
public PaytrailService(IOHandler ioHandler, Marshaller marshaller, String merchantId, String merchantSecret) {
this(ioHandler, marshaller, merchantId, merchantSecret, SERVICE_URL);
}
public PaytrailService(IOHandler ioHandler, Marshaller marshaller, String merchantId, String merchantSecret, String serviceUrl) {
this.ioHandler = ioHandler;
this.marshaller = marshaller;
this.merchantId = merchantId;
this.merchantSecret = merchantSecret;
this.serviceUrl = serviceUrl;
}
public Payment addProduct(Payment payment, String title, String code, Double amount, Double price, Double vat, Double discount, Integer type) throws PaytrailException {
Product product = new Product(title, code, amount, price, vat, discount, type);
OrderDetails orderDetails = payment.getOrderDetails();
if (orderDetails == null) {
throw new PaytrailException("orderDetails is null");
}
Product[] products = orderDetails.getProducts();
if (products == null) {
orderDetails.setProducts(new Product[] {
product
});
} else {
orderDetails.setProducts(ArrayUtils.add(products, product));
}
return payment;
}
/**
* Get url for payment
*
* @param payment payment
* @return Result result
* @throws PaytrailException
*/
public Result processPayment(Payment payment) throws PaytrailException {
try {
String data = marshaller.objectToString(payment);
String url = serviceUrl + "/api-payment/create";
IOHandlerResult requestResult = postJsonRequest(url, data);
if (requestResult.getCode() != 201) {
JsonError jsonError = marshaller.stringToObject(JsonError.class, requestResult.getResponse());
throw new PaytrailException(jsonError.getErrorMessage());
}
Result result = marshaller.stringToObject(Result.class, requestResult.getResponse());
return result;
} catch (IOException e) {
throw new PaytrailException(e);
}
}
/**
* This function can be used to validate parameters returned by return and notify requests.
* Parameters must be validated in order to avoid hacking of payment confirmation.
*/
public boolean confirmPayment(String orderNumber, String timestamp, String paid, String method, String authCode) {
String base = new StringBuilder()
.append(orderNumber)
.append('|')
.append(timestamp)
.append('|')
.append(paid)
.append('|')
.append(method)
.append('|')
.append(merchantSecret)
.toString();
return
StringUtils.equals(
StringUtils.upperCase(DigestUtils.md5Hex(base)),
authCode
);
}
private IOHandlerResult postJsonRequest(String url, String data) throws IOException {
return ioHandler.doPost(merchantId, merchantSecret, url, data);
}
private Marshaller marshaller;
private IOHandler ioHandler;
private String merchantId;
private String merchantSecret;
private String serviceUrl;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy