0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-04 02:01:58 -05:00
ghost/core/server/services/members/api.js
Fabien O'Carroll f63577fa4f
Implemented stripe checkout handling for members
no-issue

* Installed members-api@0.5.0 members-ssr@0.3.1
* Supported multiple members-forms
* Used members canary api
* Added GET handler to /members/ssr for id token
The identity token will be used to ensure that a payment is linked to the correct member
* Added stripe.js to ghost_head when members enabled
* Added basic support for linking to stripe checkout
* Removed listener to title and icon settings changes
* Added stripe subscription config
2019-09-06 15:14:21 +08:00

121 lines
3.4 KiB
JavaScript

const {URL} = require('url');
const settingsCache = require('../settings/cache');
const urlUtils = require('../../lib/url-utils');
const MembersApi = require('@tryghost/members-api');
const common = require('../../lib/common');
const mail = require('../mail');
const models = require('../../models');
function createMember({email}) {
return models.Member.add({
email
}).then((member) => {
return member.toJSON();
});
}
function getMember(data, options = {}) {
if (!data.email && !data.id) {
return Promise.resolve(null);
}
return models.Member.findOne(data, options).then((model) => {
if (!model) {
return null;
}
return model.toJSON(options);
});
}
function deleteMember(options) {
options = options || {};
return models.Member.destroy(options).catch(models.Member.NotFoundError, () => {
throw new common.errors.NotFoundError({
message: common.i18n.t('errors.api.resource.resourceNotFound', {
resource: 'Member'
})
});
});
}
function listMembers(options) {
return models.Member.findPage(options).then((models) => {
return {
members: models.data.map(model => model.toJSON(options)),
meta: models.meta
};
});
}
const getApiUrl = ({version, type}) => {
const {href} = new URL(
urlUtils.getApiPath({version, type}),
urlUtils.urlFor('admin', true)
);
return href;
};
const siteUrl = urlUtils.getSiteUrl();
const membersApiUrl = getApiUrl({version: 'v2', type: 'members'});
const ghostMailer = new mail.GhostMailer();
function getStripePaymentConfig() {
const subscriptionSettings = settingsCache.get('members_subscription_settings');
if (!subscriptionSettings || subscriptionSettings.isPaid === false) {
return null;
}
const stripePaymentProcessor = subscriptionSettings.paymentProcessors.find(
paymentProcessor => paymentProcessor.adapter === 'stripe'
);
if (!stripePaymentProcessor || !stripePaymentProcessor.config) {
return null;
}
return {
publicKey: stripePaymentProcessor.config.public_token,
secretKey: stripePaymentProcessor.config.secret_token,
checkoutSuccessUrl: siteUrl,
checkoutCancelUrl: siteUrl,
product: stripePaymentProcessor.config.product,
plans: stripePaymentProcessor.config.plans
};
}
module.exports = createApiInstance;
function createApiInstance() {
const membersApiInstance = MembersApi({
tokenConfig: {
issuer: membersApiUrl,
publicKey: settingsCache.get('members_public_key'),
privateKey: settingsCache.get('members_private_key')
},
auth: {
getSigninURL(token) {
const signinURL = new URL(siteUrl);
signinURL.searchParams.set('token', token);
return signinURL.href;
}
},
mail: {
transporter: {
sendMail(message) {
return ghostMailer.send(Object.assign({subject: 'Signin'}, message));
}
}
},
paymentConfig: {
stripe: getStripePaymentConfig()
},
createMember,
getMember,
deleteMember,
listMembers
});
membersApiInstance.setLogger(common.logging);
return membersApiInstance;
}