Creating Credit Cards using Transparent Redirect

To create credit cards using Transparent Redirect, you’ll need to build an HTML form that submits directly to Braintree.

Form Action URL

You should set the action attribute of your form to the url for TransparentRedirect.

You’ll want to set the transparent redirect URL in your controller action. In ASP.NET MVC, for example:

  ViewData["TransparentRedirectURL"] = gateway.TransparentRedirect.Url;

And then set the action on the form in your view.

TR Data

The form needs to include a hidden field named tr_data. It needs to include the URL that Braintree will redirect the user to after storing the form params. The CustomerId is also required and can only be passed in via tr_data. You should also include in the tr_data any parameters that you want to send to Braintree that you’re not asking your users to enter. For example, if you are specifying the token rather than letting the gateway generate one, it should be included in tr_data.

var request = new CreditCardRequest
{
  CustomerId = "a_customer_id",
  Token = "a_credit_card_token"
};
string trData = gateway.TrData(request, "http://example.com/url_to_redirect_to");

Use a hidden field to add the tr_data to your form.

TR Form Fields

Create text fields for data parameters that you want to have your users enter. The credit card number and expiration date are required fields.

<input type="text" name="credit_card[number]" />
<input type="text" name="credit_card[expiration_date]" />

You can also include other credit card and billing address fields. For example:

<input type="text" name="credit_card[cardholder_name]" />
<input type="text" name="credit_card[billing_address][street_address]" />
<input type="text" name="credit_card[billing_address][postal_code]" />

See the full list of TR html fields.

TR Confirmation

Before the credit card is actually created, you will need to confirm the TR request. For the confirmation, you will need to use the query string from the URL on the Redirect. Braintree will add parameters to the query string that identify the request, so the redirect URL will look something like:

http://example.com/path?http_status=200&id=vgqssrhqhxfhgrwz&hash=0c3c641f1de3ed1c732c54cab367355350603b28

Use the query string to confirm. You’ll receive a result object just like if you created a credit card using the server-to-server API.

Result<CreditCard> result = gateway.TransparentRedirect.ConfirmCreditCard(Request.Url.Query);

See Also