From f88b6bd02f30f7616690963768360fd93f222b06 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Tue, 15 Feb 2022 11:47:00 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20Stripe=20checkout=20sess?= =?UTF-8?q?ion=20urls=20being=20invalid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs https://github.com/TryGhost/Team/issues/1322 Since the Members service is no longer started when Stripe is reconfigured the url config used for checkout sessions was not updated. This moves all of the default Stripe urls into the Stripe service, where they will be updated as/when Stripe is connected/disconnected. --- core/server/services/members/config.js | 26 ----------------- core/server/services/stripe/config.js | 39 ++++++++++++++++++++++++-- package.json | 4 +-- yarn.lock | 18 ++++++------ 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/core/server/services/members/config.js b/core/server/services/members/config.js index 9386d4704d..543285c131 100644 --- a/core/server/services/members/config.js +++ b/core/server/services/members/config.js @@ -134,44 +134,18 @@ class MembersConfigProvider { return this.getActiveStripeKeys() !== null; } - getStripeUrlConfig() { - const siteUrl = this._urlUtils.getSiteUrl(); - - const checkoutSuccessUrl = new URL(siteUrl); - checkoutSuccessUrl.searchParams.set('stripe', 'success'); - const checkoutCancelUrl = new URL(siteUrl); - checkoutCancelUrl.searchParams.set('stripe', 'cancel'); - - const billingSuccessUrl = new URL(siteUrl); - billingSuccessUrl.searchParams.set('stripe', 'billing-update-success'); - const billingCancelUrl = new URL(siteUrl); - billingCancelUrl.searchParams.set('stripe', 'billing-update-cancel'); - - return { - checkoutSuccess: checkoutSuccessUrl.href, - checkoutCancel: checkoutCancelUrl.href, - billingSuccess: billingSuccessUrl.href, - billingCancel: billingCancelUrl.href - }; - } - getStripePaymentConfig() { if (!this.isStripeConnected()) { return null; } const stripeApiKeys = this.getActiveStripeKeys(); - const urls = this.getStripeUrlConfig(); if (!stripeApiKeys) { return null; } return { - checkoutSuccessUrl: urls.checkoutSuccess, - checkoutCancelUrl: urls.checkoutCancel, - billingSuccessUrl: urls.billingSuccess, - billingCancelUrl: urls.billingCancel, product: { name: this._settingsCache.get('stripe_product_name') }, diff --git a/core/server/services/stripe/config.js b/core/server/services/stripe/config.js index f34b50ffee..3d11521252 100644 --- a/core/server/services/stripe/config.js +++ b/core/server/services/stripe/config.js @@ -6,8 +6,41 @@ const messages = { }; // @TODO Refactor to a class w/ constructor + +/** + * @typedef {object} StripeURLConfig + * @prop {string} checkoutSessionSuccessUrl + * @prop {string} checkoutSessionCancelUrl + * @prop {string} checkoutSetupSessionSuccessUrl + * @prop {string} checkoutSetupSessionCancelUrl + */ + module.exports = { getConfig(settings, config, urlUtils) { + /** + * @returns {StripeURLConfig} + */ + function getStripeUrlConfig() { + const siteUrl = urlUtils.getSiteUrl(); + + const checkoutSuccessUrl = new URL(siteUrl); + checkoutSuccessUrl.searchParams.set('stripe', 'success'); + const checkoutCancelUrl = new URL(siteUrl); + checkoutCancelUrl.searchParams.set('stripe', 'cancel'); + + const billingSuccessUrl = new URL(siteUrl); + billingSuccessUrl.searchParams.set('stripe', 'billing-update-success'); + const billingCancelUrl = new URL(siteUrl); + billingCancelUrl.searchParams.set('stripe', 'billing-update-cancel'); + + return { + checkoutSessionSuccessUrl: checkoutSuccessUrl.href, + checkoutSessionCancelUrl: checkoutCancelUrl.href, + checkoutSetupSessionSuccessUrl: billingSuccessUrl.href, + checkoutSetupSessionCancelUrl: billingCancelUrl.href + }; + } + /** * @param {'direct' | 'connect'} type - The "type" of keys to fetch from settings * @returns {{publicKey: string, secretKey: string} | null} @@ -61,9 +94,11 @@ module.exports = { const webhookHandlerUrl = new URL('members/webhooks/stripe/', urlUtils.getSiteUrl()); + const urls = getStripeUrlConfig(); + return { - secretKey: keys.secretKey, - publicKey: keys.publicKey, + ...keys, + ...urls, enablePromoCodes: config.get('enableStripePromoCodes'), webhookSecret: webhookSecret, webhookHandlerUrl: webhookHandlerUrl.href diff --git a/package.json b/package.json index 58392704bf..c613752289 100644 --- a/package.json +++ b/package.json @@ -80,11 +80,11 @@ "@tryghost/limit-service": "1.0.9", "@tryghost/logging": "2.0.3", "@tryghost/magic-link": "1.0.17", - "@tryghost/members-api": "4.8.15", + "@tryghost/members-api": "5.0.0", "@tryghost/members-importer": "0.5.0", "@tryghost/members-offers": "0.10.6", "@tryghost/members-ssr": "1.0.19", - "@tryghost/members-stripe-service": "0.7.0", + "@tryghost/members-stripe-service": "0.8.0", "@tryghost/metrics": "1.0.5", "@tryghost/minifier": "0.1.10", "@tryghost/mw-error-handler": "0.1.2", diff --git a/yarn.lock b/yarn.lock index 113f455e79..5e8eb0166c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1856,10 +1856,10 @@ "@tryghost/domain-events" "^0.1.6" "@tryghost/member-events" "^0.3.4" -"@tryghost/members-api@4.8.15": - version "4.8.15" - resolved "https://registry.yarnpkg.com/@tryghost/members-api/-/members-api-4.8.15.tgz#75c2ad49ec93a73c50a2f8a6107084ba87df07cb" - integrity sha512-yi2U6tJlAKFF2S1QXxlD2JI/xB7yYjtDswPIhsw9pL/zSVLSEWxv4Jbj8U84Oo6E+I6cR2jenn7qHE2yCXy84g== +"@tryghost/members-api@5.0.0": + version "5.0.0" + resolved "https://registry.yarnpkg.com/@tryghost/members-api/-/members-api-5.0.0.tgz#1726cc61726d6bc9cc55b702044b6e1dc4954481" + integrity sha512-7U2Xd7VcRh3JCjyiE+qOFyx1LPLTdWpzt6oTrUOoyus33WolRKHbncu2lj1C98UYRsqrL3hxiPnuIghsOZXy5A== dependencies: "@nexes/nql" "^0.6.0" "@tryghost/debug" "^0.1.2" @@ -1871,7 +1871,7 @@ "@tryghost/member-events" "^0.3.4" "@tryghost/members-analytics-ingress" "^0.1.9" "@tryghost/members-payments" "^0.1.8" - "@tryghost/members-stripe-service" "^0.7.0" + "@tryghost/members-stripe-service" "^0.8.0" "@tryghost/tpl" "^0.1.2" "@types/jsonwebtoken" "^8.5.1" bluebird "^3.5.4" @@ -1934,10 +1934,10 @@ jsonwebtoken "^8.5.1" lodash "^4.17.11" -"@tryghost/members-stripe-service@0.7.0", "@tryghost/members-stripe-service@^0.7.0": - version "0.7.0" - resolved "https://registry.yarnpkg.com/@tryghost/members-stripe-service/-/members-stripe-service-0.7.0.tgz#3e75d3fa554740ce639a4feda769a5563ae71ccf" - integrity sha512-jBf56kG6vgBwTkA3wC2yIFblDPJ1J9D2iL3FUeyA1EOo6u5SYJixJu9y2xnAVD12rujzzuOx2A4vABTAPrcEfw== +"@tryghost/members-stripe-service@0.8.0", "@tryghost/members-stripe-service@^0.8.0": + version "0.8.0" + resolved "https://registry.yarnpkg.com/@tryghost/members-stripe-service/-/members-stripe-service-0.8.0.tgz#a44e9af5dd577b9becacc2241a5fbd0043fd70fe" + integrity sha512-7GZ+sTRxWomhNKzsvNwg5j3XUE4y062FZck7NKjWq4kZP7Vre7hAH3gMQl3E0qPEuqICcR9C4mM6lWE9zebkLA== dependencies: "@tryghost/debug" "^0.1.4" "@tryghost/errors" "1.2.0"