Set Up Your Server

Choose an SDK preference:

Install and configure

availability

Supports both .NET Framework 4.5.2+ and .NET Core 1.0+.

Get started with our .NET library by hitting the download link below.

Download .NET library

  • Version: 4.7.0
  • SHA256: 27a43e6bd034c0fd71d106bd85f2f912a68f0299914ef1abb4a68c8e0e4b0949

Or use NuGet

NuGet is a package manager for Visual Studio.

To install the Braintree .NET Client Library, run the following command in the Package Manager Console:

PM> Install-Package Braintree

Construct your gateway object

In your code, you must first construct a Gateway object with the environment-specific (sandbox or production) access token provided in your PayPal dashboard:

BraintreeGateway gateway = new BraintreeGateway(useYourAccessToken);

This must be called before performing any method (e.g. client token generation, authorizations, captures).

note

Keep in mind that the step to initialize the gateway using the Express Checkout integration is different than that used for a direct integration with Braintree.

Generate a client token

Your server is responsible for generating a client token, which contains all the authorization and configuration information your client needs to initialize the client SDK to communicate with Braintree. The client-side page 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:

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, although you could load the client token from an AJAX call instead. These methods are discussed in the client token setup section.

Receive a payment method nonce from your client

Once your customer has successfully completed the PayPal checkout flow your client will receive a PaymentMethodNonce representing customer payment authorization, which it then sends to your server.

Your server implementation is then responsible for receiving the PaymentMethodNonce and creating a transaction.

[HttpPost]
public ActionResult CreatePurchase(FormCollection collection)
{
  string nonce = collection["payment_method_nonce"];
  // Use payment method nonce here
}

Create a transaction

You can create a transaction using a PaymentMethodNonce and an Amount. Below is an example of all relevant PayPal parameters you can pass in your transaction call:

BraintreeGateway gateway = new BraintreeGateway(useYourAccessToken);

TransactionRequest request = new TransactionRequest
{
    Amount = 1000.0M,
    MerchantAccountId = "USD",
    PaymentMethodNonce = Request.Form["payment_method_nonce"],
    OrderID = "Mapped to PayPal Invoice Number",
    Descriptor = new DescriptorRequest
    {
      Name = "Descriptor displayed in customer CC statements. 22 char max"
    },
    ShippingAddress = new AddressRequest
    {
        FirstName = "Jen",
        LastName = "Smith",
        Company = "Braintree",
        StreetAddress = "1 E 1st St",
        ExtendedAddress = "Suite 403",
        Locality = "Bartlett",
        Region = "IL",
        PostalCode = "60103",
        CountryCodeAlpha2 = "US"
    },
    Options = new TransactionOptionsRequest
    {
      PayPal = new TransactionOptionsPayPalRequest
      {
        CustomField = "PayPal custom field",
        Description = "Description for PayPal email receipt",
      },
      SubmitForSettlement = true
    }
};

Result<Transaction> result = gateway.Transaction.Sale(request);

if (result.IsSuccess())
{
    System.Console.WriteLine("Transaction ID: " + transaction.Id);
}
else
{
    System.Console.WriteLine(result.Message);
}

Specify currency

The customer will be charged in the currency you specify in the merchantAccountId field on the transaction call. You should pass the same currency here that you specified on the client side.

important

If you don't pass the merchantAccountId field, the customer will be charged in the default currency for the country associated with your account.

The merchantAccountId field accepts the 3-letter currency code of each of the currencies currently enabled for your account. You can see these currencies on the My Apps & Credentials page in your PayPal Developer Dashboard.

Shipping addresses

PayPal validates the shipping address, so it must follow the PayPal address conventions.

Seller Protection

By passing a shipping address, you may also be eligible for PayPal Seller Protection. You can check the status of Seller Protection as follows:

Transaction transaction = gateway.Transaction.Find("the_transaction_id");

transaction.PayPalDetails.SellerProtectionStatus;
// "ELIGIBLE"

See also

The below are links to reference pages used by merchants performing direct integrations with Braintree. If you have questions about your Express Checkout integration, please be sure to contact PayPal Support rather than Braintree Support.

Next Page: Testing and Go Live →

Still Have Questions?

Browse our support options or submit a question to our technical support team.

Feedback