mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
[WIP] Amazon refactor to lib (#8403)
* Moved amazon tests to folder * Abstracted amazon payment code and added initial test * Abstracted cancel and subscribe logic to amazon payment lib * Added arg checks to checkout * Added constants. Added more subscription test * Added with arg checks to cancel * Fixed linting issues * Added integration tests for amazon subscribe cancel * Added integration test for amazon checkout * Added integration test for amazon subscribe * Added coupon unit test * Fixed lint * Fixed minor test issue and changed header expectations * Fixed line endings
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import {
|
||||
BadRequest,
|
||||
NotAuthorized,
|
||||
NotFound,
|
||||
} from '../../../libs/errors';
|
||||
import amzLib from '../../../libs/amazonPayments';
|
||||
import {
|
||||
@@ -9,15 +7,6 @@ import {
|
||||
authWithUrl,
|
||||
} from '../../../middlewares/auth';
|
||||
import shared from '../../../../common';
|
||||
import payments from '../../../libs/payments';
|
||||
import moment from 'moment';
|
||||
import { model as Coupon } from '../../../models/coupon';
|
||||
import { model as User } from '../../../models/user';
|
||||
import {
|
||||
model as Group,
|
||||
basicFields as basicGroupFields,
|
||||
} from '../../../models/group';
|
||||
import cc from 'coupon-code';
|
||||
|
||||
let api = {};
|
||||
|
||||
@@ -89,67 +78,10 @@ api.checkout = {
|
||||
let gift = req.body.gift;
|
||||
let user = res.locals.user;
|
||||
let orderReferenceId = req.body.orderReferenceId;
|
||||
let amount = 5;
|
||||
|
||||
// @TODO: Make thise use payment.subscribeWithAmazon
|
||||
|
||||
if (!orderReferenceId) throw new BadRequest('Missing req.body.orderReferenceId');
|
||||
|
||||
if (gift) {
|
||||
if (gift.type === 'gems') {
|
||||
amount = gift.gems.amount / 4;
|
||||
} else if (gift.type === 'subscription') {
|
||||
amount = shared.content.subscriptionBlocks[gift.subscription.key].price;
|
||||
}
|
||||
}
|
||||
|
||||
await amzLib.setOrderReferenceDetails({
|
||||
AmazonOrderReferenceId: orderReferenceId,
|
||||
OrderReferenceAttributes: {
|
||||
OrderTotal: {
|
||||
CurrencyCode: 'USD',
|
||||
Amount: amount,
|
||||
},
|
||||
SellerNote: 'Habitica Payment',
|
||||
SellerOrderAttributes: {
|
||||
SellerOrderId: shared.uuid(),
|
||||
StoreName: 'Habitica',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await amzLib.confirmOrderReference({ AmazonOrderReferenceId: orderReferenceId });
|
||||
|
||||
await amzLib.authorize({
|
||||
AmazonOrderReferenceId: orderReferenceId,
|
||||
AuthorizationReferenceId: shared.uuid().substring(0, 32),
|
||||
AuthorizationAmount: {
|
||||
CurrencyCode: 'USD',
|
||||
Amount: amount,
|
||||
},
|
||||
SellerAuthorizationNote: 'Habitica Payment',
|
||||
TransactionTimeout: 0,
|
||||
CaptureNow: true,
|
||||
});
|
||||
|
||||
await amzLib.closeOrderReference({ AmazonOrderReferenceId: orderReferenceId });
|
||||
|
||||
// execute payment
|
||||
let method = 'buyGems';
|
||||
let data = {
|
||||
user,
|
||||
paymentMethod: 'Amazon Payments',
|
||||
headers: req.headers,
|
||||
};
|
||||
|
||||
if (gift) {
|
||||
if (gift.type === 'subscription') method = 'createSubscription';
|
||||
gift.member = await User.findById(gift ? gift.uuid : undefined).exec();
|
||||
data.gift = gift;
|
||||
data.paymentMethod = 'Amazon Payments (Gift)';
|
||||
}
|
||||
|
||||
await payments[method](data);
|
||||
await amzLib.checkout({gift, user, orderReferenceId, headers: req.headers});
|
||||
|
||||
res.respond(200);
|
||||
},
|
||||
@@ -174,55 +106,13 @@ api.subscribe = {
|
||||
let user = res.locals.user;
|
||||
let groupId = req.body.groupId;
|
||||
|
||||
if (!sub) throw new BadRequest(res.t('missingSubscriptionCode'));
|
||||
if (!billingAgreementId) throw new BadRequest('Missing req.body.billingAgreementId');
|
||||
|
||||
if (sub.discount) { // apply discount
|
||||
if (!coupon) throw new BadRequest(res.t('couponCodeRequired'));
|
||||
let result = await Coupon.findOne({_id: cc.validate(coupon), event: sub.key}).exec();
|
||||
if (!result) throw new NotAuthorized(res.t('invalidCoupon'));
|
||||
}
|
||||
|
||||
await amzLib.setBillingAgreementDetails({
|
||||
AmazonBillingAgreementId: billingAgreementId,
|
||||
BillingAgreementAttributes: {
|
||||
SellerNote: 'Habitica Subscription',
|
||||
SellerBillingAgreementAttributes: {
|
||||
SellerBillingAgreementId: shared.uuid(),
|
||||
StoreName: 'Habitica',
|
||||
CustomInformation: 'Habitica Subscription',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await amzLib.confirmBillingAgreement({
|
||||
AmazonBillingAgreementId: billingAgreementId,
|
||||
});
|
||||
|
||||
await amzLib.authorizeOnBillingAgreement({
|
||||
AmazonBillingAgreementId: billingAgreementId,
|
||||
AuthorizationReferenceId: shared.uuid().substring(0, 32),
|
||||
AuthorizationAmount: {
|
||||
CurrencyCode: 'USD',
|
||||
Amount: sub.price,
|
||||
},
|
||||
SellerAuthorizationNote: 'Habitica Subscription Payment',
|
||||
TransactionTimeout: 0,
|
||||
CaptureNow: true,
|
||||
SellerNote: 'Habitica Subscription Payment',
|
||||
SellerOrderAttributes: {
|
||||
SellerOrderId: shared.uuid(),
|
||||
StoreName: 'Habitica',
|
||||
},
|
||||
});
|
||||
|
||||
await payments.createSubscription({
|
||||
user,
|
||||
customerId: billingAgreementId,
|
||||
paymentMethod: 'Amazon Payments',
|
||||
await amzLib.subscribe({
|
||||
billingAgreementId,
|
||||
sub,
|
||||
headers: req.headers,
|
||||
coupon,
|
||||
user,
|
||||
groupId,
|
||||
headers: req.headers,
|
||||
});
|
||||
|
||||
res.respond(200);
|
||||
@@ -243,53 +133,7 @@ api.subscribeCancel = {
|
||||
let user = res.locals.user;
|
||||
let groupId = req.query.groupId;
|
||||
|
||||
let billingAgreementId;
|
||||
let planId;
|
||||
let lastBillingDate;
|
||||
|
||||
if (groupId) {
|
||||
let groupFields = basicGroupFields.concat(' purchased');
|
||||
let group = await Group.getGroup({user, groupId, populateLeader: false, groupFields});
|
||||
|
||||
if (!group) {
|
||||
throw new NotFound(res.t('groupNotFound'));
|
||||
}
|
||||
|
||||
if (!group.leader === user._id) {
|
||||
throw new NotAuthorized(res.t('onlyGroupLeaderCanManageSubscription'));
|
||||
}
|
||||
|
||||
billingAgreementId = group.purchased.plan.customerId;
|
||||
planId = group.purchased.plan.planId;
|
||||
lastBillingDate = group.purchased.plan.lastBillingDate;
|
||||
} else {
|
||||
billingAgreementId = user.purchased.plan.customerId;
|
||||
planId = user.purchased.plan.planId;
|
||||
lastBillingDate = user.purchased.plan.lastBillingDate;
|
||||
}
|
||||
|
||||
if (!billingAgreementId) throw new NotAuthorized(res.t('missingSubscription'));
|
||||
|
||||
let details = await amzLib.getBillingAgreementDetails({
|
||||
AmazonBillingAgreementId: billingAgreementId,
|
||||
});
|
||||
|
||||
if (details.BillingAgreementDetails.BillingAgreementStatus.State !== 'Closed') {
|
||||
await amzLib.closeBillingAgreement({
|
||||
AmazonBillingAgreementId: billingAgreementId,
|
||||
});
|
||||
}
|
||||
|
||||
let subscriptionBlock = shared.content.subscriptionBlocks[planId];
|
||||
let subscriptionLength = subscriptionBlock.months * 30;
|
||||
|
||||
await payments.cancelSubscription({
|
||||
user,
|
||||
groupId,
|
||||
nextBill: moment(lastBillingDate).add({ days: subscriptionLength }),
|
||||
paymentMethod: 'Amazon Payments',
|
||||
headers: req.headers,
|
||||
});
|
||||
await amzLib.cancelSubscription({user, groupId, headers: req.headers});
|
||||
|
||||
if (req.query.noRedirect) {
|
||||
res.respond(200);
|
||||
|
||||
Reference in New Issue
Block a user