Set Up Your Server
Install and configure
Supports both .NET Framework 4.5.2+, .NET Core 1.0+ and higher.
Get started with our .NET library by hitting the download link below.
- Version: 5.31.0
- SHA256: 544d3d77a4d3b52ee646d876c38885a2ae21a52b0418daef3abb0ca398ed5b7f
Or use NuGet
To install the Braintree .NET Client Library, run the following command in the Package Manager Console:
PM> Install-Package Braintree
In your code, configure the environment and API credentials:
- C#
var gateway = new BraintreeGateway
{
Environment = Braintree.Environment.SANDBOX,
MerchantId = "the_merchant_id",
PublicKey = "a_public_key",
PrivateKey = "a_private_key"
};
See the Braintree .NET Version Changelog.
Generate a client token
CustomerId
when generating the client token lets returning customers select from previously used payment method options, improving user experience over multiple checkouts.- C#
// pass clientToken to your front-end
var clientToken = gateway.ClientToken.Generate(
new ClientTokenRequest
{
CustomerId = aCustomerId
}
);
If the customer can't be found, it will throw an ArgumentException
.
Set Up Your Client covers the client side of the exchange.
Send a client token to your client
Here is an example of how your server would generate and expose a client token:
- C#
public class ClientTokenHandler : IHttpHandler {
public void ProcessRequest(HttpContext context) {
var clientToken = gateway.ClientToken.Generate();
context.Response.Write(clientToken);
}
}
How the token is used by the client may vary. In JavaScript integrations the client token is often included in the generated HTML/JS, while in mobile apps the client token must be requested. These methods are discussed in the client token setup section.
Receive a payment method nonce from your client
Once your client successfully obtains a customer payment method, it receives a payment_method_nonce
representing customer payment authorization, which it then sends to your server.
Your server implementation is then responsible for receiving the payment_method_nonce
and using it appropriately.
- C#
// [HttpPost]
public ActionResult CreatePurchase(FormCollection collection)
{
string nonceFromTheClient = collection["payment_method_nonce"];
// Use payment method nonce here
}
Create a transaction
- C#
var request = new TransactionRequest
{
Amount = 10.00M,
PaymentMethodNonce = nonceFromTheClient,
DeviceData = deviceDataFromTheClient,
Options = new TransactionOptionsRequest
{
SubmitForSettlement = true
}
};
Result<Transaction> result = gateway.Transaction.Sale(request);
The sale call returns a Transaction Result Object which contains the transaction and information about the request.
Test your integration
Always develop and test your code against your sandbox account before processing live transactions against a production account.
Transition to production
At this point, you should be able to accept a payment method nonce and create a transaction in our sandbox. When you're ready to start charging real money, transition over to our production environment. We'll explain that process next.