I am using the Braintree drop-in and had it all working until I needed to check some client-side validators. I accomplished this via the onPaymentMethodReceived function where I was able to fire some JS to check the validators before continuing with the postback (as per the Braintree documentation).
However, by manuall firing the postback through onPaymentMethodReceived I now receive the following error... Cannot determine payment method. CVV is required.
My HTML code:
<form id="form1" runat="server">
<div id="payment-container" style="width:400px"></div>
<asp:Button ID="Submit" runat="server" Text="Submit Payment" />
</form>
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="//js.braintreegateway.com/v2/braintree.js"></script>
<script>
var paymentForm;
$(function () {
braintree.setup("<%=ProcessorToken %>", "dropin", {
container: "payment-container",
form: "form1",
onReady: function (integration) {
paymentForm = integration;
},
onPaymentMethodReceived: function (obj) {
document.forms[0].submit();
}
});
});
</script>
My vb.Net code:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If IsPostBack Then
'Process Braintree Payment
Validate()
If Page.IsValid Then
Pay()
End If
Else
'Create Braintree token
ProcessorToken = Gateway.ClientToken.generate()
End If
End Sub
Protected Sub Pay()
'Get the Payment Nonce from the form post
PaymentNonce = Request.Form("payment_method_nonce")
Dim tr = New TransactionRequest() With { _
.Amount = 1.99, _
.MerchantAccountId = "my_merchant_account", _
.PaymentMethodNonce = PaymentNonce _
}
Dim result As Result(Of Transaction) = Gateway.Transaction.Sale(tr)
If result.IsSuccess Then
Response.Write("Payment Successful: " & result.Message)
Else
Response.Write("Payment Error: " & result.Message)
End If
Submit.Visible = False
End Sub
Public ReadOnly Property Gateway() As BraintreeGateway
Get
If _gateway Is Nothing Then
_gateway = New BraintreeGateway() With { _
.Environment = Braintree.Environment.SANDBOX, _
.PublicKey = "my_public_key", _
.PrivateKey = "my_private_keys", _
.MerchantId = "my_merchant_id" _
}
End If
Return _gateway
End Get
End Property
Private _gateway As BraintreeGateway
Protected ProcessorToken As String
Protected PaymentNonce As String
If I remove the following code from the JS, the payment will complete successfully...
onPaymentMethodReceived: function (obj) {
document.forms[0].submit();
}