-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
216 lines (185 loc) · 6.98 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(function() {
'use strict';
angular.module('App', [
// vendors
'kinvey',
// ours
'App.env',
'App.discount',
'App.mentors',
'App.navbar',
'App.process',
'App.faq',
])
.config(function($locationProvider) {
$locationProvider.hashPrefix('#').html5Mode(true);
})
.run(function($rootScope, $kinvey, ENV) {
$rootScope.$kinvey = $kinvey;
Stripe.setPublishableKey(ENV.STRIPE_PUBLISHABLE_KEY);
})
.controller('Controller', ['$scope', '$kinvey', '$http', '$interval', '$q', '$sce',
function($scope, $kinvey, $http, $interval, $q, $sce) {
$scope.init = function() {
$scope.user = $kinvey.getActiveUser();
$scope.applicationErrors = [];
$scope.isProcessingPayment = false;
$scope.card = {
//number: '4242 4242 4242 4242',
//expMonth: '12',
//expYear: 2015,
//cvc: '232'
};
$scope.livereloadUrl = $sce.trustAsResourceUrl('//localhost:35729/livereload.js');
$scope.getPricing();
$interval(function() {
$scope.getPricing();
}, 5000);
};
$scope.getPricing = function() {
$http.get('/kinvey/pricing/')
.success(function(data, status, headers, config) {
$scope.pricing = data;
})
.error(function(data, status, headers, config) {
console.error(data);
});
};
$scope.$watch('card.number', function(newVal, oldVal) {
if (newVal === undefined || oldVal === undefined) {
return;
}
var reValidDigits = new RegExp(/^( *\d *){16}$/g);
var isValidDigits = newVal.match(reValidDigits) || false;
$scope.applicationForm.number.$setValidity('digit-count', isValidDigits);
if (
newVal === undefined || // ignore undefined
oldVal === undefined || // ignore undefined
newVal.length === 0 || // ignore empty
newVal.length < oldVal.length || // ignore backspace
newVal[newVal.length - 1] === ' ' // ignore trailing space
) {
return;
}
var reDigits = new RegExp(/\d+/g);
var digits = newVal.match(reDigits);
var digitArray = digits ? digits.join('').split('') : null;
// add a space every 4 digits
if (digitArray.length < 16 && digitArray.length % 4 === 0) {
$scope.card.number += ' ';
}
});
$scope.submitPayment = function() {
var user = $kinvey.getActiveUser();
$scope.isProcessingPayment = true;
$scope.applicationErrors = [];
// return promise with activeUser or signup
(function() {
var deferred = $q.defer();
deferred.resolve(user);
return user ? deferred.promise : $scope.signup();
}())
// charge
.then(function(activeUser) {
var $form = jQuery('#application-form');
var user = $kinvey.getActiveUser();
console.log($form);
Stripe.card.createToken($form, function(status, response) {
console.log(status, response);
if (response.error) {
$scope.applicationErrors.push(response.error.message);
user.paymentErrors.push({
date: new Date().toUTCString(),
message: response.error.message
});
$scope.isProcessingPayment = false;
$kinvey.User.update(user);
} else {
$http.post('/charge/', {
stripeToken: response.id, // response contains id and
// card, which contains
// additional card details
expMonth: $scope.applicationForm.expMonth,
expYear: $scope.applicationForm.expYear,
cvc: $scope.applicationForm.cvc,
amount: $scope.pricing.priceInCents.current,
description: user.first_name + ' ' + user.last_name + ' - ' + user.email
})
.success(function(charge) {
user.payments.push({
date: new Date().toUTCString(),
payment: charge
});
$scope.isProcessingPayment = false;
$kinvey.User.update(user);
})
.error(function(err) {
user.paymentErrors.push({
date: new Date().toUTCString(),
message: err
});
$kinvey.User.update(user);
$scope.isProcessingPayment = false;
$scope.applicationErrors.push(err);
});
}
});
}, function(error) {
if (error.name === 'UserAlreadyExists') {
$scope.applicationErrors.push('That email is already registered.');
$scope.isProcessingPayment = false;
}
return error;
})
.catch(function(err) {
console.error(err);
})
};
$scope.signup = function() {
return $kinvey.User.signup({
first_name: $scope.user.first_name,
last_name: $scope.user.last_name,
email: $scope.user.email,
username: $scope.user.email,
payments: [],
paymentErrors: []
});
};
$scope.logout = function() {
$kinvey.User.logout()
.finally(function() {
window.location.reload();
});
};
$scope.init();
}]);
////////////////////////////////////////////////////////
// bootstrap angular AFTER initializing kinvey
//
var $injector = angular.injector([
'ng',
'kinvey',
'App.env',
]);
// We're outside of an angular module here, explicitly invoke ng-annotate
// @ngInject
$injector.invoke(['$kinvey', 'ENV', function initAndBootstrap($kinvey, ENV) {
$kinvey.init({
appKey: ENV.KINVEY_APP_KEY,
appSecret: ENV.KINVEY_APP_SECRET
})
.then(function(activeUser) {
console.debug('Kinvey init success. User:', activeUser);
$kinvey.ping().then(function(response) {
console.log('Kinvey Ping Success. Kinvey Service is alive, version: ' + response.version + ', response: ' + response.kinvey);
}, function(error) {
console.log('Kinvey Ping Failed. Response: ' + error.description);
});
angular.element(document).ready(function() {
angular.bootstrap(document, ['App']);
});
}, function(err) {
console.error('Kinvey init error', err);
});
}]);
}());