From 68e165884952ca4af2dbcbf4f6b60b323fa42747 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Fri, 8 Nov 2013 17:50:42 -0500 Subject: [PATCH 1/2] Add automatic email loading to the stripe dialog --- endpoints/api.py | 1 + static/js/app.js | 38 +++++++++++++++++++++++++------------- static/js/controllers.js | 3 +-- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/endpoints/api.py b/endpoints/api.py index bac8d10c9..5cedca1f5 100644 --- a/endpoints/api.py +++ b/endpoints/api.py @@ -347,6 +347,7 @@ def get_organization(orgname): is_admin = admin_org.can() return { 'name': o.username, + 'email': o.email if is_admin else '', 'gravatar': compute_hash(o.email), 'teams': {t.name : team_view(orgname, t) for t in teams}, 'is_admin': is_admin diff --git a/static/js/app.js b/static/js/app.js index 207ef56f0..c832be465 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -48,7 +48,7 @@ function getMarkedDown(string) { // Start the application code itself. quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics', 'angulartics.mixpanel', '$strap.directives', 'ngCookies'], function($provide) { - $provide.factory('UserService', ['Restangular', 'PlanService', function(Restangular, PlanService) { + $provide.factory('UserService', ['Restangular', function(Restangular) { var userResponse = { verified: false, anonymous: true, @@ -59,7 +59,6 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics', } var userService = {} - var currentSubscription = null; userService.load = function() { var userFetch = Restangular.one('user/'); @@ -80,21 +79,21 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics', }); }; - userService.resetCurrentSubscription = function() { - currentSubscription = null; - }; + userService.getOrganization = function(name) { + if (!userResponse || !userResponse.organizations) { return null; } + for (var i = 0; i < userResponse.organizations.length; ++i) { + var org = userResponse.organizations[i]; + if (org.name == name) { + return org; + } + } - userService.getCurrentSubscription = function(callback, failure) { - if (currentSubscription) { callback(currentSubscription); } - PlanService.getSubscription(null, function(sub) { - currentSubscription = sub; - callback(sub); - }, failure); + return null; }; userService.currentUser = function() { return userResponse; - } + }; // Load the user the first time. userService.load(); @@ -116,7 +115,7 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics', return keyService; }]); - $provide.factory('PlanService', ['Restangular', 'KeyService', function(Restangular, KeyService) { + $provide.factory('PlanService', ['Restangular', 'KeyService', 'UserService', function(Restangular, KeyService, UserService) { var plans = null; var planDict = {}; var planService = {} @@ -234,9 +233,22 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics', }; planService.getPlan(planId, function(planDetails) { + var email = null; + if (UserService.currentUser()) { + email = UserService.currentUser().email; + + if (orgname) { + org = UserService.getOrganization(orgname); + if (org) { + emaiil = org.email; + } + } + } + StripeCheckout.open({ key: KeyService.stripePublishableKey, address: false, + email: email, amount: planDetails.price, currency: 'usd', name: 'Quay ' + planDetails.title + ' Subscription', diff --git a/static/js/controllers.js b/static/js/controllers.js index 2da04c2a5..39784f77b 100644 --- a/static/js/controllers.js +++ b/static/js/controllers.js @@ -1043,7 +1043,6 @@ function NewRepoCtrl($scope, $location, $http, $timeout, UserService, Restangula $scope.planChanging = true; }, function(plan) { // Subscribed. - UserService.resetCurrentSubscription(); subscribedToPlan(plan); }, function() { // Failure. @@ -1067,7 +1066,7 @@ function NewRepoCtrl($scope, $location, $http, $timeout, UserService, Restangula if (isUserNamespace) { // Load the user's subscription information in case they want to create a private // repository. - UserService.getCurrentSubscription(subscribedToPlan, function() { + PlanService.getSubscription(null, subscribedToPlan, function() { PlanService.getMinimumPlan(1, false, function(minimum) { $scope.planRequired = minimum; }); }); } else { From 0f473a3a825628173f569b542f089c72458e9cbc Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Fri, 8 Nov 2013 20:32:56 -0500 Subject: [PATCH 2/2] Support the additional stripe callbacks --- static/js/app.js | 46 ++++++++++++++++++++++++---------------- static/js/controllers.js | 42 +++++++++++++++++------------------- templates/index.html | 2 +- 3 files changed, 49 insertions(+), 41 deletions(-) diff --git a/static/js/app.js b/static/js/app.js index c832be465..8fb7773be 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -212,23 +212,31 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics', createSubscriptionRequest.customPUT(subscriptionDetails).then(success, failure); }; - planService.changePlan = function($scope, orgname, planId, hasExistingSubscription, started, success, failure) { + planService.changePlan = function($scope, orgname, planId, hasExistingSubscription, callbacks) { if (!hasExistingSubscription) { - planService.showSubscribeDialog($scope, orgname, planId, started, success, failure); + planService.showSubscribeDialog($scope, orgname, planId, callbacks); return; } - started(); - planService.setSubscription(orgname, planId, success, failure); + if (callbacks['started']) { + callbacks['started'](); + } + planService.setSubscription(orgname, planId, callbacks['success'], callbacks['failure']); }; - planService.showSubscribeDialog = function($scope, orgname, planId, started, success, failure) { + planService.showSubscribeDialog = function($scope, orgname, planId, callbacks) { + if (callbacks['opening']) { + callbacks['opening'](); + } + var submitToken = function(token) { mixpanel.track('plan_subscribe'); $scope.$apply(function() { - started(); - planService.setSubscription(orgname, planId, success, failure); + if (callbacks['started']) { + callbacks['started'](); + } + planService.setSubscription(orgname, planId, callbacks['success'], callbacks['failure']); }); }; @@ -255,7 +263,9 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics', description: 'Up to ' + planDetails.privateRepos + ' private repositories', panelLabel: 'Subscribe', token: submitToken, - image: 'static/img/quay-icon-stripe.png' + image: 'static/img/quay-icon-stripe.png', + opened: function() { $scope.$apply(function() { callbacks['opened']() }); }, + closed: function() { $scope.$apply(function() { callbacks['closed']() }); } }); }); }; @@ -629,16 +639,16 @@ quayApp.directive('planManager', function () { $scope.changeSubscription = function(planId) { if ($scope.planChanging) { return; } - PlanService.changePlan($scope, $scope.organization, planId, hasSubscription, function() { - // Started. - $scope.planChanging = true; - }, function(sub) { - // Success. - subscribedToPlan(sub); - }, function() { - // Failure. - $scope.planChanging = false; - }); + var callbacks = { + 'opening': function() { $scope.planChanging = true; }, + 'started': function() { $scope.planChanging = true; }, + 'opened': function() { $scope.planChanging = true; }, + 'closed': function() { $scope.planChanging = false; }, + 'success': subscribedToPlan, + 'failure': function() { $scope.planChanging = false; } + }; + + PlanService.changePlan($scope, $scope.organization, planId, hasSubscription, callbacks); }; $scope.cancelSubscription = function() { diff --git a/static/js/controllers.js b/static/js/controllers.js index 39784f77b..456c1ebe6 100644 --- a/static/js/controllers.js +++ b/static/js/controllers.js @@ -1038,17 +1038,14 @@ function NewRepoCtrl($scope, $location, $http, $timeout, UserService, Restangula }; $scope.upgradePlan = function() { - PlanService.changePlan($scope, null, $scope.planRequired.stripeId, null, function() { - // Subscribing. - $scope.planChanging = true; - }, function(plan) { - // Subscribed. - subscribedToPlan(plan); - }, function() { - // Failure. - $('#couldnotsubscribeModal').modal(); - $scope.planChanging = false; - }); + var callbacks = { + 'opened': function() { $scope.planChanging = true; }, + 'closed': function() { $scope.planChanging = false; }, + 'success': subscribedToPlan, + 'failure': function() { $('#couldnotsubscribeModal').modal(); $scope.planChanging = false; } + }; + + PlanService.changePlan($scope, null, $scope.planRequired.stripeId, null, callbacks); }; // Watch the namespace on the repo. If it changes, we update the plan and the public/private @@ -1364,24 +1361,25 @@ function NewOrgCtrl($scope, $routeParams, $timeout, $location, UserService, Plan // Reset the organizations list. UserService.load(); + var showOrg = function() { + $location.path('/organization/' + org.name + '/'); + }; + // If the selected plan is free, simply move to the org page. if ($scope.currentPlan.price == 0) { - $location.path('/organization/' + org.name + '/'); + showOrg(); return; } // Otherwise, show the subscribe for the plan. - PlanService.changePlan($scope, org.name, $scope.currentPlan.stripeId, false, function() { - // Started. - $scope.creating = true; - }, function(sub) { - // Success. - $location.path('/organization/' + org.name + '/'); - }, function() { - // Failure. - $location.path('/organization/' + org.name + '/'); - }); + var callbacks = { + 'opened': function() { $scope.creating = true; }, + 'closed': showOrg, + 'success': showOrg, + 'failure': showOrg + }; + PlanService.changePlan($scope, org.name, $scope.currentPlan.stripeId, false, callbacks); }, function(result) { $scope.creating = false; $scope.createError = result.data.message || result.data; diff --git a/templates/index.html b/templates/index.html index 22e993024..00c387d59 100644 --- a/templates/index.html +++ b/templates/index.html @@ -16,7 +16,7 @@ {% endblock %} {% block added_dependencies %} - +