2019-01-22 17:29:44 +03:00
|
|
|
module.exports = function ({
|
2019-09-06 08:16:35 +03:00
|
|
|
stripe,
|
2019-01-22 17:29:44 +03:00
|
|
|
createMember,
|
|
|
|
getMember,
|
2019-01-30 14:36:09 +03:00
|
|
|
listMembers,
|
2019-09-03 07:32:00 +03:00
|
|
|
deleteMember
|
2019-01-22 17:29:44 +03:00
|
|
|
}) {
|
2019-09-06 08:16:35 +03:00
|
|
|
async function get(data, options) {
|
|
|
|
const member = await getMember(data, options);
|
|
|
|
if (!member) {
|
|
|
|
return member;
|
|
|
|
}
|
|
|
|
if (!stripe) {
|
|
|
|
return Object.assign(member, {
|
|
|
|
plans: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const subscription = await stripe.getSubscription(member);
|
|
|
|
if (subscription.status !== 'active') {
|
|
|
|
return Object.assign(member, {
|
|
|
|
plans: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.assign(member, {
|
|
|
|
plans: [subscription.plan]
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 08:22:53 +03:00
|
|
|
async function destroy(data, options) {
|
|
|
|
const member = await getMember(data, options);
|
|
|
|
if (!member) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (stripe) {
|
|
|
|
await stripe.removeCustomer(member);
|
|
|
|
}
|
|
|
|
return deleteMember(data, options);
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:29:44 +03:00
|
|
|
return {
|
|
|
|
create: createMember,
|
2019-02-07 12:41:39 +03:00
|
|
|
list: listMembers,
|
2019-09-06 08:16:35 +03:00
|
|
|
get,
|
2019-09-06 08:22:53 +03:00
|
|
|
destroy
|
2019-01-22 17:29:44 +03:00
|
|
|
};
|
|
|
|
};
|