简体   繁体   中英

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. The message you receive in your error response should provide a customer ID; if you search for that customer in your Stripe dashboard (or via the API), do you see it there? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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