mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-30 21:40:39 +03:00
88b9f25541
These changes introduce a new "service" to the members api, which handles getting and creating subscriptions. This is wired up to get subscription information when creating tokens, and attaching information to the token, so that the Content API can allow/deny access. Behind the subscription service we have a Stripe "payment processor", this holds the logic for creating subscriptions etc... in Stripe. The logic for getting items out of stripe uses a hash of the relevant data as the id to search for, this allows us to forgo keeping stripe data in a db, so that this feature can get out quicker.
60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
const stripe = require('./payment-processors/stripe');
|
|
|
|
const adapters = {
|
|
stripe
|
|
};
|
|
|
|
module.exports = class PaymentProcessorService {
|
|
constructor(config) {
|
|
this._ready = new Promise(() => {});
|
|
process.nextTick(() => this.configure(config));
|
|
}
|
|
|
|
configure({processors}) {
|
|
this._processors = {};
|
|
this._ready = Promise.all(processors.map(({
|
|
adapter,
|
|
config
|
|
}) => {
|
|
this._processors[adapter] = new adapters[adapter];
|
|
return this._processors[adapter].configure(config);
|
|
})).then(() => {
|
|
return Object.keys(this._processors);
|
|
});
|
|
|
|
return this._ready;
|
|
}
|
|
|
|
getAdapters() {
|
|
return this._ready;
|
|
}
|
|
|
|
getConfig(adapter) {
|
|
if (!adapter) {
|
|
return Promise.reject(new Error('getConfig(adapter) requires an adapter'));
|
|
}
|
|
|
|
return this._ready.then(() => {
|
|
return this._processors[adapter].getConfig();
|
|
});
|
|
}
|
|
|
|
createSubscription(member, metadata) {
|
|
if (!metadata.adapter) {
|
|
return Promise.reject(new Error('createSubscription(member, { adapter }) requires an adapter'));
|
|
}
|
|
return this._ready.then(() => {
|
|
return this._processors[metadata.adapter].createSubscription(member, metadata);
|
|
});
|
|
}
|
|
|
|
getSubscription(member, metadata) {
|
|
if (!metadata.adapter) {
|
|
return Promise.reject(new Error('getSubscription(member, { adapter }) requires an adapter'));
|
|
}
|
|
return this._ready.then(() => {
|
|
return this._processors[metadata.adapter].getSubscription(member, metadata);
|
|
});
|
|
}
|
|
};
|