问题
I am trying to use Braintree's API for payments on my side, and although they have excellent documentation, I am stuck when trying to receive the values from the form (not receiving any values from the fields at all).
The code is as follows:
HTML:
<p>
<asp:Label ID="Label1" runat="server" Text="Credit Card Number"></asp:Label><br />
<asp:TextBox data-encrypted-name="number" ID="CardNumberTextBox" AutoCompleteType="Disabled" MaxLength="20" Width="350" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label2" runat="server" Text="CVV"></asp:Label><br />
<asp:TextBox ID="CVVTextbox" AutoCompleteType="Disabled" MaxLength="4" Width="50" data-encrypted-name="cvv" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label3" runat="server" Text="Expiration (MM/YYYY)"></asp:Label><br />
<asp:TextBox ID="MonthTextbox" AutoCompleteType="Disabled" MaxLength="2" data-encrypted-name="month" Width="50" runat="server"></asp:TextBox>
/
<asp:TextBox ID="YearTextbox" AutoCompleteType="Disabled" MaxLength="4" data-encrypted-name="year" Width="50" runat="server"></asp:TextBox>
</p>
<input type="submit" id="SubmitButton" onserverclick="SubmitButton_Click" runat="server" />
Since I have the main form in the master page, I modify its properties on page load:
Code-Behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Form.Action = "./default.aspx"
Form.Method = "POST"
Form.ID = "braitree"
End Sub
Protected Sub SubmitButton_Click(sender As Object, e As EventArgs)
Dim number As String = Request.Form("number").ToString
Dim cvv As String = Request.Form("cvv").ToString
Dim month As String = Request.Form("month").ToString
Dim year As String = Request.Form("year").ToString
Dim gateway As New Braintree.BraintreeGateway
With gateway
.Environment = Braintree.Environment.SANDBOX
.PublicKey = "xxx"
.PrivateKey = "xxx"
.MerchantId = "xxx"
End With
Dim transactionRequest As New TransactionRequest
With transactionRequest
.Amount = 100D
.CreditCard = New TransactionCreditCardRequest
With .CreditCard
.Number = number
.CVV = cvv
.ExpirationMonth = month
.ExpirationYear = year
End With
End With
Dim result As Result(Of Transaction) = gateway.Transaction.Sale(transactionRequest)
End Sub
When I break at and expand the first Request.Form
, I notice that it does not contain the data-encrypted-name
values but the HTML-generated name
values of ID
.
Note: This also happens when I replace the ASP
controls with HTML
controls.
Is there anything that I am missing as to how it should receive the data-encrypted-name
?
回答1:
The solution to this was simple, actually. It seems that some of my control where interfering with the Request
object. After testing with an empty project to see that the problem was indeed on my side, I created a new master page.
By default, the form is wrapped around the ContentPlaceholder
so I set the its attributes as instructed by Braintree's documentation, copied the javascript call and declarations below that, and in my default.aspx
, the HTML is as follows:
<p>
<label>Card Number</label>
<input type="text" size="20" autocomplete="off" data-encrypted-name="number" />
</p>
<p>
<label>CVV</label>
<input type="text" size="4" autocomplete="off" data-encrypted-name="cvv" />
</p>
<p>
<label>Expiration (MM/YYYY)</label>
<input type="text" size="2" data-encrypted-name="month" /> / <input type="text" size="4" data-encrypted-name="year" />
</p>
<input type="submit" id="submit" onserverclick="SubmitButton_Click" runat="server"/>
Notice the runat="server"
and onserverclick
attributes on the input
. I haven't actually tested this if it will work with normal asp control.
That works now, so I am moving to the tough part :)
EDIT: This works with asp controls also!
回答2:
In c#
using Braintree
(download liberary from: https://developers.braintreepayments.com/start/hello-server/dotnet)
// Paste this code in page load
string number = HttpContext.Current.Request.QueryString["CardNumber"];
string cvv = HttpContext.Current.Request.QueryString["cvv"];
string month = HttpContext.Current.Request.QueryString["month"];
string year = HttpContext.Current.Request.QueryString["year"];
string AmountPay = HttpContext.Current.Request.QueryString["amount"];
string rMsg = "";
Boolean rBool;
BraintreeGateway gateway = new BraintreeGateway();
var _with1 = gateway;
_with1.Environment = Braintree.Environment.SANDBOX;
_with1.PublicKey = "xxx";
_with1.PrivateKey = "xxx";
_with1.MerchantId = "xxx";
Decimal amount;
amount = Convert.ToDecimal(AmountPay);
TransactionRequest transactionRequest = new TransactionRequest();
var _with3 = transactionRequest;
_with3.Amount = amount;
_with3.PaymentMethodNonce = "nonce-from-the-client";
_with3.CreditCard = new TransactionCreditCardRequest();
var with2 = with3.CreditCard;
_with2.Number = number;
_with2.CVV = cvv;
_with2.ExpirationMonth = month;
_with2.ExpirationYear = year;
Result<Transaction> result = gateway.Transaction.Sale(transactionRequest);
rMsg = result.Message;
rBool = result.IsSuccess();
来源:https://stackoverflow.com/questions/23491649/braintree-with-web-forms-and-data-encrypted-value