如何从网络发送自定义金额平方付款API

时间:2019-08-15 23:33:54

标签: javascript square-connect

我正在考虑通过网站上的Square Connect API进行付款,但是我不知道如何允许用户键入他们希望的任何付款金额(例如PayPal支付我按钮)。可以这样做,还是只能通过Square以预设的固定金额付款?

在Square的示例中,似乎是在后端代码中设置了金额,无法通过前端发送交易的金额。

我正在使用https://developer.squareup.com/docs/payment-form/payment-form-walkthrough

在以下节点上跟踪Square的演练

很抱歉,是否曾经有人问过这个问题,但是最近找不到解决此问题的信息。

4 个答案:

答案 0 :(得分:0)

如果要从前端发送金额,则需要通过表单中的字段(例如,您提供的示例中的现时)提交。即,如果您希望客户填写金额,您将得到以下信息:

<form>
...
<input type="text" id="amount" name="amount">
...
</form>

作为表单中的字段,然后在后端中从amount名称中检索(取决于您使用的后端语言,但是例如,PHP类似于{{1} }。

答案 1 :(得分:0)

我最终通过在前端.js代码中将获取代码显式地编码为/process-payment来解决了这个问题,而不是将其以表单形式提交,所以看起来像这样:

function submitPaymentRequest(event) {
  let nonce = document.getElementById("card-nonce").value;

  fetch("process-payment", {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      nonce: nonce,
      amount: document.querySelector('input[name="amount"]').value
    })
  })
    .then(response => {
      console.log(response);
      response.json().then(data => {
        console.log(data);
        alert(JSON.stringify(data));
      });
    })
    .catch(function(error) {
      console.log("the error was", error);
    });
}

我发布了此here的完整示例。

答案 2 :(得分:0)

是的,我遇到了同样的问题,但我提出了解决方案。 首先,您创建一个隐藏类型字段并给它们一些值,例如 100

**>  <input type="hidden" id="amount" name="amount" value="1000"  />**

在表单下方添加此行。然后转到付款处理页面,

**

> string am = Request.Form["amount"];

** 获取我在 c# 中所做的输入值,因此它是一个 c# 代码。 然后将此 am 字符串类型变量传递给 Amountmoney 部分。如下图。

var amountMoney = new Money.Builder()
                  **.Amount(Convert.ToInt64(am))**
                  .Currency("USD")
                  .Build();

答案 3 :(得分:0)

完整代码如下:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using Square;
 using Square.Models;
 using Square.Exceptions;
 using Square.Apis;
 
 public partial class process_payment : System.Web.UI.Page
 {
     private SquareClient client;
     public string ResultMessage
     {
         get;
         set;
     }
     protected  void Page_Load(object sender, EventArgs e)
     {
         client = new SquareClient.Builder()
                        .Environment(Square.Environment.Sandbox)
                        .AccessToken("YOUR ACCESS TOKEN")
                        .Build();
 
 
 
         string nonce = Request.Form["nonce"];
         string am = Request.Form["amount"];
         IPaymentsApi PaymentsApi = client.PaymentsApi;
 
         var amountMoney = new Money.Builder()
                   .Amount(Convert.ToInt64(am))
                   .Currency("USD")
                   .Build();
         string idempotencyKey = NewIdempotencyKey();
         var body = new CreatePaymentRequest.Builder(
             sourceId: nonce,
             idempotencyKey: idempotencyKey,
             amountMoney: amountMoney)
           .Build();
 
         CreatePaymentRequest createPaymentRequest = new CreatePaymentRequest.Builder(nonce, idempotencyKey, amountMoney)
            .Note("From Square Sample Csharp App")
            .Build();
 
         try
         {
             CreatePaymentResponse response = PaymentsApi.CreatePayment(createPaymentRequest);
 
             this.ResultMessage = "Payment complete! " + response.Payment.Note;
         }
         catch (ApiException es)
         {
             this.ResultMessage = es.Message;
         }
 
     }

     private static string NewIdempotencyKey()
     {
         return Guid.NewGuid().ToString();
     }
 }