gpt4 book ai didi

ios - Apple Pay - "Payment Not Completed"- 使用 Stripe

转载 作者:可可西里 更新时间:2023-11-01 05:42:45 29 4
gpt4 key购买 nike

我正在使用 Stripe “付款请求按钮”为我的网站实现 Apple Pay。在事物的 Stripe 方面一切都很好。正如我在 Stripe 日志中验证的那样, token 已正确传递。 https://stripe.com/docs/stripe-js/elements/payment-request-button

但是,每次我尝试完成测试付款时,Apple Pay 都会收到一条错误消息:“付款未完成”。

这让我卡住了,我不确定如何调试或修复。有任何想法吗?

我得到一个未定义的 token

这是错误:

enter image description here

我的设置:

前端:

<script src="https://js.stripe.com/v3/"></script>
<div id="payment-request-button">
<!-- A Stripe Element will be inserted here. -->
</div>



<script>
var stripe = Stripe('pk_test_xxxxx');

var paymentRequest = stripe.paymentRequest({
country: 'US',
currency: 'usd',
total: {
label: 'JobQuiz',
amount: 999,
},
requestPayerName: true,
requestPayerEmail: false,
});


var elements = stripe.elements();
var prButton = elements.create('paymentRequestButton', {
paymentRequest: paymentRequest,
});

// Check the availability of the Payment Request API first.
paymentRequest.canMakePayment().then(function(result) {
if (result) {
prButton.mount('#payment-request-button');
} else {
document.getElementById('payment-request-button').style.display = 'none';
}
});

paymentRequest.on('token', function(ev) {
// Send the token to your server to charge it!
fetch('/apple-pay', {
method: 'POST',
body: JSON.stringify({token: ev.token.id}),
headers: {'content-type': 'application/json'},
})
.then(function(response) {
if (response.ok) {
// Report to the browser that the payment was successful, prompting
// it to close the browser payment interface.
ev.complete('success');
} else {
// Report to the browser that the payment failed, prompting it to
// re-show the payment interface, or show an error message and close
// the payment interface.
ev.complete('fail');
}
});
});

</script>

app.js 中的服务器端代码

app.post('/apple-pay', function(req, res, next) {


// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
var stripe = require("stripe")("sk_test_xxxxxx");

// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
const token = req.body.token; // Using Express

const charge = stripe.charges.create({
amount: 999,
currency: 'usd',
description: 'Example charge',
source: token,
}, function(err, charge){
if (err){

} else {

}
});
});

最佳答案

终于解决了。它最终成为我的 bodyParser 设置的问题。这解释了为什么 token 是空的。我忽略了包括 app.use(bodyParser.json());下面...

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

关于ios - Apple Pay - "Payment Not Completed"- 使用 Stripe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53094547/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com