refactor amazon payments and add one time purchases once again

This commit is contained in:
Matteo Pagliazzi 2015-07-15 16:15:00 +02:00
parent bade139b4b
commit ecc98411c7
4 changed files with 75 additions and 49 deletions

View file

@ -72,15 +72,12 @@ function($rootScope, User, $http, Content) {
Payments.amazonPayments.reset = function(){
Payments.amazonPayments.modal.close();
// TODO this is needed because if we do not logout
// users then if they use donation & then subscription
// the billing agreement will be wrong
amazon.Login.logout();
Payments.amazonPayments.modal = null;
Payments.amazonPayments.type = null;
Payments.amazonPayments.loggedIn = false;
Payments.amazonPayments.gift = null;
Payments.amazonPayments.billingAgreementId = null;
Payments.amazonPayments.orderReferenceId = null;
Payments.amazonPayments.paymentSelected = false;
Payments.amazonPayments.recurringConsent = false;
Payments.amazonPayments.subscription = null;
@ -90,11 +87,11 @@ function($rootScope, User, $http, Content) {
// Needs to be called everytime the modal/router is accessed
Payments.amazonPayments.init = function(data){
if(!isAmazonReady) return;
if(data.type !== 'donation' && data.type !== 'subscription') return;
if(data.type !== 'single' && data.type !== 'subscription') return;
if(data.gift){
if(data.gift.gems && data.gift.gems.amount && data.gift.gems.amount <= 0) return;
gift.uuid = data.giftedTo;
data.gift.uuid = data.giftedTo;
}
if(data.subscription){
@ -118,6 +115,27 @@ function($rootScope, User, $http, Content) {
type: 'PwA',
color: 'Gold',
size: 'small',
agreementType: 'BillingAgreement',
onSignIn: function(contract){
Payments.amazonPayments.billingAgreementId = contract.getAmazonBillingAgreementId();
if(Payments.amazonPayments.type === 'subscription'){
Payments.amazonPayments.loggedIn = true;
Payments.amazonPayments.initWidgets();
}else{
var url = '/amazon/createOrderReferenceId'
$http.post(url, {
billingAgreementId: Payments.amazonPayments.billingAgreementId
}).success(function(data){
Payments.amazonPayments.loggedIn = true;
Payments.amazonPayments.orderReferenceId = data.orderReferenceId;
Payments.amazonPayments.initWidgets();
}).error(function(res){
alert(res.err);
});
}
},
authorization: function(){
amazon.Login.authorize({
@ -127,10 +145,7 @@ function($rootScope, User, $http, Content) {
if(response.error) return alert(response.error);
var url = '/amazon/verifyAccessToken'
$http.post(url, response).success(function(){
Payments.amazonPayments.loggedIn = true;
Payments.amazonPayments.initWidgets();
}).error(function(res){
$http.post(url, response).error(function(res){
alert(res.err);
});
});
@ -143,7 +158,7 @@ function($rootScope, User, $http, Content) {
}
Payments.amazonPayments.canCheckout = function(){
if(Payments.amazonPayments.type === 'donation'){
if(Payments.amazonPayments.type === 'single'){
return Payments.amazonPayments.paymentSelected === true;
}else if(Payments.amazonPayments.type === 'subscription'){
return Payments.amazonPayments.paymentSelected === true &&
@ -160,55 +175,58 @@ function($rootScope, User, $http, Content) {
design: {
designMode: 'responsive'
},
agreementType: 'BillingAgreement',
onReady: function(billingAgreement){
Payments.amazonPayments.billingAgreementId = billingAgreement.getAmazonBillingAgreementId();
if(Payments.amazonPayments.type === 'subscription'){
new OffAmazonPayments.Widgets.Consent({
sellerId: window.env.AMAZON_PAYMENTS.SELLER_ID,
amazonBillingAgreementId: Payments.amazonPayments.billingAgreementId,
design: {
designMode: 'responsive'
},
onReady: function(consent){
$rootScope.$apply(function(){
var getConsent = consent.getConsentStatus
Payments.amazonPayments.recurringConsent = getConsent ? getConsent() : false;
});
},
onConsent: function(consent){
$rootScope.$apply(function(){
Payments.amazonPayments.recurringConsent = consent.getConsentStatus();
});
},
onError: amazonOnError
}).bind('AmazonPayRecurring');
}
},
onPaymentSelect: function() {
$rootScope.$apply(function(){
console.log('Payment plan selected')
Payments.amazonPayments.paymentSelected = true;
});
},
onError: amazonOnError
};
}
if(Payments.amazonPayments.type === 'subscription'){
walletParams.agreementType = 'BillingAgreement';
console.log(Payments.amazonPayments.billingAgreementId);
walletParams.billingAgreementId = Payments.amazonPayments.billingAgreementId;
walletParams.onReady = function(billingAgreement){
Payments.amazonPayments.billingAgreementId = billingAgreement.getAmazonBillingAgreementId();
new OffAmazonPayments.Widgets.Consent({
sellerId: window.env.AMAZON_PAYMENTS.SELLER_ID,
amazonBillingAgreementId: Payments.amazonPayments.billingAgreementId,
design: {
designMode: 'responsive'
},
onReady: function(consent){
$rootScope.$apply(function(){
var getConsent = consent.getConsentStatus
Payments.amazonPayments.recurringConsent = getConsent ? getConsent() : false;
});
},
onConsent: function(consent){
$rootScope.$apply(function(){
Payments.amazonPayments.recurringConsent = consent.getConsentStatus();
});
},
onError: amazonOnError
}).bind('AmazonPayRecurring');
}
}else{
walletParams.amazonOrderReferenceId = Payments.amazonPayments.orderReferenceId;
}
new OffAmazonPayments.Widgets.Wallet(walletParams).bind('AmazonPayWallet');
}
Payments.amazonPayments.checkout = function(){
if(Payments.amazonPayments.type === 'donation'){
var url = '/amazon/createOrderReferenceId'
if(Payments.amazonPayments.type === 'single'){
var url = '/amazon/checkout';
$http.post(url, {
billingAgreementId: Payments.amazonPayments.billingAgreementId,
orderReferenceId: Payments.amazonPayments.orderReferenceId,
gift: Payments.amazonPayments.gift
}).success(function(){
Payments.amazonPayments.reset();

View file

@ -103,7 +103,15 @@ exports.checkout = function(req, res, next){
SellerAuthorizationNote: 'HabitRPG Payment',
TransactionTimeout: 0,
CaptureNow: true
}, cb);
}, function(err, res){
if(err) return cb(err);
if(res.AuthorizationDetails.AuthorizationStatus.State === 'Declined'){
return cb(new Error('The payment was not successfull.'));
}
return cb();
});
},
closeOrderReference: function(cb){

View file

@ -22,7 +22,7 @@ script(id='modals/buyGems.html', type='text/ng-template')
small.muted=env.t('paymentMethods')
.btn.btn-primary(ng-click='Payments.showStripe({})')=env.t('card')
a.btn.btn-warning(href='/paypal/checkout?_id={{user._id}}&apiToken={{user.apiToken}}') PayPal
.btn.btn-default(ng-click="Payments.amazonPayments.init({type: 'donation'})") Amazon Payments
.btn.btn-default(ng-click="Payments.amazonPayments.init({type: 'single'})") Amazon Payments
div(ng-include="'partials/options.settings.subscription.html'")

View file

@ -97,7 +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="true", ng-click="Payments.amazonPayments.init({type: gift.type == 'gems' ? 'donation' : 'subscription', gift: gift, giftedTo: profile._id})") Amazon Payments
.btn.btn-default(ng-hide=fromBal, ng-click="Payments.amazonPayments.init({type: 'single', gift: gift, giftedTo: profile._id})") Amazon Payments
button.btn.btn-default(ng-click='$close()')=env.t('cancel')
script(type='text/ng-template', id='modals/abuse-flag.html')