Integration with HTTPS and JSONata

Description

In the context of an e-commerce purchase, consider an integration that manages the payment processing through the following steps:

  1. Order reception

  2. Purchase information retrieval

  3. Data transformation

  4. Payment processing

  5. Status update

  6. Final response

See the complete script and the integration’s diagram.

flow source code https

See below the description of the executed steps:

  1. Order reception

    The flow begins when an order is received with an identification code (ClientID). The POST request is forwarded to the /simple endpoint. The response to the request is logged.

    - from:
        uri: rest:post:/simple
    - setProperty:
        expression:
          simple:
            expression: ${header[ClientID]}
        name: "clientID"
    - log:
        message: ${exchangeProperty.clientID}
  2. Purchase information retrieval

    With the ClientID, the system queries an internal service for more details. The GET request is made to the shopping endpoint, passing the ClientID as a parameter. The response to the request is logged.

    - toD:
        uri: https://{{URL}}/interno/compras
        parameters:
            bridgeEndpoint: true
            httpMethod: GET
            ClientID: '${exchangeProperty.clientID}'
    - log:
        message: 'COMPRAS - ${body}'
  3. Data transformation

    The received information is processed and formatted using a transformation tool, JSONata. The applied transformation is logged for verification.

    - to:
        uri: jsonata:classpath:extractTotal.jsonata
        parameters:
           inputType: JsonString
    - log:
        message: 'TRANSFORMAÇÃO - ${body}'
    • The instructions for creating a new JSON object are in the extractTotal.jsonata file, available in the Resources folder.

    • The extractTotal.jsonata file is referenced in the flow script along with the classpath (snippet above).

    • Below is the content of the extractTotal.jsonata file:

    {
       "Conta": Cliente.Conta,
       "TipoPagamento": Cliente.TipoPagamento,
       "Valor": $sum(Cliente.Ordens.Produto.(Preco * Quantidade))
    }
  4. Payment processing

    The processed data is then sent to an external service to make the payment. First, the transformed message body is converted to JSON format, and the Content-Type header (set to application/json) and the Authorization header (with an example token) are configured. A POST request is then made to the payment endpoint. The received response is logged.

    - marshal:
        json:
           library: Jackson
    - setHeader:
        expression:
          constant:
            expression: application/json
        name: Content-Type
    - setHeader:
        expression:
          constant:
            expression: 'Bearer tokenDeAutorizacaoExemplo'
        name: Authorization
    - toD:
        uri: https://{{URL}}/externo/pagamento
        parameters:
            bridgeEndpoint: true
            httpMethod: POST
    - log:
        message: 'PAGAMENTO - ${body}'
  5. Status update

    After the payment is processed, the result is combined with the ClientID and transformed again to update the payment status in an internal service. The adjusted message body is converted to JSON format and sent in a POST request to the status endpoint.

    - setBody:
        expression:
          simple:
            expression: '{"ClientID": "${exchangeProperty.clientID}", "status":${body}}'
    - log:
        message: 'CONCATENAÇÃO - ${body}'
    - to:
        uri: jsonata:classpath:statusPayload.jsonata
        parameters:
           inputType: JsonString
    - log:
        message: 'STATUS PAYLOAD - ${body}'
    - marshal:
        json:
           library: Jackson
    - setHeader:
        expression:
          constant:
            expression: application/json
        name: Content-Type
    - toD:
        uri: https://{{URL}}/interno/status
        parameters:
            bridgeEndpoint: true
            httpMethod: POST
    • The instructions for creating a new JSON object are in the statusPayload.jsonata file, available in the Resources folder.

    • The statusPayload.jsonata file is referenced in the flow script along with the classpath (snippet above).

    • Below is the content of the statusPayload.jsonata file:

    {
        "ClientID": ClientID,
        "Status":status.status
    }
  6. Final response

The system sends a final confirmation to indicate that the payment was successful. The HTTP response code is set to 200, and the response body confirms the success of the operation. Additionally, the content of the final response is logged.

- setHeader:
    expression:
      constant:
        expression: "200"
    name: CamelHttpResponseCode
- setBody:
    expression:
      simple:
        expression: '{"success": "true"}'
- log:
    message: ${body}

Flow script

Below is the complete script for the described integration flow:

- from:
    uri: rest:post:/simple
    steps:
       - setProperty:
           expression:
             simple:
               expression: ${header[ClientID]}
           name: "clientID"
       - log:
          message: ${exchangeProperty.clientID}
       - toD:
          uri: https://{{URL}}/interno/compras
          parameters:
              bridgeEndpoint: true
              httpMethod: GET
              ClientID: '${exchangeProperty.clientID}'
       - log:
          message: 'COMPRAS - ${body}'
       - to:
            uri: jsonata:classpath:extractTotal.jsonata
            parameters:
               inputType: JsonString
       - log:
            message: 'TRANSFORMAÇÃO - ${body}'
       - marshal:
            json:
               library: Jackson
       - setHeader:
            expression:
              constant:
                expression: application/json
            name: Content-Type
       - setHeader:
            expression:
              constant:
                expression: 'Bearer tokenDeAutorizacaoExemplo'
            name: Authorization
       - toD:
          uri: https://{{URL}}/externo/pagamento
          parameters:
              bridgeEndpoint: true
              httpMethod: POST
       - log:
          message: 'PAGAMENTO - ${body}'
       - setBody:
          expression:
            simple:
              expression: '{"ClientID": "${exchangeProperty.clientID}", "status":${body}}'
       - log:
          message: 'CONCATENAÇÃO - ${body}'
       - to:
            uri: jsonata:classpath:statusPayload.jsonata
            parameters:
               inputType: JsonString
       - log:
            message: 'STATUS PAYLOAD - ${body}'
       - marshal:
            json:
               library: Jackson
       - setHeader:
            expression:
              constant:
                expression: application/json
            name: Content-Type
       - toD:
          uri: https://{{URL}}/interno/status
          parameters:
              bridgeEndpoint: true
              httpMethod: POST
       - setHeader:
          expression:
            constant:
                expression: "200"
          name: CamelHttpResponseCode
       - setBody:
          expression:
            simple:
              expression: '{"success": "true"}'
       - log:
          message: ${body}

Diagram

https diagrama

Components and EIPs

Components EIPs

HTTPS

from

JSONata

log

REST

marshal

setBody

setHeader

setProperty

to

toD

Check out the description of each component and EIP.
Thanks for your feedback!
EDIT

Share your suggestions with us!
Click here and then [+ Submit idea]