gpt4 book ai didi

javascript - 如何使用 Stripe 接受比特币和美元付款?

转载 作者:行者123 更新时间:2023-12-03 02:50:27 25 4
gpt4 key购买 nike

引用:

https://stripe.com/docs/sources/bitcoin

<小时/>

问题:

我正在尝试使用 Stripe 将比特币支付集成到我的代码中。

根据我对文档的理解,我应该通过创建一个新表单来创建一个 Source 对象,并将数据(在本例中为电子邮件和金额)提交到:

stripe.createSource({
type: 'bitcoin',
amount: 1000,
currency: 'usd',
owner: {
email: 'jenny.rosen@example.com',
},
}).then(function(result) {
// handle result.error or result.source
});

然后将结果传递给服务器对Source对象进行计费。唯一的问题:我不知道如何将结果传递到服务器。我应该创建一个新的处理程序吗?

var handler = StripeCheckout.configure({
key: 'key',
locale: 'auto',
name: 'website',
description: 'Secure Payment',
token: function(token) {
$('#stripeToken').val(token.id);
$("#stripeEmail").val(token.email);
$('form').submit();
}
});

我迷路了。

这是我目前拥有的代码,非常适合美元付款。

<小时/>

我的代码:

客户端( payment.ejs )

<% include partials/header %>
<div class="background">
<div class="message">
<div class="paymentBlock">
<h1 class="title">TITLE</h1>

<form class="paymentForm" action="/payment/charge" method="POST">
<input id="inputAmount" class="amountInput" name="amount" type="number/>
<input type="hidden" id="stripeToken" name="stripeToken" />
<input type="hidden" id="stripeEmail" name="stripeEmail"/>
<button type="submit" class="btn btn-success" id="paymentButton" >Submit Payment</button>
</form>
</div>
</div>
</div>

<script src="https://checkout.stripe.com/checkout.js"></script>

<script>

var handler = StripeCheckout.configure({
key: 'key',
locale: 'auto',
name: 'website',
description: 'Secure Payment',
token: function(token) {
$('#stripeToken').val(token.id);
$("#stripeEmail").val(token.email);
$('form').submit();
}
});

$('#paymentButton').on('click', function(e) {
e.preventDefault();

$('#error_explanation').html('');

var amount = $('#inputAmount').val();
amount = amount.replace(/\$/g, '').replace(/\,/g, '');

amount = parseFloat(amount);

if (isNaN(amount)) {
$('#error_explanation').html('<p>Please enter a valid amount in USD ($).</p>');
}
else if (amount < 1.00) {
$('#error_explanation').html('<p>Payment amount must be at least $1.</p>');
}
else {
amount = amount * 100;
handler.open({
amount: Math.round(amount)
})
}
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>

<% include partials/indexScripts %>

服务器端( payment.js )

router.post("/", (req, res) => {

var amount = req.body.amount;
var object;
var ARef = admin.database().ref("ref");
var ARefList;
amount = amount * 100;

var object = {
amount: amount,
email: email,
inversedTimeStamp: now
}

stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
})
.then(customer =>
stripe.charges.create({
amount: amount,
description: "desc",
currency: "usd",
customer: customer.id
})
)
.then(charge =>
ARef.transaction(function(dbAmount){
if (!dbAmount) {
dbAmount = 0;
}
dbAmount = dbAmount + amount/100;
return dbAmount;
})
)
.then( () =>
ARef.push(object)
)
.then ( () =>
ARefList.push(object)
)
.then( () =>
res.render("received",{amount:amount/100})
);
});

最佳答案

感谢来自 freenode IRC#stripe 的“pksk321”帮助我解决这个问题!

显然,所需要的只是将 bitcoin: true 添加到处理程序中,如下所示:

客户端

var handler = StripeCheckout.configure({
key: 'key',
locale: 'auto',
name: 'website',
description: 'Secure Payment',
bitcoin: true,
token: function(token) {
$('#stripeToken').val(token.id);
$("#stripeEmail").val(token.email);
$('form').submit();
}
});

关于javascript - 如何使用 Stripe 接受比特币和美元付款?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47891077/

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