Javascript 是什么导致平方积分的误差
我正在与Squareup.com建立我的第一个集成。在文档的演练页面()中,我已经按照说明复制了代码,但是回调中的“catch error”导致了一个错误 [更新] 好的,在被告知applicationID错误后,我们发现是流程付款文件是空的,这导致了错误。我重新安装了npm并启动了它,我们正在继续:) 最后的问题 我认为server.js需要使用特定顺序的数据进行更新。在什么阶段最好这样做?大概是在一个隐藏参数内提交卡片详细信息时。但是应该给它起什么名字呢?我在文件里什么也没找到 [/更新] 感谢您的评论,作为回应,这里是完整的代码Javascript 是什么导致平方积分的误差,javascript,json,square,Javascript,Json,Square,我正在与Squareup.com建立我的第一个集成。在文档的演练页面()中,我已经按照说明复制了代码,但是回调中的“catch error”导致了一个错误 [更新] 好的,在被告知applicationID错误后,我们发现是流程付款文件是空的,这导致了错误。我重新安装了npm并启动了它,我们正在继续:) 最后的问题 我认为server.js需要使用特定顺序的数据进行更新。在什么阶段最好这样做?大概是在一个隐藏参数内提交卡片详细信息时。但是应该给它起什么名字呢?我在文件里什么也没找到 [/更新]
<body>
<div id="form-container">
<div id="sq-card-number"></div>
<div class="third" id="sq-expiration-date"></div>
<div class="third" id="sq-cvv"></div>
<div class="third" id="sq-postal-code"></div>
<button id="sq-creditcard" class="button-credit-card" onclick="onGetCardNonce(event)">Pay $1.00</button>
</div>
<!-- end #form-container -->
<script type="text/javascript">
const idempotency_key = uuidv4();
// Create and initialize a payment form object
const paymentForm = new SqPaymentForm({
// Initialize the payment form elements
applicationId: "edited-out-for-security",
inputClass: 'sq-input',
autoBuild: false,
// Customize the CSS for SqPaymentForm iframe elements
inputStyles: [{
fontSize: '16px',
lineHeight: '24px',
padding: '16px',
placeholderColor: '#a0a0a0',
backgroundColor: 'transparent',
}],
// Initialize the credit card placeholders
cardNumber: {
elementId: 'sq-card-number',
placeholder: 'Card Number'
},
cvv: {
elementId: 'sq-cvv',
placeholder: 'CVV'
},
expirationDate: {
elementId: 'sq-expiration-date',
placeholder: 'MM/YY'
},
postalCode: {
elementId: 'sq-postal-code',
placeholder: 'Postal'
},
// SqPaymentForm callback functions
callbacks: {
/*
* callback function: cardNonceResponseReceived
* Triggered when: SqPaymentForm completes a card nonce request
*/
cardNonceResponseReceived: function (errors, nonce, cardData) {
if (errors) {
// Log errors from nonce generation to the browser developer console.
console.error('Encountered errors:');
errors.forEach(function (error) {
console.error(' ' + error.message);
});
alert('Encountered errors, check browser developer console for more details');
return;
}
fetch('/cgi-bin/square-payment-processor/process-payment', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
nonce: nonce,
idempotency_key: idempotency_key,
location_id: "edited-out-for-security"
})
})
.catch(err => {
alert('Network error: ' + err);
})
.then(response => {
if (!response.ok) {
return response.json().then(
errorInfo => Promise.reject(errorInfo));
}
return response.json();
})
.then(data => {
console.log(data);
alert('Payment complete successfully!\nCheck browser developer
console for more details');
})
.catch(err => {
console.error(err);
alert('Payment failed to complete!\nCheck browser developer console for more details');
});
}
}
});
paymentForm.build();
//Generate a random UUID as an idempotency key for the payment request
// length of idempotency_key should be less than 45
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// onGetCardNonce is triggered when the "Pay $1.00" button is clicked
function onGetCardNonce(event) {
// Don't submit the form until SqPaymentForm returns with a nonce
event.preventDefault();
// Request a nonce from the SqPaymentForm object
paymentForm.requestCardNonce();
}
</script>
</body>
付1美元
常量幂等性_key=uuidv4();
//创建并初始化付款表单对象
const paymentForm=新的SqPaymentForm({
//初始化付款表单元素
applicationId:“出于安全考虑已编辑”,
inputClass:“sq输入”,
自动生成:错误,
//自定义SqPaymentForm iframe元素的CSS
输入样式:[{
fontSize:'16px',
线宽:“24px”,
填充:“16px”,
占位符颜色:'#A0',
背景色:“透明”,
}],
//初始化信用卡占位符
卡号:{
elementId:“sq卡号”,
占位符:“卡号”
},
cvv:{
elementId:'sq cvv',
占位符:“CVV”
},
截止日期:{
elementId:“sq到期日期”,
占位符:“MM/YY”
},
后代码:{
elementId:“sq邮政编码”,
占位符:“邮政”
},
//SqPaymentForm回调函数
回调:{
/*
*回调函数:CardNonCresponseReceived
*当:SqPaymentForm完成卡的临时请求时触发
*/
CardNoneResponseReceived:函数(错误、nonce、cardData){
如果(错误){
//将nonce生成中的错误记录到浏览器开发人员控制台。
console.error('遇到错误:');
errors.forEach(函数(错误){
控制台错误(“”+错误消息);
});
警报(“遇到错误,请查看浏览器开发人员控制台以了解更多详细信息”);
返回;
}
获取('/cgi bin/square付款处理程序/处理付款'{
方法:“POST”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”
},
正文:JSON.stringify({
暂时的,暂时的,
幂等键:幂等键,
位置\u id:“出于安全考虑已编辑”
})
})
.catch(错误=>{
警报(“网络错误:”+错误);
})
。然后(响应=>{
如果(!response.ok){
返回response.json()。然后(
errorInfo=>Promise.reject(errorInfo));
}
返回response.json();
})
。然后(数据=>{
控制台日志(数据);
警报('付款成功完成!\n检查浏览器开发人员
控制台以获取更多详细信息);
})
.catch(错误=>{
控制台错误(err);
警报(“付款未能完成!\n有关详细信息,请检查浏览器开发人员控制台”);
});
}
}
});
paymentForm.build();
//生成随机UUID作为付款请求的幂等键
//幂等键的长度应小于45
函数uuidv4(){
返回“xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxx”。替换(/[xy]/g),
职能(c){
var r=Math.random()*16 | 0,v=c='x'?r:(r&0x3 | 0x8);
返回v.toString(16);
});
}
//单击“支付$1.00”按钮时触发onGetCardNonce
函数onGetCardNonce(事件){
//在SqPaymentForm返回nonce之前不要提交表单
event.preventDefault();
//从SqPaymentForm对象请求一个nonce
paymentForm.requestCardNonce();
}
您可能会得到一个HTML错误页面,而不是JSON响应。查看浏览器开发工具以获得实际响应。恐怕您的问题没有包含足够的信息,我们无法提供进一步的信息。为什么您要向我们展示您的异常处理程序,而不是异常本身?解析JSON对象的代码在哪里?我感谢您添加代码,但是您是否按照建议查看了响应?谢谢Amy。我在代码的第二部分添加了console.log结果。它似乎没有定义,但似乎所有提交的数据都是正确的。Stingized json已正确填充。不,这还不够好。您的错误显示它以