mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-08-03 12:40:00 +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.modal = null;
|
||||||
Payments.amazonPayments.type = null;
|
Payments.amazonPayments.type = null;
|
||||||
Payments.amazonPayments.loggedIn = false;
|
Payments.amazonPayments.loggedIn = false;
|
||||||
|
Payments.amazonPayments.gift = null;
|
||||||
Payments.amazonPayments.orderReferenceId = null;
|
Payments.amazonPayments.orderReferenceId = null;
|
||||||
Payments.amazonPayments.billingAgreementId = null;
|
Payments.amazonPayments.billingAgreementId = null;
|
||||||
Payments.amazonPayments.paymentSelected = false;
|
Payments.amazonPayments.paymentSelected = false;
|
||||||
|
|
@ -85,9 +86,16 @@ function($rootScope, User, $http, Content) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Needs to be called everytime the modal/router is accessed
|
// 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(!isAmazonReady) return;
|
||||||
if(type !== 'donation' && type !== 'subscription') return;
|
if(type !== 'donation' && type !== 'subscription') return;
|
||||||
|
|
||||||
|
Payments.amazonPayments.gift = gift;
|
||||||
Payments.amazonPayments.type = type;
|
Payments.amazonPayments.type = type;
|
||||||
|
|
||||||
var modal = Payments.amazonPayments.modal = $rootScope.openModal('amazonPayments', {
|
var modal = Payments.amazonPayments.modal = $rootScope.openModal('amazonPayments', {
|
||||||
|
|
@ -195,9 +203,12 @@ function($rootScope, User, $http, Content) {
|
||||||
Payments.amazonPayments.checkout = function(){
|
Payments.amazonPayments.checkout = function(){
|
||||||
if(Payments.amazonPayments.type === 'donation'){
|
if(Payments.amazonPayments.type === 'donation'){
|
||||||
var url = '/amazon/checkout'
|
var url = '/amazon/checkout'
|
||||||
$http.post(url, {orderReferenceId: Payments.amazonPayments.orderReferenceId}).success(function(){
|
$http.post(url, {
|
||||||
console.log(arguments);
|
orderReferenceId: Payments.amazonPayments.orderReferenceId,
|
||||||
|
gift: Payments.amazonPayments.gift
|
||||||
|
}).success(function(){
|
||||||
Payments.amazonPayments.reset();
|
Payments.amazonPayments.reset();
|
||||||
|
window.location.reload(true);
|
||||||
}).error(function(res){
|
}).error(function(res){
|
||||||
alert(res.err);
|
alert(res.err);
|
||||||
Payments.amazonPayments.reset();
|
Payments.amazonPayments.reset();
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
var amazonPayments = require('amazon-payments');
|
var amazonPayments = require('amazon-payments');
|
||||||
var nconf = require('nconf');
|
var nconf = require('nconf');
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
var User = require('mongoose').model('User');
|
||||||
var shared = require('../../../../common');
|
var shared = require('../../../../common');
|
||||||
|
var payments = require('./index');
|
||||||
var isProd = nconf.get("NODE_ENV") === 'production';
|
var isProd = nconf.get("NODE_ENV") === 'production';
|
||||||
|
|
||||||
var payment = amazonPayments.connect({
|
var amzPayment = amazonPayments.connect({
|
||||||
environment: amazonPayments.Environment.Sandbox,
|
environment: amazonPayments.Environment[isProd ? 'Production' : 'Sandbox'],
|
||||||
sellerId: nconf.get('AMAZON_PAYMENTS:SELLER_ID'),
|
sellerId: nconf.get('AMAZON_PAYMENTS:SELLER_ID'),
|
||||||
mwsAccessKey: nconf.get('AMAZON_PAYMENTS:MWS_KEY'),
|
mwsAccessKey: nconf.get('AMAZON_PAYMENTS:MWS_KEY'),
|
||||||
mwsSecretKey: nconf.get('AMAZON_PAYMENTS:MWS_SECRET'),
|
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.'});
|
return res.json(400, {err: 'Access token not supplied.'});
|
||||||
}
|
}
|
||||||
|
|
||||||
payment.api.getTokenInfo(req.body['access_token'], function(err, tokenInfo){
|
amzPayment.api.getTokenInfo(req.body['access_token'], function(err, tokenInfo){
|
||||||
if(err) return next(err);
|
if(err) return res.json(400, {err:err});
|
||||||
|
|
||||||
res.send(200);
|
res.send(200);
|
||||||
});
|
});
|
||||||
|
|
@ -29,17 +31,18 @@ exports.checkout = function(req, res, next){
|
||||||
return res.json(400, {err: 'Order Reference Id not supplied.'});
|
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;
|
var orderReferenceId = req.body.orderReferenceId;
|
||||||
|
|
||||||
async.series({
|
async.series({
|
||||||
setOrderReferenceDetails: function(cb){
|
setOrderReferenceDetails: function(cb){
|
||||||
console.log('setOrderReferenceDetails')
|
amzPayment.offAmazonPayments.setOrderReferenceDetails({
|
||||||
payment.offAmazonPayments.setOrderReferenceDetails({
|
|
||||||
AmazonOrderReferenceId: orderReferenceId,
|
AmazonOrderReferenceId: orderReferenceId,
|
||||||
OrderReferenceAttributes: {
|
OrderReferenceAttributes: {
|
||||||
OrderTotal: {
|
OrderTotal: {
|
||||||
CurrencyCode: 'USD',
|
CurrencyCode: 'USD',
|
||||||
Amount: 5
|
Amount: gift ? (gift.gems.amount/4) : 5
|
||||||
},
|
},
|
||||||
SellerNote: 'HabitRPG Gems',
|
SellerNote: 'HabitRPG Gems',
|
||||||
SellerOrderAttributes: {
|
SellerOrderAttributes: {
|
||||||
|
|
@ -51,20 +54,18 @@ exports.checkout = function(req, res, next){
|
||||||
},
|
},
|
||||||
|
|
||||||
confirmOrderReference: function(cb){
|
confirmOrderReference: function(cb){
|
||||||
console.log('confirmOrderReference')
|
amzPayment.offAmazonPayments.confirmOrderReference({
|
||||||
payment.offAmazonPayments.confirmOrderReference({
|
|
||||||
AmazonOrderReferenceId: orderReferenceId
|
AmazonOrderReferenceId: orderReferenceId
|
||||||
}, cb);
|
}, cb);
|
||||||
},
|
},
|
||||||
|
|
||||||
authorize: function(cb){
|
authorize: function(cb){
|
||||||
console.log('authorize')
|
amzPayment.offAmazonPayments.authorize({
|
||||||
payment.offAmazonPayments.authorize({
|
|
||||||
AmazonOrderReferenceId: orderReferenceId,
|
AmazonOrderReferenceId: orderReferenceId,
|
||||||
AuthorizationReferenceId: shared.uuid().substring(0, 32),
|
AuthorizationReferenceId: shared.uuid().substring(0, 32),
|
||||||
AuthorizationAmount: {
|
AuthorizationAmount: {
|
||||||
CurrencyCode: 'USD',
|
CurrencyCode: 'USD',
|
||||||
Amount: 5
|
Amount: gift ? (gift.gems.amount/4) : 5
|
||||||
},
|
},
|
||||||
SellerAuthorizationNote: 'HabitRPG Donation',
|
SellerAuthorizationNote: 'HabitRPG Donation',
|
||||||
TransactionTimeout: 0,
|
TransactionTimeout: 0,
|
||||||
|
|
@ -73,10 +74,27 @@ exports.checkout = function(req, res, next){
|
||||||
},
|
},
|
||||||
|
|
||||||
closeOrderReference: function(cb){
|
closeOrderReference: function(cb){
|
||||||
console.log('closeOrderReference')
|
amzPayment.offAmazonPayments.closeOrderReference({
|
||||||
payment.offAmazonPayments.closeOrderReference({
|
|
||||||
AmazonOrderReferenceId: orderReferenceId
|
AmazonOrderReferenceId: orderReferenceId
|
||||||
}, cb);
|
}, 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){
|
}, function(err, results){
|
||||||
if(err) return next(err);
|
if(err) return next(err);
|
||||||
|
|
@ -84,4 +102,10 @@ exports.checkout = function(req, res, next){
|
||||||
res.send(200);
|
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")
|
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-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
|
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')
|
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-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')
|
.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")
|
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-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
|
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')
|
button.btn.btn-default(ng-click='$close()')=env.t('cancel')
|
||||||
|
|
||||||
script(type='text/ng-template', id='modals/abuse-flag.html')
|
script(type='text/ng-template', id='modals/abuse-flag.html')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue