functions (Preview)
Act on a Fraud Score from a Vendor
Initialize a New Function
- 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 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"
}
};
};
Execute the request and map the result back to Braintree's system.
- 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:
- 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
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, make sure your configuration file is correct.
- yaml
name: "MyFraudCheck"
type: "paymentMethod"
triggers:
beforeAuthorization: "beforeAuthorization.js"
Run the deploy
command.
- 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 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
}
}
);
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 →