functions (Preview)

Act on a Fraud Score from a Vendoranchor

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 Functionanchor

  1. bash
$ btfns init MyFraudCheck --template=paymentMethod --triggers=beforeAuthorization

The CLI will create a new directory, MyFraudCheck with an beforeAuthorization.js file, which you can build off of to create your function.

Write Codeanchor

To obtain a fraud score you will write Javascript code within the index.js file to communicate with the API for your fraud service. Details from the transaction call will be shared with the function for use in obtaining the result.

Create the payload and HTTP options for your request.

  1. JavaScript
const fetch = require("node-fetch");

exports.MyNewPaymentMethod = context => {
  const transactionData = JSON.parse(context.payload.transaction);

  const scoringBody = {
    sessionId: transactionData.customFields.sessionId,
    amount: Number(transactionData.amount) * 100,
    orderId: transactionData.orderId,
    billing_address: transactionData.billing_address
  };

  const httpOptions = {
    method: "POST",
    body: JSON.stringify(orderBody),
    headers: {
      "Content-Type": "application/json"
    }
  };
};

Execute the request and map the result back to Braintree's system.

  1. JavaScript
...
return fetch("https://fraud.service/decisions", options)
  .then(r => r.json())
  .then(result => {
    if (result.decision === "YES") {
      return {
        statusCode: 200,
        body: JSON.stringify({
          transaction: {
            customFields: {
              fraudDecision: result.decision
            }
          }
        })
      };
    } else {
      return {
        statusCode: 200,
        body: JSON.stringify({
          transaction: {
            status: braintree.Transaction.Status.GatewayRejected,
            customFields: {
              fraudDecision: result.decision
            }
          }
        })
      };
    }
  });
...

Your entire function should now look like this:

  1. JavaScript
const fetch = require("node-fetch");

exports.MyNewPaymentMethod = context => {
  const transactionData = JSON.parse(context.payload.transaction);

  const scoringBody = {
    sessionId: transactionData.customFields.sessionId,
    amount: Number(transactionData.amount) * 100,
    orderId: transactionData.orderId,
    billing_address: transactionData.billing_address
  };

  const httpOptions = {
    method: "POST",
    body: JSON.stringify(orderBody),
    headers: {
      "Content-Type": "application/json"
    }
  };

  return fetch("https://fraud.service/decisions", options)
    .then(r => r.json())
    .then(result => {
      if (result.decision === "YES") {
        return {
          statusCode: 200,
          body: JSON.stringify({
            transaction: {
              customFields: {
                fraudDecision: result.decision
              }
            }
          })
        };
      } else {
        return {
          statusCode: 200,
          body: JSON.stringify({
            transaction: {
              status: braintree.Transaction.Status.GatewayRejected,
              customFields: {
                fraudDecision: result.decision
              }
            }
          })
        };
      }
    });
};

Test and Deploy Your Functionanchor

Testinganchor

Before deploying, it is important to test your function locally. You can use the built-in testing tool to ensure everything works as expected.

  1. bash
$ btfns test

You can also generate a custom mock dataset for use in your own custom testing.

  1. 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.

Deploymentanchor

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

  1. yaml
name: "MyFraudCheck"
type: "paymentMethod"
triggers:
  beforeAuthorization: "beforeAuthorization.js"

Run the deploy command.

  1. bash
$ btfns deploy

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.

Create a Transactionanchor

You can interact with your new function by using the name when creating a new sale.

  1. JavaScript
gateway.transaction.sale(
  {
    amount: "10.00",
    functionName: "MyFraudCheck",
    customFields: {
      // Provide custom data to your function handler
    }
  },
  function(err, result) {
    if (result.success) {
      // See result.transaction for details
    } else {
      // Handle errors
    }
  }
);

Passing the functionName will automatically call your function at each of triggers defined in your configuration file.

If you need to pass additional data to your function that is not available in the Transaction API, use Custom Fields.


Next Page: Stream data to another system