Search in sources :

Example 6 with BankRequestFailedException

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);
    }
}
Also used : Response(okhttp3.Response) Jsonb(javax.json.bind.Jsonb) Consent(net.petafuel.styx.core.xs2a.entities.Consent) PersistentConsent(net.petafuel.styx.core.persistence.layers.PersistentConsent) PersistentConsent(net.petafuel.styx.core.persistence.layers.PersistentConsent) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) IOException(java.io.IOException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)

Example 7 with BankRequestFailedException

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);
    }
}
Also used : BodyPart(javax.mail.BodyPart) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SEPAParser(net.petafuel.jsepa.SEPAParser) BulkPayment(net.petafuel.styx.core.xs2a.entities.BulkPayment) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) SEPAParsingException(net.petafuel.jsepa.exception.SEPAParsingException) IOException(java.io.IOException) Response(okhttp3.Response) PeriodicPayment(net.petafuel.styx.core.xs2a.entities.PeriodicPayment) SinglePaymentWrapper(net.petafuel.styx.core.xs2a.SinglePaymentWrapper) Jsonb(javax.json.bind.Jsonb) MimeMultipart(javax.mail.internet.MimeMultipart) BufferedReader(java.io.BufferedReader) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 8 with BankRequestFailedException

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);
    }
}
Also used : Response(okhttp3.Response) Jsonb(javax.json.bind.Jsonb) ReportConverter(net.petafuel.jsepa.facades.ReportConverter) SEPAParsingException(net.petafuel.jsepa.exception.SEPAParsingException) TransactionReport(net.petafuel.jsepa.model.pain002.TransactionReport) IOException(java.io.IOException) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) PaymentStatus(net.petafuel.styx.core.xs2a.entities.PaymentStatus) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) SEPAParsingException(net.petafuel.jsepa.exception.SEPAParsingException) IOException(java.io.IOException)

Example 9 with BankRequestFailedException

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);
    }
}
Also used : Response(okhttp3.Response) SCA(net.petafuel.styx.core.xs2a.entities.SCA) Jsonb(javax.json.bind.Jsonb) LinkType(net.petafuel.styx.core.xs2a.entities.LinkType) Map(java.util.Map) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)

Example 10 with BankRequestFailedException

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);
    }
}
Also used : ReadAccountDetailsResponse(net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.ReadAccountDetailsResponse) ReadTransactionDetailsResponse(net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.ReadTransactionDetailsResponse) Response(okhttp3.Response) ReadAccountListResponse(net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.ReadAccountListResponse) Jsonb(javax.json.bind.Jsonb) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)

Aggregations

BankRequestFailedException (net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException)28 Jsonb (javax.json.bind.Jsonb)22 Response (okhttp3.Response)22 SerializerException (net.petafuel.styx.core.xs2a.exceptions.SerializerException)8 IOException (java.io.IOException)7 SCA (net.petafuel.styx.core.xs2a.entities.SCA)7 GetAuthorisationResponse (net.petafuel.styx.core.xs2a.standards.berlingroup.v1_3.http.GetAuthorisationResponse)7 GetSCAStatusResponse (net.petafuel.styx.core.xs2a.standards.berlingroup.v1_3.http.GetSCAStatusResponse)7 PersistentConsent (net.petafuel.styx.core.persistence.layers.PersistentConsent)5 Consent (net.petafuel.styx.core.xs2a.entities.Consent)5 ReadAccountDetailsResponse (net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.ReadAccountDetailsResponse)5 ReadAccountListResponse (net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.ReadAccountListResponse)5 ReadTransactionDetailsResponse (net.petafuel.styx.core.xs2a.standards.berlingroup.v1_2.http.ReadTransactionDetailsResponse)5 SEPAParsingException (net.petafuel.jsepa.exception.SEPAParsingException)3 Map (java.util.Map)2 LinkType (net.petafuel.styx.core.xs2a.entities.LinkType)2 PaymentStatus (net.petafuel.styx.core.xs2a.entities.PaymentStatus)2 OAuthTokenExpiredException (net.petafuel.styx.core.xs2a.exceptions.OAuthTokenExpiredException)2 RefreshTokenRequest (net.petafuel.styx.core.xs2a.oauth.http.RefreshTokenRequest)2 TaskFinalFailureException (net.petafuel.styx.keepalive.entities.TaskFinalFailureException)2