javascript - 从前端条带连接的调用,以编程方式更改条带连接的 ID
问题描述
我正在尝试解决这个我似乎无法用 Stripe 的 API 解决的问题
因此,当使用他们的新版本 API 创建费用时,他们说在前端我们应该调用 loadStripe('Pusblishable Key',{'Connected account ID'}) 并将其设置为 const。
现在我不明白我们应该如何获取存储在某个地方的 ID,比如数据库?
我目前正在做的是这样的
import React from 'react';
import ReactDOM from 'react-dom';
import {Elements} from '@stripe/react-stripe-js';
import {loadStripe} from '@stripe/stripe-js';
import CheckoutForm from './CheckoutForm';
//btw I have set this to const and to let and none work
const stripePromise =
fetch("url", {
method: "POST",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
anything:window.sessionStorage.getItem("Variable Account")
//here store something that will tell what account to pull ID data from
})
}).then(data => data.json()).then((result)=>{
return(loadStripe('KEY',{stripeAccount:result}))
})
class App extends React.Component{
render(){
return(
<Elements stripe={stripePromise}>
<CheckoutForm />
</Elements>
)}
}
export default(App)
但是 const 似乎无法正确加载,如果与应用程序的常规流程一起使用,例如来自 myapp.com/home -> 点击 myapp.com/home/something-> 那么 myapp.com/home/something/payment 条带是没有加载但刷新浏览器现在可以工作,但这告诉我我可能做错了什么,或者我必须在'componentDidMount()'中刷新应用程序?
可以将其设置为静态,但连接的帐户可能很多,所以如果有人可以帮助我,我将不胜感激
解决方案
回答与您的重复问题相同:如何使用异步值正确加载全局变量(Reactjs)?
通常,您希望在您的应用程序中使用此帐户 ID。但是,如果您需要检索它,那很好,但请确保stripePromise
它是您认为的那样。例如,我可以在这里使用模拟的 fetch 调用来完成这项工作:https ://codesandbox.io/s/stripe-connect-w-resolve-wts34
请注意,我正在明确管理 Promise:
const stripePromise = new Promise((resolve, reject) => {
fetch(...)
.then(data => data.json())
.then(result => {
resolve(
loadStripe(STRIPE_PUBKEY, { stripeAccount: "acct_xxx" })
);
});
});
您描述这种与导航中断的事实表明您可能路由不正确。如果这是一个单页应用程序,导航不应导致App
组件重新呈现。
推荐阅读
- javascript - 从 API 调用外部 URL
- php - PHP PDO 查询不返回任何数据,返回显示数据的 isset()
- regex - 如何编写使用正则表达式过滤 where 子句的 Kusto 查询
- python - 在 tkinter 中确定网格空间是否为空
- google-cloud-functions - 谷歌云函数:触发器
- highcharts - 我在哪里可以找到旧版本 Highcharts 的 API 文档?
- c# - 从另一个 c# 项目调用 api 路由
- performance - 由于不可避免的 CPU 负载,Direct2D 的“立即模式”性质是否会影响性能?
- react-native - 导航器导致不变违规
- python - 从命令行运行python脚本时找不到模块