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=dataImport

The CLI will create a new directory, MyImportFunction 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: transactionData.id,
    status:
      transactionData.state === "approved"
        ? braintree.Transaction.Status.Authorized
        : braintree.Transaction.Status.Declined,
    currencyIsoCode: transactionData.currencyIsoCode,
    billingAddress: transactionData.billingAddress
  };
};

Then, make the call and map the result back to Braintree's system.

JavaScript
...
  return {
    transaction: transactionAttributes
  }
...

Your entire function should now look like this:

JavaScript
exports.MyImportFunction = context => {
  const transactionData = JSON.parse(context.payload);

  const transactionAttributes = {
    amount: String(Number(transactionData.orderAmount / 100).toFixed(2)),
    orderId: transactionData.id,
    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": {}
}

And then point to that file when running test:

bash
$ btfns test -p tests/sample.json

Deployment

When your function is ready, make sure your configuration file is correct.

yaml
name: "MyImportFunction"
type: "dataImport"
events:
  - http:
      method: "POST"

Then, run the deploy command.

bash
$ btfns deploy
> Success! .... https://sandbox.btfns.co/076580eb-fd0d-4c69-be41-f1714f43b8be

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: Advanced Topics →