mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-03 08:21:07 +00:00
feat(amazon-payments): finalize one time purchases
This commit is contained in:
parent
78cca423f0
commit
271245f1e6
4 changed files with 54 additions and 17 deletions
|
|
@ -78,6 +78,7 @@ function($rootScope, User, $http, Content) {
|
|||
Payments.amazonPayments.modal = null;
|
||||
Payments.amazonPayments.type = null;
|
||||
Payments.amazonPayments.loggedIn = false;
|
||||
Payments.amazonPayments.gift = null;
|
||||
Payments.amazonPayments.orderReferenceId = null;
|
||||
Payments.amazonPayments.billingAgreementId = null;
|
||||
Payments.amazonPayments.paymentSelected = false;
|
||||
|
|
@ -85,9 +86,16 @@ function($rootScope, User, $http, Content) {
|
|||
};
|
||||
|
||||
// Needs to be called everytime the modal/router is accessed
|
||||
Payments.amazonPayments.init = function(type){
|
||||
Payments.amazonPayments.init = function(type, gift, giftedTo){
|
||||
if(gift){
|
||||
if(gift.gems && gift.gems.amount && gift.gems.amount <= 0) return;
|
||||
gift.uuid = giftedTo;
|
||||
}
|
||||
|
||||
if(!isAmazonReady) return;
|
||||
if(type !== 'donation' && type !== 'subscription') return;
|
||||
|
||||
Payments.amazonPayments.gift = gift;
|
||||
Payments.amazonPayments.type = type;
|
||||
|
||||
var modal = Payments.amazonPayments.modal = $rootScope.openModal('amazonPayments', {
|
||||
|
|
@ -195,9 +203,12 @@ function($rootScope, User, $http, Content) {
|
|||
Payments.amazonPayments.checkout = function(){
|
||||
if(Payments.amazonPayments.type === 'donation'){
|
||||
var url = '/amazon/checkout'
|
||||
$http.post(url, {orderReferenceId: Payments.amazonPayments.orderReferenceId}).success(function(){
|
||||
console.log(arguments);
|
||||
$http.post(url, {
|
||||
orderReferenceId: Payments.amazonPayments.orderReferenceId,
|
||||
gift: Payments.amazonPayments.gift
|
||||
}).success(function(){
|
||||
Payments.amazonPayments.reset();
|
||||
window.location.reload(true);
|
||||
}).error(function(res){
|
||||
alert(res.err);
|
||||
Payments.amazonPayments.reset();
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
var amazonPayments = require('amazon-payments');
|
||||
var nconf = require('nconf');
|
||||
var async = require('async');
|
||||
var User = require('mongoose').model('User');
|
||||
var shared = require('../../../../common');
|
||||
var payments = require('./index');
|
||||
var isProd = nconf.get("NODE_ENV") === 'production';
|
||||
|
||||
var payment = amazonPayments.connect({
|
||||
environment: amazonPayments.Environment.Sandbox,
|
||||
var amzPayment = amazonPayments.connect({
|
||||
environment: amazonPayments.Environment[isProd ? 'Production' : 'Sandbox'],
|
||||
sellerId: nconf.get('AMAZON_PAYMENTS:SELLER_ID'),
|
||||
mwsAccessKey: nconf.get('AMAZON_PAYMENTS:MWS_KEY'),
|
||||
mwsSecretKey: nconf.get('AMAZON_PAYMENTS:MWS_SECRET'),
|
||||
|
|
@ -17,8 +19,8 @@ exports.verifyAccessToken = function(req, res, next){
|
|||
return res.json(400, {err: 'Access token not supplied.'});
|
||||
}
|
||||
|
||||
payment.api.getTokenInfo(req.body['access_token'], function(err, tokenInfo){
|
||||
if(err) return next(err);
|
||||
amzPayment.api.getTokenInfo(req.body['access_token'], function(err, tokenInfo){
|
||||
if(err) return res.json(400, {err:err});
|
||||
|
||||
res.send(200);
|
||||
});
|
||||
|
|
@ -29,17 +31,18 @@ exports.checkout = function(req, res, next){
|
|||
return res.json(400, {err: 'Order Reference Id not supplied.'});
|
||||
}
|
||||
|
||||
var gift = req.body.gift;
|
||||
var user = res.locals.user;
|
||||
var orderReferenceId = req.body.orderReferenceId;
|
||||
|
||||
async.series({
|
||||
setOrderReferenceDetails: function(cb){
|
||||
console.log('setOrderReferenceDetails')
|
||||
payment.offAmazonPayments.setOrderReferenceDetails({
|
||||
amzPayment.offAmazonPayments.setOrderReferenceDetails({
|
||||
AmazonOrderReferenceId: orderReferenceId,
|
||||
OrderReferenceAttributes: {
|
||||
OrderTotal: {
|
||||
CurrencyCode: 'USD',
|
||||
Amount: 5
|
||||
Amount: gift ? (gift.gems.amount/4) : 5
|
||||
},
|
||||
SellerNote: 'HabitRPG Gems',
|
||||
SellerOrderAttributes: {
|
||||
|
|
@ -51,20 +54,18 @@ exports.checkout = function(req, res, next){
|
|||
},
|
||||
|
||||
confirmOrderReference: function(cb){
|
||||
console.log('confirmOrderReference')
|
||||
payment.offAmazonPayments.confirmOrderReference({
|
||||
amzPayment.offAmazonPayments.confirmOrderReference({
|
||||
AmazonOrderReferenceId: orderReferenceId
|
||||
}, cb);
|
||||
},
|
||||
|
||||
authorize: function(cb){
|
||||
console.log('authorize')
|
||||
payment.offAmazonPayments.authorize({
|
||||
amzPayment.offAmazonPayments.authorize({
|
||||
AmazonOrderReferenceId: orderReferenceId,
|
||||
AuthorizationReferenceId: shared.uuid().substring(0, 32),
|
||||
AuthorizationAmount: {
|
||||
CurrencyCode: 'USD',
|
||||
Amount: 5
|
||||
Amount: gift ? (gift.gems.amount/4) : 5
|
||||
},
|
||||
SellerAuthorizationNote: 'HabitRPG Donation',
|
||||
TransactionTimeout: 0,
|
||||
|
|
@ -73,10 +74,27 @@ exports.checkout = function(req, res, next){
|
|||
},
|
||||
|
||||
closeOrderReference: function(cb){
|
||||
console.log('closeOrderReference')
|
||||
payment.offAmazonPayments.closeOrderReference({
|
||||
amzPayment.offAmazonPayments.closeOrderReference({
|
||||
AmazonOrderReferenceId: orderReferenceId
|
||||
}, cb);
|
||||
},
|
||||
|
||||
executePayment: function(cb){
|
||||
async.waterfall([
|
||||
function(cb2){ User.findById(gift ? gift.uuid : undefined, cb2) },
|
||||
function(member, cb2){
|
||||
var data = {user:user, paymentMethod:'Amazon Payments'};
|
||||
var method = 'buyGems';
|
||||
|
||||
if (gift){
|
||||
gift.member = member;
|
||||
data.gift = gift;
|
||||
data.paymentMethod = 'Gift';
|
||||
}
|
||||
|
||||
payments.buyGems(data, cb2);
|
||||
}
|
||||
], cb);
|
||||
}
|
||||
}, function(err, results){
|
||||
if(err) return next(err);
|
||||
|
|
@ -84,4 +102,10 @@ exports.checkout = function(req, res, next){
|
|||
res.send(200);
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
exports.setupSubscription = function(req, res, next){
|
||||
if(!req.body || !req.body['orderReferenceId']){
|
||||
return res.json(400, {err: 'Billing Agreement Id not supplied.'});
|
||||
}
|
||||
};
|
||||
|
|
@ -339,6 +339,7 @@ script(id='partials/options.settings.subscription.html',type='text/ng-template')
|
|||
h3(ng-if='(user.purchased.plan.customerId && user.purchased.plan.dateTerminated)')= env.t("resubscribe")
|
||||
a.btn.btn-primary(ng-click='Payments.showStripe({subscription:_subscription.key, coupon:_subscription.coupon})', ng-disabled='!_subscription.key')= env.t('card')
|
||||
a.btn.btn-warning(href='/paypal/subscribe?_id={{user._id}}&apiToken={{user.apiToken}}&sub={{_subscription.key}}{{_subscription.coupon ? "&coupon="+_subscription.coupon : ""}}', ng-disabled='!_subscription.key') PayPal
|
||||
.btn.btn-default(ng-click="Payments.amazonPayments.init('subscription')") Amazon Payments
|
||||
div(ng-if='user.purchased.plan.customerId')
|
||||
.btn.btn-primary(ng-if='!user.purchased.plan.dateTerminated && user.purchased.plan.paymentMethod=="Stripe"', ng-click='Payments.showStripeEdit()')=env.t('subUpdateCard')
|
||||
.btn.btn-sm.btn-danger(ng-if='!user.purchased.plan.dateTerminated', ng-click='Payments.cancelSubscription()')=env.t('cancelSub')
|
||||
|
|
|
|||
|
|
@ -97,6 +97,7 @@ script(type='text/ng-template', id='modals/send-gift.html')
|
|||
button.btn.btn-primary(ng-show=fromBal, ng-click='sendGift(profile._id, gift)')=env.t("send")
|
||||
a.btn.btn-primary(ng-hide=fromBal, ng-click='Payments.showStripe({gift:gift, uuid:profile._id})')=env.t('card')
|
||||
a.btn.btn-warning(ng-hide=fromBal, href='/paypal/checkout?_id={{::user._id}}&apiToken={{::user.apiToken}}&gift={{Payments.encodeGift(profile._id, gift)}}') PayPal
|
||||
.btn.btn-default(ng-hide=fromBal, ng-click="Payments.amazonPayments.init(gift.type == 'gems' ? 'donation' : 'subscription', gift, profile._id)") Amazon Payments
|
||||
button.btn.btn-default(ng-click='$close()')=env.t('cancel')
|
||||
|
||||
script(type='text/ng-template', id='modals/abuse-flag.html')
|
||||
|
|
|
|||
Loading…
Reference in a new issue