mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Ensured upgraded groups are charge correctly (#8567)
* Ensured upgraded groups are charge correctly * Added names to magic numbers * Limited group fields query
This commit is contained in:
committed by
Sabe Jones
parent
cfa433aa75
commit
3dc20a9832
@@ -383,6 +383,72 @@ describe('Amazon Payments', () => {
|
|||||||
groupId,
|
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', () => {
|
describe('cancelSubscription', () => {
|
||||||
|
|||||||
@@ -395,6 +395,50 @@ describe('Stripe Payments', () => {
|
|||||||
subscriptionId,
|
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', () => {
|
describe('edit subscription', () => {
|
||||||
|
|||||||
@@ -252,6 +252,17 @@ api.subscribe = async function subscribe (options) {
|
|||||||
if (!result) throw new NotAuthorized(i18n.t('invalidCoupon'));
|
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({
|
await this.setBillingAgreementDetails({
|
||||||
AmazonBillingAgreementId: billingAgreementId,
|
AmazonBillingAgreementId: billingAgreementId,
|
||||||
BillingAgreementAttributes: {
|
BillingAgreementAttributes: {
|
||||||
@@ -273,7 +284,7 @@ api.subscribe = async function subscribe (options) {
|
|||||||
AuthorizationReferenceId: common.uuid().substring(0, 32),
|
AuthorizationReferenceId: common.uuid().substring(0, 32),
|
||||||
AuthorizationAmount: {
|
AuthorizationAmount: {
|
||||||
CurrencyCode: this.constants.CURRENCY_CODE,
|
CurrencyCode: this.constants.CURRENCY_CODE,
|
||||||
Amount: sub.price,
|
Amount: amount,
|
||||||
},
|
},
|
||||||
SellerAuthorizationNote: this.constants.SELLER_NOTE_ATHORIZATION_SUBSCRIPTION,
|
SellerAuthorizationNote: this.constants.SELLER_NOTE_ATHORIZATION_SUBSCRIPTION,
|
||||||
TransactionTimeout: 0,
|
TransactionTimeout: 0,
|
||||||
|
|||||||
@@ -92,6 +92,9 @@ api.checkout = async function checkout (options, stripeInc) {
|
|||||||
|
|
||||||
if (groupId) {
|
if (groupId) {
|
||||||
customerObject.quantity = sub.quantity;
|
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);
|
response = await stripeApi.customers.create(customerObject);
|
||||||
|
|||||||
Reference in New Issue
Block a user