Files
habitica/website/server/controllers/top-level/payments/amazon.js
Keith Holliday ea24eeb019 Thehollidayinn/group plans part 2 (#8262)
* Added all ui components back

* Added group ui items back and initial group approval directive

* Added approval list view with approving functionality

* Added notification display for group approvals

* Fixed linting issues

* Removed expectation from beforeEach

* Moved string to locale

* Added per use group plan for stripe

* Added tests for stripe group plan upgrade

* Removed paypal option

* Abstract sub blocks. Hit group sub block from user settings page. Added group subscriptin beneifts display

* Fixed lint issue

* Added pricing and adjusted styles

* Moved text to translations

* Added group email types

* Fixed typo

* Fixed group plan abstraction and other style issues

* Fixed email unit test

* Added type to group plan to filter our group plans

* Removed dev protection from routes

* Removed hard coding and fixed upgrade plan

* Added error when group has subscription and tries to remove

* Fixed payment unit tests

* Added custom string and moved subscription check up in the logic

* Added ability for old leader to delete subscription the created

* Allowed old guild leader to edit their group subscription

* Fixed linting and tests

* Added group sub page to user sub settings

* Added approval and group tasks requests back. Hid user group sub on profile

* Added group tasks sync after adding to allow for editing

* Fixed promise chain when resolving group

* Added approvals to group promise chain

* Ensured compelted group todos are not delted at cron

* Updated copy and other minor styles

* Added group field to tags and recolored group tag.

* Added chat message when task is claimed

* Preventing task scoring when approval is needed

* Added approval requested indicator

* Updated column with for tasks on group page

* Added checklist sync on assign

* Added sync for checklist items

* Added checkilist sync when task is updated

* Added checklist sync remove

* Sanatized group tasks when updated

* Fixed lint issues

* Added instant scoring of approved task

* Added task modal

* Fixed editing of challenge and group tasks

* Added cancel button

* Added add new checklist option to update sync

* Added remove for checklist

* Added checklist update

* Added difference check and sync for checklist if there is a diff

* Fixed task syncing

* Fixed linting issues

* Fixed styles and karma tests

* Fixed minor style issues

* Fixed obj transfer on scope

* Fixed broken tests

* Added new benefits page

* Updated group page styles

* Updated benefits page style

* Added translations

* Prevented sync with empty trask list

* Added task title to edit modal

* Added new group plans page and upgrade redirect

* Added group plans redirect to upgrade

* Fixed party home page being hidden and home button click

* Fixed dynamic changing of task status and grey popup

* Fixed tag editing

* Hid benifites information if group has subscription

* Added quotes to task name

* Fixed issue with assigning multiple users

* Added new group plans ctrl

* Hid menu from public guilds

* Fixed task sync issue

* Updated placeholder for assign field

* Added correct cost to subscribe details

* Hid create, edit, delete task options from non group leaders

* Prevented some front end modifications to group tasks

* Hid tags option from group original task

* Added refresh for approvals and group tasks

* Prepend new group tasks

* Fix last checklist item sync

* Fixed casing issue with tags

* Added claimed by message on hover

* Prevent user from deleting assigned task

* Added single route for group plan sign up and payments

* Abstracted stripe payments and added initial tests

* Abstracted amazon and added initial tests

* Fixed create group message

* Update group id check and return group

* Updated to use the new returned group

* Fixed linting and promise issues

* Fixed broken leave test after merge issue

* Fixed undefined approval error and editing/deleting challenge tasks

* Add pricing to group plans, removed confirmation, and fixed redirect after payment

* Updated group plan cost text
2016-12-21 13:45:45 -06:00

308 lines
8.7 KiB
JavaScript

import {
BadRequest,
NotAuthorized,
NotFound,
} from '../../../libs/errors';
import amzLib from '../../../libs/amazonPayments';
import {
authWithHeaders,
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 = {};
/**
* @apiIgnore Payments are considered part of the private API
* @api {post} /amazon/verifyAccessToken Amazon Payments: verify access token
* @apiName AmazonVerifyAccessToken
* @apiGroup Payments
*
* @apiSuccess {Object} data Empty object
**/
api.verifyAccessToken = {
method: 'POST',
url: '/amazon/verifyAccessToken',
middlewares: [authWithHeaders()],
async handler (req, res) {
let accessToken = req.body.access_token;
if (!accessToken) throw new BadRequest('Missing req.body.access_token');
await amzLib.getTokenInfo(accessToken);
res.respond(200, {});
},
};
/**
* @apiIgnore Payments are considered part of the private API
* @api {post} /amazon/createOrderReferenceId Amazon Payments: create order reference id
* @apiName AmazonCreateOrderReferenceId
* @apiGroup Payments
*
* @apiSuccess {String} data.orderReferenceId The order reference id.
**/
api.createOrderReferenceId = {
method: 'POST',
url: '/amazon/createOrderReferenceId',
middlewares: [authWithHeaders()],
async handler (req, res) {
let billingAgreementId = req.body.billingAgreementId;
if (!billingAgreementId) throw new BadRequest('Missing req.body.billingAgreementId');
let response = await amzLib.createOrderReferenceId({
Id: billingAgreementId,
IdType: 'BillingAgreement',
ConfirmNow: false,
});
res.respond(200, {
orderReferenceId: response.OrderReferenceDetails.AmazonOrderReferenceId,
});
},
};
/**
* @apiIgnore Payments are considered part of the private API
* @api {post} /amazon/checkout Amazon Payments: checkout
* @apiName AmazonCheckout
* @apiGroup Payments
*
* @apiSuccess {Object} data Empty object
**/
api.checkout = {
method: 'POST',
url: '/amazon/checkout',
middlewares: [authWithHeaders()],
async handler (req, res) {
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);
data.gift = gift;
data.paymentMethod = 'Amazon Payments (Gift)';
}
await payments[method](data);
if (gift && gift.type === 'subscription' && gift.member._id !== user._id) {
gift.member = user;
await payments.createSubscription(data);
}
res.respond(200);
},
};
/**
* @apiIgnore Payments are considered part of the private API
* @api {post} /amazon/subscribe Amazon Payments: subscribe
* @apiName AmazonSubscribe
* @apiGroup Payments
*
* @apiSuccess {Object} data Empty object
**/
api.subscribe = {
method: 'POST',
url: '/amazon/subscribe',
middlewares: [authWithHeaders()],
async handler (req, res) {
let billingAgreementId = req.body.billingAgreementId;
let sub = req.body.subscription ? shared.content.subscriptionBlocks[req.body.subscription] : false;
let coupon = req.body.coupon;
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});
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',
sub,
headers: req.headers,
groupId,
});
res.respond(200);
},
};
/**
* @apiIgnore Payments are considered part of the private API
* @api {get} /amazon/subscribe/cancel Amazon Payments: subscribe cancel
* @apiName AmazonSubscribe
* @apiGroup Payments
**/
api.subscribeCancel = {
method: 'GET',
url: '/amazon/subscribe/cancel',
middlewares: [authWithUrl],
async handler (req, res) {
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,
});
if (req.query.noRedirect) {
res.respond(200);
} else {
res.redirect('/');
}
},
};
module.exports = api;