【问题标题】:Can I Use Braintree.js With A .NET Web Application?我可以将 Braintree.js 与 .NET Web 应用程序一起使用吗?
【发布时间】:2023-04-01 03:30:01
【问题描述】:

所以我这几天一直在研究 Braintree Payments。我喜欢架构、概念等。在查看了文档和 .NET 演练之后,我注意到 .NET 的所有示例都在 MVC3 中。我正在尝试使用常规 Web 表单将 Braintree 集成到我当前的 .NET Web 应用程序中。

我的目标是将包含客户数据和卡数据的普通网络表单发回支付页面。卡片数据应该使用他们的 Braintree.js 加密。这样我就可以将所有内容发送到 Braintree 进行处理,包括加密的卡数据。

表单看起来像这样:

<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>

然后在代码隐藏中,我将 Form.Action、Form.Method 和 Form.ID 设置如下:

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

那么希望当用户提交表单时,它会点击“CreateTransaction()”成员以及“collection”参数中的加密卡数据,如下所示:

[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()”成员。我错过了什么?这可以使用常规的旧网络表单来完成吗?

【问题讨论】:

    标签: c# asp.net visual-studio-2012 braintree


    【解决方案1】:

    好的,所以经过大量的实验和在黑暗中拍摄后,我能够得到这个 Braintree.js 来加密数据,然后再将其传递到我的服务器。从那里我可以使用我们背后的代码来处理付款处理。

    这是我正在使用的 ASP.NET Web 表单:

    卡号

        <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>
    

    请注意这里的一些关键细节:

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

    • braintree.js 将加密任何具有“data-encrypted-name”属性的字段。

    • “data-encrypted-name”属性需要相同 作为控件的 ID 属性。

    • braintree.js 脚本正在发布到 “braintree-payment-form”表格。

    所以当我点击提交按钮时,这个表单自然会发回。我使用的特定表单有一个母版页,所以我需要更改 Page_Load 中的 Form.ID:

    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 对象中提取加密值,然后将交易请求提交给 Braintree 网关:

    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);
    }
    

    好的,这是一个非常基本的示例,说明如何将交易发布到 Braintree。然而,它解决了我在卡数据到达我的服务器之前对其进行加密的第一个大障碍。希望这会有所帮助...

    【讨论】:

    猜你喜欢
    • 2022-07-20
    • 2020-08-14
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多