Search in sources :

Example 1 with PAIN00100303Document

use of net.petafuel.jsepa.model.PAIN00100303Document in project styx by petafuel.

the class PaymentInitiationRequest method createPeriodicPayment.

private Optional<String> createPeriodicPayment() {
    if (getPaymentProduct().isXml()) {
        PAIN00100303Document document = (new PaymentXMLSerializer()).serialize(UUID.randomUUID().toString(), (PeriodicPayment) payment);
        SEPAWriter writer = new SEPAWriter(document);
        String painXmlBody;
        try {
            painXmlBody = new String(writer.writeSEPA());
        } catch (SEPAWriteException e) {
            throw new SerializerException("Unable to create xml body for periodic payment initiation multipart http message");
        }
        Headers headersXml = new Headers.Builder().add("Content-Disposition", "form-data; name=\"xml_sct\"").build();
        MultipartBody.Part xmlPart = MultipartBody.Part.create(headersXml, RequestBody.create(painXmlBody, MediaType.get("application/xml; charset=utf-8")));
        Headers headersJson = new Headers.Builder().add("Content-Disposition", "form-data; name=\"json_standingorderType\"").build();
        MultipartBody.Part jsonPart;
        try (Jsonb jsonb = JsonbBuilder.create()) {
            jsonPart = MultipartBody.Part.create(headersJson, RequestBody.create(jsonb.toJson(payment), MediaType.get("application/json; charset=utf-8")));
        } catch (Exception e) {
            throw new SerializerException(e.getMessage());
        }
        // add a uuid as boundary to avoid boundary repetition in http message
        RequestBody requestBody = new MultipartBody.Builder(getMultipartBoundary()).addPart(xmlPart).addPart(jsonPart).setType(MultipartBody.FORM).build();
        final Buffer buffer = new Buffer();
        try {
            // build raw multipartBody with xml and json
            requestBody.writeTo(buffer);
            return Optional.of(buffer.readUtf8());
        } catch (IOException e) {
            LOG.warn("Error creating raw body for periodic payment initiation message={} cause={}", e.getMessage(), e.getCause().getCause());
            return Optional.empty();
        }
    } else {
        try (Jsonb jsonb = JsonbBuilder.create()) {
            return Optional.ofNullable(jsonb.toJson(payment));
        } catch (Exception e) {
            return Optional.empty();
        }
    }
}
Also used : Buffer(okio.Buffer) SEPAWriteException(net.petafuel.jsepa.exception.SEPAWriteException) PAIN00100303Document(net.petafuel.jsepa.model.PAIN00100303Document) Headers(okhttp3.Headers) JsonbBuilder(javax.json.bind.JsonbBuilder) PaymentXMLSerializer(net.petafuel.styx.core.xs2a.utils.PaymentXMLSerializer) IOException(java.io.IOException) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) IOException(java.io.IOException) SEPAWriteException(net.petafuel.jsepa.exception.SEPAWriteException) Jsonb(javax.json.bind.Jsonb) SEPAWriter(net.petafuel.jsepa.SEPAWriter) MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Example 2 with PAIN00100303Document

use of net.petafuel.jsepa.model.PAIN00100303Document in project styx by petafuel.

the class PaymentInitiationRequest method createSinglePayment.

/*
     * @return payment request body for sepa-single-payments in xml or json as String
     */
private Optional<String> createSinglePayment() {
    if (getPaymentProduct().isXml()) {
        PAIN00100303Document document = (new PaymentXMLSerializer()).serialize(UUID.randomUUID().toString(), (SinglePayment) payment);
        PaymentInstructionInformation xmlPayment = document.getCctInitiation().getPmtInfos().get(0);
        if (this.getPaymentProduct().equals(PaymentProduct.PAIN_001_SEPA_CREDIT_TRANSFERS) && (xmlPayment.getRequestedExecutionDate() == null || "".equals(xmlPayment.getRequestedExecutionDate()))) {
            xmlPayment.setRequestedExecutionDate(document.getCctInitiation().getGrpHeader().getCreationTime());
            document.getCctInitiation().getPmtInfos().set(0, xmlPayment);
        }
        SEPAWriter writer = new SEPAWriter(document);
        try {
            return Optional.of(new String(writer.writeSEPA()));
        } catch (SEPAWriteException exception) {
            LOG.warn("Error creating raw body for single payment initiation message={} cause={}", exception.getMessage(), exception.getCause().getCause());
            return Optional.empty();
        }
    } else {
        JsonbConfig jsonbConfig = new JsonbConfig();
        jsonbConfig.withNullValues(false);
        jsonbConfig.withLocale(Locale.GERMANY);
        jsonbConfig.withDateFormat("yyyy-MM-dd", Locale.GERMANY);
        try (Jsonb jsonb = JsonbBuilder.create(jsonbConfig)) {
            String paymentBodyJson = jsonb.toJson(payment);
            LOG.debug("Outgoing Single Payment Body={}", paymentBodyJson);
            return Optional.ofNullable(paymentBodyJson);
        } catch (Exception e) {
            return Optional.empty();
        }
    }
}
Also used : PaymentInstructionInformation(net.petafuel.jsepa.model.PaymentInstructionInformation) SEPAWriteException(net.petafuel.jsepa.exception.SEPAWriteException) JsonbConfig(javax.json.bind.JsonbConfig) Jsonb(javax.json.bind.Jsonb) SEPAWriter(net.petafuel.jsepa.SEPAWriter) PAIN00100303Document(net.petafuel.jsepa.model.PAIN00100303Document) PaymentXMLSerializer(net.petafuel.styx.core.xs2a.utils.PaymentXMLSerializer) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) IOException(java.io.IOException) SEPAWriteException(net.petafuel.jsepa.exception.SEPAWriteException)

Example 3 with PAIN00100303Document

use of net.petafuel.jsepa.model.PAIN00100303Document in project styx by petafuel.

the class PaymentInitiationRequest method createBulkPayment.

private Optional<String> createBulkPayment() {
    if (getPaymentProduct().isXml()) {
        PAIN00100303Document document = (new PaymentXMLSerializer()).serialize(UUID.randomUUID().toString(), (BulkPayment) payment);
        PaymentInstructionInformation xmlPayment = document.getCctInitiation().getPmtInfos().get(0);
        if (this.getPaymentProduct().equals(PaymentProduct.PAIN_001_SEPA_CREDIT_TRANSFERS) && (xmlPayment.getRequestedExecutionDate() == null || "".equals(xmlPayment.getRequestedExecutionDate()))) {
            xmlPayment.setRequestedExecutionDate(document.getCctInitiation().getGrpHeader().getCreationTime());
            document.getCctInitiation().getPmtInfos().set(0, xmlPayment);
        }
        SEPAWriter writer = new SEPAWriter(document);
        try {
            return Optional.of(new String(writer.writeSEPA()));
        } catch (SEPAWriteException exception) {
            LOG.warn("Error creating raw body for PaymentInitiationPain001Request message={} cause={}", exception.getMessage(), exception.getCause().getCause());
            return Optional.empty();
        }
    } else {
        try (Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withAdapters(new BulkPaymentAdapter()))) {
            return Optional.ofNullable(jsonb.toJson(payment));
        } catch (Exception e) {
            return Optional.empty();
        }
    }
}
Also used : PaymentInstructionInformation(net.petafuel.jsepa.model.PaymentInstructionInformation) SEPAWriteException(net.petafuel.jsepa.exception.SEPAWriteException) Jsonb(javax.json.bind.Jsonb) JsonbConfig(javax.json.bind.JsonbConfig) BulkPaymentAdapter(net.petafuel.styx.core.xs2a.entities.BulkPaymentAdapter) SEPAWriter(net.petafuel.jsepa.SEPAWriter) PAIN00100303Document(net.petafuel.jsepa.model.PAIN00100303Document) PaymentXMLSerializer(net.petafuel.styx.core.xs2a.utils.PaymentXMLSerializer) SerializerException(net.petafuel.styx.core.xs2a.exceptions.SerializerException) IOException(java.io.IOException) SEPAWriteException(net.petafuel.jsepa.exception.SEPAWriteException)

Aggregations

IOException (java.io.IOException)3 Jsonb (javax.json.bind.Jsonb)3 SEPAWriter (net.petafuel.jsepa.SEPAWriter)3 SEPAWriteException (net.petafuel.jsepa.exception.SEPAWriteException)3 PAIN00100303Document (net.petafuel.jsepa.model.PAIN00100303Document)3 SerializerException (net.petafuel.styx.core.xs2a.exceptions.SerializerException)3 PaymentXMLSerializer (net.petafuel.styx.core.xs2a.utils.PaymentXMLSerializer)3 JsonbConfig (javax.json.bind.JsonbConfig)2 PaymentInstructionInformation (net.petafuel.jsepa.model.PaymentInstructionInformation)2 JsonbBuilder (javax.json.bind.JsonbBuilder)1 BulkPaymentAdapter (net.petafuel.styx.core.xs2a.entities.BulkPaymentAdapter)1 Headers (okhttp3.Headers)1 MultipartBody (okhttp3.MultipartBody)1 RequestBody (okhttp3.RequestBody)1 Buffer (okio.Buffer)1