From cd674fb470ec9c245a4752b703ca082d085d51d0 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Wed, 13 Feb 2019 10:12:15 +0100 Subject: [PATCH] Added config endpoint to Member API (#10467) no-issue * Added getPublicConfig method to stripe payment processor * Added getPublicConfig method to subscriptions service * Added initial config endpoint for members api * Added getConfig method to members gateway --- ghost/members-api/index.js | 11 ++++++++++ ghost/members-api/static/gateway/bundle.js | 8 ++++++++ ghost/members-api/subscriptions/index.js | 10 ++++++++++ .../payment-processors/stripe/index.js | 20 +++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/ghost/members-api/index.js b/ghost/members-api/index.js index 4ae58d595c..be51dde0f7 100644 --- a/ghost/members-api/index.js +++ b/ghost/members-api/index.js @@ -75,6 +75,17 @@ module.exports = function MembersApi({ .catch(handleError(403, res)); }); + apiRouter.get('/config', (req, res) => { + subscriptions.getAdapters() + .then((adapters) => { + return Promise.all(adapters.map((adapter) => { + return subscriptions.getPublicConfig(adapter); + })); + }) + .then(data => res.json(data)) + .catch(handleError(500, res)); + }); + /* security */ function ssoOriginCheck(req, res, next) { if (!req.data.origin || req.data.origin !== ssoOrigin) { diff --git a/ghost/members-api/static/gateway/bundle.js b/ghost/members-api/static/gateway/bundle.js index c50516898f..18249cae0e 100644 --- a/ghost/members-api/static/gateway/bundle.js +++ b/ghost/members-api/static/gateway/bundle.js @@ -150,6 +150,14 @@ }); }); + addMethod('getConfig', function getConfig() { + return fetch(`${membersApi}/config`, { + method: 'GET' + }).then((res) => { + return res.json(); + }); + }); + window.addEventListener('storage', function (event) { if (event.storageArea !== storage) { return; diff --git a/ghost/members-api/subscriptions/index.js b/ghost/members-api/subscriptions/index.js index 472e627f28..d70e0cf019 100644 --- a/ghost/members-api/subscriptions/index.js +++ b/ghost/members-api/subscriptions/index.js @@ -39,6 +39,16 @@ module.exports = class PaymentProcessorService { }); } + getPublicConfig(adapter) { + if (!adapter) { + return Promise.reject(new Error('getPublicConfig(adapter) requires an adapter')); + } + + return this._ready.then(() => { + return this._processors[adapter].getPublicConfig(); + }); + } + createSubscription(member, metadata) { if (!metadata.adapter) { return Promise.reject(new Error('createSubscription(member, { adapter }) requires an adapter')); diff --git a/ghost/members-api/subscriptions/payment-processors/stripe/index.js b/ghost/members-api/subscriptions/payment-processors/stripe/index.js index 7ebd321d92..65bac4cde5 100644 --- a/ghost/members-api/subscriptions/payment-processors/stripe/index.js +++ b/ghost/members-api/subscriptions/payment-processors/stripe/index.js @@ -15,6 +15,7 @@ module.exports = class StripePaymentProcessor { this._stripe = stripe; this._product = product; this._plans = plans; + this._public_token = config.public_token; return { product, plans @@ -35,6 +36,25 @@ module.exports = class StripePaymentProcessor { }); } + getPublicConfig() { + if (!this._plans) { + throw new Error('StripePaymentProcessor must be configured()'); + } + + return this._ready.then(() => { + return { + adapter: 'stripe', + config: { + public_token: this._public_token, + plans: this._plans.map(({id, currency, amount, interval, nickname}) => ({ + id, currency, amount, interval, + name: nickname + })) + } + }; + }); + } + createSubscription(member, metadata) { if (!this._stripe) { throw new Error('StripePaymentProcessor must be configured()');