React上下文在Stripe返回成功页面后丢失。

huangapple go评论68阅读模式
英文:

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

  1. const checkoutSession: Stripe.Checkout.Session =
  2. await stripe.checkout.sessions.create(params);

My context covers the whole app.

  1. <UserProvider>
  2. <LayoutProvider>
  3. <BasketProvider>
  4. <Layout>
  5. <Component {...pageProps} />
  6. </Layout>
  7. </BasketProvider>
  8. </LayoutProvider>
  9. </UserProvider>

How do I retrieve the BasketProvider data in the stripe success page?

答案1

得分: 0

您的上下文为空,因为在重定向到 Stripe 时您离开了页面。有两种处理方法:

  1. 可能有一些参数由 Stripe 返回到您的页面,这将允许您在成功页面中再次获取数据。

  2. 您可以以某种形式持久化您的数据,并在成功页面呈现时使用它重新加载您的上下文。

英文:

Your context is empty because you leave the page when redirecting to stripe. There are two options how this can be tackled:

  1. 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.

  2. 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:

  1. Save the context values to DB and save the ID as metadata item in the stripe call;
  2. 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.

huangapple
  • 本文由 发表于 2023年5月17日 15:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定