我可以用Braintree.js与.NET Web应用程序? [英] Can I Use Braintree.js With A .NET Web Application?

查看:269
本文介绍了我可以用Braintree.js与.NET Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在寻找到布伦特里支付几天的了。我爱的架构,概念等,通过文档和.NET穿行测试我注意到,所有for .NET的例子在MVC3细算。我试图布伦特里集成到使用普通web表单我现在的.NET Web应用程序。

So I've been looking into Braintree Payments for a couple of days now. I love the architecture, concept, etc. After looking through the documentation and the .NET walk-throughs I've noticed that all of the examples for .NET are in MVC3. I am trying to integrate Braintree into my current .NET Web Application using regular web forms.

我的目标是有一个正常的网页表单后回用客户数据和数据卡同时支付页面。该卡数据应使用其Braintree.js进行加密。这样我可以送过来一切布伦特里进行处理,包括加密卡的数据。

My goal is to have a normal web form post back to the payment page with both customer data and card data. The card data should be encrypted using their Braintree.js. That way I can send everything over to Braintree for processing including the encrypted card data.

的形式将是这个样子:

<p>
  <label>Card Number</label>
  <asp:TextBox ID="number" AutoCompleteType="Disabled" MaxLength="20" Width="150" data-encrypted-name="number" runat="server" />        
</p>
<p>
  <label>CVV</label>
  <asp:TextBox ID="cvv" AutoCompleteType="Disabled" MaxLength="4" Width="50" data-encrypted-name="cvv" runat="server" />
</p>
<p>
  <label>Expiration (MM/YYYY)</label>
  <asp:TextBox ID="month" AutoCompleteType="Disabled" MaxLength="2" data-encrypted-name="month" runat="server" />
 /
  <asp:TextBox ID="year" AutoCompleteType="Disabled" MaxLength="4" data-encrypted-name="year" runat="server" />
</p>
<asp:Button ID="btnSubmit" Text="SUBMIT" runat="server" />

<script type="text/javascript" src="https://js.braintreegateway.com/v1/braintree.js"></script>
<script type="text/javascript">
  var braintree = Braintree.create("MyClientSideKey");
  braintree.onSubmitEncryptForm('braintree-payment-form');
</script>

然后在code-后面我将设置Form.Action,Form.Method和Form.ID如下:

Then in the code-behind I would set the Form.Action, Form.Method and Form.ID as follows:

protected void Page_Load(object sender, EventArgs e)
{
  Form.Action = "CreateTransaction()";
  Form.Method = "POST";
  Form.ID = "braintree-payment-form";  
}

于是希望,当用户提交表单它击中了CreateTransaction()的成员,在这样的收藏参数中的加密卡数据一起:

So then hopefully when the user submits the form it hits the "CreateTransaction()" member along with the encrypted card data in the "collection" parameter like this:

[HttpPost]
public ActionResult CreateTransaction(FormCollection collection)
{
  TransactionRequest request = new TransactionRequest
  {
    Amount = 1000.0M,
    CreditCard = new TransactionCreditCardRequest
  {
  Number = collection["number"],
  CVV = collection["cvv"],
  ExpirationMonth = collection["month"],
  ExpirationYear = collection["year"]
  },
  Options = new TransactionOptionsRequest
  {
    SubmitForSettlement = true
  }
};

Result<Transaction> result = Constants.Gateway.Transaction.Sale(request);

return null;
}

当我采取这种方式的形式从来没有回传CreateTransaction()的成员。我在想什么?可通过使用普通的旧网页的形式来完成?

When I take this approach the form never posts back to the "CreateTransaction()" member. What am I missing? Can this be done using regular old web forms?

推荐答案

确定,所以实验和在黑暗中拍摄了一下我能得到这个Braintree.js将数据传递到加密之前,大量的后我的服务器。从那里,我能够给我们的code后面来处理支付处理。

OK, so after LOTS of experimenting and shooting in the dark for a bit I was able to get this the Braintree.js to encrypt the data before passing it to my server. From there I am able to us the code behind to handle the payment processing.

下面是我使用的ASP.NET Web表单:

Here is the ASP.NET web form that I'm using:


            卡号
                   
        

Card Number

    <p>
        <label>CVV</label>
        <asp:TextBox ID="txtCVV" AutoCompleteType="Disabled" MaxLength="4" Width="50" data-encrypted-name="cvv" runat="server" />
    </p>
    <p>
        <label>Expiration (MM/YYYY)</label>
        <asp:TextBox ID="txtMonth" AutoCompleteType="Disabled" MaxLength="2" data-encrypted-name="month" runat="server" />
        /
        <asp:TextBox ID="txtYear" AutoCompleteType="Disabled" MaxLength="4" data-encrypted-name="year" runat="server" />
    </p>
    <asp:Button ID="btnSubmit" Text="SUBMIT" runat="server" />

    <script type="text/javascript" src="https://js.braintreegateway.com/v1/braintree.js"></script>
    <script type="text/javascript">
        var braintree = Braintree.create("YOURKEYHERE");
            braintree.onSubmitEncryptForm('braintree-payment-form');
    </script>

请注意几个关键细节在这里:

Please take note of a few key details here:


  • 我使用的服务器控件。并不简单的HTML标签。

  • I am using server controls. Not simple HTML tags.

braintree.js将加密具有数据加密的名称属性的任何领域。

braintree.js will encrypt any field that has the "data-encrypted-name" attribute.

数据加密-name属性确实不会需要是相同的
作为控件的ID属性。

The "data-encrypted-name" attribute does NOT need to be the same as the control's ID attribute.

braintree.js 脚本发布到
布伦特里支付形式的形式。

所以,当我点击提交按钮这种形式自然会后回来。我使用的具体形式有一个母版页,所以我需要改变Form.ID在Page_Load:

So when I click Submit button this form will naturally post back. The particular form that I'm using has a master page, so I need to alter the Form.ID in the Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
  //Adjust the properties of the form itself as this
  //form has a master page.         
  Form.ID = "braintree-payment-form";

  //Wire up the click event
  btnSubmit.Click += btnSubmit_Click;
}

在单击事件处理程序,我就能够从的Request.Form对象提取加密的值,然后提交交易请求布伦特里网关:

In the click event handler I am then able to extract the encrypted values from the Request.Form object and then submit the transaction request to the Braintree Gateway:

void btnSubmit_Click(object sender, EventArgs e)
{
  //--------------------------------------------------------------------------------------------------
  //Extract encrypted values from the Request.Form object
  //braintree.js has encrypted these values before placing them in
  //the Request object.
  //--------------------------------------------------------------------------------------------------
  string number = Request.Form["number"].ToString();
  string cvv = Request.Form["cvv"].ToString();
  string month = Request.Form["month"].ToString();
  string year = Request.Form["year"].ToString();

  //--------------------------------------------------------------------------------------------------
  //Gateway
  //This is the Braintree Gateway that we will use to post the transaction
  //to.  This is included here in the example but should be extracted out
  //into some static class somewhere.  Possibly in another layer.
  //--------------------------------------------------------------------------------------------------
  BraintreeGateway Gateway = new BraintreeGateway
  {
    Environment = Braintree.Environment.SANDBOX,
    PublicKey = "YOURPUBLICKEYHERE",
    PrivateKey = "YOURPRIVATEKEYHERE",
    MerchantId = "YOURMERCHANTIDHERE"
  };

  //--------------------------------------------------------------------------------------------------
  //Transaction Request
  //This is the actual transaction request that we are posting to the 
  //Braintree gateway.  The values for number, cvv, month and year have 
  //been encrypted above using the braintree.js.  If you were to put a 
  //watch on the actual server controls their ".Text" property is blank
  //at this point in the process.
  //--------------------------------------------------------------------------------------------------
  TransactionRequest transactionRequest = new TransactionRequest
  {
    Amount = 100.00M,
    CreditCard = new TransactionCreditCardRequest
    {
      Number = number,
      CVV = cvv,
      ExpirationMonth = month,
      ExpirationYear = year,
    }
  };

  //--------------------------------------------------------------------------------------------------
  //Transaction Result
  //Here we are going to post our information, including the encrypted
  //values to Braintree.  
  //--------------------------------------------------------------------------------------------------
  Result<Transaction> result = Gateway.Transaction.Sale(transactionRequest);
}

OK,所以这是一个非常基本的如何在交易后布伦特里例子。然而,它解决了第一个大障碍,我有得到它获取到我的服务器之前,卡中的数据进行加密。希望这有助于...

OK, so this is a very basic example of how to post a transaction to Braintree. However it solves the first big hurdle that I have with getting the card data to be encrypted before it gets to my server. Hope this helps...

这篇关于我可以用Braintree.js与.NET Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆