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
This commit is contained in:
Fabien O'Carroll 2019-02-13 10:12:15 +01:00 committed by GitHub
parent 36547a9c3a
commit 5472aa61ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 0 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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'));

View File

@ -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()');