-
-
Save benmills/3678413c4bde46bd0dda to your computer and use it in GitHub Desktop.
Simple Braintree ASP.NET Transparent Redirect Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" Inherits="braintreetest.Default" %> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html> | |
<head runat="server"> | |
<title>Default</title> | |
</head> | |
<body> | |
<form id="NewPaymentForm" method="post" action="<%= braintreeGateway().TransparentRedirect.Url %>"> | |
<% if (TransactionSuccessful) { %> | |
<h1>Thank you!</h1> | |
<% } else { %> | |
<% if (ErrorMessage.Length > 0) { %> | |
<p style="color:red;"><%= ErrorMessage %></p> | |
<% } %> | |
<input id="tr_data" name="tr_data" type="hidden" value="<%= TrData %>" /> | |
<p> | |
<label>Credit Card Number</label> | |
<input type="text" value="4111111111111111" name="transaction[credit_card][number]" /> | |
</p> | |
<p> | |
<label>Expiration Date</label> | |
<input type="text" value="10/14" name="transaction[credit_card][expiration_date]" /> | |
</p> | |
<input type="submit" value="Pay" /> | |
<% } %> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using System.Web.UI; | |
using Braintree; | |
namespace braintreetest | |
{ | |
public partial class Default : System.Web.UI.Page | |
{ | |
public String TrData; | |
public Boolean TransactionSuccessful; | |
public String ErrorMessage; | |
public virtual void Page_Load (object sender, EventArgs args) | |
{ | |
TransactionSuccessful = false; | |
ErrorMessage = String.Empty; | |
if (Request.Url.Query.Length > 0) { | |
TransactionSuccessful = confirmTransparentRedirect(); | |
ErrorMessage = "Problem with your credit card number or expiration date."; | |
} else { | |
TrData = buildTransparentRedirectData(); | |
} | |
} | |
public String buildTransparentRedirectData () | |
{ | |
var transactionRequest = new TransactionRequest | |
{ | |
Amount = 10.00M | |
}; | |
return braintreeGateway ().Transaction.SaleTrData (transactionRequest, "http://localhost:8080/"); | |
} | |
public Boolean confirmTransparentRedirect () | |
{ | |
Result<Transaction> ChargeResult = braintreeGateway().TransparentRedirect.ConfirmTransaction(Request.Url.Query); | |
return ChargeResult.IsSuccess(); | |
} | |
public BraintreeGateway braintreeGateway() | |
{ | |
return new BraintreeGateway | |
{ | |
Environment = Braintree.Environment.SANDBOX, | |
MerchantId = "merchant_id", | |
PublicKey = "public_key", | |
PrivateKey = "private_key" | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just found this code via google search, I'm wondering if this transaction type is valid with braintree? also there is not card holder name and CVV input in the code. The code actually works, i tried to add field transaction[credit_card][cardholdername] but i get invalid parameter error. Any help would be great, thanks.