对此很新。一切正常,卡验证,但当他们提交信息时,网页显示"内部服务器错误"并且错误日志显示"错误:没有这样的客户"。对该错误消息的研究表明我的代码中出现了问题,但我发现它时遇到了问题。
客户正在为订阅产品付费。
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();
});
});
答案 0 :(得分:0)
因此,您在此处看到的消息只是意味着您尝试更新的客户在您尝试更新的Stripe帐户中不存在。您在错误响应中收到的消息应提供客户ID;如果您在Stripe仪表板(或通过API)中搜索该客户,您是否在那里看到它?我怀疑user.stripe_user_id
已经设置好了,您要么尝试更新另一个帐户中存在的客户,要么就是那里有其他无效值。