Search code examples
c#braintree

Braintree Multiple Requests Error


I am currently working with the Braintree API to try and upload a database of users as customers to their server using their gateway functions. Right now I can create a customer through our C# code.

But any time I try and call the request a second time when we get to the next stage of the loop I get an unhandled web exception on this line :

Result<Customer> result = gateway.Customer.Create(request);

An unhandled exception of type 'System.Net.WebException' occurred in Braintree-2.59.0.dll Additional information: The request was aborted: Could not create SSL/TLS secure channel.

I've changed the code so that we are setting up our gateway connection each time inside our foreach loop but still it errors out. We speculated that we might need to tear down the connection and reinitialize but I can't find any documentation regarding this.

Any ideas?

EDIT: Here is a test case that reproduces the error, you will need to have a sandbox account with your own MerchantId, PublicKey, and PrivateKey to test. I've also already tested creating customers who have the identical Company name and that works fine, Braintree will still create a new account with a unique ID for me so that isn't the issue.

using Braintree;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace BraintreeFailExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string companyName = "Test Company";

            for (int i = 0; i < 3; i++)
            {
                // Initialization information (Replace with AppConfig settings)
                var gateway = new BraintreeGateway
                {
                    Environment = Braintree.Environment.SANDBOX,
                    MerchantId = "Insert Sandbox MerchantId here",
                    PublicKey = "Insert Sandbox PublicKey here",
                    PrivateKey = "Insert Sandbox PrivateKey here"
                };

                // setup data for a customer request object
                var request = new CustomerRequest
                {
                    Company = companyName
                };

                // send the request to the Braintree gateway
                // Braintree doesn't care about duplicate company requests for new customer
                Result<Customer> result = gateway.Customer.Create(request);

            }

        }
    }
}

Solution

  • I was able to resolve this issue. It turns out we had firewall issues that were preventing us from receiving further responses after the first.