英文:
react context lost after stripe returns to success page
问题
My app uses NextJs 13 using pages
我的应用程序使用NextJs 13,使用页面
I am using react context holding basket info.
我使用React上下文来保存购物篮信息。
After I make a call to Stripe checkout, I get a redirect from Stripe to my payment success page. In the success page my context information is now empty.
在我调用Stripe结帐后,我从Stripe被重定向到我的付款成功页面。在成功页面上,我的上下文信息现在为空。
How to get the context information back.
如何获取上下文信息。
I call stripe with
我使用以下方式调用Stripe
const checkoutSession: Stripe.Checkout.Session = await stripe.checkout.sessions.create(params);
My context covers the whole app.
我的上下文覆盖整个应用程序。
How do I retrieve the BasketProvider data in the stripe success page?
如何在Stripe成功页面中检索BasketProvider的数据?
英文:
My app uses NextJs 13 using pages
I am using react context holding basket info.
After I make a call to Stripe checkout, I get a redirect from Stripe to my payment success page. In the success page my context information is now empty.
How get the context information back.
I call stripe with
const checkoutSession: Stripe.Checkout.Session =
await stripe.checkout.sessions.create(params);
My context covers the whole app.
<UserProvider>
<LayoutProvider>
<BasketProvider>
<Layout>
<Component {...pageProps} />
</Layout>
</BasketProvider>
</LayoutProvider>
</UserProvider>
How do I retrieve the BasketProvider data in the stripe success page?
答案1
得分: 0
您的上下文为空,因为在重定向到 Stripe 时您离开了页面。有两种处理方法:
-
可能有一些参数由 Stripe 返回到您的页面,这将允许您在成功页面中再次获取数据。
-
您可以以某种形式持久化您的数据,并在成功页面呈现时使用它重新加载您的上下文。
英文:
Your context is empty because you leave the page when redirecting to stripe. There are two options how this can be tackled:
-
There is probably some parameters that are passed by stripe back to your page that will allow you to fetch the data again in the success page.
-
You can persist your data in some form or shape and rehydrate your context with it when the success page is rendered.
答案2
得分: 0
感谢您的建议。我采取的方法是:
1)将上下文值保存到数据库,并将ID保存为Stripe调用中的元数据项;
2)在pages/api目录中监听webhook事件checkout.session.completed,然后从数据库中获取上下文ID,以便重新创建上下文对象。
英文:
Thanks for the suggestion. The approach I took was:
- Save the context values to DB and save the ID as metadata item in the stripe call;
- In the pages/api directory listen for webhook event checkout.session.completed and then get the context id from DB so I can recreate the context object.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论