Accept a New Payment Method
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 MyNewPaymentMethod --template=paymentMethodMyNewPaymentMethod with an
authorization.js file, which you can build off of to create your function. By default,
the CLI will also create files for capturing, voiding, refunding, and vaulting.
Write Code
To authorize a payment you will write Javascript code within the authorization.js file
to communicate with the API for the payment method service. Details from the transaction call will
be shared with the function for use in obtaining the authorization. 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 orderBody = {
purchase_country: transactionData.billing_address.country,
purchase_currency: transactionData.currencyIsoCode,
locale: "en-us",
order_amount: Number(transactionData.amount) * 100,
order_tax_amount: Number(transactionData.taxAmount) * 100,
order_lines: transactionData.order_lines,
billing_address: transactionData.billing_address
};
const httpOptions = {
method: "POST",
body: JSON.stringify(orderBody),
headers: {
"Content-Type": "application/json"
}
};
};- JavaScript
return fetch("https://paymentmethod.service/authorization", options)
.then(r => r.json())
.then(result => {
return {
statusCode: 200,
body: JSON.stringify({
transaction: {
status: braintree.Transaction.Status.Authorized,
orderId: result.order_id
}
})
};
});- JavaScript
const fetch = require("node-fetch");
exports.MyNewPaymentMethod = context => {
const transactionData = JSON.parse(context.payload.transaction);
const orderBody = {
purchase_country: transactionData.billing_address.country,
purchase_currency: transactionData.currencyIsoCode,
locale: "en-us",
order_amount: Number(transactionData.amount) * 100,
order_tax_amount: Number(transactionData.taxAmount) * 100,
order_lines: transactionData.order_lines,
billing_address: transactionData.billing_address
};
const httpOptions = {
method: "POST",
body: JSON.stringify(orderBody),
headers: {
"Content-Type": "application/json"
}
};
return fetch("https://paymentmethod.service/authorization", httpOptions)
.then(r => r.json())
.then(result => {
return {
statusCode: 200,
body: JSON.stringify({
transaction: {
status: braintree.Transaction.Status.Authorized,
orderId: result.order_id
}
})
};
});
};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: "MyNewPaymentMethod"
type: "paymentMethod"
triggers:
authorization: "authorization.js"
capture: "capture.js"
refund: "refund.js"
void: "void.js"
vault: "vault.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: "MyNewPaymentMethod",
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. Subsequent calls for this transaction will automatically route
to your function so you do not need to pass any additional information to refund or void the
transaction. If you need to pass additional data to your function that is not available in the
Transaction API, use Custom Fields.
- JavaScript
gateway.transaction.refund(
"theTransactionId",
function(err, result) {
}
);