Braintree Payment Gateway Integration

Brief:

Braintree provides payment processing options for many devices. Braintree provides businesses with the ability to accept payments online or within their mobile application. Its full-stack payment platform can replace the traditional model of sourcing a payment gateway and merchant account from different providers. It provides range of Client SDKs (iOS, Android, Web/Javascript) and Server SDKs (Ruby, Python, PHP, Node.js, Java, .Net).

Need:
 

If we are implementing an eCommerce site or providing any paid service online, we need to implement an option for our customers to do payments online. One option is to contact to different banks and implement for each. Also we will have to make sure that we are compliant  with the rules & regulation and standards led by respective authorities to accept the payments.
Another option is to contact to online payment systems like Braintree and let them take care of the things. You just need to forward the users to Braintree site for payments and they return back after payment along with process.

How It Works:

First need to create a page with all the details required for payment like, Credit card number, name on card, expiry date and CVV. Also need to set some parameters in encrypted format which is required by Braintree to identify the Merchant who the funds should be transferred. When user submits the page, the data directly goes to Braintree site. All the validation and processing is done by Braintree. Whatever is the status, it returns back to our site to a specific page (result page) we specified in the parameter.
On the result page we can extract the values along with transaction status and show appropriate message to user and update the transaction status in our system.
Here is sample code.

Default.aspx

<form id='payment-form' action='<%=TransparentRedirectURL %>' method='POST'>
      <input type='hidden' name='tr_data' value='<%=TrData %>' />
      <div>
        <h2>Credit Card</h2>
        <span>Amount = $<%=Amount %></span>
        <label for='braintree_credit_card_number'>Credit Card Number</label>
        <input type='text' name='transaction[credit_card][number]' id='braintree_credit_card_number' value='4111111111111111'></input>
        <label for='braintree_credit_card_exp'>Credit Card Expiry (mm/yyyy)</label>
        <input type='text' name='transaction[credit_card][expiration_date]' id='braintree_credit_card_exp' value='12/2015'></input>
        <label for='braintree_credit_card_cvv'>CVV</label>
        <input type='text' name='transaction[credit_card][cvv]' id='braintree_credit_card_cvv' value='100'></input>
      </div>
      <input class='submit-button' type='submit' value="Submit" />
    </form>


Default.aspx.cs

protected string TrData,TransparentRedirectURL,Amount;
    protected void Page_Load(object sender, EventArgs e)
    {
        Amount = (10.00M).ToString("f2");
        TrData = Constants.Gateway.Transaction.SaleTrData(
                new TransactionRequest
                {
                    Amount = 10.00M,
                    Options = new TransactionOptionsRequest
                    {
                        SubmitForSettlement = true
                    }

                },
                "http://localhost:51641/result.aspx"
            );
        TransactionRequest a = new TransactionRequest();
      
        TransparentRedirectURL = Constants.Gateway.TransparentRedirect.Url;
    }



Constants.cs

using Braintree;
public class Constants
{
    public Constants()
    {
        //
        // TODO: Add constructor logic here
        //

    }

    public static BraintreeGateway Gateway = new BraintreeGateway
    {
        Environment = Braintree.Environment.SANDBOX,
        MerchantId = "<<Merchant id here>>",
        PublicKey = "Public key here",
        PrivateKey = "Private Key here"
    };
}


SessionUtils.cs

public class SessionUtils
{
    #region Singleton

    private const string SESSION_SINGLETON_NAME = "<<Singleton name here>>";

  
    #endregion

    public object TrData { get; set; }
    public object TransparentRedirectURL { get; set; }
    public string Message { get; set; }
}



Result.aspx.cs

Result<Transaction> result = Constants.Gateway.TransparentRedirect.ConfirmTransaction(Request.Url.Query);
        if (result.IsSuccess())
        {
            Transaction transaction = result.Target;
            //string Message = transaction.Status.ToString();
            lblStatus.Text = transaction.Status.ToString();
            //transaction.
        }
        else
        {
            List<ValidationError> errors = result.Errors.DeepAll();
            lblStatus.Text = "Transaction failed. Possible errors are<br/>";
            foreach (ValidationError er in errors)
            {
                lblStatus.Text += er.Message + "<br/>";
            }
            //string[] errors = result.Errors.DeepAll();
            //SessionUtils.Instance.Message = string.Join(", ", result.Errors.DeepAll());
        }

Podcast

Michael Patterson sat down with the CEO of Boston Byte, Mustapha Shaikh to discuss the significance and rapid digitization of the healthcar...