Skip to content

Instantly share code, notes, and snippets.

@krasnopolsky
Created November 2, 2015 21:56
Show Gist options
  • Save krasnopolsky/a095929f3ae64a48ae29 to your computer and use it in GitHub Desktop.
Save krasnopolsky/a095929f3ae64a48ae29 to your computer and use it in GitHub Desktop.
.NET Webforms Example
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" enableEventValidation="false" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
#card-number
{
border: 1px solid #333;
height: 25px;
}
#expiration-month
{
border: 1px solid #333;
height: 25px;
}
#expiration-year
{
border: 1px solid #333;
height: 25px;
}
#cvv
{
border: 1px solid #333;
height: 25px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<label for="card-number">Card Number</label>
<div id="card-number"></div>
<label for="cvv">CVV</label>
<div id="cvv"></div>
<label for="expiration-date">Expiration date</label>
<div id="expiration-date"></div>
<br/>
<input type="submit" value="pay" onclick="javascript:document.getElementById('__EVENTTARGET').value = '<%=btnPay.ClientID%>';" />
<asp:Button runat="server" ID="btnPay" Text="Pay" OnClick="btnPay_Click" Visible="false" />
<br/>
<br/>
<asp:Label runat="server" ID="lblResult"></asp:Label>
</div>
</form>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
braintree.setup("<%=clientToken%>", "custom", {
id: "form1",
hostedFields: {
number: { selector: "#card-number" },
cvv: { selector: "#cvv" },
expirationDate: { selector: "#expiration-date" },
styles: {
// Style all elements
"input": { "font-size": "16pt", "color": "#3A3A3A" },
// Styling a specific field
".number": { "font-family": "monospace" },
".invalid": { "color": "red" }
}
}
});
</script>
</body>
</html>
using Braintree;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
private BraintreeGateway gateway = new BraintreeGateway
{
Environment = Braintree.Environment.SANDBOX,
MerchantId = //the_merchant_id
PublicKey = //a_public_key
PrivateKey = //a_private_key
};
#region page variables
public string clientToken;
#endregion
protected void Page_Load(object sender, EventArgs e)
{
//Generate event form (asp.net only hack)
ClientScript.GetPostBackEventReference(this, string.Empty);
if (!IsPostBack)
{
//Generate a client token
clientToken = gateway.ClientToken.generate();
}
}
protected void btnPay_Click(object sender, EventArgs e)
{
//Get the nonce & device data from the client
var nonce = Request.Form["payment_method_nonce"];
var deviceData = Request.Form["device_data"];
//Create auth
var request = new TransactionRequest
{
Amount = 100.00M,
PaymentMethodNonce = nonce,
DeviceData = deviceData
};
//Send transaction request to server
Result<Transaction> result = gateway.Transaction.Sale(request);
//did we get any money?
if (result.IsSuccess())
{
//Transaction is successful
lblResult.Text = string.Format("<b>Transaction id:</b>{0}", result.Target.Id);
#region
btnPay.Visible = false;
#endregion
}
else
{
//Something went wrong
lblResult.Text = "Error";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment