2019-02-07 12:41:39 +03:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:12:15 +03:00
|
|
|
getPublicConfig(adapter) {
|
|
|
|
if (!adapter) {
|
|
|
|
return Promise.reject(new Error('getPublicConfig(adapter) requires an adapter'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._ready.then(() => {
|
|
|
|
return this._processors[adapter].getPublicConfig();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-07 12:41:39 +03:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|