Sometimes you will need currency conversions in your Betty Blocks applications, this article will help you do this. In this article I will use the API located at https://fixer.io/
Step 1: Set up the Web Service
- Go to 'Tools' > 'Webservices'
- Create a new Web service
- Fill in the form with the following information:
Name: Exchange Rates API
Protocol: HTTP
Host: api.fixer.io
Authentication Type: None
Request Content-Type: JSON
Response Content-Type: JSON
- Create a new Webservice Endpoint
- Fill in the form for the new Endpoint with the following information
Name: Get the latest foreign exchange rates
HTTP method: GET
Path: "/latest"
- Save the Endpoint
- Perform a 'Run Test'
- When the service responds with JSON hit the 'Generate Custom Model'
- You're done setting up the Webservice.
Step 2: Set up the Datamodel
- Create a model 'Order' to store the example Orders, from here we would like to do the conversions.
- Add a price property 'euro_value' and add a price property "Converted_price".
- You're all done setting up your datamodel. This also means we are now halfway, time for a break.
Step 3: Set up the Action
- Fill in the action form with following information:
Enabled: True
Description: Convert EURO pricing to USD
Model: Order
Triggers: Manual
Background: False
- Add the following action events
- HTTP Request, select Exchange Rates API and select the Endpoint. Hit Save.
- Add an Update event.
- Select the Order Object
- Add a Variable of the type ""Number with decimal Expression", name it "Conversion". Use the following expression in this variable:
var:record.euro_value * round(var:item.rates.USD, 4 )
The round expressions (with parameter 4) tells Betty Blocks to use a decimal scale of 4.
- Assign the variable to the property "Converted_price"
- Done building the action :)
Step 4: Build the View
We are not Betty Blocks noobs here. If you are integrating Web service I assume you've left your 'How can I set up a view'-days long behind you. I will help, but just with a few easy steps.
- Create a view for Order
- Add the "Convert" action to the order form
Step 5: Start converting
On the order form, fill in a euro pricing. Then press the Action Button to start converting, after conversion the "Converted_price" should be filled.