我正在try 确定客户是否有活动订阅.为此,我使用以下代码:
const stripe = require('stripe')('some-api-key');
export default function Example(){
// the user will automatically be considered non-subbed by default
const [isSubscriber, setIsSubscriber] = useState(false)
// grab the customer id from stripe
async function get_customer_id() {
const customers = await stripe.customers.search({
query: `metadata[\'some-meta-data-key\']:\'some-meta-data-value\'`
});
return customers.data[0]['id']
}
// grab the list of active subscriptions from stripe
async function customer_is_subscriber(){
const subs = await stripe.subscriptions.list({
status: 'active',
});
return subs
}
// determine if the customer id is in the list of active subscriptions.
// return true if so, false otherwise
async function test_equality(){
const customer_id = await get_customer_id();
const subbed = await customer_is_subscriber();
const answer = subbed.find(sub => sub.customer === customer_id)
return !!answer;
}
useEffect( () => {
async function load_result() {
const promise_function_return = await test_equality()
setIsSubscriber(promise_function_return)
}
load_result();
}, [isSubscriber]);
return (
// some react code
)
}
我已经能够成功地获得我的所有其他功能,其中我正在对用户是否是订阅者进行比较,但我遇到的问题是更新状态值(例如,如果是子床,则为true,否则为false).
我发现了一些关于这个特定主题的好问题,比如:
这里:setState inside Promise in React
这里是:setState inside a Promise function in a useEffect with hooks?
但我就是没能让它正常工作.这是我目前最接近解决这个问题的一次.