简体   繁体   English

带有 Angular 错误的条带付款:“客户 | 类型”上不存在“属性”来源已删除客户'

[英]Stripe Payments with Angular error: 'Property 'sources' does not exist on type 'Customer | DeletedCustomer'

I am trying to follow a stripe/cloud functions implementation tutorial but i am getting an error.我正在尝试遵循条带/云功能实现教程,但出现错误。

I am trying to attach a payment source to a customer, but i am getting error: 'Property 'sources' does not exist on type 'Customer |我正在尝试将付款来源附加到客户,但出现错误:'客户 | 类型不存在属性'来源' DeletedCustomer'已删除客户'

export const attachSource = async(uid: string, source: string) => {

const customer = await getOrCreateCustomer(uid);

const existingSource = customer.sources.data.filter(s => s.id === source).pop();

if (existingSource) {
    return existingSource;
} 
else {
    await stripe.customers.createSource(customer.id, { source: source });
    // update default
    return await stripe.customers.update(customer.id, { default_source: source });
}

} }

the customer.sources is what is giving the error. customer.sources 是给出错误的原因。

Here is the getOrCreateCustomer() function:这是 getOrCreateCustomer() function:

export const getOrCreateCustomer = async (uid: string) => {
     const user = await getUser(uid);
     const customerId = user && user.stripeCustomerId;

     // If missing customerId, create it
     if (!customerId) {
        return createCustomer(uid);
     } else {
        return stripe.customers.retrieve(customerId);
     }
 }

Your getOrCreateCustomer function returns a Promise if the customer ID already exists instead of a Stripe customer object.如果客户 ID 已经存在,而不是 Stripe 客户 object,则您的getOrCreateCustomer function 返回 Promise。 Likely your createCustomer function does the same as you aren't using await or using callback functions to get the result asynchronously.您的createCustomer function 的作用可能与您不使用await或使用回调函数异步获取结果相同。 You likely want to amend your code to the following:您可能希望将代码修改为以下内容:

export const getOrCreateCustomer = async (uid: string) => {
     const user = await getUser(uid);
     const customerId = user && user.stripeCustomerId;

     // If missing customerId, create it
     if (!customerId) {
        // createCustomer likely uses stripe.customers.create, so needs to work asynchronously 
        return await createCustomer(uid);
     } else {
        // asynchronously wait for a customer
        return await stripe.customers.retrieve(customerId);
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM