diff --git a/test/api/v3/unit/libs/amazonPayments.test.js b/test/api/v3/unit/libs/amazonPayments.test.js index e4e593e7d7..a22abb90a1 100644 --- a/test/api/v3/unit/libs/amazonPayments.test.js +++ b/test/api/v3/unit/libs/amazonPayments.test.js @@ -383,6 +383,72 @@ describe('Amazon Payments', () => { groupId, }); }); + + it('subscribes with amazon with price to existing users', async () => { + user = new User(); + user.guilds.push(groupId); + await user.save(); + group.memberCount = 2; + await group.save(); + sub.key = 'group_monthly'; + sub.price = 9; + amount = 12; + + await amzLib.subscribe({ + billingAgreementId, + sub, + coupon, + user, + groupId, + headers, + }); + + expect(amazonSetBillingAgreementDetailsSpy).to.be.calledOnce; + expect(amazonSetBillingAgreementDetailsSpy).to.be.calledWith({ + AmazonBillingAgreementId: billingAgreementId, + BillingAgreementAttributes: { + SellerNote: amzLib.constants.SELLER_NOTE_SUBSCRIPTION, + SellerBillingAgreementAttributes: { + SellerBillingAgreementId: common.uuid(), + StoreName: amzLib.constants.STORE_NAME, + CustomInformation: amzLib.constants.SELLER_NOTE_SUBSCRIPTION, + }, + }, + }); + + expect(amazonConfirmBillingAgreementSpy).to.be.calledOnce; + expect(amazonConfirmBillingAgreementSpy).to.be.calledWith({ + AmazonBillingAgreementId: billingAgreementId, + }); + + expect(amazongAuthorizeOnBillingAgreementSpy).to.be.calledOnce; + expect(amazongAuthorizeOnBillingAgreementSpy).to.be.calledWith({ + AmazonBillingAgreementId: billingAgreementId, + AuthorizationReferenceId: common.uuid().substring(0, 32), + AuthorizationAmount: { + CurrencyCode: amzLib.constants.CURRENCY_CODE, + Amount: amount, + }, + SellerAuthorizationNote: amzLib.constants.SELLER_NOTE_ATHORIZATION_SUBSCRIPTION, + TransactionTimeout: 0, + CaptureNow: true, + SellerNote: amzLib.constants.SELLER_NOTE_ATHORIZATION_SUBSCRIPTION, + SellerOrderAttributes: { + SellerOrderId: common.uuid(), + StoreName: amzLib.constants.STORE_NAME, + }, + }); + + expect(createSubSpy).to.be.calledOnce; + expect(createSubSpy).to.be.calledWith({ + user, + customerId: billingAgreementId, + paymentMethod: amzLib.constants.PAYMENT_METHOD, + sub, + headers, + groupId, + }); + }); }); describe('cancelSubscription', () => { diff --git a/test/api/v3/unit/libs/stripePayments.test.js b/test/api/v3/unit/libs/stripePayments.test.js index 26ce880715..6d1cc31a86 100644 --- a/test/api/v3/unit/libs/stripePayments.test.js +++ b/test/api/v3/unit/libs/stripePayments.test.js @@ -395,6 +395,50 @@ describe('Stripe Payments', () => { subscriptionId, }); }); + + it('subscribes a group with the correct number of group members', async () => { + token = 'test-token'; + sub = data.sub; + groupId = group._id; + email = 'test@test.com'; + headers = {}; + user = new User(); + user.guilds.push(groupId); + await user.save(); + group.memberCount = 2; + await group.save(); + + await stripePayments.checkout({ + token, + user, + gift, + sub, + groupId, + email, + headers, + coupon, + }, stripe); + + expect(stripeCreateCustomerSpy).to.be.calledOnce; + expect(stripeCreateCustomerSpy).to.be.calledWith({ + email, + metadata: { uuid: user._id }, + card: token, + plan: sub.key, + quantity: 4, + }); + + expect(stripePaymentsCreateSubSpy).to.be.calledOnce; + expect(stripePaymentsCreateSubSpy).to.be.calledWith({ + user, + customerId: customerIdResponse, + paymentMethod: 'Stripe', + sub, + headers, + groupId, + subscriptionId, + }); + }); }); describe('edit subscription', () => { diff --git a/website/server/libs/amazonPayments.js b/website/server/libs/amazonPayments.js index 272b8e8445..8c5c88eca7 100644 --- a/website/server/libs/amazonPayments.js +++ b/website/server/libs/amazonPayments.js @@ -252,6 +252,17 @@ api.subscribe = async function subscribe (options) { if (!result) throw new NotAuthorized(i18n.t('invalidCoupon')); } + let amount = sub.price; + let leaderCount = 1; + let priceOfSingleMember = 3; + + if (groupId) { + let groupFields = basicGroupFields.concat(' purchased'); + let group = await Group.getGroup({user, groupId, populateLeader: false, groupFields}); + + amount = sub.price + (group.memberCount - leaderCount) * priceOfSingleMember; + } + await this.setBillingAgreementDetails({ AmazonBillingAgreementId: billingAgreementId, BillingAgreementAttributes: { @@ -273,7 +284,7 @@ api.subscribe = async function subscribe (options) { AuthorizationReferenceId: common.uuid().substring(0, 32), AuthorizationAmount: { CurrencyCode: this.constants.CURRENCY_CODE, - Amount: sub.price, + Amount: amount, }, SellerAuthorizationNote: this.constants.SELLER_NOTE_ATHORIZATION_SUBSCRIPTION, TransactionTimeout: 0, diff --git a/website/server/libs/stripePayments.js b/website/server/libs/stripePayments.js index 3da5d71703..4f8b96aa47 100644 --- a/website/server/libs/stripePayments.js +++ b/website/server/libs/stripePayments.js @@ -92,6 +92,9 @@ api.checkout = async function checkout (options, stripeInc) { if (groupId) { customerObject.quantity = sub.quantity; + let groupFields = basicGroupFields.concat(' purchased'); + let group = await Group.getGroup({user, groupId, populateLeader: false, groupFields}); + customerObject.quantity = group.memberCount + sub.quantity - 1; } response = await stripeApi.customers.create(customerObject);