Import Data
Availability
We're building functions with the same proven expertise and rock solid foundations you expect from
Braintree. Take a look at our documentation preview to get a jump start on what you'll make with
functions. Email
functions-requests@braintreepayments.com
to learn more.
Initialize a New Function
- bash
$ btfns init MyImportFunction --template=dataImportMyImportFunction with an
index.js file, which you can build off of to create your function.
Write Code to Ingest Data
To ingest data you will write Javascript code within the index.js to accept incoming
requests and create corresponding Braintree entities.
- JavaScript
exports.MyImportFunction = context => {
const transactionData = JSON.parse(context.payload);
const transactionAttributes = {
amount: String(Number(transactionData.orderAmount / 100).toFixed(2)),
orderId: <a href="http://transactionData.id">transactionData.id</a>,
status: transactionData.state === "approved" ? braintree.Transaction.Status.Authorized : braintree.Transaction.Status.Declined,
currencyIsoCode: transactionData.currencyIsoCode,
billingAddress: transactionData.billingAddress
};
};- JavaScript
... return { transaction: transactionAttributes } ...- JavaScript
exports.MyImportFunction = context => {
const transactionData = JSON.parse(context.payload);
const transactionAttributes = {
amount: String(Number(transactionData.orderAmount / 100).toFixed(2)),
orderId: <a href="http://transactionData.id">transactionData.id</a>,
status: transactionData.state === "approved" ? braintree.Transaction.Status.Authorized : braintree.Transaction.Status.Declined,
currencyIsoCode: transactionData.currencyIsoCode,
billingAddress: transactionData.billingAddress
};
return { transaction: transactionAttributes };
};Validation Errors
What you return from the function will be validated using our normal validations. If your data does
not conform to our validations we'll return a 422 with errors to the
calling service and log the errors to your console.
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. You can create an example JSON file that mirrors your
expected inbound payload and the testing tool will provide it to your function alongside the
appropriate metadata. For example:
- JSON
{
"orderAmount": 12300,
"id": "076580eb-fd0d-4c69-be41-f1714f43b8be",
"state": "authorized",
"currencyIsoCode": "USD",
"billingAddress": {}
}test:
- bash
$ btfns test -p tests/sample.jsonDeployment
When your function is ready, make sure your configuration file is correct.
- yaml
{
`name: "MyImportFunction",
type: "dataImport",
events: <ul>
<li>http: method: "POST"</li>
</ul>
}deploy command.
- bash
{
`$ btfns deploy <blockquote>
Success! .... <a href="https://sandbox.btfns.co/076580eb-fd0d-4c69-be41-f1714f43b8be">https://sandbox.btfns.co/076580eb-fd0d-4c69-be41-f1714f43b8be</a>
</blockquote>`
}production option in the prompt or run: btfns deploy --production.