Javascript 如何从web发送自定义金额平方付款API
我正在考虑通过网站上的Square connect API进行付款,但我不知道如何允许用户输入他们想要的任何付款金额(如PayPal pay me按钮)。这是可能的,还是您只能通过Square接受预设固定金额的付款 在Square的示例中,金额是在后端代码中设置的,无法从前端发送交易金额 我将在此处使用Node完成Square的演练:Javascript 如何从web发送自定义金额平方付款API,javascript,square-connect,Javascript,Square Connect,我正在考虑通过网站上的Square connect API进行付款,但我不知道如何允许用户输入他们想要的任何付款金额(如PayPal pay me按钮)。这是可能的,还是您只能通过Square接受预设固定金额的付款 在Square的示例中,金额是在后端代码中设置的,无法从前端发送交易金额 我将在此处使用Node完成Square的演练: 如果以前有人问过这个问题,我很抱歉,但我找不到解决这个问题的最新信息。如果您想从前端发送金额,您需要通过表单中的字段提交(如您提供的示例中的nonce)。也就是说
如果以前有人问过这个问题,我很抱歉,但我找不到解决这个问题的最新信息。如果您想从前端发送金额,您需要通过表单中的字段提交(如您提供的示例中的nonce)。也就是说,如果您希望客户填写金额,您可以使用以下内容:
<form>
...
<input type="text" id="amount" name="amount">
...
</form>
...
...
作为表单中的一个字段,然后在后端从
amount
名称中检索它(这取决于您使用的后端语言,但作为一个示例,PHP类似于$\u POST['amount']
如果您想从前端发送金额,则需要通过表单中的字段提交(如您提供的示例中的nonce)。例如,如果您希望客户填写金额,您可以使用以下内容:
<form>
...
<input type="text" id="amount" name="amount">
...
</form>
...
...
作为表单中的字段,然后在后端从
amount
名称中检索它(这取决于您使用的后端语言,但作为示例,PHP类似于$\u POST['amount']
我最终解决了这个问题,在frontend.js代码中显式地对fetch to/process payment
进行编码,而不是将其放在表单submit中,因此它看起来像这样:
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);
});
}
我发布了一个完整的工作示例。我最终解决了这个问题,在frontend.js代码中显式地对fetch to
/process payment
进行了编码,而不是在表单submit中进行编码,因此它看起来像这样:
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);
});
}
我发布了一个完整的工作示例。是的,我面临同样的问题,但我提出了解决方案。 首先,创建一个隐藏类型字段,并给它们一些值,例如100
**> <input type="hidden" id="amount" name="amount" value="1000" />**
**
获取我在c中所做的输入值,因此这是一个c代码。
然后将这个am字符串类型变量传递给Amountmoney部分,如下所示
var amountMoney = new Money.Builder()
**.Amount(Convert.ToInt64(am))**
.Currency("USD")
.Build();
是的,我面临同样的问题,但我提出了解决方案。 首先,创建一个隐藏类型字段,并给它们一些值,例如100
**> <input type="hidden" id="amount" name="amount" value="1000" />**
**
获取我在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();
}
}
以下是完整的代码:
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();
}
}
Ryan-名为process payment的文件在您的Github文件中不存在。fetch命令不是对实际文件的调用吗?Ryan-名为process payment的文件在您的Github文件中不存在。fetch命令不是对实际文件的调用吗?请不要只发布代码作为答案,还要解释代码的作用和作用w它解决了问题。带解释的答案通常更有帮助,质量更好,更有可能吸引观众。请不要只发布代码作为答案,还要解释代码的作用以及它如何解决问题。带解释的答案通常更有帮助ul和更高的质量,并且更有可能吸引更多的选票