Skip to content

Commit

Permalink
Merge pull request #21 from enhancv/descriptor
Browse files Browse the repository at this point in the history
Add option for adding descriptor to subscription
  • Loading branch information
ginovski authored Mar 12, 2019
2 parents 09b988d + 8a7e149 commit 01cf7c9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-subscriptions",
"version": "2.8.1",
"version": "2.8.2",
"description": "Processor agnostic payment subscription backend",
"main": "src/index.js",
"repository": "git@github.com:enhancv/mongoose-subscriptions.git",
Expand Down
17 changes: 14 additions & 3 deletions src/Schema/Customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,12 @@ Customer.method("addPaymentMethodNonce", function addPaymentMethodNonce(nonce, a
return paymentMethod;
});

Customer.method("addSubscription", function addSubscription(plan, paymentMethod, activeDate) {
Customer.method("addSubscription", function addSubscription(
plan,
paymentMethod,
descriptor,
activeDate
) {
const date = activeDate || new Date();
const nonTrialSubs = this.validSubscriptions(date).filter(
item => !(item.isTrial && item.processor.state === ProcessorItem.LOCAL)
Expand All @@ -260,7 +265,7 @@ Customer.method("addSubscription", function addSubscription(plan, paymentMethod,
.filter(item => item.plan.level < plan.level)
.filter(item => item.processor.state !== ProcessorItem.LOCAL);

let subscription = this.subscriptions.create({
const newSubscription = {
plan,
firstBillingDate: waitForSubs.length
? waitForSubs[0].isTrial
Expand All @@ -269,7 +274,13 @@ Customer.method("addSubscription", function addSubscription(plan, paymentMethod,
: null,
price: plan.price,
createdAt: date,
});
};

if (descriptor) {
newSubscription.descriptor = descriptor;
}

let subscription = this.subscriptions.create(newSubscription);

if (!waitForSubs.length) {
subscription = subscription.addDiscounts(newSub => [
Expand Down
1 change: 1 addition & 0 deletions test/Schema/CustomerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,7 @@ describe(
const result = customer.addSubscription(
newPlan,
customer.defaultPaymentMethod(),
null,
nowDate
);
const resultDiscount = result.discounts[0] ? result.discounts[0].amount : false;
Expand Down
2 changes: 1 addition & 1 deletion test/integrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ describe(

user.resetProcessor();
user.cancelSubscriptions();
user.addSubscription(plan, user.defaultPaymentMethod(), now);
user.addSubscription(plan, user.defaultPaymentMethod(), null, now);

return user.saveProcessor(processor);
})
Expand Down

0 comments on commit 01cf7c9

Please sign in to comment.