functions (Preview)
Stream Data to Another System
Initialize a New Function
- bash
$ btfns init MyExportFunction --template=dataExport
The CLI will create a new directory, MyExportFunction
with an index.js
file, which you can build off of to create your function.
- bash
MyExportFunction
├── config.yml
├── index.js
└── package.json
Write Code
You'll need to decide on the events you want to expose to your function.
You can see available events in the docs or by running:
- bash
$ btfns events --list-available
> Available events
> ----------------
> transaction.authorized
> transaction.declined
> transaction.settled
...
Inside your project directory, change the YAML file to reflect the events to send to your function.
- yaml
name: "MyExportFunction"
type: "dataExport"
events:
- transaction.settled
Braintree will provide your function with a batch of transactions that match the criteria. Our example will filter on settled transactions. You can process the data however you'd like and send those results to an external system.
In the index.js
file, add the following code:
- JavaScript
const fetch = require("node-fetch");
exports.MyExportFunction = context => {
const payload = context.payload.transactions.map(transaction => ({
braintreeId: transaction.id,
amount: transaction.amount
}));
const httpOptions = {
method: "POST",
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json"
}
};
return fetch("https://accounting.service/transactions", httpOptions)
.then(r => r.json())
.then(result => {
if (result.status === "200") {
console.log("success!");
} else {
console.log("error: " + result.status);
}
});
};
Test and Deploy Your Function
Testing
Before deploying, it is important to test your function locally. You can use the built-in testing tool to ensure everything works as expected.
- bash
$ btfns test
You can also generate a custom mock dataset for use in your own custom testing.
- bash
$ btfns generate-test-data
This will create a testData.json
file in the __tests__
directory which will contain an example payload as you can expect from Braintree’s services.
Deployment
When your function is ready, run the deploy
command.
- bash
$ btfns deploy
> Deploying...
...
> SUCCESS!
By default this will be deployed to your sandbox account, but you can select the production
option in the prompt or run: btfns deploy --production
.
Next Page: Import Data →