-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathable-plastics.js
More file actions
217 lines (208 loc) · 5.84 KB
/
able-plastics.js
File metadata and controls
217 lines (208 loc) · 5.84 KB
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
217
(function() {
'use strict';
angular.module('able').directive('plasticsElement', plasticsDirective)
function plasticsDirective() {
var directive = {
restrict: 'A',
controller: plasticsController,
controllerAs: 'plastics',
bindToController: true
}
return directive
}
function plasticsController($rootScope, $scope, $localStorage, $state, auth, $http, config, $q, $window) {
var vm = this;
vm.new = {}
vm.plastics = []
vm.save = save
vm.activate = activate
vm.remove = remove
vm.sending = false
vm.loading = true
vm.saveDucument = saveDocument
vm.sendingDocument = false
if (typeof $localStorage.plastic == "undefined") {
$localStorage.plastic = {}
}
vm.plastic = $localStorage.plastic
$rootScope.plastic = $localStorage.plastic
get()
function get() {
var req_config = {
headers: {
'Authorization': auth.token
}
};
$http.get(config.api + '/users/' + auth.id + '/plastics', req_config).then(function successCallback(response) {
vm.plastics = response.data.resources;
if (!vm.plastic.path && vm.plastics && vm.plastics[0]) {
activate(vm.plastics[0])
}
vm.loading = false
}, function errorCallback(response) {
vm.plastics = []
vm.loading = false
if (response.status == 401) {
if (navigator && navigator.notification) {
navigator.notification.alert('Você precisa se logar novamente.', false, 'Able', 'Ok')
} else {
window.alert('Você precisa se logar novamente.')
}
auth.signout()
return
}
if (response.status == 403) {
if (navigator && navigator.notification) {
navigator.notification.alert(response.data.errors[0].error, false, 'Able', 'Ok')
} else {
window.alert(response.data.errors[0].error)
}
return
}
if (navigator && navigator.notification) {
navigator.notification.alert('Verifique sua conexão.', get, 'Able', 'Ok')
return
} else {
window.alert('Verifique sua conexão.')
get()
return
}
})
}
function save() {
if (Keyboard) {
Keyboard.close()
}
vm.sending = true
var a = angular.copy(vm.new.number)
var b = angular.copy(vm.new.cvc)
var c = angular.copy(vm.new.expiry)
stripePlasticToken(a, b, c).then(function(resolvedToken) {
var payload = {
self_key: new Date().getTime().toString(),
mask: maskPlasticNumber(a),
expiry: c,
token: resolvedToken
}
var req_config = {
headers: {
'Authorization': auth.token
}
};
$http.post(config.api + '/users/' + auth.id + '/plastics', payload, req_config).then(function(response) {
vm.new = {}
$scope.plasticForm.$setPristine()
$scope.plasticForm.$setUntouched()
activate(response.data)
get()
vm.sending = false
}, function(response) {
vm.sending = false
if (response.status == 401) {
if (navigator && navigator.notification) {
navigator.notification.alert('Você precisa se logar novamente.', false, 'Able', 'Ok')
} else {
window.alert('Você precisa se logar novamente.')
}
auth.signout()
return
}
if (response.status == 403) {
if (navigator && navigator.notification) {
navigator.notification.alert(response.data.errors[0].error, false, 'Able', 'Ok')
} else {
window.alert('Você precisa se logar novamente.')
}
return
}
if (navigator && navigator.notification) {
navigator.notification.alert('Verifique sua conexão.', false, 'Able', 'Ok')
return
} else {
window.alert('Verifique sua conexão.')
return
}
});
}, function(rejectedToken) {
vm.sending = false
if (navigator && navigator.notification) {
navigator.notification.alert("Este cartão não foi aceito. Verifique os dados.", false, 'Able', 'Ok')
} else {
window.alert("Este cartão não foi aceito. Verifique os dados.")
}
})
}
function activate(plastic) {
$localStorage.plastic = plastic
$rootScope.plastic = plastic
vm.plastic = plastic
}
function remove(plastic) {
//call delete on plastic.path
var req_config = {
headers: {
'Authorization': auth.token
}
};
$http.delete(config.api + plastic.path, req_config).then(function(response) {
if (plastic.path == vm.plastic.path) {
$localStorage.plastic = ''
$rootScope.plastic = ''
vm.plastic = ''
}
//remove this item from the list
vm.plastics.slice().reverse().forEach(function(item, index, object) {
if (item.path == plastic.path) {
vm.plastics.splice(object.length - 1 - index, 1);
}
})
}, function(response) {
if (navigator && navigator.notification) {
navigator.notification.alert('Houve um erro ao remover este cartão. Tente novamente.', false, 'Able', 'Ok')
return
} else {
window.alert('Houve um erro ao remover este cartão. Tente novamente.')
return
}
})
}
function maskPlasticNumber(number) {
var a = number.substring(0, 4)
var b = number.substring(12, 16)
var masked = b
return masked
}
function stripePlasticToken(number, cvc, expiry) {
var payload = {
number: number,
cvc: cvc,
exp_month: expiry.substring(0, 2),
exp_year: "20" + expiry.substring(expiry.length - 2, expiry.length),
}
return $q(function(resolve, reject) {
Stripe.setPublishableKey($localStorage.stripe);
Stripe.card.createToken(payload, function(status, response) {
if (status < 200 || status > 299) {
reject(status)
} else {
resolve(response.id)
}
})
})
}
function saveDocument() {
if (Keyboard) {
Keyboard.close()
}
vm.sendingDocument = true
var payload = {
document: angular.copy(vm.document)
}
auth.patchUser(payload).then(function(resolve) {
vm.sendingDocument = false
}, function(reject) {
vm.sendingDocument = false
});
}
}
})();