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

How to send custom amount Square payment API from web

我正在考虑通过网站上的 Square connect API 进行付款,但我不知道如何让用户输入他们想要的任何付款金额(例如 PayPal 付款我按钮)。这可能吗,或者您只能通过 Square 接受预先设定的固定金额的付款吗?

看起来在 Square 的示例中,金额是在后端代码中设置的,无法从前端发送交易金额。

我正在关注 Square 在此处使用 Node 的演练:https://developer.squareup.com/docs/payment-form/payment-form-walkthrough

如果之前有人问过这个问题,我深表歉意,但我最近找不到解决该问题的任何内容。

如果您想从前端发送金额,您需要通过表单中的一个字段提交它(就像您提供的示例中的随机数)。也就是说,如果您希望客户填写金额,您会得到如下内容:

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

作为表单中的一个字段,然后在后端从 amount 名称中检索它(这取决于您使用的后端语言,但作为示例 PHP 将是像 $_POST['amount'].

我最终通过在前端 .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 的完整工作示例。

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

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

在 form.Then 转到付款处理页面下方添加此行,

**

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

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

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

完整代码如下:

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