Act on a Fraud Score from a Vendor
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 MyFraudCheck --template=paymentMethod --triggers=beforeAuthorizationMyFraudCheck with an
beforeAuthorization.js file, which you can build off of to create your function.
Write Code
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.
- 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"
}
};
};- 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
}
}
})
};
}
});- 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 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- bash
$ btfns generate-test-datatestData.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, make sure your configuration file is correct.
- yaml
name: "MyFraudCheck"
type: "paymentMethod"
triggers:
beforeAuthorization: "beforeAuthorization.js"deploy command.
- bash
$ btfns deployproduction option in the prompt or run: btfns deploy --production.
Create a Transaction
You can interact with your new function by using the name when creating a new sale.
- 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
}
});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.