【发布时间】:2020-10-24 16:15:40
【问题描述】:
Same issue with other frontend framework and it fixed in some
我正在尝试在 Sapper 应用中使用条带。 Stripe 需要使用您在页面顶部引用的安全 stripe.js 发送信用卡数据。我使用 svelte onMount() 是因为使用他们的脚本进行条带提交数据并且不涉及服务器。
单击购物车组件中的结帐按钮后,我到达了此付款组件,该组件使用 goTo 将我定向到此付款页面/组件:
function checkout(){
// handle shopping cart items
await goto('./checkout/pay');
}
navigateAndSave()
}
我的支付组件有以下代码来收集信用卡数据:
onMount(async () => {
var stripe = await Stripe('pk_test....');
// call stripe.elements which will create the credit card fields
// use the fields for card #, expiration and cvv
var elements = await stripe.elements();
var card = await elements.create('card');
// mount the card to the dom
card.mount("#card-element");
card.on('change', ({error}) => {
// code the validate the credit card number, expiration date and other data.
}); //end of card.on fn
// code to charge the card. Call
// now that you mounted the stripe credit card Elment. Collect credit
// card details and submit it to stripe to process the payment using
// their api confirmCardPayment
stripe.confirmCardPayment(client_secret, {
payment_method: {
// the stripe card element has the credit card data
card: card,
billing_details: {
name: 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
}
}
}).then(function(result) {
if (result.error) {
console.log(result.error.message);
} else {
if (result.paymentIntent.status === 'succeeded') {
// update db, send emails to customer and admin...etc
}
}
});
// End of submit card information code
} // end onMount()
我的html表单是:
<svelte:head>
<script src="https://js.stripe.com/v3/"></script>
</svelte:head>
<h1> clientSecret Checkout </h1>
<form on:submit|preventDefault={onMount}>
<div id="card-element">
<!-- Elements will create input elements here -->
</div>
<!-- We'll put the error messages in this element -->
<div id="card-errors" role="alert"></div>
<button id="submit">Pay </button>
</form>
当我通过填写所有字段(iframe 来自条纹)提交付款表单并单击付款按钮时,我收到以下错误 未捕获(承诺中)IntegrationError:我们无法从指定的元素中检索数据。 请确保您尝试使用的元素仍处于挂载状态。
我看到卡片字段并输入了所需的卡片详细信息,但错误显示该元素未安装或我正在使用的元素未安装。所以在我看来,这与安装组件有关。
任何了解 svelte、组件安装的人都应该能够帮助解决这个问题。在阅读了 vue、react 和 angular 中的一些其他类似问题后,我意识到这是安装前端的具体问题,但我还不能用 svlete 弄清楚。
在 Svelte/sapper 中安装 iframe 是否存在问题? 查看了 sapper preload fn 但我如何在我的 onMount() 中访问卡是我面临的另一个问题。如果我在预加载中放入任何内容,我如何访问卡?
有什么办法吗?
【问题讨论】:
-
你试过运行 await tick();作为 onMount() 的第一行? svelte.dev/docs#tick
-
@digby280,是的,试过了,问题仍然存在。
标签: javascript stripe-payments sapper svelte-3 svelte-component