所以我目前正处于设计一家小型在线商店的最后阶段,我在理解 strip token 包含什么内容、如何在 Node.js 服务器上获取它以及诸如此类的事情上遇到了一些困难.
目前,我的客户端代码如下所示:
<div style="text-align: center;">
<form id="paymentForm" action="//httpbin.org/post" method="POST">
<input type="hidden" id="stripeToken" name="stripeToken" />
<input type="hidden" id="stripeEmail" name="stripeEmail" />
<input type="hidden" id="cartTotal" name="cartTotal" />
<input type="hidden" id="cartContents" name="cartContents" />
</form>
<p><input type="button" class="button" id="purchaseButton" value="チェックアウト"></p>
<script>
var totalCost = 0;
var totalCartLoad = "";
totalCost = localStorage.getItem('totalCartPrice');
totalCartLoad = localStorage.getItem('whatsInCart');
totalCartLoad = totalCartLoad.replace('undefined','');
totalCartLoad = '_____________________________________' + totalCartLoad;
var finalCartLoad = String(totalCartLoad); //convert it to a string for display
var handler = StripeCheckout.configure({
key: 'pk_test_6pRNASCoBOKtIshFeQd4XMUh',
token: function(token) {
$("#stripeToken").val(token.id);
$("#stripeEmail").val(token.email);
$("#cartTotal").val(totalCost);
$("#cartContents").val(finalCartLoad);
$("#paymentForm").submit();
}
});
$('#purchaseButton').on('click', function(e) {
// Open Checkout with further options
handler.open({
name: "チェックアウト",
description: finalCartLoad,
shippingAddress: true,
billingAddress: true,
zipCode: true,
allowRememberMe: true,
currency: 'JPY',
amount: totalCost
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>
</div>
我的服务器代码如下所示:
const stripe = require("stripe")("sk_test_BQokikJOvBiI2HlWgH4olfQ2");
module.exports = (req) => {
// the token is generated by Stripe and POST'ed
// to the `action` URL in our form
const token = req.body.stripeToken;
// now we create a charge which returns a `promise`
// so we need to make sure we correctly handle
// success and failure, but that's outside of this
// function (and we'll see it later)
return stripe.charges.create({
// ensures we send a number, and not a string
amount: parseInt(process.env.STRIPE_COST, 10),
currency: process.env.STRIPE_CCY,
source: token,
description: process.env.STRIPE_DESCRIPTION, // 👈 remember to change this!
// this metadata object property can hold any
// extra private meta data (like IP address)
metadata: {},
});
}
但是,我不确定如何确保我从客户处收集的所需详细信息(例如送货地址、客户电子邮件、产品 list 等)最终出现在我需要的地方(发票上)或者我帐户中 Stripe 上的某个位置。我也不确定具体是如何收费的(我知道我也需要一个 app.js 文件来配合此操作,所以我很感激此时的一些指示,因为它确实让我很头疼。
最佳答案
Token.id
是您在创建费用时想要用作源
的内容,看起来这就是您正在做的事情,所以您应该很高兴从那边开始。
您当前应该在 req.body.stripeEmail
中找到该电子邮件;事实上,您应该在 req.body
中找到以下所有内容:
$("#stripeToken").val(token.id); // req.body.stripeToken
$("#stripeEmail").val(token.email); // req.body.stripeEmail
$("#cartTotal").val(totalCost); // req.body.cartTotal
$("#cartContents").val(finalCartLoad); // req.body.cartContents
为了获取送货地址,您还需要传递这些地址;您可以在 args
中找到它们token()
函数的参数,因此您只需从那里提取所需的内容并将其也以表单形式发送即可。
关于javascript - 对 strip token 功能的澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51092443/