use of net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException in project styx by petafuel.
the class BerlinGroupCS method getStatus.
@Override
public ConsentStatus getStatus(AISRequest consentStatusRequest) throws BankRequestFailedException {
this.setUrl(this.url + consentStatusRequest.getServicePath());
this.createBody(RequestType.GET);
this.createHeaders(consentStatusRequest);
try (Response response = this.execute();
Jsonb jsonb = JsonbBuilder.create()) {
String responseBody = extractResponseBody(response, 200);
PersistentConsent persistentConsent = new PersistentConsent();
Consent consent = jsonb.fromJson(responseBody, Consent.class);
consent.setId(consentStatusRequest.getConsentId());
persistentConsent.updateState(consent, consent.getState());
return consent.getState();
} catch (Exception e) {
throw new BankRequestFailedException(e.getMessage(), e);
}
}
use of net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException in project styx by petafuel.
the class BerlinGroupPIS method getPayment.
// not possible to avoid code complexity at this point
@SuppressWarnings("squid:S3776")
@Override
public InitializablePayment getPayment(PISRequest xs2AGetRequest) throws BankRequestFailedException {
this.setUrl(this.url + xs2AGetRequest.getServicePath());
this.createBody(RequestType.GET);
if (xs2AGetRequest.getPaymentProduct().isXml()) {
xs2AGetRequest.addHeader(XS2AHeader.ACCEPT, XML.toString());
} else {
xs2AGetRequest.addHeader(XS2AHeader.ACCEPT, JSON.toString());
}
this.createHeaders(xs2AGetRequest);
try (Response response = this.execute();
Jsonb jsonb = JsonbBuilder.create()) {
String contentType;
if ((contentType = response.headers().get("content-type")) == null) {
throw new BankRequestFailedException("Content-Type Header is not set, parsing of json or xml is not possible", response.code());
}
String responseBody = extractResponseBody(response, 200);
if (contentType.contains(MediaType.APPLICATION_JSON)) {
if (xs2AGetRequest.getPaymentService().equals(PaymentService.PERIODIC_PAYMENTS)) {
return jsonb.fromJson(responseBody, PeriodicPayment.class);
} else if (xs2AGetRequest.getPaymentService().equals(PaymentService.PAYMENTS)) {
SinglePaymentWrapper singlePaymentWrapper = jsonb.fromJson(responseBody, SinglePaymentWrapper.class);
return singlePaymentWrapper.getPayment() != null ? singlePaymentWrapper.getPayment() : singlePaymentWrapper;
} else {
return jsonb.fromJson(responseBody, BulkPayment.class);
}
} else {
if (xs2AGetRequest.getPaymentService().equals(PaymentService.PERIODIC_PAYMENTS)) {
ByteArrayDataSource datasource = new ByteArrayDataSource(responseBody, "multipart/form-data");
MimeMultipart multipart = new MimeMultipart(datasource);
int count = multipart.getCount();
StringBuilder xmlSb = new StringBuilder();
StringBuilder jsonStringBuilder = new StringBuilder();
String str;
for (int i = 0; i < count; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) bodyPart.getContent()));
if (bodyPart.getContentType().contains("xml")) {
while ((str = reader.readLine()) != null) {
xmlSb.append(str);
}
} else {
while ((str = reader.readLine()) != null) {
jsonStringBuilder.append(str);
}
}
}
SEPAParser sepaParser = new SEPAParser(xmlSb.toString());
String jsonString = jsonStringBuilder.toString();
return PeriodicPaymentMultipartBodySerializer.xmlDeserialize(sepaParser.parseSEPA().getSepaDocument(), jsonb.fromJson(jsonString, PeriodicPayment.class));
} else {
SEPAParser sepaParser = new SEPAParser(responseBody);
return PaymentSerializer.xmlDeserialize(sepaParser.parseSEPA().getSepaDocument(), xs2AGetRequest.getPaymentService());
}
}
} catch (Exception e) {
throw new BankRequestFailedException(e.getMessage(), e);
}
}
use of net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException in project styx by petafuel.
the class BerlinGroupPIS method getPaymentStatus.
@Override
public PaymentStatus getPaymentStatus(PISRequest xs2AGetRequest) throws BankRequestFailedException {
this.setUrl(this.url + xs2AGetRequest.getServicePath());
this.createBody(RequestType.GET);
if (xs2AGetRequest.getPaymentProduct().isXml()) {
xs2AGetRequest.addHeader(XS2AHeader.ACCEPT, XML.toString());
} else {
xs2AGetRequest.addHeader(XS2AHeader.ACCEPT, JSON.toString());
}
this.createHeaders(xs2AGetRequest);
try (Response response = this.execute();
Jsonb jsonb = JsonbBuilder.create()) {
String contentType;
if ((contentType = response.headers().get("content-type")) == null) {
throw new BankRequestFailedException("Content-Type Header is not set, parsing of json or xml is not possible", response.code());
}
String responseBody = extractResponseBody(response, 200);
if (contentType.contains(MediaType.APPLICATION_JSON)) {
return jsonb.fromJson(responseBody, PaymentStatus.class);
} else {
ReportConverter converter = new ReportConverter();
TransactionReport report = converter.processReport(responseBody);
return new PaymentStatus(TransactionStatus.valueOf(report.getStatus()), null);
}
} catch (IOException | SEPAParsingException e) {
throw new BankRequestFailedException(e.getMessage(), e);
} catch (Exception e) {
throw new SerializerException("Unable to deserialize json to PaymentStatus", e);
}
}
use of net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException in project styx by petafuel.
the class BerlinGroupPIS method startAuthorisation.
@Override
public /*
* BerlinGroup 1.2: the authorisationId of an Authorisation Resource is not a json key in the start SCA Request
* Therefore we take it out of an authorisation link that contains this id
*/
SCA startAuthorisation(SCARequest xs2ARequest) throws BankRequestFailedException {
this.setUrl(this.url + xs2ARequest.getServicePath());
this.createBody(RequestType.POST, JSON, xs2ARequest);
this.createHeaders(xs2ARequest);
try (Response response = this.execute();
Jsonb jsonb = JsonbBuilder.create()) {
String responseBody = extractResponseBody(response, 201);
SCA sca = jsonb.fromJson(responseBody, SCA.class);
SCAUtils.parseSCAApproach(sca, response);
// extract the authorisation id out of an Href Object that contains the authorisations/... route
for (Map.Entry<LinkType, Links.Href> entry : sca.getLinks().getUrlMapping().entrySet()) {
if (entry.getValue().getUrl().contains("authorisations/")) {
String[] routeParts = entry.getValue().getUrl().split("/");
sca.setAuthorisationId(routeParts[routeParts.length - 1]);
break;
}
}
return sca;
} catch (Exception e) {
throw new BankRequestFailedException(e.getMessage(), e);
}
}
use of net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException in project styx by petafuel.
the class BerlinGroupAIS method getBalancesByAccount.
@Override
public BalanceContainer getBalancesByAccount(AISRequest request) throws BankRequestFailedException {
this.setUrl(this.url + request.getServicePath() + this.getHttpQueryString(request));
this.createBody(RequestType.GET);
this.createHeaders(request);
try (Response response = this.execute();
Jsonb jsonb = JsonbBuilder.create()) {
String responseBody = extractResponseBody(response, 200);
return jsonb.fromJson(responseBody, BalanceContainer.class);
} catch (Exception e) {
throw new BankRequestFailedException(e.getMessage(), e);
}
}
Aggregations