Check for older webhooks and ignore

This commit is contained in:
Jonathan Barrow
2022-06-24 19:39:21 -04:00
parent e622a1e759
commit f32ea19630
2 changed files with 22 additions and 5 deletions

View File

@@ -597,15 +597,17 @@ router.post('/checkout/:priceId', async (request, response) => {
pnid_pid: pid
}
});
}
await database.PNID.updateOne({ pid }, { $set: {
await database.PNID.updateOne({ pid }, {
$set: {
connections: {
stripe: {
customer_id: customer.id
customer_id: customer.id // ensure PNID always has latest customer ID
}
}
} }, { upsert: true }).exec();
}
}
}, { upsert: true }).exec();
const priceId = request.params.priceId;

View File

@@ -102,6 +102,13 @@ async function handleStripeEvent(event) {
const pid = Number(customer.metadata.pnid_pid);
const pnid = await database.PNID.findOne({ pid });
const latestWebhookTimestamp = pnid.get('connections.stripe.latest_webhook_timestamp');
if (latestWebhookTimestamp && latestWebhookTimestamp > event.created) {
// Do nothing, this webhook is older than the latest seen
return;
}
if (!pnid && subscription.status !== 'canceled' && subscription.status !== 'unpaid') {
// PNID does not exist. Abort and refund!
await stripe.subscriptions.del(subscription.id);
@@ -120,7 +127,15 @@ async function handleStripeEvent(event) {
return;
}
const updateData = {};
const updateData = {
connections: {
stripe: {
price_id: subscription.plan.id,
tier_level: Number(product.metadata.tier_level || 0),
latest_webhook_timestamp: event.created
}
}
};
if (product.metadata.beta === 'true') {
switch (subscription.status) {