forest-express
Version:
Official package for all Forest Express Lianas
32 lines (31 loc) • 1.32 kB
JavaScript
;
var P = require('bluebird');
function SourceGetter(Implementation, params, opts, integrationInfo) {
var stripe = opts.integrations.stripe.stripe(opts.integrations.stripe.apiKey);
var collectionModel = null;
function getSource(customerId, objectId) {
return new P(function (resolve, reject) {
stripe.customers.retrieveSource(customerId, objectId, function (error, source) {
if (error) {
return reject(error);
}
return resolve(source);
});
});
}
this.perform = function () {
collectionModel = integrationInfo.collection;
var collectionFieldName = integrationInfo.field,
embeddedPath = integrationInfo.embeddedPath;
var fieldName = embeddedPath ? "".concat(collectionFieldName, ".").concat(embeddedPath) : collectionFieldName;
return Implementation.Stripe.getCustomer(collectionModel, collectionFieldName, params.recordId).then(function (customer) {
return getSource(customer[collectionFieldName], params.objectId).then(function (source) {
return Implementation.Stripe.getCustomerByUserField(collectionModel, fieldName, source.customer).then(function (customerFound) {
source.customer = customerFound;
return source;
});
});
});
};
}
module.exports = SourceGetter;