简体   繁体   English

Node.js上的Stripe API错误:没有这样的客户:

[英]Stripe API on Node.js Error: No such customer:

Pretty new to this. 这很新。 Everything works fine, card validates, but when they submit the information the webpage shows "internal server error" and the error log shows "Error: no such customer". 一切正常,卡进行了验证,但是当他们提交信息时,网页显示“内部服务器错误”,错误日志显示“错误:没有这样的客户”。 Research on that error message shows that something is wrong in my code, but i'm having trouble spotting it. 对错误消息的研究表明,我的代码有问题,但是我很难发现它。

Customers are paying for a subscription product. 客户正在为订阅产品付费。

  let promise = Promise.resolve();
    if(user.stripe_plan_id !== plan || card_token){
        promise = promise.then( () => {
            return new Promise( (resolve, reject) => {
                if(!user.stripe_user_id){
                    stripe.customers.create({
                        description: 'Customer for test@example.com',
                        email: user.email,
                        //source: "tok_16UzfA2eZvKYlo2CVJRxXLSR" // obtained with Stripe.js
                        plan: 'free',
                        metadata: {
                            userId: user.id
                        }
                    }, function(err, customer) {
                        if(err){
                            reject(err);
                        }else{
                            user.stripe_user_id = customer.id;
                            resolve();
                        }
                    });
                }else{
                    resolve();
                }
            });
        });
    }
    if(card_token){
        promise = promise.then( () => {
            return new Promise( (resolve, reject) => {
                stripe.customers.update(user.stripe_user_id, {
                    source: card_token
                }, function(err, customer) {
                    if(err){
                        reject(err);
                    }else{
                        let last4;
                        if(customer.default_source){
                            customer.sources.data.forEach( (source) => {
                                if(source.id == customer.default_source){
                                    last4 = source.last4;
                                }
                            });
                        }
                        user.last4 = last4 || '';
                        resolve(); //next section will save the user anyways.
                    }
                });
            });
        });
    }
    if(user.stripe_plan_id !== plan){
        promise = promise.then( () => {
            return new Promise( (resolve, reject) => {
                stripe.customers.listSubscriptions(user.stripe_user_id, function(err, subscriptions) {
                    if(err){
                        reject(err);
                    }else{
                        resolve(subscriptions);
                    }
                });
            });
        }).then( (subscriptions) => {
            return new Promise( (resolve, reject) => {
                if(subscriptions.data[0]){
                    stripe.customers.updateSubscription(
                        user.stripe_user_id,
                        subscriptions.data[0].id,
                        { plan },
                        function(err, subscription) {
                            if(err){
                                reject(err);
                            }else{
                                user.stripe_plan_id = plan;
                                resolve();
                            }
                        }
                    );
                }else{
                    stripe.customers.createSubscription(
                        user.stripe_user_id,
                        {plan},
                        function(err, subscription) {
                            if(err){
                                reject(err);
                            }else{
                                user.stripe_plan_id = plan;
                                resolve();
                            }
                        }
                    );
                }
            });
        });
    }
    return promise.then( () => {
        return user.save();
    });
}).then( (user) => {
    res.json({
        plan: user.stripe_plan_id
    }).end();
}).catch( (err) => {
    console.error(err);
    if(err.message === 'This customer has no attached payment source'){
        res.json({
            error: true,
            message:'Payment source missing.'
        }).end();
        return;
    }
    res.json({
        error: true,
        message:typeof(err)==="string"?err:'internal server error'
    }).end();
});

}); });

So the message you're seeing here just means that the customer you're trying to update doesn't exist in the Stripe account you're attempting this from. 因此,您在此处看到的消息仅表示您尝试更新的Stripe帐户中不存在您要更新的客户。 The message you receive in your error response should provide a customer ID; 您在错误响应中收到的消息应提供客户ID; if you search for that customer in your Stripe dashboard (or via the API), do you see it there? 如果您在Stripe仪表板中(或通过API)搜索该客户,您在那看到吗? I would suspect that user.stripe_user_id is already set and you're either trying to update a customer that exists on another account or you've got some other invalid value there. 我怀疑user.stripe_user_id已经设置好了,或者您试图更新另一个帐户上存在的客户,或者那里有一些其他无效值。

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

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