From 9c12a2a043952b0e82feb001e267eb5f445134ed Mon Sep 17 00:00:00 2001 From: Simon Backx Date: Thu, 17 Nov 2022 14:33:46 +0100 Subject: [PATCH] Limited Stripe price lookups (#15823) refs https://github.com/TryGhost/Team/issues/2262 Makes sure we only loop active Stripe prices. If we find an inactive price, we also update it in our database now after this change. --- ghost/payments/lib/payments.js | 13 ++++++++++--- ghost/stripe/lib/StripeAPI.js | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/ghost/payments/lib/payments.js b/ghost/payments/lib/payments.js index e6c40f1021..b24df82a02 100644 --- a/ghost/payments/lib/payments.js +++ b/ghost/payments/lib/payments.js @@ -214,8 +214,9 @@ class PaymentsService { stripe_product_id: product.id, currency, interval: cadence, - amount - }).query().select('stripe_price_id'); + amount, + active: true + }).query().select('id', 'stripe_price_id'); for (const row of rows) { try { @@ -224,9 +225,15 @@ class PaymentsService { return { id: price.id }; + } else { + // Update the database model to prevent future Stripe fetches when it is not needed + await this.StripePriceModel.edit({ + active: !!price.active + }, {id: row.id}); } } catch (err) { - logging.warn(err); + logging.error(`Failed to lookup Stripe Price ${row.stripe_price_id}`); + logging.error(err); } } diff --git a/ghost/stripe/lib/StripeAPI.js b/ghost/stripe/lib/StripeAPI.js index 7a09083402..b5ae3be4ed 100644 --- a/ghost/stripe/lib/StripeAPI.js +++ b/ghost/stripe/lib/StripeAPI.js @@ -1,5 +1,5 @@ const {VersionMismatchError} = require('@tryghost/errors'); -const debug = require('@tryghost/debug'); +const debug = require('@tryghost/debug')('stripe'); const Stripe = require('stripe').Stripe; const LeakyBucket = require('leaky-bucket'); const EXPECTED_API_EFFICIENCY = 0.95;