Skip to content

Commit

Permalink
fix for invoices bug (undefined customerId)
Browse files Browse the repository at this point in the history
  • Loading branch information
tima101 committed Jul 20, 2019
1 parent 56983a8 commit d28af82
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions api/server/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,24 @@ class UserClass extends mongoose.Model {

logger.debug('called static method on User');

logger.debug(user.stripeCustomer.id);

if (!user.stripeCustomer.id) {
throw new Error('You are not a customer and you have no payment history.');
}

const newListOfInvoices = await getListOfInvoices({
customerId: user.stripeCustomer.id,
});

if (newListOfInvoices.data === undefined || newListOfInvoices.data.length === 0) {
throw new Error('You are a customer. But there is no payment history.');
}

const modifier = {
stripeListOfInvoices: newListOfInvoices,
};

if (!newListOfInvoices) {
throw new Error('There is no payment history.');
}

return this.findByIdAndUpdate(userId, { $set: modifier }, { new: true, runValidators: true })
.select('stripeListOfInvoices')
.setOptions({ lean: true });
Expand Down
2 changes: 1 addition & 1 deletion api/server/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function stripeWebHooks({ server }) {

function getListOfInvoices({ customerId }) {
logger.debug('getting list of invoices for customer', customerId);
return stripeInstance.invoices.list({ customer: customerId });
return stripeInstance.invoices.list({ customer: customerId, limit: 100 });
}

export {
Expand Down

0 comments on commit d28af82

Please sign in to comment.