UNPKG

5.86 kBtext/coffeescriptView Raw
1class MainViewModel
2 constructor: ->
3 @services = [
4 'slack'
5 ]
6
7 @fieldnames = [
8 'HUBOT_HIGHFIVE_CHAT_SERVICE',
9 'HUBOT_HIGHFIVE_ROOM',
10 'HUBOT_HIGHFIVE_ALLOW_EAVESDROPPING',
11 'HUBOT_HIGHFIVE_AWARD_LIMIT',
12 'HUBOT_HIGHFIVE_DAILY_LIMIT',
13 'HUBOT_HIGHFIVE_DOUBLE_RATE',
14 'HUBOT_HIGHFIVE_BOOMERANG_RATE',
15 'HUBOT_TANGOCARD_ROOTURL'
16 'HUBOT_TANGOCARD_USER',
17 'HUBOT_TANGOCARD_KEY',
18 'HUBOT_TANGOCARD_CC',
19 'HUBOT_TANGOCARD_AUTH',
20 'HUBOT_TANGOCARD_CUSTOMER',
21 'HUBOT_TANGOCARD_ACCOUNT',
22 'HUBOT_TANGOCARD_EMAIL',
23 'HUBOT_HIGHFIVE_SHEET_EMAIL',
24 'HUBOT_HIGHFIVE_SHEET_KEY',
25 'HUBOT_HIGHFIVE_SHEET_DOCID',
26 'HUBOT_HIGHFIVE_SHEET_SHEETNAME',
27 'ipaddr',
28 ]
29
30 for f in @fieldnames
31 @[f] = ko.observable()
32
33 @cc_number = ko.observable ''
34 @cc_expiration = ko.observable ''
35 @cc_fname = ko.observable ''
36 @cc_lname = ko.observable ''
37 @cc_address = ko.observable ''
38 @cc_city = ko.observable ''
39 @cc_state = ko.observable ''
40 @cc_zip = ko.observable ''
41 @cc_country = ko.observable ''
42 @tangocard_status = ko.observable ''
43 @tangocard_class = ko.observable ''
44 @cardnote = ko.observable 'optional'
45 @cardclass = ko.computed =>
46 if @cardnote() == 'optional'
47 ''
48 else
49 'success'
50
51 @configoutput = ko.computed =>
52 vars = []
53 for k in @fieldnames
54 v = @[k]()
55 vars.push "#{k}='#{v}'" if v? and v != ''
56 vars.join ' \\\n'
57
58 basic_auth: ->
59 user = @HUBOT_TANGOCARD_USER()
60 pass = @HUBOT_TANGOCARD_KEY()
61 "Basic " + btoa(user + ":" + pass)
62
63 tangocard_get: (url, success, error) ->
64 baseurl = @HUBOT_TANGOCARD_ROOTURL() || 'https://api.tangocard.com/raas/v1/'
65 fullurl = "#{baseurl}#{url}"
66 $.ajax
67 type: 'GET'
68 url: fullurl
69 dataType: 'json'
70 success: success
71 error: error
72 headers:
73 Authorization: @basic_auth()
74
75 tangocard_post: (url, data, success, error) ->
76 baseurl = @HUBOT_TANGOCARD_ROOTURL() || 'https://api.tangocard.com/raas/v1/'
77 fullurl = "#{baseurl}#{url}"
78 $.ajax
79 type: 'POST'
80 url: fullurl
81 dataType: 'json'
82 success: success
83 error: error
84 data: JSON.stringify data
85 headers:
86 Authorization: @basic_auth()
87
88 set_tangocard_error: (msg) ->
89 @tangocard_status msg
90 @tangocard_class 'fail'
91
92 tangocard_setup: ->
93 cc = @HUBOT_TANGOCARD_CC()
94 auth = @HUBOT_TANGOCARD_AUTH()
95 cust = @HUBOT_TANGOCARD_CUSTOMER()
96 acct = @HUBOT_TANGOCARD_ACCOUNT() || 'HubotHighfive'
97 email = @HUBOT_TANGOCARD_EMAIL()
98
99 @tangocard_class ''
100 @tangocard_status 'Checking customer/account status...'
101
102 # TODO: validate inputs
103
104 # Check the account status
105 setupAccount = $.Deferred()
106 @tangocard_get "accounts/#{cust}/#{acct}"
107 , (resp) ->
108 setupAccount.resolve()
109 , (xhr, status, err) =>
110 if xhr.status == 401
111 @set_tangocard_error 'Invalid credentials'
112 return setupAccount.reject()
113 if xhr.status != 404
114 @set_tangocard_error "Error: #{err}"
115 return setupAccount.reject()
116
117 @tangocard_post 'accounts',
118 customer: cust
119 identifier: acct
120 email: email
121 , (resp) ->
122 setupAccount.resolve()
123 , (xhr, status, err) =>
124 @tangocard_status "Error creating account: #{xhr.responseJSON.invalid_inputs_message}"
125 @tangocard_class 'fail'
126 setupAccount.reject()
127
128
129 # TODO: create the credit card
130 setupAccount.then =>
131 # Create the credit card
132 data =
133 customer: cust
134 account_identifier: acct
135 client_ip: @ipaddr()
136 credit_card:
137 number: @cc_number()
138 security_code: auth
139 expiration: @cc_expiration()
140 billing_address:
141 f_name: @cc_fname()
142 l_name: @cc_lname()
143 address: @cc_address()
144 city: @cc_city()
145 state: @cc_state()
146 zip: @cc_zip()
147 country: @cc_country()
148 email: email
149 @tangocard_post 'cc_register', data
150 , (resp) =>
151 console.log resp
152 @HUBOT_TANGOCARD_CC resp.cc_token
153 @tangocard_status 'Success!'
154 @tangocard_class 'success'
155
156 , (xhr, status, err) =>
157 json = xhr.responseJSON
158 if xhr.responseJSON.denial_code == 'CC_DUPREGISTER'
159 return @set_tangocard_error 'This card is already registered. Contact Tango Card to have it removed.'
160
161 errmsg = json.invalid_inputs_message || json.error_message || json.denial_message
162 return @set_tangocard_error errmsg
163
164$ ->
165 window.vm = new MainViewModel()
166
167 $.getJSON 'values.json', (data) ->
168 console.log data
169 for k,v of data
170 vm[k](v)
171 if (data.HUBOT_TANGOCARD_CC || '') != ''
172 vm.cardnote 'already completed'
173
174 ko.applyBindings vm
175
176 $('.config-text-area').click ->
177 @select()